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

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

E-mail: