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