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

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

E-mail: