Annotation of parser3/src/include/pa_common.h, revision 1.125

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.125   ! misha      11: static const char * const IDENT_COMMON_H="$Date: 2008-07-15 12:50:24 $";
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.123     misha     108:                                         HashStringValue* options=0,
                    109:                                         bool transcode_result=true);
1.93      paf       110: 
                    111: struct File_read_result {
                    112:        bool success;
                    113:        char* str; size_t length;
                    114:        HashStringValue* headers;
                    115: };
1.17      paf       116: 
                    117: /**
1.93      paf       118:        read specified file using 
1.29      paf       119:        if fail_on_read_problem is true[default] throws an exception
1.83      paf       120: 
1.93      paf       121:        WARNING: charset is used for http header case conversion, it's not a charset of input file!
                    122: 
1.83      paf       123:        @returns true if read OK
1.29      paf       124: */
1.93      paf       125: File_read_result file_read(Request_charsets& charsets, 
                    126:                           const String& file_spec, 
1.29      paf       127:                           bool as_text,
1.93      paf       128:                           HashStringValue* options=0,
1.100     paf       129:                           bool fail_on_read_problem=true,
1.123     misha     130:                           char* buf=0, size_t offset=0, size_t size=0, bool transcode_text_result=true);
1.83      paf       131: 
                    132: typedef void (*File_write_action)(int f, void *context);
1.29      paf       133: 
                    134: /**
1.71      paf       135:        lock specified file exclusively, 
                    136:        do actions under lock.
                    137:        throws an exception in case of problems
1.72      paf       138:        
                    139:        if block=false does non-blocking lock
                    140:        @returns true if locked OK, or false if non-blocking locking failed
1.71      paf       141: */
1.72      paf       142: bool file_write_action_under_lock(
1.71      paf       143:                                const String& file_spec, 
1.114     misha     144:                                const char* action_name,
                    145:                                File_write_action action,
                    146:                                void *context,
1.71      paf       147:                                bool as_text=false,
1.72      paf       148:                                bool do_append=false,
1.77      paf       149:                                bool do_block=true,
                    150:                                bool fail_on_lock_problem=true);
1.71      paf       151: 
                    152: /**
                    153:        write data to specified file, 
1.17      paf       154:        throws an exception in case of problems
                    155: */
1.70      paf       156: void file_write(
1.25      paf       157:                                const String& file_spec,
1.93      paf       158:                                const char* data, size_t size, 
1.58      paf       159:                                bool as_text,
1.67      paf       160:                                bool do_append=false);
1.26      paf       161: 
                    162: /**
                    163:        delete specified file 
                    164:        throws an exception in case of problems
                    165: */
1.96      paf       166: bool file_delete(const String& file_spec, bool fail_on_problem=true);
1.47      parser    167: /**
                    168:        move specified file 
                    169:        throws an exception in case of problems
                    170: */
1.70      paf       171: void file_move(const String& old_spec, const String& new_spec);
1.27      paf       172: 
1.93      paf       173: bool entry_exists(const char* fname, struct stat *afinfo=0);
1.82      paf       174: bool entry_exists(const String& file_spec);
1.107     paf       175: bool file_exist(const String& file_spec);
                    176: bool dir_exists(const String& file_spec);
                    177: const String* file_exist(const String& path, const String& name);
1.36      paf       178: bool file_executable(const String& file_spec);
1.37      paf       179: 
1.48      parser    180: bool file_stat(const String& file_spec, 
1.46      parser    181:                           size_t& rsize, 
                    182:                           time_t& ratime,
                    183:                           time_t& rmtime,
1.48      parser    184:                           time_t& rctime,
                    185:                           bool fail_on_read_problem=true);
1.15      paf       186: 
                    187: /**
1.18      paf       188:        scans for @a delim[default \n] in @a *row_ref, 
                    189:        @return piece of line before it or end of string, if no @a delim found
                    190:        assigns @a *row_ref to point right after delimiter if there were one
                    191:        or to zero if no @a delim were found.
1.15      paf       192: */
1.7       paf       193: char *getrow(char **row_ref,char delim='\n');
1.76      paf       194: char *lsplit(char *string, char delim);
1.7       paf       195: char *lsplit(char **string_ref,char delim);
1.8       paf       196: char *rsplit(char *string, char delim);
1.93      paf       197: const char* format(double value, char *fmt);
1.10      paf       198: 
1.30      paf       199: size_t stdout_write(const void *buf, size_t size);
1.14      paf       200: 
1.124     misha     201: char* unescape_chars(const char* cp, int len, Charset* client_charset=0, bool ignore_plus=false);
1.23      paf       202: 
                    203: #ifdef WIN32
                    204: void back_slashes_to_slashes(char *s);
1.35      paf       205: //void slashes_to_back_slashes(char *s);
1.23      paf       206: #endif
                    207: 
1.28      paf       208: #ifndef _qsort
                    209: #      define _qsort(names,cnt,sizeof_names,func_addr) \
                    210:                qsort(names,cnt,sizeof_names,func_addr)
                    211: #endif
1.34      paf       212: 
1.119     misha     213: bool StrStartFromNC(const char* str, const char* substr, bool equal=false);
1.121     misha     214: size_t strpos(const char *str, const char *substr);
                    215: 
1.122     misha     216: Charset* detect_charset(const char* content_type);
                    217: Charset* detect_charset(Charset& source_charset, const String& content_type);
1.45      parser    218: 
                    219: #define SECS_PER_DAY (60*60*24)
                    220: int getMonthDays(int year, int month);
1.52      parser    221: 
1.116     misha     222: int remove_crlf(char *start, char *end);
1.90      paf       223: 
1.93      paf       224: #ifdef PA_SAFE_MODE 
                    225: void check_safe_mode(struct stat finfo, const String& file_spec, const char* fname); 
                    226: #endif 
1.99      paf       227: 
1.108     misha     228: void pa_base64_decode(const char *in, size_t in_size, char*& result, size_t& result_size);
1.104     paf       229: char* pa_base64_encode(const char *in, size_t in_size);
1.112     misha     230: struct File_base64_action_info {
                    231:        unsigned char** base64;
                    232: }; 
                    233: char* pa_base64_encode(const String& file_spec);
                    234: static void file_base64_file_action(
                    235:                             struct stat& finfo, 
                    236:                             int f, 
                    237:                             const String&, const char* /*fname*/, bool, 
                    238:                             void *context);
1.108     misha     239: 
1.109     misha     240: #define FILE_BUFFER_SIZE       4096
1.110     misha     241: static unsigned long crc32Table[256];
1.108     misha     242: static void InitCrc32Table()
                    243: {
1.110     misha     244:        if(crc32Table[1] == 0){
1.108     misha     245:                // This is the official polynomial used by CRC32 in PKZip.
                    246:                // Often times the polynomial shown reversed as 0x04C11DB7.
                    247:                static const unsigned long dwPolynomial = 0xEDB88320;
                    248: 
                    249:                for(int i = 0; i < 256; i++)
                    250:                {
                    251:                        unsigned long dwCrc = i;
                    252:                        for(int j = 8; j > 0; j--)
                    253:                        {
                    254:                                if(dwCrc & 1)
                    255:                                        dwCrc = (dwCrc >> 1) ^ dwPolynomial;
                    256:                                else
                    257:                                        dwCrc >>= 1;
                    258:                        }
1.110     misha     259:                        crc32Table[i] = dwCrc;
1.108     misha     260:                }
                    261:        }
                    262: }
                    263: 
1.111     misha     264: int file_block_read(const int f, unsigned char* buffer, const size_t size);
                    265: 
1.108     misha     266: inline void CalcCrc32(const unsigned char byte, unsigned long &crc32)
                    267: {
1.110     misha     268:        crc32 = ((crc32) >> 8) ^ crc32Table[(byte) ^ ((crc32) & 0x000000FF)];
1.108     misha     269: }
                    270: 
                    271: const unsigned long pa_crc32(const char *in, size_t in_size);
                    272: const unsigned long pa_crc32(const String& file_spec);
                    273: static void file_crc32_file_action(
                    274:                             struct stat& finfo, 
                    275:                             int f, 
1.109     misha     276:                             const String&, const char* /*fname*/, bool, 
1.108     misha     277:                             void *context);
1.125   ! misha     278: char* print_pcre_exec_error_text(int exec_result);
1.93      paf       279: 
1.109     misha     280: static const char* hex_string(unsigned char* bytes, size_t size, bool upcase) {
                    281:        char *bytes_hex=new(PointerFreeGC) char [size*2/*byte->hh*/+1/*for zero-teminator*/];
                    282:        unsigned char *src=bytes;
                    283:        unsigned char *end=bytes+size;
                    284:        char *dest=bytes_hex;
                    285: 
                    286:        const char *hex=upcase?"0123456789ABCDEF":"0123456789abcdef";
                    287: 
                    288:        for(; src<end; src++) {
                    289:                 *dest++=hex[*src/0x10];
                    290:                 *dest++=hex[*src%0x10];
                    291:        }
                    292:        *dest=0;
                    293: 
                    294:        return bytes_hex;
                    295: }
                    296: 
1.106     paf       297: int pa_get_valid_file_options_count(HashStringValue& options);
                    298: 
1.115     misha     299: // some stuff for use with .for_each
                    300: static void copy_all_overwrite_to(
                    301:                                                                  HashStringValue::key_type key, 
                    302:                                                                  HashStringValue::value_type value, 
                    303:                                                                  HashStringValue* dest) {
                    304:        dest->put(key, value);
                    305: }
                    306: 
                    307: static void remove_key_from(
                    308:                                                        HashStringValue::key_type key, 
                    309:                                                        HashStringValue::value_type /*value*/, 
                    310:                                                        HashStringValue* dest) {
                    311:        dest->remove(key);
                    312: }
                    313: 
1.117     misha     314: static String::C date_gmt_string(tm* tms) {
                    315:        /// http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3
                    316:        static const char month_names[12][4]={
                    317:                "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
                    318:        static const char days[7][4]={
                    319:                "Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
                    320: 
                    321:        char *buf=new(PointerFreeGC) char[MAX_STRING];
                    322:        return String::C(buf, 
                    323:                snprintf(buf, MAX_STRING, "%s, %.2d %s %.4d %.2d:%.2d:%.2d GMT", 
                    324:                days[tms->tm_wday],
                    325:                tms->tm_mday,month_names[tms->tm_mon],tms->tm_year+1900,
                    326:                tms->tm_hour,tms->tm_min,tms->tm_sec));
                    327: }
                    328: 
1.115     misha     329: 
1.93      paf       330: // globals
                    331: 
                    332: extern const String file_status_name;
1.1       paf       333: 
1.103     paf       334: // global defines for file options which are handled but not checked elsewhere, we check them
                    335: 
                    336: #define PA_SQL_LIMIT_NAME "limit"
                    337: #define PA_SQL_OFFSET_NAME "offset"
                    338: #define PA_COLUMN_SEPARATOR_NAME "separator"
                    339: #define PA_COLUMN_ENCLOSER_NAME "encloser"
1.113     misha     340: #define PA_CHARSET_NAME "charset"
1.103     paf       341: 
1.115     misha     342: // globals defines for sql options
                    343: 
                    344: #define SQL_BIND_NAME "bind"
                    345: #define SQL_DEFAULT_NAME "default"
                    346: #define SQL_DISTINCT_NAME "distinct"
                    347: #define SQL_VALUE_TYPE_NAME "type"
                    348: 
                    349: #ifndef DOXYGEN
                    350: enum Table2hash_distint { D_ILLEGAL, D_FIRST };
                    351: enum Table2hash_value_type { C_HASH, C_STRING, C_TABLE };
                    352: #endif
                    353: 
1.1       paf       354: #endif
1.115     misha     355: 

E-mail: