Annotation of parser3/src/main/pa_common.C, revision 1.118

1.15      paf         1: /** @file
1.16      paf         2:        Parser: commonly functions.
                      3: 
1.102     paf         4:        Copyright(c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.101     paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.111     paf         6: */
1.16      paf         7: 
1.118   ! paf         8: static const char* IDENT_COMMON_C="$Date: 2002/08/15 12:05:36 $";
1.1       paf         9: 
                     10: #include "pa_common.h"
1.4       paf        11: #include "pa_exception.h"
1.14      paf        12: #include "pa_globals.h"
1.1       paf        13: 
1.98      paf        14: #ifdef WIN32
                     15: #      include <windows.h>
                     16: #endif
                     17: 
1.93      paf        18: // some maybe-undefined constants
                     19: 
1.82      paf        20: #ifndef _O_TEXT
                     21: #      define _O_TEXT 0
                     22: #endif
                     23: #ifndef _O_BINARY
                     24: #      define _O_BINARY 0
1.47      paf        25: #endif
1.80      paf        26: #ifndef O_TRUNC
                     27: #      define O_TRUNC 0
                     28: #endif
                     29: 
1.93      paf        30: // locking constants
                     31: 
1.99      paf        32: #ifdef HAVE_FLOCK
                     33: 
                     34: static int lock_shared_blocking(int fd) { return flock(fd, LOCK_SH); }
                     35: static int lock_exclusive_blocking(int fd) { return flock(fd, LOCK_EX); }
                     36: static int lock_exclusive_nonblocking(int fd) { return flock(fd, LOCK_EX || LOCK_NB); }
                     37: static int unlock(int fd) { return flock(fd, LOCK_UN); }
                     38: 
1.98      paf        39: #else
1.99      paf        40: #ifdef HAVE__LOCKING
1.98      paf        41: 
1.99      paf        42: #define FLOCK(operation) lseek(fd, 0, SEEK_SET); return _locking(fd, operation, 1)
                     43: static int lock_shared_blocking(int fd) { FLOCK(_LK_LOCK); }
                     44: static int lock_exclusive_blocking(int fd) { FLOCK(_LK_LOCK); }
                     45: static int lock_exclusive_nonblocking(int fd) { FLOCK(_LK_NBLCK); }
                     46: static int unlock(int fd) { FLOCK(_LK_UNLCK); }
1.93      paf        47: 
1.99      paf        48: #else
                     49: #ifdef HAVE_FCNTL
1.93      paf        50: 
1.99      paf        51: #define FLOCK(cmd, arg) struct flock ls={arg, SEEK_SET}; return fcntl(fd, cmd, &ls)
                     52: static int lock_shared_blocking(int fd) { FLOCK(F_SETLKW, F_RDLCK); }
                     53: static int lock_exclusive_blocking(int fd) { FLOCK(F_SETLKW, F_WRLCK); }
                     54: static int lock_exclusive_nonblocking(int fd) { FLOCK(F_SETLK, F_RDLCK); }
                     55: static int unlock(int fd) { FLOCK(F_SETLK, F_UNLCK); }
1.93      paf        56: 
                     57: #else
                     58: #ifdef HAVE_LOCKF
1.99      paf        59: 
                     60: #define FLOCK(fd, operation) lseek(fd, 0, SEEK_SET); return lockf(fd, operation, 1)
                     61: static int lock_shared_blocking(int fd) { FLOCK(F_LOCK); } // on intel solaris man doesn't have doc on shared blocking
                     62: static int lock_exclusive_blocking(int fd) { FLOCK(F_LOCK); }
                     63: static int lock_exclusive_nonblocking(int fd) { FLOCK(F_TLOCK); }
                     64: static int unlock(int fd) { FLOCK(F_TLOCK); }
                     65: 
1.93      paf        66: #else
1.99      paf        67: 
                     68: #error unable to find file locking func
                     69: 
                     70: #endif
1.93      paf        71: #endif
                     72: #endif
                     73: #endif
                     74: 
1.86      paf        75: static char *strnchr(char *buf, size_t size, char c) {
1.103     paf        76:        // sanity check
                     77:        if(!buf)
                     78:                return 0;
                     79: 
1.86      paf        80:        for(; size-->0; buf++) {
                     81:                if(*buf==c)
                     82:                        return buf;
                     83:        }
                     84: 
                     85:        return 0;
                     86: }
                     87: 
                     88: void fix_line_breaks(char *buf, size_t& size) {
1.87      paf        89:        //_asm int 3;
1.86      paf        90:        const char * const eob=buf+size;
1.87      paf        91:        char *dest=buf;
1.72      parser     92:        // fix DOS: \r\n -> \n
                     93:        // fix Macintosh: \r -> \n
1.87      paf        94:        char *bol=buf;
1.86      paf        95:        while(char *eol=strnchr(bol, eob -bol, '\r')) {
1.72      parser     96:                size_t len=eol-bol;
                     97:                if(dest!=bol)
                     98:                        memcpy(dest, bol, len);
                     99:                dest+=len;
                    100:                *dest++='\n';
                    101: 
1.87      paf       102:                if(&eol[1]<eob && eol[1]=='\n') { // \r,\n = DOS
1.72      parser    103:                        bol=eol+2;
                    104:                        size--;
                    105:                } else // \r,not \n = Macintosh
                    106:                        bol=eol+1;
                    107:        }
                    108:        // last piece without \r, including terminating 0
                    109:        if(dest!=bol)
1.87      paf       110:                memcpy(dest, bol, eob-bol);
1.72      parser    111: }
1.18      paf       112: 
1.28      paf       113: char *file_read_text(Pool& pool, const String& file_spec, bool fail_on_read_problem) {
1.72      parser    114:        void *result;  size_t size;
                    115:        return file_read(pool, file_spec, result, size, true, fail_on_read_problem)?(char *)result:0;
1.34      paf       116: }
                    117: bool file_read(Pool& pool, const String& file_spec, 
1.87      paf       118:                           void*& data, size_t& data_size, bool as_text,
1.44      paf       119:                           bool fail_on_read_problem,
                    120:                           size_t offset, size_t limit) {
1.64      parser    121:        const char *fname=file_spec.cstr(String::UL_FILE_SPEC);
1.33      paf       122:        int f;
1.2       paf       123:     struct stat finfo;
1.33      paf       124: 
                    125:        // first open, next stat:
1.45      paf       126:        // directory update of NTFS hard links performed on open.
1.33      paf       127:        // ex: 
                    128:        //   a.html:^test[] and b.html hardlink to a.html
                    129:        //   user inserts ! before ^test in a.html
                    130:        //   directory entry of b.html in NTFS not updated at once,
1.35      paf       131:        //   they delay update till open, so we would receive "!^test[" string
                    132:        //   if would do stat, next open.
1.98      paf       133:     if((f=open(fname, O_RDONLY|(as_text?_O_TEXT:_O_BINARY)))>=0) {
1.110     paf       134:                if(lock_shared_blocking(f)!=0) {
                    135:                        Exception e("file.lock",
                    136:                                        &file_spec, 
                    137:                                        "shared lock failed: %s (%d), actual filename '%s'", 
                    138:                                                strerror(errno), errno, fname);
                    139:                        unlock(f);
                    140:                        close(f);
                    141:                        if(fail_on_read_problem)
                    142:                                throw e;
                    143:                        return false;
                    144:                }
1.98      paf       145:                if(stat(fname, &finfo)!=0) {
1.106     paf       146:                        Exception e("file.missing",
1.98      paf       147:                                        &file_spec, 
                    148:                                        "stat failed: %s (%d), actual filename '%s'", 
                    149:                                                strerror(errno), errno, fname);
1.99      paf       150:                        unlock(f);
1.98      paf       151:                        close(f);
                    152:                        if(fail_on_read_problem)
                    153:                                throw e;
                    154:                        return false;
                    155:                }
1.105     paf       156: #ifdef NO_FOREIGN_GROUP_FILES
                    157:                if(finfo.st_gid/*foreign?*/!=getegid()) {
1.107     paf       158:                        Exception e("parser.runtime",
1.105     paf       159:                                &file_spec,
                    160:                                "parser reading files of foreign group disabled [recompile parser without --disable-foreign-group-files configure option], actual filename '%s'", 
                    161:                                        fname);
                    162:                        unlock(f);
                    163:                        close(f);
                    164:                        if(fail_on_read_problem)
                    165:                                throw e;
                    166:                        return false;
                    167:                }
                    168: #endif
1.98      paf       169:                size_t max_size=limit?min(offset+limit, (size_t)finfo.st_size)-offset:finfo.st_size;
1.47      paf       170:                if(!max_size) { // eof
1.83      paf       171:                        if(as_text) {
                    172:                                data=pool.malloc(1);
                    173:                                *(char*)data=0;
                    174:                        } else 
                    175:                                data=0;
1.98      paf       176:                        data_size=0;
1.59      parser    177:                } else {
1.78      paf       178:                        data=pool.malloc(max_size+(as_text?1:0), 3);
1.59      parser    179:                        if(offset)
                    180:                                lseek(f, offset, SEEK_SET);
1.98      paf       181:                        data_size=read(f, data, max_size);
1.47      paf       182:                }
1.99      paf       183:                unlock(f);
1.2       paf       184:                close(f);
1.59      parser    185:                if(!max_size) // eof
                    186:                        return true;
1.32      paf       187: 
1.98      paf       188:                if(int(data_size)<0 || data_size>max_size)
1.106     paf       189:                        throw Exception(0,
1.33      paf       190:                                &file_spec, 
1.44      paf       191:                                "read failed: actually read %d bytes count not in [0..%lu] valid range", 
1.98      paf       192:                                        data_size, (unsigned long)max_size); //never
1.87      paf       193: 
                    194:                if(as_text) {
                    195:                        fix_line_breaks((char *)data, data_size);
                    196:                        // note: after fixing
                    197:                        ((char*&)data)[data_size]=0;
                    198:                }
1.72      parser    199:                return true;
1.118   ! paf       200:     } else {
        !           201:                if(fail_on_read_problem)
        !           202:                        throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0,
        !           203:                                &file_spec, 
        !           204:                                "read failed: %s (%d), actual filename '%s'", 
        !           205:                                        strerror(errno), errno, fname);
        !           206:                return false;
        !           207:        }
1.8       paf       208: }
                    209: 
1.63      parser    210: static void create_dir_for_file(const String& file_spec) {
                    211:        size_t pos_after=1;
                    212:        int pos_before;
                    213:        while((pos_before=file_spec.pos("/", 1, pos_after))>=0) {
1.64      parser    214:                mkdir(file_spec.mid(0, pos_before).cstr(String::UL_FILE_SPEC), 0775);
1.63      parser    215:                pos_after=pos_before+1;
                    216:        }
                    217: }
                    218: 
1.98      paf       219: bool file_write_action_under_lock(
1.28      paf       220:                                const String& file_spec, 
1.96      paf       221:                                const char *action_name, void (*action)(int, void *), void *context,
1.80      paf       222:                                bool as_text,
1.98      paf       223:                                bool do_append,
1.110     paf       224:                                bool do_block,
                    225:                                bool fail_on_lock_problem) {
1.64      parser    226:        const char *fname=file_spec.cstr(String::UL_FILE_SPEC);
1.28      paf       227:        int f;
1.80      paf       228:        if(access(fname, W_OK)!=0) // no
1.63      parser    229:                create_dir_for_file(file_spec);
1.50      paf       230: 
1.80      paf       231:        if((f=open(fname, 
                    232:                O_CREAT|O_RDWR
                    233:                |(as_text?_O_TEXT:_O_BINARY)
1.104     paf       234:                |(do_append?O_APPEND:O_TRUNC), 0664))>=0) {
1.99      paf       235:                if((do_block?lock_exclusive_blocking(f):lock_exclusive_nonblocking(f))!=0) {
1.110     paf       236:                        Exception e("file.lock",
                    237:                                &file_spec, 
                    238:                                "shared lock failed: %s (%d), actual filename '%s'", 
                    239:                                strerror(errno), errno, fname);
1.98      paf       240:                        close(f);
1.110     paf       241:                        if(fail_on_lock_problem)
                    242:                                throw e;
1.98      paf       243:                        return false;
                    244:                }
1.96      paf       245: 
                    246:                try {
                    247:                        action(f, context);
                    248:                } catch(...) {
1.98      paf       249: #if O_TRUNC==0
1.104     paf       250:                        if(!do_append)
                    251:                                ftruncate(f, tell(f));
1.98      paf       252: #endif
1.99      paf       253:                        unlock(f);
1.96      paf       254:                        close(f);
                    255:                        /*re*/throw;
                    256:                }
1.80      paf       257:                
1.98      paf       258: #if O_TRUNC==0
1.104     paf       259:                if(!do_append)
                    260:                        ftruncate(f, tell(f));
1.98      paf       261: #endif
1.99      paf       262:                unlock(f);
1.80      paf       263:                close(f);
1.98      paf       264:                return true;
1.80      paf       265:        } else
1.106     paf       266:                throw Exception(errno==EACCES?"file.access":0,
1.80      paf       267:                        &file_spec, 
1.96      paf       268:                        "%s failed: %s (%d), actual filename '%s'", 
                    269:                                action_name, strerror(errno), errno, fname);
                    270:        // here should be nothing, see rethrow above
                    271: }
                    272: 
                    273: #ifndef DOXYGEN
                    274: struct File_write_action_info {
                    275:        const void *data; size_t size;
                    276: };
                    277: #endif
                    278: static void file_write_action(int f, void *context) {
                    279:        File_write_action_info& info=*static_cast<File_write_action_info *>(context);
1.113     paf       280:        if(info.size) {
1.116     paf       281:                int written=write(f, info.data, info.size);
                    282:                if(written<0)
1.113     paf       283:                        throw Exception(0,
                    284:                                0,
1.115     paf       285:                                "write failed: %s (%d)",  strerror(errno), errno);
1.113     paf       286:        }
1.96      paf       287: }
                    288: void file_write(
                    289:                                const String& file_spec, 
                    290:                                const void *data, size_t size, 
                    291:                                bool as_text,
                    292:                                bool do_append) {
                    293:        File_write_action_info info={data, size};
1.98      paf       294:        file_write_action_under_lock(
1.96      paf       295:                                file_spec, 
                    296:                                "write", file_write_action, &info,
                    297:                                as_text,
                    298:                                do_append);
1.30      paf       299: }
                    300: 
1.63      parser    301: // throws nothing! [this is required in file_move & file_delete]
1.50      paf       302: static void rmdir(const String& file_spec, size_t pos_after) {
                    303:        int pos_before;
                    304:        if((pos_before=file_spec.pos("/", 1, pos_after))>=0)
                    305:                rmdir(file_spec, pos_before+1);
                    306:        
1.64      parser    307:        rmdir(file_spec.mid(0, pos_after-1/* / */).cstr(String::UL_FILE_SPEC));
1.50      paf       308: }
1.95      paf       309: bool file_delete(const String& file_spec, bool fail_on_read_problem) {
1.64      parser    310:        const char *fname=file_spec.cstr(String::UL_FILE_SPEC);
1.54      parser    311:        if(unlink(fname)!=0)
1.93      paf       312:                if(fail_on_read_problem)
1.106     paf       313:                        throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0,
1.93      paf       314:                                &file_spec, 
                    315:                                "unlink failed: %s (%d), actual filename '%s'", 
                    316:                                        strerror(errno), errno, fname);
                    317:                else
                    318:                        return false;
1.50      paf       319: 
                    320:        rmdir(file_spec, 1);
1.93      paf       321:        return true;
1.60      parser    322: }
1.95      paf       323: void file_move(const String& old_spec, const String& new_spec) {
1.64      parser    324:        const char *old_spec_cstr=old_spec.cstr(String::UL_FILE_SPEC);
                    325:        const char *new_spec_cstr=new_spec.cstr(String::UL_FILE_SPEC);
1.63      parser    326:        
                    327:        create_dir_for_file(new_spec);
                    328: 
1.60      parser    329:        if(rename(old_spec_cstr, new_spec_cstr)!=0)
1.106     paf       330:                throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0,
1.60      parser    331:                        &old_spec, 
                    332:                        "rename failed: %s (%d), actual filename '%s' to '%s'", 
                    333:                                strerror(errno), errno, old_spec_cstr, new_spec_cstr);
1.63      parser    334: 
                    335:        rmdir(old_spec, 1);
1.31      paf       336: }
                    337: 
1.51      paf       338: 
1.118   ! paf       339: bool entry_exists(const char *fname, struct stat *afinfo) {
        !           340:        struct stat lfinfo;
        !           341:        bool result=stat(fname, &lfinfo)==0;
        !           342:        if(afinfo)
        !           343:                *afinfo=lfinfo;
        !           344:        return result;
        !           345: }
        !           346: 
1.51      paf       347: static bool entry_readable(const String& file_spec, bool need_dir) {
1.64      parser    348:     const char *fname=file_spec.cstr(String::UL_FILE_SPEC);
1.51      paf       349:        struct stat finfo;
1.118   ! paf       350:        if(access(fname, R_OK)==0 && entry_exists(fname, &finfo)) {
1.109     paf       351:                bool is_dir=(finfo.st_mode&S_IFDIR) != 0;
1.51      paf       352:                return is_dir==need_dir;
                    353:        }
                    354:        return false;
                    355: }
1.31      paf       356: bool file_readable(const String& file_spec) {
1.51      paf       357:        return entry_readable(file_spec, false);
                    358: }
                    359: bool dir_readable(const String& file_spec) {
                    360:        return entry_readable(file_spec, true);
1.65      parser    361: }
                    362: String *file_readable(const String& path, const String& name) {
                    363:        String *result=new(path.pool()) String(path);
                    364:        *result << "/";
                    365:        *result << name;
                    366:        return file_readable(*result)?result:0;
1.43      paf       367: }
                    368: bool file_executable(const String& file_spec) {
1.64      parser    369:     return access(file_spec.cstr(String::UL_FILE_SPEC), X_OK)==0;
1.44      paf       370: }
                    371: 
1.64      parser    372: bool file_stat(const String& file_spec, 
1.58      parser    373:                           size_t& rsize, 
                    374:                           time_t& ratime,
                    375:                           time_t& rmtime,
1.64      parser    376:                           time_t& rctime,
                    377:                           bool fail_on_read_problem) {
1.44      paf       378:        Pool& pool=file_spec.pool();
1.64      parser    379:        const char *fname=file_spec.cstr(String::UL_FILE_SPEC);
1.44      paf       380:     struct stat finfo;
                    381:        if(stat(fname, &finfo)!=0)
1.64      parser    382:                if(fail_on_read_problem)
1.106     paf       383:                        throw Exception("file.missing",
1.67      parser    384:                                &file_spec, 
                    385:                                "getting file size failed: %s (%d), real filename '%s'", 
                    386:                                        strerror(errno), errno, fname);
1.64      parser    387:                else
                    388:                        return false;
1.58      parser    389:        rsize=finfo.st_size;
                    390:        ratime=finfo.st_atime;
                    391:        rmtime=finfo.st_mtime;
                    392:        rctime=finfo.st_ctime;
1.64      parser    393:        return true;
1.18      paf       394: }
                    395: 
1.8       paf       396: char *getrow(char **row_ref, char delim) {
                    397:     char *result=*row_ref;
                    398:     if(result) {
                    399:                *row_ref=strchr(result, delim);
                    400:                if(*row_ref) 
                    401:                        *((*row_ref)++)=0; 
                    402:                else if(!*result) 
                    403:                        return 0;
                    404:     }
                    405:     return result;
                    406: }
                    407: 
1.23      paf       408: char *lsplit(char *string, char delim) {
                    409:     if(string) {
                    410:                char *v=strchr(string, delim);
1.8       paf       411:                if(v) {
                    412:                        *v=0;
                    413:                        return v+1;
                    414:                }
                    415:     }
                    416:     return 0;
                    417: }
                    418: 
                    419: char *lsplit(char **string_ref, char delim) {
                    420:     char *result=*string_ref;
                    421:        char *next=lsplit(*string_ref, delim);
                    422:     *string_ref=next;
                    423:     return result;
1.9       paf       424: }
                    425: 
                    426: char *rsplit(char *string, char delim) {
1.18      paf       427:     if(string) {
1.9       paf       428:                char *v=strrchr(string, delim);
1.18      paf       429:                if(v) {
1.9       paf       430:                        *v=0;
                    431:                        return v+1;
                    432:                }
                    433:     }
                    434:     return NULL;       
1.10      paf       435: }
                    436: 
1.37      paf       437: /// @todo less stupid type detection
1.10      paf       438: char *format(Pool& pool, double value, char *fmt) {
1.108     paf       439:        char local_buf[MAX_NUMBER];
                    440:        size_t size;
                    441:        
1.10      paf       442:        if(fmt)
                    443:                if(strpbrk(fmt, "diouxX"))
                    444:                        if(strpbrk(fmt, "ouxX"))
1.108     paf       445:                                size=snprintf(local_buf, sizeof(local_buf), fmt, (uint)value);
1.10      paf       446:                        else
1.108     paf       447:                                size=snprintf(local_buf, sizeof(local_buf), fmt, (int)value);
1.10      paf       448:                else
1.108     paf       449:                        size=snprintf(local_buf, sizeof(local_buf), fmt, value);
1.10      paf       450:        else
1.108     paf       451:                size=snprintf(local_buf, sizeof(local_buf), "%d", (int)value);
1.10      paf       452:        
1.108     paf       453:        char *pool_buf=(char *)pool.malloc(size+1, 4);
                    454:        memcpy(pool_buf, local_buf, size+1);
                    455:        return pool_buf;
1.12      paf       456: }
                    457: 
1.36      paf       458: size_t stdout_write(const void *buf, size_t size) {
1.12      paf       459: #ifdef WIN32
                    460:        do{
                    461:                int chunk_written=fwrite(buf, 1, min(8*0x400, size), stdout);
                    462:                if(chunk_written<=0)
                    463:                        break;
                    464:                size-=chunk_written;
1.36      paf       465:                buf=((const char*)buf)+chunk_written;
1.12      paf       466:        } while(size>0);
                    467: 
                    468:        return size;
                    469: #else
1.13      paf       470:        return fwrite(buf, 1, size, stdout);
1.12      paf       471: #endif
1.2       paf       472: }
1.14      paf       473: 
1.77      parser    474: char *unescape_chars(Pool& pool, const char *cp, int len) {
1.78      paf       475:        char *s=(char *)pool.malloc(len + 1, 5);
1.14      paf       476:        enum EscapeState {
1.33      paf       477:                EscapeRest, 
                    478:                EscapeFirst, 
1.14      paf       479:                EscapeSecond
                    480:        } escapeState=EscapeRest;
                    481:        int escapedValue=0;
                    482:        int srcPos=0;
                    483:        int dstPos=0;
                    484:        while(srcPos < len) {
                    485:                int ch=cp[srcPos];
                    486:                switch(escapeState) {
                    487:                        case EscapeRest:
                    488:                        if(ch=='%') {
                    489:                                escapeState=EscapeFirst;
                    490:                        } else if(ch=='+') {
                    491:                                s[dstPos++]=' ';
                    492:                        } else {
                    493:                                s[dstPos++]=ch; 
                    494:                        }
                    495:                        break;
                    496:                        case EscapeFirst:
                    497:                        escapedValue=hex_value[ch] << 4;        
                    498:                        escapeState=EscapeSecond;
                    499:                        break;
                    500:                        case EscapeSecond:
                    501:                        escapedValue +=hex_value[ch];
                    502:                        s[dstPos++]=escapedValue;
                    503:                        escapeState=EscapeRest;
                    504:                        break;
                    505:                }
                    506:                srcPos++;
                    507:        }
                    508:        s[dstPos]=0;
                    509:        return s;
1.24      paf       510: }
                    511: 
                    512: #ifdef WIN32
                    513: void back_slashes_to_slashes(char *s) {
                    514:        if(s)
                    515:                for(; *s; s++)
                    516:                        if(*s=='\\')
                    517:                                *s='/';
                    518: }
1.42      paf       519: /*
                    520: void slashes_to_back_slashes(char *s) {
                    521:        if(s)
                    522:                for(; *s; s++)
                    523:                        if(*s=='/')
                    524:                                *s='\\';
                    525: }
                    526: */
1.24      paf       527: #endif
1.41      paf       528: 
                    529: bool StrEqNc(const char *s1, const char *s2, bool strict) {
                    530:        while(true) {
                    531:                if(!(*s1)) {
                    532:                        if(!(*s2))
                    533:                                return true;
                    534:                        else
                    535:                                return !strict;
                    536:                } else if(!(*s2))
                    537:                        return !strict;
                    538:                if(isalpha(*s1)) {
                    539:                        if(tolower(*s1) !=tolower(*s2))
                    540:                                return false;
                    541:                } else if((*s1) !=(*s2))
                    542:                        return false;
                    543:                s1++;
                    544:                s2++;
                    545:        }
1.57      parser    546: }
                    547: 
1.84      paf       548: static bool isLeap(int year) {
1.57      parser    549:     return !(
                    550:              (year % 4) || ((year % 400) && !(year % 100))
                    551:             );
                    552: }
                    553: 
                    554: int getMonthDays(int year, int month) {
                    555:     int monthDays[]={
                    556:         31,
                    557:         isLeap(year) ? 29 : 28,
                    558:         31,
                    559:         30,
                    560:         31,
                    561:         30,
                    562:         31,
                    563:         31,
                    564:         30,
                    565:         31,
                    566:         30,
                    567:         31
                    568:     };
                    569:     return monthDays[month];
1.41      paf       570: }
1.69      parser    571: 
                    572: void remove_crlf(char *start, char *end) {
                    573:        for(char *p=start; p<end; p++)
                    574:                switch(*p) {
                    575:                        case '\n': *p='|'; break;
                    576:                        case '\r': *p=' '; break;
                    577:                }
1.91      paf       578: }
                    579: 
                    580: 
                    581: /// must be last in this file
                    582: #undef vsnprintf
                    583: int __vsnprintf(char *b, size_t s, const char *f, va_list l) {
                    584:        if(!s)
                    585:                return 0;
                    586: 
                    587:        int r;
                    588:        // note: on win32& maybe somewhere else
                    589:        // vsnprintf do not writes terminating 0 in 'buffer full' case, reducing
                    590:        --s;
                    591: #if _MSC_VER
                    592:        /*
                    593:        win32: 
                    594:        mk:@MSITStore:C:\Program%20Files\Microsoft%20Visual%20Studio\MSDN\2001APR\1033\vccore.chm::/html/_crt__vsnprintf.2c_._vsnwprintf.htm
                    595: 
                    596:          if the number of bytes to write exceeds buffer, then count bytes are written and –1 is returned
                    597:        */
                    598:        r=_vsnprintf(b, s, f, l);
                    599:        if(r<0) 
                    600:                r=s;
                    601: #else
                    602:        r=vsnprintf(b, s, f, l);
                    603:        /*
                    604:        solaris: 
                    605:        man vsnprintf
                    606: 
                    607:          The snprintf() function returns  the  number  of  characters
                    608:        formatted, that is, the number of characters that would have
                    609:        been written to the buffer if it were large enough.  If  the
                    610:        value  of  n  is  0  on a call to snprintf(), an unspecified
                    611:        value less than 1 is returned.
                    612:        */
                    613: 
                    614:        if(r<0)
                    615:                r=0;
                    616:        else if(r>s)
                    617:                r=s;
                    618: #endif
                    619:        b[r]=0;
                    620:        return r;
                    621: }
                    622: 
                    623: int __snprintf(char *b, size_t s, const char *f, ...) {
                    624:        va_list l;
                    625:     va_start(l, f);
                    626:     int r=__vsnprintf(b, s, f, l);
                    627:     va_end(l);
                    628:        return r;
1.98      paf       629: }
                    630: 
                    631: int pa_sleep(unsigned long secs, unsigned long usecs) {
                    632:        for (; usecs >= 1000000; ++secs, usecs -= 1000000);
                    633: 
                    634: #ifdef WIN32
                    635:        Sleep(secs * 1000 + usecs / 1000);
                    636:        return 0;
                    637: #else
                    638:        struct timeval t;
                    639:        t.tv_sec = secs;
                    640:        t.tv_usec = usecs;
                    641:        return (select(0, NULL, NULL, NULL, &t) == -1 ? errno : 0);
                    642: #endif
1.74      parser    643: }

E-mail: