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

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

E-mail: