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

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

E-mail: