Annotation of parser3/src/include/pa_common.h, revision 1.89.2.9
1.15 paf 1: /** @file
1.16 paf 2: Parser: commonly used functions.
3:
1.89.2.7 paf 4: Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
1.74 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 paf 6: */
7:
8: #ifndef PA_COMMON_H
9: #define PA_COMMON_H
1.78 paf 10:
1.89.2.9! paf 11: static const char* IDENT_COMMON_H="$Date: 2003/01/31 14:03:52 $";
1.1 paf 12:
1.2 paf 13: #include "pa_pool.h"
1.33 paf 14: #include "pa_string.h"
1.89.2.3 paf 15: #include "pa_hash.h"
16: #include "pa_value.h"
1.1 paf 17:
1.89.2.6 paf 18: typedef Hash<StringPtr, ValuePtr> HashStringValue;
1.64 paf 19:
20: // replace system s*nprintf with our versions
1.69 paf 21: #undef vsnprintf
1.89.2.7 paf 22: int __vsnprintf(char *, size_t, const char* , va_list);
1.64 paf 23: #define vsnprintf __vsnprintf
24: #undef snprintf
1.89.2.7 paf 25: int __snprintf(char *, size_t, const char* , ...);
1.64 paf 26: #define snprintf __snprintf
1.63 paf 27:
1.21 paf 28: #if _MSC_VER
1.56 paf 29: /*
1.89.2.7 paf 30: inline int open( const char* filename, int oflag ) { return _open(filename, oflag); }
1.56 paf 31: inline int close( int handle ) { return _close(handle); }
32: inline int read( int handle, void *buffer, unsigned int count ) { return _read(handle,buffer,count); }
33: inline int write( int handle, const void *buffer, unsigned int count ) { return _write(handle,buffer,count); }
1.89.2.7 paf 34: inline int stat( const char* path, struct _stat *buffer ) { return _stat(path, buffer); }
1.56 paf 35: inline long lseek( int handle, long offset, int origin ) { return _lseek(handle, offset, origin); }
36: */
1.17 paf 37:
38: //access
39: #define F_OK 0
40: #define X_OK 1
41: #define W_OK 2
42: #define R_OK 4
43:
1.20 paf 44: #ifndef strcasecmp
45: # define strcasecmp _stricmp
46: #endif
47: #ifndef strncasecmp
48: # define strncasecmp _strnicmp
49: #endif
50: #ifndef mkdir
51: # define mkdir(path, mode) _mkdir(path)
52: #endif
1.17 paf 53:
1.20 paf 54: #ifndef putenv
55: # define putenv _putenv
1.1 paf 56: #endif
1.2 paf 57:
1.17 paf 58: #endif
1.59 paf 59:
1.61 paf 60: #ifdef HAVE_TRUNC
61: # ifndef trunc
62: extern "C" double trunc(double);
63: # endif
64: #else
1.59 paf 65: inline double trunc(double param) { return param > 0? floor(param) : ceil(param); }
66: #endif
1.61 paf 67:
68: #ifdef HAVE_ROUND
69: # ifndef round
70: extern "C" double round(double);
71: # endif
72: #else
1.59 paf 73: inline double round(double param) { return floor(param+0.5); }
74: #endif
1.61 paf 75: #ifdef HAVE_SIGN
76: # ifndef sign
77: extern "C" double sign(double);
78: # endif
79: #else
1.59 paf 80: inline double sign(double param) { return param > 0 ? 1 : ( param < 0 ? -1 : 0 ); }
81: #endif
1.87 paf 82:
1.72 paf 83: /// yields to OS for secs secs and usecs milliseconds
84: int pa_sleep(unsigned long secs, unsigned long usecs);
1.54 parser 85:
86: /** under WIN32 "t" mode fixes DOS chars OK,
87: can't say that about other systems/ line break styles
88: */
1.57 paf 89: void fix_line_breaks(
1.62 paf 90: char *buf,
91: size_t& size ///< may change! used to speedup next actions
1.57 paf 92: );
1.17 paf 93:
1.89.2.4 paf 94: typedef void (*File_read_action)(Pool& pool,
1.84 paf 95: struct stat& finfo,
1.83 paf 96: int f,
1.89.2.7 paf 97: StringPtr file_spec, const char* fname, bool as_text,
1.83 paf 98: void *context);
99:
100: /**
101: shared-lock specified file,
102: do actions under lock.
103: if fail_on_read_problem is true[default] throws an exception
104:
105: @returns true if read OK
106: */
1.89.2.6 paf 107: bool file_read_action_under_lock(Pool& pool, StringPtr file_spec,
1.89.2.7 paf 108: const char* action_name, File_read_action action, void *context,
1.83 paf 109: bool as_text=false,
110: bool fail_on_read_problem=true);
1.15 paf 111: /**
1.19 paf 112: read specified text file using pool,
1.15 paf 113: if fail_on_read_problem is true[default] throws an exception
1.83 paf 114:
1.89.2.8 paf 115: WARNING: charset is used for http header case conversion, it's not a charset of input file!
116:
1.83 paf 117: @returns true if read OK
1.15 paf 118: */
1.89.2.3 paf 119: char *file_read_text(Pool& pool, Charset& charset,
1.89.2.6 paf 120: StringPtr file_spec,
1.85 paf 121: bool fail_on_read_problem=true,
1.89.2.9! paf 122: HashStringValue* options=0/*, HashStringValuePtr* out_fields=0*/);
1.17 paf 123:
1.89.2.4 paf 124: struct File_read_result {
125: bool success;
1.89.2.9! paf 126: char* data; size_t data_size;
! 127: HashStringValuePtr headers;
1.89.2.4 paf 128: };
129:
1.17 paf 130: /**
1.29 paf 131: read specified file using pool,
132: if fail_on_read_problem is true[default] throws an exception
1.89.2.8 paf 133:
134: WARNING: charset is used for http header case conversion, it's not a charset of input file!
1.83 paf 135:
136: @returns true if read OK
1.29 paf 137: */
1.89.2.4 paf 138: File_read_result file_read(Pool& pool, Charset& charset,
1.89.2.6 paf 139: StringPtr file_spec,
1.89.2.4 paf 140: char *& ptr, size_t& size,
1.29 paf 141: bool as_text,
1.89.2.9! paf 142: HashStringValue* options=0,
1.83 paf 143: bool fail_on_read_problem=true);
144:
145: typedef void (*File_write_action)(int f, void *context);
1.29 paf 146:
147: /**
1.71 paf 148: lock specified file exclusively,
149: do actions under lock.
150: throws an exception in case of problems
1.72 paf 151:
152: if block=false does non-blocking lock
153: @returns true if locked OK, or false if non-blocking locking failed
1.71 paf 154: */
1.72 paf 155: bool file_write_action_under_lock(
1.89.2.6 paf 156: StringPtr file_spec,
1.89.2.7 paf 157: const char* action_name, File_write_action action, void *context,
1.71 paf 158: bool as_text=false,
1.72 paf 159: bool do_append=false,
1.77 paf 160: bool do_block=true,
161: bool fail_on_lock_problem=true);
1.71 paf 162:
163: /**
164: write data to specified file,
1.17 paf 165: throws an exception in case of problems
166: */
1.70 paf 167: void file_write(
1.89.2.6 paf 168: StringPtr file_spec,
1.29 paf 169: const void *data, size_t size,
1.58 paf 170: bool as_text,
1.67 paf 171: bool do_append=false);
1.26 paf 172:
173: /**
174: delete specified file
175: throws an exception in case of problems
176: */
1.89.2.6 paf 177: bool file_delete(StringPtr file_spec, bool fail_on_read_problem=true);
1.47 parser 178: /**
179: move specified file
180: throws an exception in case of problems
181: */
1.89.2.6 paf 182: void file_move(StringPtr old_spec, StringPtr new_spec);
1.27 paf 183:
1.89.2.7 paf 184: bool entry_exists(const char* fname, struct stat *afinfo=0);
1.89.2.6 paf 185: bool entry_exists(StringPtr file_spec);
186: bool file_readable(StringPtr file_spec);
187: bool dir_readable(StringPtr file_spec);
188: StringPtr file_readable(StringPtr path, StringPtr name);
189: bool file_executable(StringPtr file_spec);
1.37 paf 190:
1.89.2.6 paf 191: bool file_stat(StringPtr file_spec,
1.46 parser 192: size_t& rsize,
193: time_t& ratime,
194: time_t& rmtime,
1.48 parser 195: time_t& rctime,
196: bool fail_on_read_problem=true);
1.15 paf 197:
198: /**
1.18 paf 199: scans for @a delim[default \n] in @a *row_ref,
200: @return piece of line before it or end of string, if no @a delim found
201: assigns @a *row_ref to point right after delimiter if there were one
202: or to zero if no @a delim were found.
1.15 paf 203: */
1.7 paf 204: char *getrow(char **row_ref,char delim='\n');
1.76 paf 205: char *lsplit(char *string, char delim);
1.7 paf 206: char *lsplit(char **string_ref,char delim);
1.8 paf 207: char *rsplit(char *string, char delim);
1.89.2.7 paf 208: const char* format(Pool& pool, double value, char *fmt);
1.10 paf 209:
210: #ifndef max
1.75 paf 211: inline int max(int a, int b) { return a>b?a:b; }
212: inline int min(int a, int b){ return a<b?a:b; }
213: inline size_t max(size_t a, size_t b) { return a>b?a:b; }
214: inline size_t min(size_t a, size_t b){ return a<b?a:b; }
1.10 paf 215: #endif
216:
1.30 paf 217: size_t stdout_write(const void *buf, size_t size);
1.14 paf 218:
1.89.2.7 paf 219: char *unescape_chars(Pool& pool, const char* cp, int len);
1.23 paf 220:
221: #ifdef WIN32
222: void back_slashes_to_slashes(char *s);
1.35 paf 223: //void slashes_to_back_slashes(char *s);
1.23 paf 224: #endif
225:
1.28 paf 226: #ifndef _qsort
227: # define _qsort(names,cnt,sizeof_names,func_addr) \
228: qsort(names,cnt,sizeof_names,func_addr)
229: #endif
1.34 paf 230:
1.89.2.7 paf 231: bool StrEqNc(const char* s1, const char* s2, bool strict=true);
1.45 parser 232:
233: #define SECS_PER_DAY (60*60*24)
234: int getMonthDays(int year, int month);
1.52 parser 235:
236: void remove_crlf(char *start, char *end);
1.89.2.9! paf 237:
! 238: // globals
! 239:
! 240: extern StringPtr file_status_name;
1.1 paf 241:
242: #endif
E-mail: