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

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.119   ! paf         8: static const char* IDENT_COMMON_C="$Date: 2002/08/23 07:32:24 $";
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;
1.119   ! paf       345: }
        !           346: 
        !           347: bool entry_exists(const String& file_spec) {
        !           348:        const char *fname=file_spec.cstr(String::UL_FILE_SPEC);
        !           349:        return entry_exists(fname, 0);
1.118     paf       350: }
                    351: 
1.51      paf       352: static bool entry_readable(const String& file_spec, bool need_dir) {
1.64      parser    353:     const char *fname=file_spec.cstr(String::UL_FILE_SPEC);
1.51      paf       354:        struct stat finfo;
1.118     paf       355:        if(access(fname, R_OK)==0 && entry_exists(fname, &finfo)) {
1.109     paf       356:                bool is_dir=(finfo.st_mode&S_IFDIR) != 0;
1.51      paf       357:                return is_dir==need_dir;
                    358:        }
                    359:        return false;
                    360: }
1.31      paf       361: bool file_readable(const String& file_spec) {
1.51      paf       362:        return entry_readable(file_spec, false);
                    363: }
                    364: bool dir_readable(const String& file_spec) {
                    365:        return entry_readable(file_spec, true);
1.65      parser    366: }
                    367: String *file_readable(const String& path, const String& name) {
                    368:        String *result=new(path.pool()) String(path);
                    369:        *result << "/";
                    370:        *result << name;
                    371:        return file_readable(*result)?result:0;
1.43      paf       372: }
                    373: bool file_executable(const String& file_spec) {
1.64      parser    374:     return access(file_spec.cstr(String::UL_FILE_SPEC), X_OK)==0;
1.44      paf       375: }
                    376: 
1.64      parser    377: bool file_stat(const String& file_spec, 
1.58      parser    378:                           size_t& rsize, 
                    379:                           time_t& ratime,
                    380:                           time_t& rmtime,
1.64      parser    381:                           time_t& rctime,
                    382:                           bool fail_on_read_problem) {
1.44      paf       383:        Pool& pool=file_spec.pool();
1.64      parser    384:        const char *fname=file_spec.cstr(String::UL_FILE_SPEC);
1.44      paf       385:     struct stat finfo;
                    386:        if(stat(fname, &finfo)!=0)
1.64      parser    387:                if(fail_on_read_problem)
1.106     paf       388:                        throw Exception("file.missing",
1.67      parser    389:                                &file_spec, 
                    390:                                "getting file size failed: %s (%d), real filename '%s'", 
                    391:                                        strerror(errno), errno, fname);
1.64      parser    392:                else
                    393:                        return false;
1.58      parser    394:        rsize=finfo.st_size;
                    395:        ratime=finfo.st_atime;
                    396:        rmtime=finfo.st_mtime;
                    397:        rctime=finfo.st_ctime;
1.64      parser    398:        return true;
1.18      paf       399: }
                    400: 
1.8       paf       401: char *getrow(char **row_ref, char delim) {
                    402:     char *result=*row_ref;
                    403:     if(result) {
                    404:                *row_ref=strchr(result, delim);
                    405:                if(*row_ref) 
                    406:                        *((*row_ref)++)=0; 
                    407:                else if(!*result) 
                    408:                        return 0;
                    409:     }
                    410:     return result;
                    411: }
                    412: 
1.23      paf       413: char *lsplit(char *string, char delim) {
                    414:     if(string) {
                    415:                char *v=strchr(string, delim);
1.8       paf       416:                if(v) {
                    417:                        *v=0;
                    418:                        return v+1;
                    419:                }
                    420:     }
                    421:     return 0;
                    422: }
                    423: 
                    424: char *lsplit(char **string_ref, char delim) {
                    425:     char *result=*string_ref;
                    426:        char *next=lsplit(*string_ref, delim);
                    427:     *string_ref=next;
                    428:     return result;
1.9       paf       429: }
                    430: 
                    431: char *rsplit(char *string, char delim) {
1.18      paf       432:     if(string) {
1.9       paf       433:                char *v=strrchr(string, delim);
1.18      paf       434:                if(v) {
1.9       paf       435:                        *v=0;
                    436:                        return v+1;
                    437:                }
                    438:     }
                    439:     return NULL;       
1.10      paf       440: }
                    441: 
1.37      paf       442: /// @todo less stupid type detection
1.10      paf       443: char *format(Pool& pool, double value, char *fmt) {
1.108     paf       444:        char local_buf[MAX_NUMBER];
                    445:        size_t size;
                    446:        
1.10      paf       447:        if(fmt)
                    448:                if(strpbrk(fmt, "diouxX"))
                    449:                        if(strpbrk(fmt, "ouxX"))
1.108     paf       450:                                size=snprintf(local_buf, sizeof(local_buf), fmt, (uint)value);
1.10      paf       451:                        else
1.108     paf       452:                                size=snprintf(local_buf, sizeof(local_buf), fmt, (int)value);
1.10      paf       453:                else
1.108     paf       454:                        size=snprintf(local_buf, sizeof(local_buf), fmt, value);
1.10      paf       455:        else
1.108     paf       456:                size=snprintf(local_buf, sizeof(local_buf), "%d", (int)value);
1.10      paf       457:        
1.108     paf       458:        char *pool_buf=(char *)pool.malloc(size+1, 4);
                    459:        memcpy(pool_buf, local_buf, size+1);
                    460:        return pool_buf;
1.12      paf       461: }
                    462: 
1.36      paf       463: size_t stdout_write(const void *buf, size_t size) {
1.12      paf       464: #ifdef WIN32
                    465:        do{
                    466:                int chunk_written=fwrite(buf, 1, min(8*0x400, size), stdout);
                    467:                if(chunk_written<=0)
                    468:                        break;
                    469:                size-=chunk_written;
1.36      paf       470:                buf=((const char*)buf)+chunk_written;
1.12      paf       471:        } while(size>0);
                    472: 
                    473:        return size;
                    474: #else
1.13      paf       475:        return fwrite(buf, 1, size, stdout);
1.12      paf       476: #endif
1.2       paf       477: }
1.14      paf       478: 
1.77      parser    479: char *unescape_chars(Pool& pool, const char *cp, int len) {
1.78      paf       480:        char *s=(char *)pool.malloc(len + 1, 5);
1.14      paf       481:        enum EscapeState {
1.33      paf       482:                EscapeRest, 
                    483:                EscapeFirst, 
1.14      paf       484:                EscapeSecond
                    485:        } escapeState=EscapeRest;
                    486:        int escapedValue=0;
                    487:        int srcPos=0;
                    488:        int dstPos=0;
                    489:        while(srcPos < len) {
                    490:                int ch=cp[srcPos];
                    491:                switch(escapeState) {
                    492:                        case EscapeRest:
                    493:                        if(ch=='%') {
                    494:                                escapeState=EscapeFirst;
                    495:                        } else if(ch=='+') {
                    496:                                s[dstPos++]=' ';
                    497:                        } else {
                    498:                                s[dstPos++]=ch; 
                    499:                        }
                    500:                        break;
                    501:                        case EscapeFirst:
                    502:                        escapedValue=hex_value[ch] << 4;        
                    503:                        escapeState=EscapeSecond;
                    504:                        break;
                    505:                        case EscapeSecond:
                    506:                        escapedValue +=hex_value[ch];
                    507:                        s[dstPos++]=escapedValue;
                    508:                        escapeState=EscapeRest;
                    509:                        break;
                    510:                }
                    511:                srcPos++;
                    512:        }
                    513:        s[dstPos]=0;
                    514:        return s;
1.24      paf       515: }
                    516: 
                    517: #ifdef WIN32
                    518: void back_slashes_to_slashes(char *s) {
                    519:        if(s)
                    520:                for(; *s; s++)
                    521:                        if(*s=='\\')
                    522:                                *s='/';
                    523: }
1.42      paf       524: /*
                    525: void slashes_to_back_slashes(char *s) {
                    526:        if(s)
                    527:                for(; *s; s++)
                    528:                        if(*s=='/')
                    529:                                *s='\\';
                    530: }
                    531: */
1.24      paf       532: #endif
1.41      paf       533: 
                    534: bool StrEqNc(const char *s1, const char *s2, bool strict) {
                    535:        while(true) {
                    536:                if(!(*s1)) {
                    537:                        if(!(*s2))
                    538:                                return true;
                    539:                        else
                    540:                                return !strict;
                    541:                } else if(!(*s2))
                    542:                        return !strict;
                    543:                if(isalpha(*s1)) {
                    544:                        if(tolower(*s1) !=tolower(*s2))
                    545:                                return false;
                    546:                } else if((*s1) !=(*s2))
                    547:                        return false;
                    548:                s1++;
                    549:                s2++;
                    550:        }
1.57      parser    551: }
                    552: 
1.84      paf       553: static bool isLeap(int year) {
1.57      parser    554:     return !(
                    555:              (year % 4) || ((year % 400) && !(year % 100))
                    556:             );
                    557: }
                    558: 
                    559: int getMonthDays(int year, int month) {
                    560:     int monthDays[]={
                    561:         31,
                    562:         isLeap(year) ? 29 : 28,
                    563:         31,
                    564:         30,
                    565:         31,
                    566:         30,
                    567:         31,
                    568:         31,
                    569:         30,
                    570:         31,
                    571:         30,
                    572:         31
                    573:     };
                    574:     return monthDays[month];
1.41      paf       575: }
1.69      parser    576: 
                    577: void remove_crlf(char *start, char *end) {
                    578:        for(char *p=start; p<end; p++)
                    579:                switch(*p) {
                    580:                        case '\n': *p='|'; break;
                    581:                        case '\r': *p=' '; break;
                    582:                }
1.91      paf       583: }
                    584: 
                    585: 
                    586: /// must be last in this file
                    587: #undef vsnprintf
                    588: int __vsnprintf(char *b, size_t s, const char *f, va_list l) {
                    589:        if(!s)
                    590:                return 0;
                    591: 
                    592:        int r;
                    593:        // note: on win32& maybe somewhere else
                    594:        // vsnprintf do not writes terminating 0 in 'buffer full' case, reducing
                    595:        --s;
                    596: #if _MSC_VER
                    597:        /*
                    598:        win32: 
                    599:        mk:@MSITStore:C:\Program%20Files\Microsoft%20Visual%20Studio\MSDN\2001APR\1033\vccore.chm::/html/_crt__vsnprintf.2c_._vsnwprintf.htm
                    600: 
                    601:          if the number of bytes to write exceeds buffer, then count bytes are written and –1 is returned
                    602:        */
                    603:        r=_vsnprintf(b, s, f, l);
                    604:        if(r<0) 
                    605:                r=s;
                    606: #else
                    607:        r=vsnprintf(b, s, f, l);
                    608:        /*
                    609:        solaris: 
                    610:        man vsnprintf
                    611: 
                    612:          The snprintf() function returns  the  number  of  characters
                    613:        formatted, that is, the number of characters that would have
                    614:        been written to the buffer if it were large enough.  If  the
                    615:        value  of  n  is  0  on a call to snprintf(), an unspecified
                    616:        value less than 1 is returned.
                    617:        */
                    618: 
                    619:        if(r<0)
                    620:                r=0;
                    621:        else if(r>s)
                    622:                r=s;
                    623: #endif
                    624:        b[r]=0;
                    625:        return r;
                    626: }
                    627: 
                    628: int __snprintf(char *b, size_t s, const char *f, ...) {
                    629:        va_list l;
                    630:     va_start(l, f);
                    631:     int r=__vsnprintf(b, s, f, l);
                    632:     va_end(l);
                    633:        return r;
1.98      paf       634: }
                    635: 
                    636: int pa_sleep(unsigned long secs, unsigned long usecs) {
                    637:        for (; usecs >= 1000000; ++secs, usecs -= 1000000);
                    638: 
                    639: #ifdef WIN32
                    640:        Sleep(secs * 1000 + usecs / 1000);
                    641:        return 0;
                    642: #else
                    643:        struct timeval t;
                    644:        t.tv_sec = secs;
                    645:        t.tv_usec = usecs;
                    646:        return (select(0, NULL, NULL, NULL, &t) == -1 ? errno : 0);
                    647: #endif
1.74      parser    648: }

E-mail: