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

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.108   ! misha      11: static const char * const IDENT_COMMON_H="$Date: 2005/11/24 14:00:34 $";
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.93      paf       137:                                const char* action_name, File_write_action action, void *context,
1.71      paf       138:                                bool as_text=false,
1.72      paf       139:                                bool do_append=false,
1.77      paf       140:                                bool do_block=true,
                    141:                                bool fail_on_lock_problem=true);
1.71      paf       142: 
                    143: /**
                    144:        write data to specified file, 
1.17      paf       145:        throws an exception in case of problems
                    146: */
1.70      paf       147: void file_write(
1.25      paf       148:                                const String& file_spec,
1.93      paf       149:                                const char* data, size_t size, 
1.58      paf       150:                                bool as_text,
1.67      paf       151:                                bool do_append=false);
1.26      paf       152: 
                    153: /**
                    154:        delete specified file 
                    155:        throws an exception in case of problems
                    156: */
1.96      paf       157: bool file_delete(const String& file_spec, bool fail_on_problem=true);
1.47      parser    158: /**
                    159:        move specified file 
                    160:        throws an exception in case of problems
                    161: */
1.70      paf       162: void file_move(const String& old_spec, const String& new_spec);
1.27      paf       163: 
1.93      paf       164: bool entry_exists(const char* fname, struct stat *afinfo=0);
1.82      paf       165: bool entry_exists(const String& file_spec);
1.107     paf       166: bool file_exist(const String& file_spec);
                    167: bool dir_exists(const String& file_spec);
                    168: const String* file_exist(const String& path, const String& name);
1.36      paf       169: bool file_executable(const String& file_spec);
1.37      paf       170: 
1.48      parser    171: bool file_stat(const String& file_spec, 
1.46      parser    172:                           size_t& rsize, 
                    173:                           time_t& ratime,
                    174:                           time_t& rmtime,
1.48      parser    175:                           time_t& rctime,
                    176:                           bool fail_on_read_problem=true);
1.15      paf       177: 
                    178: /**
1.18      paf       179:        scans for @a delim[default \n] in @a *row_ref, 
                    180:        @return piece of line before it or end of string, if no @a delim found
                    181:        assigns @a *row_ref to point right after delimiter if there were one
                    182:        or to zero if no @a delim were found.
1.15      paf       183: */
1.7       paf       184: char *getrow(char **row_ref,char delim='\n');
1.76      paf       185: char *lsplit(char *string, char delim);
1.7       paf       186: char *lsplit(char **string_ref,char delim);
1.8       paf       187: char *rsplit(char *string, char delim);
1.93      paf       188: const char* format(double value, char *fmt);
1.10      paf       189: 
1.30      paf       190: size_t stdout_write(const void *buf, size_t size);
1.14      paf       191: 
1.93      paf       192: char *unescape_chars(const char* cp, int len);
1.23      paf       193: 
                    194: #ifdef WIN32
                    195: void back_slashes_to_slashes(char *s);
1.35      paf       196: //void slashes_to_back_slashes(char *s);
1.23      paf       197: #endif
                    198: 
1.28      paf       199: #ifndef _qsort
                    200: #      define _qsort(names,cnt,sizeof_names,func_addr) \
                    201:                qsort(names,cnt,sizeof_names,func_addr)
                    202: #endif
1.34      paf       203: 
1.93      paf       204: bool StrEqNc(const char* s1, const char* s2, bool strict=true);
1.45      parser    205: 
                    206: #define SECS_PER_DAY (60*60*24)
                    207: int getMonthDays(int year, int month);
1.52      parser    208: 
                    209: void remove_crlf(char *start, char *end);
1.90      paf       210: 
1.93      paf       211: #ifdef PA_SAFE_MODE 
                    212: void check_safe_mode(struct stat finfo, const String& file_spec, const char* fname); 
                    213: #endif 
1.99      paf       214: 
1.108   ! misha     215: void pa_base64_decode(const char *in, size_t in_size, char*& result, size_t& result_size);
1.104     paf       216: char* pa_base64_encode(const char *in, size_t in_size);
1.108   ! misha     217: static void file_base64_file_action(
        !           218:                             struct stat& finfo, 
        !           219:                             int f, 
        !           220:                             const String& file_spec, const char* /*fname*/, bool, 
        !           221:                             void *context);
        !           222: 
        !           223: #define CRC32_MAX_BUFFER_SIZE  4096
        !           224: static unsigned long *pCrc32Table;
        !           225: static void InitCrc32Table()
        !           226: {
        !           227:        if(pCrc32Table == 0){
        !           228:                // This is the official polynomial used by CRC32 in PKZip.
        !           229:                // Often times the polynomial shown reversed as 0x04C11DB7.
        !           230:                static const unsigned long dwPolynomial = 0xEDB88320;
        !           231: 
        !           232:                pCrc32Table = new(PointerFreeGC) unsigned long[256];
        !           233: 
        !           234:                for(int i = 0; i < 256; i++)
        !           235:                {
        !           236:                        unsigned long dwCrc = i;
        !           237:                        for(int j = 8; j > 0; j--)
        !           238:                        {
        !           239:                                if(dwCrc & 1)
        !           240:                                        dwCrc = (dwCrc >> 1) ^ dwPolynomial;
        !           241:                                else
        !           242:                                        dwCrc >>= 1;
        !           243:                        }
        !           244:                        pCrc32Table[i] = dwCrc;
        !           245:                }
        !           246:        }
        !           247: }
        !           248: 
        !           249: inline void CalcCrc32(const unsigned char byte, unsigned long &crc32)
        !           250: {
        !           251:        crc32 = ((crc32) >> 8) ^ pCrc32Table[(byte) ^ ((crc32) & 0x000000FF)];
        !           252: }
        !           253: 
        !           254: const unsigned long pa_crc32(const char *in, size_t in_size);
        !           255: const unsigned long pa_crc32(const String& file_spec);
        !           256: static void file_crc32_file_action(
        !           257:                             struct stat& finfo, 
        !           258:                             int f, 
        !           259:                             const String& file_spec, const char* /*fname*/, bool, 
        !           260:                             void *context);
1.93      paf       261: 
1.106     paf       262: int pa_get_valid_file_options_count(HashStringValue& options);
                    263: 
1.93      paf       264: // globals
                    265: 
                    266: extern const String file_status_name;
1.1       paf       267: 
1.103     paf       268: // global defines for file options which are handled but not checked elsewhere, we check them
                    269: 
                    270: #define PA_SQL_LIMIT_NAME "limit"
                    271: #define PA_SQL_OFFSET_NAME "offset"
                    272: #define PA_COLUMN_SEPARATOR_NAME "separator"
                    273: #define PA_COLUMN_ENCLOSER_NAME "encloser"
                    274: 
1.1       paf       275: #endif

E-mail: