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