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

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

E-mail: