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

1.15      paf         1: /** @file
1.16      paf         2:        Parser: commonly used functions.
                      3: 
1.89      paf         4:        Copyright (c) 2001, 2003 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.89.2.2! paf        11: static const char* IDENT_COMMON_H="$Date: 2003/01/23 17:05:33 $";
1.1       paf        12: 
1.2       paf        13: #include "pa_pool.h"
1.33      paf        14: #include "pa_string.h"
1.1       paf        15: 
1.89.2.1  paf        16: template<typename K, typename V> class Hash;
                     17: typedef Hash<ConstStringPtr, ConstStringPtr> StringHash;
                     18: 
1.87      paf        19: class Value;
1.64      paf        20: 
                     21: // replace system s*nprintf with our versions
1.69      paf        22: #undef vsnprintf 
1.63      paf        23: int __vsnprintf(char *, size_t, const char *, va_list);
1.64      paf        24: #define vsnprintf __vsnprintf 
                     25: #undef snprintf
1.63      paf        26: int __snprintf(char *, size_t, const char *, ...);
1.64      paf        27: #define snprintf __snprintf
1.63      paf        28: 
1.21      paf        29: #if _MSC_VER
1.56      paf        30: /*
                     31: inline int open( const char *filename, int oflag ) { return _open(filename, oflag); }
                     32: inline int close( int handle ) { return _close(handle); }
                     33: inline int read( int handle, void *buffer, unsigned int count ) { return _read(handle,buffer,count); }
                     34: inline int write( int handle, const void *buffer, unsigned int count ) { return _write(handle,buffer,count); }
                     35: inline int stat( const char *path, struct _stat *buffer ) { return _stat(path, buffer); }
                     36: inline long lseek( int handle, long offset, int origin ) { return _lseek(handle, offset, origin); }
                     37: */
1.17      paf        38: 
                     39: //access
                     40: #define F_OK 0
                     41: #define X_OK 1
                     42: #define W_OK 2
                     43: #define R_OK 4
                     44: 
1.20      paf        45: #ifndef strcasecmp
                     46: #      define strcasecmp _stricmp
                     47: #endif
                     48: #ifndef strncasecmp
                     49: #      define strncasecmp _strnicmp
                     50: #endif
                     51: #ifndef mkdir
                     52: #      define mkdir(path, mode) _mkdir(path)
                     53: #endif
1.17      paf        54: 
1.20      paf        55: #ifndef putenv
                     56: #      define putenv _putenv
1.1       paf        57: #endif
1.2       paf        58: 
1.17      paf        59: #endif
1.59      paf        60: 
1.61      paf        61: #ifdef HAVE_TRUNC
                     62: #      ifndef trunc
                     63: extern "C" double trunc(double);
                     64: #      endif
                     65: #else
1.59      paf        66: inline double trunc(double param) { return param > 0? floor(param) : ceil(param); }
                     67: #endif
1.61      paf        68: 
                     69: #ifdef HAVE_ROUND
                     70: #      ifndef round
                     71: extern "C" double round(double);
                     72: #      endif
                     73: #else
1.59      paf        74: inline double round(double param) { return floor(param+0.5); }
                     75: #endif
1.61      paf        76: #ifdef HAVE_SIGN
                     77: #      ifndef sign
                     78: extern "C" double sign(double);
                     79: #      endif
                     80: #else
1.59      paf        81: inline double sign(double param) { return param > 0 ? 1 : ( param < 0 ? -1 : 0 ); }
                     82: #endif
1.87      paf        83: 
1.72      paf        84: /// yields to OS for secs secs and usecs milliseconds
                     85: int pa_sleep(unsigned long secs, unsigned long usecs);
1.54      parser     86: 
                     87: /** under WIN32 "t" mode fixes DOS chars OK, 
                     88:        can't say that about other systems/ line break styles
                     89: */
1.57      paf        90: void fix_line_breaks(
1.62      paf        91:                                         char *buf,
                     92:                                         size_t& size ///< may change! used to speedup next actions
1.57      paf        93:                                         );
1.17      paf        94: 
1.89.2.1  paf        95: typedef void (*File_read_action)(
1.84      paf        96:                                                                 struct stat& finfo,
1.83      paf        97:                                                                 int f, 
1.89.2.2! paf        98:                                                                 ConstStringPtr file_spec, CharPtr fname, bool as_text,
1.83      paf        99:                                                                 void *context);
                    100: 
                    101: /**
                    102:        shared-lock specified file, 
                    103:        do actions under lock.
                    104:        if fail_on_read_problem is true[default] throws an exception
                    105:        
                    106:        @returns true if read OK
                    107: */
1.89.2.1  paf       108: bool file_read_action_under_lock(ConstStringPtr file_spec, 
1.83      paf       109:                                const char *action_name, File_read_action action, void *context,
                    110:                                bool as_text=false,
                    111:                                bool fail_on_read_problem=true);
1.15      paf       112: /**
1.19      paf       113:        read specified text file using pool, 
1.15      paf       114:        if fail_on_read_problem is true[default] throws an exception
1.83      paf       115: 
                    116:        @returns true if read OK
1.15      paf       117: */
1.89.2.1  paf       118: char *file_read_text(Pool& pool,
                    119:                                         ConstStringPtr file_spec, 
1.85      paf       120:                                         bool fail_on_read_problem=true,
1.89.2.1  paf       121:                                         StringHash* options=0, StringHash** out_fields=0);
1.17      paf       122: 
                    123: /**
1.29      paf       124:        read specified file using pool, 
                    125:        if fail_on_read_problem is true[default] throws an exception
1.83      paf       126: 
                    127:        @returns true if read OK
1.29      paf       128: */
1.89.2.1  paf       129: bool file_read(Pool& pool,
                    130:                           ConstStringPtr file_spec, 
                    131:                           void *& ptr, size_t& size, 
1.29      paf       132:                           bool as_text,
1.89.2.1  paf       133:                           StringHash* options=0, StringHash** out_fields=0,
1.83      paf       134:                           bool fail_on_read_problem=true);
                    135: 
                    136: typedef void (*File_write_action)(int f, void *context);
1.29      paf       137: 
                    138: /**
1.71      paf       139:        lock specified file exclusively, 
                    140:        do actions under lock.
                    141:        throws an exception in case of problems
1.72      paf       142:        
                    143:        if block=false does non-blocking lock
                    144:        @returns true if locked OK, or false if non-blocking locking failed
1.71      paf       145: */
1.72      paf       146: bool file_write_action_under_lock(
1.89.2.1  paf       147:                                ConstStringPtr file_spec, 
1.83      paf       148:                                const char *action_name, File_write_action action, void *context,
1.71      paf       149:                                bool as_text=false,
1.72      paf       150:                                bool do_append=false,
1.77      paf       151:                                bool do_block=true,
                    152:                                bool fail_on_lock_problem=true);
1.71      paf       153: 
                    154: /**
                    155:        write data to specified file, 
1.17      paf       156:        throws an exception in case of problems
                    157: */
1.70      paf       158: void file_write(
1.89.2.1  paf       159:                                ConstStringPtr file_spec,
1.29      paf       160:                                const void *data, size_t size, 
1.58      paf       161:                                bool as_text,
1.67      paf       162:                                bool do_append=false);
1.26      paf       163: 
                    164: /**
                    165:        delete specified file 
                    166:        throws an exception in case of problems
                    167: */
1.89.2.1  paf       168: bool file_delete(ConstStringPtr file_spec, bool fail_on_read_problem=true);
1.47      parser    169: /**
                    170:        move specified file 
                    171:        throws an exception in case of problems
                    172: */
1.89.2.1  paf       173: void file_move(ConstStringPtr old_spec, ConstStringPtr new_spec);
1.27      paf       174: 
1.81      paf       175: bool entry_exists(const char *fname, struct stat *afinfo=0);
1.89.2.1  paf       176: bool entry_exists(ConstStringPtr file_spec);
                    177: bool file_readable(ConstStringPtr file_spec);
                    178: bool dir_readable(ConstStringPtr file_spec);
                    179: String *file_readable(ConstStringPtr path, ConstStringPtr name);
                    180: bool file_executable(ConstStringPtr file_spec);
1.37      paf       181: 
1.89.2.1  paf       182: bool file_stat(ConstStringPtr file_spec, 
1.46      parser    183:                           size_t& rsize, 
                    184:                           time_t& ratime,
                    185:                           time_t& rmtime,
1.48      parser    186:                           time_t& rctime,
                    187:                           bool fail_on_read_problem=true);
1.15      paf       188: 
                    189: /**
1.18      paf       190:        scans for @a delim[default \n] in @a *row_ref, 
                    191:        @return piece of line before it or end of string, if no @a delim found
                    192:        assigns @a *row_ref to point right after delimiter if there were one
                    193:        or to zero if no @a delim were found.
1.15      paf       194: */
1.7       paf       195: char *getrow(char **row_ref,char delim='\n');
1.76      paf       196: char *lsplit(char *string, char delim);
1.7       paf       197: char *lsplit(char **string_ref,char delim);
1.8       paf       198: char *rsplit(char *string, char delim);
1.89.2.1  paf       199: char *format(double value, char *fmt);
1.10      paf       200: 
                    201: #ifndef max
1.75      paf       202: inline int max(int a, int b) { return a>b?a:b; }
                    203: inline int min(int a, int b){ return a<b?a:b; }
                    204: inline size_t max(size_t a, size_t b) { return a>b?a:b; }
                    205: inline size_t min(size_t a, size_t b){ return a<b?a:b; }
1.10      paf       206: #endif
                    207: 
1.30      paf       208: size_t stdout_write(const void *buf, size_t size);
1.14      paf       209: 
1.89.2.1  paf       210: char *unescape_chars(const char *cp, int len);
1.23      paf       211: 
                    212: #ifdef WIN32
                    213: void back_slashes_to_slashes(char *s);
1.35      paf       214: //void slashes_to_back_slashes(char *s);
1.23      paf       215: #endif
                    216: 
1.28      paf       217: #ifndef _qsort
                    218: #      define _qsort(names,cnt,sizeof_names,func_addr) \
                    219:                qsort(names,cnt,sizeof_names,func_addr)
                    220: #endif
1.34      paf       221: 
1.40      paf       222: bool StrEqNc(const char *s1, const char *s2, bool strict=true);
1.45      parser    223: 
                    224: #define SECS_PER_DAY (60*60*24)
                    225: int getMonthDays(int year, int month);
1.52      parser    226: 
                    227: void remove_crlf(char *start, char *end);
1.1       paf       228: 
                    229: #endif

E-mail: