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

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.117   ! paf         8: static const char* IDENT_COMMON_C="$Date: 2002/08/05 13:58:45 $";
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.62      parser    122: //printf("file_read(%s)\n", fname);
1.33      paf       123:        int f;
1.2       paf       124:     struct stat finfo;
1.33      paf       125: 
                    126:        // first open, next stat:
1.45      paf       127:        // directory update of NTFS hard links performed on open.
1.33      paf       128:        // ex: 
                    129:        //   a.html:^test[] and b.html hardlink to a.html
                    130:        //   user inserts ! before ^test in a.html
                    131:        //   directory entry of b.html in NTFS not updated at once,
1.35      paf       132:        //   they delay update till open, so we would receive "!^test[" string
                    133:        //   if would do stat, next open.
1.98      paf       134:     if((f=open(fname, O_RDONLY|(as_text?_O_TEXT:_O_BINARY)))>=0) {
1.110     paf       135:                if(lock_shared_blocking(f)!=0) {
                    136:                        Exception e("file.lock",
                    137:                                        &file_spec, 
                    138:                                        "shared lock failed: %s (%d), actual filename '%s'", 
                    139:                                                strerror(errno), errno, fname);
                    140:                        unlock(f);
                    141:                        close(f);
                    142:                        if(fail_on_read_problem)
                    143:                                throw e;
                    144:                        return false;
                    145:                }
1.98      paf       146:                if(stat(fname, &finfo)!=0) {
1.106     paf       147:                        Exception e("file.missing",
1.98      paf       148:                                        &file_spec, 
                    149:                                        "stat failed: %s (%d), actual filename '%s'", 
                    150:                                                strerror(errno), errno, fname);
1.99      paf       151:                        unlock(f);
1.98      paf       152:                        close(f);
                    153:                        if(fail_on_read_problem)
                    154:                                throw e;
                    155:                        return false;
                    156:                }
1.105     paf       157: #ifdef NO_FOREIGN_GROUP_FILES
                    158:                if(finfo.st_gid/*foreign?*/!=getegid()) {
1.107     paf       159:                        Exception e("parser.runtime",
1.105     paf       160:                                &file_spec,
                    161:                                "parser reading files of foreign group disabled [recompile parser without --disable-foreign-group-files configure option], actual filename '%s'", 
                    162:                                        fname);
                    163:                        unlock(f);
                    164:                        close(f);
                    165:                        if(fail_on_read_problem)
                    166:                                throw e;
                    167:                        return false;
                    168:                }
                    169: #endif
1.98      paf       170:                size_t max_size=limit?min(offset+limit, (size_t)finfo.st_size)-offset:finfo.st_size;
1.47      paf       171:                if(!max_size) { // eof
1.83      paf       172:                        if(as_text) {
                    173:                                data=pool.malloc(1);
                    174:                                *(char*)data=0;
                    175:                        } else 
                    176:                                data=0;
1.98      paf       177:                        data_size=0;
1.59      parser    178:                } else {
1.78      paf       179:                        data=pool.malloc(max_size+(as_text?1:0), 3);
1.59      parser    180:                        if(offset)
                    181:                                lseek(f, offset, SEEK_SET);
1.98      paf       182:                        data_size=read(f, data, max_size);
1.47      paf       183:                }
1.99      paf       184:                unlock(f);
1.2       paf       185:                close(f);
1.59      parser    186:                if(!max_size) // eof
                    187:                        return true;
1.32      paf       188: 
1.98      paf       189:                if(int(data_size)<0 || data_size>max_size)
1.106     paf       190:                        throw Exception(0,
1.33      paf       191:                                &file_spec, 
1.44      paf       192:                                "read failed: actually read %d bytes count not in [0..%lu] valid range", 
1.98      paf       193:                                        data_size, (unsigned long)max_size); //never
1.87      paf       194: 
                    195:                if(as_text) {
                    196:                        fix_line_breaks((char *)data, data_size);
                    197:                        // note: after fixing
                    198:                        ((char*&)data)[data_size]=0;
                    199:                }
1.72      parser    200:                return true;
1.2       paf       201:     }
1.4       paf       202:        if(fail_on_read_problem)
1.106     paf       203:                throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0,
1.33      paf       204:                        &file_spec, 
1.54      parser    205:                        "read failed: %s (%d), actual filename '%s'", 
                    206:                                strerror(errno), errno, fname);
1.34      paf       207:     return false;
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: 
                    339: static bool entry_readable(const String& file_spec, bool need_dir) {
1.64      parser    340:     const char *fname=file_spec.cstr(String::UL_FILE_SPEC);
1.51      paf       341:        struct stat finfo;
                    342:        if(access(fname, R_OK)==0 && stat(fname, &finfo)==0) {
1.109     paf       343:                bool is_dir=(finfo.st_mode&S_IFDIR) != 0;
1.51      paf       344:                return is_dir==need_dir;
                    345:        }
                    346:        return false;
                    347: }
1.31      paf       348: bool file_readable(const String& file_spec) {
1.51      paf       349:        return entry_readable(file_spec, false);
                    350: }
                    351: bool dir_readable(const String& file_spec) {
                    352:        return entry_readable(file_spec, true);
1.65      parser    353: }
                    354: String *file_readable(const String& path, const String& name) {
                    355:        String *result=new(path.pool()) String(path);
                    356:        *result << "/";
                    357:        *result << name;
                    358:        return file_readable(*result)?result:0;
1.43      paf       359: }
                    360: bool file_executable(const String& file_spec) {
1.64      parser    361:     return access(file_spec.cstr(String::UL_FILE_SPEC), X_OK)==0;
1.44      paf       362: }
                    363: 
1.64      parser    364: bool file_stat(const String& file_spec, 
1.58      parser    365:                           size_t& rsize, 
                    366:                           time_t& ratime,
                    367:                           time_t& rmtime,
1.64      parser    368:                           time_t& rctime,
                    369:                           bool fail_on_read_problem) {
1.44      paf       370:        Pool& pool=file_spec.pool();
1.64      parser    371:        const char *fname=file_spec.cstr(String::UL_FILE_SPEC);
1.44      paf       372:     struct stat finfo;
                    373:        if(stat(fname, &finfo)!=0)
1.64      parser    374:                if(fail_on_read_problem)
1.106     paf       375:                        throw Exception("file.missing",
1.67      parser    376:                                &file_spec, 
                    377:                                "getting file size failed: %s (%d), real filename '%s'", 
                    378:                                        strerror(errno), errno, fname);
1.64      parser    379:                else
                    380:                        return false;
1.58      parser    381:        rsize=finfo.st_size;
                    382:        ratime=finfo.st_atime;
                    383:        rmtime=finfo.st_mtime;
                    384:        rctime=finfo.st_ctime;
1.64      parser    385:        return true;
1.18      paf       386: }
                    387: 
1.8       paf       388: char *getrow(char **row_ref, char delim) {
                    389:     char *result=*row_ref;
                    390:     if(result) {
                    391:                *row_ref=strchr(result, delim);
                    392:                if(*row_ref) 
                    393:                        *((*row_ref)++)=0; 
                    394:                else if(!*result) 
                    395:                        return 0;
                    396:     }
                    397:     return result;
                    398: }
                    399: 
1.23      paf       400: char *lsplit(char *string, char delim) {
                    401:     if(string) {
                    402:                char *v=strchr(string, delim);
1.8       paf       403:                if(v) {
                    404:                        *v=0;
                    405:                        return v+1;
                    406:                }
                    407:     }
                    408:     return 0;
                    409: }
                    410: 
                    411: char *lsplit(char **string_ref, char delim) {
                    412:     char *result=*string_ref;
                    413:        char *next=lsplit(*string_ref, delim);
                    414:     *string_ref=next;
                    415:     return result;
1.9       paf       416: }
                    417: 
                    418: char *rsplit(char *string, char delim) {
1.18      paf       419:     if(string) {
1.9       paf       420:                char *v=strrchr(string, delim);
1.18      paf       421:                if(v) {
1.9       paf       422:                        *v=0;
                    423:                        return v+1;
                    424:                }
                    425:     }
                    426:     return NULL;       
1.10      paf       427: }
                    428: 
1.37      paf       429: /// @todo less stupid type detection
1.10      paf       430: char *format(Pool& pool, double value, char *fmt) {
1.108     paf       431:        char local_buf[MAX_NUMBER];
                    432:        size_t size;
                    433:        
1.10      paf       434:        if(fmt)
                    435:                if(strpbrk(fmt, "diouxX"))
                    436:                        if(strpbrk(fmt, "ouxX"))
1.108     paf       437:                                size=snprintf(local_buf, sizeof(local_buf), fmt, (uint)value);
1.10      paf       438:                        else
1.108     paf       439:                                size=snprintf(local_buf, sizeof(local_buf), fmt, (int)value);
1.10      paf       440:                else
1.108     paf       441:                        size=snprintf(local_buf, sizeof(local_buf), fmt, value);
1.10      paf       442:        else
1.108     paf       443:                size=snprintf(local_buf, sizeof(local_buf), "%d", (int)value);
1.10      paf       444:        
1.108     paf       445:        char *pool_buf=(char *)pool.malloc(size+1, 4);
                    446:        memcpy(pool_buf, local_buf, size+1);
                    447:        return pool_buf;
1.12      paf       448: }
                    449: 
1.36      paf       450: size_t stdout_write(const void *buf, size_t size) {
1.12      paf       451: #ifdef WIN32
                    452:        do{
                    453:                int chunk_written=fwrite(buf, 1, min(8*0x400, size), stdout);
                    454:                if(chunk_written<=0)
                    455:                        break;
                    456:                size-=chunk_written;
1.36      paf       457:                buf=((const char*)buf)+chunk_written;
1.12      paf       458:        } while(size>0);
                    459: 
                    460:        return size;
                    461: #else
1.13      paf       462:        return fwrite(buf, 1, size, stdout);
1.12      paf       463: #endif
1.2       paf       464: }
1.14      paf       465: 
1.77      parser    466: char *unescape_chars(Pool& pool, const char *cp, int len) {
1.78      paf       467:        char *s=(char *)pool.malloc(len + 1, 5);
1.14      paf       468:        enum EscapeState {
1.33      paf       469:                EscapeRest, 
                    470:                EscapeFirst, 
1.14      paf       471:                EscapeSecond
                    472:        } escapeState=EscapeRest;
                    473:        int escapedValue=0;
                    474:        int srcPos=0;
                    475:        int dstPos=0;
                    476:        while(srcPos < len) {
                    477:                int ch=cp[srcPos];
                    478:                switch(escapeState) {
                    479:                        case EscapeRest:
                    480:                        if(ch=='%') {
                    481:                                escapeState=EscapeFirst;
                    482:                        } else if(ch=='+') {
                    483:                                s[dstPos++]=' ';
                    484:                        } else {
                    485:                                s[dstPos++]=ch; 
                    486:                        }
                    487:                        break;
                    488:                        case EscapeFirst:
                    489:                        escapedValue=hex_value[ch] << 4;        
                    490:                        escapeState=EscapeSecond;
                    491:                        break;
                    492:                        case EscapeSecond:
                    493:                        escapedValue +=hex_value[ch];
                    494:                        s[dstPos++]=escapedValue;
                    495:                        escapeState=EscapeRest;
                    496:                        break;
                    497:                }
                    498:                srcPos++;
                    499:        }
                    500:        s[dstPos]=0;
                    501:        return s;
1.24      paf       502: }
                    503: 
                    504: #ifdef WIN32
                    505: void back_slashes_to_slashes(char *s) {
                    506:        if(s)
                    507:                for(; *s; s++)
                    508:                        if(*s=='\\')
                    509:                                *s='/';
                    510: }
1.42      paf       511: /*
                    512: void slashes_to_back_slashes(char *s) {
                    513:        if(s)
                    514:                for(; *s; s++)
                    515:                        if(*s=='/')
                    516:                                *s='\\';
                    517: }
                    518: */
1.24      paf       519: #endif
1.41      paf       520: 
                    521: bool StrEqNc(const char *s1, const char *s2, bool strict) {
                    522:        while(true) {
                    523:                if(!(*s1)) {
                    524:                        if(!(*s2))
                    525:                                return true;
                    526:                        else
                    527:                                return !strict;
                    528:                } else if(!(*s2))
                    529:                        return !strict;
                    530:                if(isalpha(*s1)) {
                    531:                        if(tolower(*s1) !=tolower(*s2))
                    532:                                return false;
                    533:                } else if((*s1) !=(*s2))
                    534:                        return false;
                    535:                s1++;
                    536:                s2++;
                    537:        }
1.57      parser    538: }
                    539: 
1.84      paf       540: static bool isLeap(int year) {
1.57      parser    541:     return !(
                    542:              (year % 4) || ((year % 400) && !(year % 100))
                    543:             );
                    544: }
                    545: 
                    546: int getMonthDays(int year, int month) {
                    547:     int monthDays[]={
                    548:         31,
                    549:         isLeap(year) ? 29 : 28,
                    550:         31,
                    551:         30,
                    552:         31,
                    553:         30,
                    554:         31,
                    555:         31,
                    556:         30,
                    557:         31,
                    558:         30,
                    559:         31
                    560:     };
                    561:     return monthDays[month];
1.41      paf       562: }
1.69      parser    563: 
                    564: void remove_crlf(char *start, char *end) {
                    565:        for(char *p=start; p<end; p++)
                    566:                switch(*p) {
                    567:                        case '\n': *p='|'; break;
                    568:                        case '\r': *p=' '; break;
                    569:                }
1.91      paf       570: }
                    571: 
                    572: 
                    573: /// must be last in this file
                    574: #undef vsnprintf
                    575: int __vsnprintf(char *b, size_t s, const char *f, va_list l) {
                    576:        if(!s)
                    577:                return 0;
                    578: 
                    579:        int r;
                    580:        // note: on win32& maybe somewhere else
                    581:        // vsnprintf do not writes terminating 0 in 'buffer full' case, reducing
                    582:        --s;
                    583: #if _MSC_VER
                    584:        /*
                    585:        win32: 
                    586:        mk:@MSITStore:C:\Program%20Files\Microsoft%20Visual%20Studio\MSDN\2001APR\1033\vccore.chm::/html/_crt__vsnprintf.2c_._vsnwprintf.htm
                    587: 
                    588:          if the number of bytes to write exceeds buffer, then count bytes are written and –1 is returned
                    589:        */
                    590:        r=_vsnprintf(b, s, f, l);
                    591:        if(r<0) 
                    592:                r=s;
                    593: #else
                    594:        r=vsnprintf(b, s, f, l);
                    595:        /*
                    596:        solaris: 
                    597:        man vsnprintf
                    598: 
                    599:          The snprintf() function returns  the  number  of  characters
                    600:        formatted, that is, the number of characters that would have
                    601:        been written to the buffer if it were large enough.  If  the
                    602:        value  of  n  is  0  on a call to snprintf(), an unspecified
                    603:        value less than 1 is returned.
                    604:        */
                    605: 
                    606:        if(r<0)
                    607:                r=0;
                    608:        else if(r>s)
                    609:                r=s;
                    610: #endif
                    611:        b[r]=0;
                    612:        return r;
                    613: }
                    614: 
                    615: int __snprintf(char *b, size_t s, const char *f, ...) {
                    616:        va_list l;
                    617:     va_start(l, f);
                    618:     int r=__vsnprintf(b, s, f, l);
                    619:     va_end(l);
                    620:        return r;
1.98      paf       621: }
                    622: 
                    623: int pa_sleep(unsigned long secs, unsigned long usecs) {
                    624:        for (; usecs >= 1000000; ++secs, usecs -= 1000000);
                    625: 
                    626: #ifdef WIN32
                    627:        Sleep(secs * 1000 + usecs / 1000);
                    628:        return 0;
                    629: #else
                    630:        struct timeval t;
                    631:        t.tv_sec = secs;
                    632:        t.tv_usec = usecs;
                    633:        return (select(0, NULL, NULL, NULL, &t) == -1 ? errno : 0);
                    634: #endif
1.74      parser    635: }

E-mail: