Annotation of parser3/src/include/pa_common.h, revision 1.120
1.15 paf 1: /** @file
1.16 paf 2: Parser: commonly used functions.
3:
1.102 paf 4: Copyright (c) 2001-2005 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.120 ! misha 11: static const char * const IDENT_COMMON_H="$Date: 2008-06-06 11:15:45 $";
1.1 paf 12:
1.33 paf 13: #include "pa_string.h"
1.93 paf 14: #include "pa_hash.h"
1.1 paf 15:
1.120 ! misha 16: // defines
! 17: #define HTTP_USER_AGENT "USER-AGENT"
! 18: #define HTTP_CONTENT_TYPE "CONTENT-TYPE"
! 19: #define HTTP_CONTENT_TYPE_FORM_URLENCODED "application/x-www-form-urlencoded"
! 20: #define HTTP_CONTENT_TYPE_MULTIPART "multipart/form-data"
! 21:
1.87 paf 22: class Value;
1.94 paf 23: typedef Hash<const String::Body , Value*> HashStringValue;
1.64 paf 24:
25: // replace system s*nprintf with our versions
1.69 paf 26: #undef vsnprintf
1.93 paf 27: int __vsnprintf(char *, size_t, const char* , va_list);
1.64 paf 28: #define vsnprintf __vsnprintf
29: #undef snprintf
1.93 paf 30: int __snprintf(char *, size_t, const char* , ...);
1.64 paf 31: #define snprintf __snprintf
1.63 paf 32:
1.21 paf 33: #if _MSC_VER
1.56 paf 34: /*
1.93 paf 35: inline int open( const char* filename, int oflag ) { return _open(filename, oflag); }
1.56 paf 36: inline int close( int handle ) { return _close(handle); }
37: inline int read( int handle, void *buffer, unsigned int count ) { return _read(handle,buffer,count); }
38: inline int write( int handle, const void *buffer, unsigned int count ) { return _write(handle,buffer,count); }
1.93 paf 39: inline int stat( const char* path, struct _stat *buffer ) { return _stat(path, buffer); }
1.56 paf 40: inline long lseek( int handle, long offset, int origin ) { return _lseek(handle, offset, origin); }
41: */
1.17 paf 42:
43: //access
44: #define F_OK 0
45: #define X_OK 1
46: #define W_OK 2
47: #define R_OK 4
48:
1.20 paf 49: #ifndef strcasecmp
50: # define strcasecmp _stricmp
51: #endif
52: #ifndef strncasecmp
53: # define strncasecmp _strnicmp
54: #endif
55: #ifndef mkdir
56: # define mkdir(path, mode) _mkdir(path)
57: #endif
1.17 paf 58:
1.20 paf 59: #ifndef putenv
60: # define putenv _putenv
1.1 paf 61: #endif
1.2 paf 62:
1.17 paf 63: #endif
1.59 paf 64:
1.54 parser 65: /** under WIN32 "t" mode fixes DOS chars OK,
66: can't say that about other systems/ line break styles
67: */
1.57 paf 68: void fix_line_breaks(
1.93 paf 69: char *str,
70: size_t& length///< may change! used to speedup next actions
71: );
1.95 paf 72:
73: int pa_lock_shared_blocking(int fd);
74: int pa_lock_exclusive_blocking(int fd);
75: int pa_lock_exclusive_nonblocking(int fd);
76: int pa_unlock(int fd);
1.101 paf 77:
78: void create_dir_for_file(const String& file_spec);
1.93 paf 79:
80: typedef void (*File_read_action)(
81: struct stat& finfo,
82: int f,
83: const String& file_spec, const char* fname, bool as_text,
84: void *context);
1.83 paf 85:
86: /**
87: shared-lock specified file,
88: do actions under lock.
89: if fail_on_read_problem is true[default] throws an exception
90:
91: @returns true if read OK
92: */
1.93 paf 93: bool file_read_action_under_lock(const String& file_spec,
94: const char* action_name, File_read_action action, void *context,
1.83 paf 95: bool as_text=false,
96: bool fail_on_read_problem=true);
1.15 paf 97: /**
1.93 paf 98: read specified text file using
1.15 paf 99: if fail_on_read_problem is true[default] throws an exception
1.83 paf 100:
1.93 paf 101: WARNING: charset is used for http header case conversion, it's not a charset of input file!
102:
1.83 paf 103: @returns true if read OK
1.15 paf 104: */
1.93 paf 105: char *file_read_text(Request_charsets& charsets,
1.25 paf 106: const String& file_spec,
1.85 paf 107: bool fail_on_read_problem=true,
1.93 paf 108: HashStringValue* options=0/*, HashStringValue* * out_fields=0*/);
109:
110: struct File_read_result {
111: bool success;
112: char* str; size_t length;
113: HashStringValue* headers;
114: };
1.17 paf 115:
116: /**
1.93 paf 117: read specified file using
1.29 paf 118: if fail_on_read_problem is true[default] throws an exception
1.83 paf 119:
1.93 paf 120: WARNING: charset is used for http header case conversion, it's not a charset of input file!
121:
1.83 paf 122: @returns true if read OK
1.29 paf 123: */
1.93 paf 124: File_read_result file_read(Request_charsets& charsets,
125: const String& file_spec,
1.29 paf 126: bool as_text,
1.93 paf 127: HashStringValue* options=0,
1.100 paf 128: bool fail_on_read_problem=true,
129: char* buf=0, size_t offset=0, size_t size=0);
1.83 paf 130:
131: typedef void (*File_write_action)(int f, void *context);
1.29 paf 132:
133: /**
1.71 paf 134: lock specified file exclusively,
135: do actions under lock.
136: throws an exception in case of problems
1.72 paf 137:
138: if block=false does non-blocking lock
139: @returns true if locked OK, or false if non-blocking locking failed
1.71 paf 140: */
1.72 paf 141: bool file_write_action_under_lock(
1.71 paf 142: const String& file_spec,
1.114 misha 143: const char* action_name,
144: File_write_action action,
145: void *context,
1.71 paf 146: bool as_text=false,
1.72 paf 147: bool do_append=false,
1.77 paf 148: bool do_block=true,
149: bool fail_on_lock_problem=true);
1.71 paf 150:
151: /**
152: write data to specified file,
1.17 paf 153: throws an exception in case of problems
154: */
1.70 paf 155: void file_write(
1.25 paf 156: const String& file_spec,
1.93 paf 157: const char* data, size_t size,
1.58 paf 158: bool as_text,
1.67 paf 159: bool do_append=false);
1.26 paf 160:
161: /**
162: delete specified file
163: throws an exception in case of problems
164: */
1.96 paf 165: bool file_delete(const String& file_spec, bool fail_on_problem=true);
1.47 parser 166: /**
167: move specified file
168: throws an exception in case of problems
169: */
1.70 paf 170: void file_move(const String& old_spec, const String& new_spec);
1.27 paf 171:
1.93 paf 172: bool entry_exists(const char* fname, struct stat *afinfo=0);
1.82 paf 173: bool entry_exists(const String& file_spec);
1.107 paf 174: bool file_exist(const String& file_spec);
175: bool dir_exists(const String& file_spec);
176: const String* file_exist(const String& path, const String& name);
1.36 paf 177: bool file_executable(const String& file_spec);
1.37 paf 178:
1.48 parser 179: bool file_stat(const String& file_spec,
1.46 parser 180: size_t& rsize,
181: time_t& ratime,
182: time_t& rmtime,
1.48 parser 183: time_t& rctime,
184: bool fail_on_read_problem=true);
1.15 paf 185:
186: /**
1.18 paf 187: scans for @a delim[default \n] in @a *row_ref,
188: @return piece of line before it or end of string, if no @a delim found
189: assigns @a *row_ref to point right after delimiter if there were one
190: or to zero if no @a delim were found.
1.15 paf 191: */
1.7 paf 192: char *getrow(char **row_ref,char delim='\n');
1.76 paf 193: char *lsplit(char *string, char delim);
1.7 paf 194: char *lsplit(char **string_ref,char delim);
1.8 paf 195: char *rsplit(char *string, char delim);
1.93 paf 196: const char* format(double value, char *fmt);
1.10 paf 197:
1.30 paf 198: size_t stdout_write(const void *buf, size_t size);
1.14 paf 199:
1.118 misha 200: char* unescape_chars(const char* cp, int len, Request_charsets* charsets=0);
1.23 paf 201:
202: #ifdef WIN32
203: void back_slashes_to_slashes(char *s);
1.35 paf 204: //void slashes_to_back_slashes(char *s);
1.23 paf 205: #endif
206:
1.28 paf 207: #ifndef _qsort
208: # define _qsort(names,cnt,sizeof_names,func_addr) \
209: qsort(names,cnt,sizeof_names,func_addr)
210: #endif
1.34 paf 211:
1.119 misha 212: bool StrStartFromNC(const char* str, const char* substr, bool equal=false);
1.45 parser 213:
214: #define SECS_PER_DAY (60*60*24)
215: int getMonthDays(int year, int month);
1.52 parser 216:
1.116 misha 217: int remove_crlf(char *start, char *end);
1.90 paf 218:
1.93 paf 219: #ifdef PA_SAFE_MODE
220: void check_safe_mode(struct stat finfo, const String& file_spec, const char* fname);
221: #endif
1.99 paf 222:
1.108 misha 223: void pa_base64_decode(const char *in, size_t in_size, char*& result, size_t& result_size);
1.104 paf 224: char* pa_base64_encode(const char *in, size_t in_size);
1.112 misha 225: struct File_base64_action_info {
226: unsigned char** base64;
227: };
228: char* pa_base64_encode(const String& file_spec);
229: static void file_base64_file_action(
230: struct stat& finfo,
231: int f,
232: const String&, const char* /*fname*/, bool,
233: void *context);
1.108 misha 234:
1.109 misha 235: #define FILE_BUFFER_SIZE 4096
1.110 misha 236: static unsigned long crc32Table[256];
1.108 misha 237: static void InitCrc32Table()
238: {
1.110 misha 239: if(crc32Table[1] == 0){
1.108 misha 240: // This is the official polynomial used by CRC32 in PKZip.
241: // Often times the polynomial shown reversed as 0x04C11DB7.
242: static const unsigned long dwPolynomial = 0xEDB88320;
243:
244: for(int i = 0; i < 256; i++)
245: {
246: unsigned long dwCrc = i;
247: for(int j = 8; j > 0; j--)
248: {
249: if(dwCrc & 1)
250: dwCrc = (dwCrc >> 1) ^ dwPolynomial;
251: else
252: dwCrc >>= 1;
253: }
1.110 misha 254: crc32Table[i] = dwCrc;
1.108 misha 255: }
256: }
257: }
258:
1.111 misha 259: int file_block_read(const int f, unsigned char* buffer, const size_t size);
260:
1.108 misha 261: inline void CalcCrc32(const unsigned char byte, unsigned long &crc32)
262: {
1.110 misha 263: crc32 = ((crc32) >> 8) ^ crc32Table[(byte) ^ ((crc32) & 0x000000FF)];
1.108 misha 264: }
265:
266: const unsigned long pa_crc32(const char *in, size_t in_size);
267: const unsigned long pa_crc32(const String& file_spec);
268: static void file_crc32_file_action(
269: struct stat& finfo,
270: int f,
1.109 misha 271: const String&, const char* /*fname*/, bool,
1.108 misha 272: void *context);
1.93 paf 273:
1.109 misha 274: static const char* hex_string(unsigned char* bytes, size_t size, bool upcase) {
275: char *bytes_hex=new(PointerFreeGC) char [size*2/*byte->hh*/+1/*for zero-teminator*/];
276: unsigned char *src=bytes;
277: unsigned char *end=bytes+size;
278: char *dest=bytes_hex;
279:
280: const char *hex=upcase?"0123456789ABCDEF":"0123456789abcdef";
281:
282: for(; src<end; src++) {
283: *dest++=hex[*src/0x10];
284: *dest++=hex[*src%0x10];
285: }
286: *dest=0;
287:
288: return bytes_hex;
289: }
290:
1.106 paf 291: int pa_get_valid_file_options_count(HashStringValue& options);
292:
1.115 misha 293: // some stuff for use with .for_each
294: static void copy_all_overwrite_to(
295: HashStringValue::key_type key,
296: HashStringValue::value_type value,
297: HashStringValue* dest) {
298: dest->put(key, value);
299: }
300:
301: static void remove_key_from(
302: HashStringValue::key_type key,
303: HashStringValue::value_type /*value*/,
304: HashStringValue* dest) {
305: dest->remove(key);
306: }
307:
1.117 misha 308: static String::C date_gmt_string(tm* tms) {
309: /// http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3
310: static const char month_names[12][4]={
311: "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
312: static const char days[7][4]={
313: "Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
314:
315: char *buf=new(PointerFreeGC) char[MAX_STRING];
316: return String::C(buf,
317: snprintf(buf, MAX_STRING, "%s, %.2d %s %.4d %.2d:%.2d:%.2d GMT",
318: days[tms->tm_wday],
319: tms->tm_mday,month_names[tms->tm_mon],tms->tm_year+1900,
320: tms->tm_hour,tms->tm_min,tms->tm_sec));
321: }
322:
1.115 misha 323:
1.93 paf 324: // globals
325:
326: extern const String file_status_name;
1.1 paf 327:
1.103 paf 328: // global defines for file options which are handled but not checked elsewhere, we check them
329:
330: #define PA_SQL_LIMIT_NAME "limit"
331: #define PA_SQL_OFFSET_NAME "offset"
332: #define PA_COLUMN_SEPARATOR_NAME "separator"
333: #define PA_COLUMN_ENCLOSER_NAME "encloser"
1.113 misha 334: #define PA_CHARSET_NAME "charset"
1.103 paf 335:
1.115 misha 336: // globals defines for sql options
337:
338: #define SQL_BIND_NAME "bind"
339: #define SQL_DEFAULT_NAME "default"
340: #define SQL_DISTINCT_NAME "distinct"
341: #define SQL_VALUE_TYPE_NAME "type"
342:
343: #ifndef DOXYGEN
344: enum Table2hash_distint { D_ILLEGAL, D_FIRST };
345: enum Table2hash_value_type { C_HASH, C_STRING, C_TABLE };
346: #endif
347:
1.1 paf 348: #endif
1.115 misha 349:
E-mail: