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