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

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.16      paf         5: 
1.8       paf         6:        Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)
1.5       paf         7: 
1.18    ! paf         8:        $Id: pa_common.C,v 1.17 2001/03/19 17:56:27 paf Exp $
1.1       paf         9: */
                     10: 
                     11: #ifdef HAVE_CONFIG_H
                     12: #      include "pa_config.h"
                     13: #endif
                     14: 
1.2       paf        15: #include <fcntl.h>
                     16: #include <sys/types.h>
                     17: #include <sys/stat.h>
                     18: #include <io.h>
1.1       paf        19: #include <stdio.h>
1.8       paf        20: #include <string.h>
1.1       paf        21: 
                     22: #include "pa_common.h"
1.2       paf        23: #include "pa_types.h"
1.4       paf        24: #include "pa_exception.h"
1.14      paf        25: #include "pa_pool.h"
                     26: #include "pa_globals.h"
                     27: #include "pa_value.h"
                     28: #include "pa_hash.h"
                     29: #include "pa_string.h"
1.1       paf        30: 
                     31: #ifdef WIN32
                     32: 
                     33: int __vsnprintf(char *b, size_t s, const char *f, va_list l) {
                     34:        int r=_vsnprintf(b, --s, f, l);
                     35:        b[s]=0;
                     36:        return r;
                     37: }
                     38: int __snprintf(char *b, size_t s, const char *f, ...) {
                     39:        va_list l;
                     40:     va_start(l, f);
                     41:     int r=__vsnprintf(b, s, f, l);
                     42:     va_end(l);
                     43:        return r;
                     44: }
                     45: 
                     46: #endif
1.2       paf        47: 
1.18    ! paf        48: 
        !            49: #ifdef WIN32
        !            50: 
        !            51: void flock(int fd, int operation) {
        !            52:        lseek(fd, 0, SEEK_SET);
        !            53:        while(_locking(fd, operation, 1)!=0);
        !            54:        lseek(fd, 0, SEEK_SET);
        !            55: }
        !            56: 
        !            57: #endif
        !            58: 
        !            59: /// @todo define it
        !            60: #ifdef SUN
        !            61: 
        !            62: void flock(int fd, int operation) {
        !            63:        lseek(fd, 0, SEEK_SET);
        !            64:        lockf(fd, operation, 1);
        !            65:        lseek(fd, 0, SEEK_SET);
        !            66: }
        !            67: #endif
        !            68: 
        !            69: void lock(FILE *f, long position) {
        !            70:        flock(fileno(f), LOCK_EX);
        !            71: }
        !            72: void unlock(FILE *f) {
        !            73:        flock(fileno(f), LOCK_UN);
        !            74: }
        !            75: 
        !            76: 
1.7       paf        77: char *file_read(Pool& pool, const char *fname, bool fail_on_read_problem) {
1.2       paf        78:     int f;
                     79:     struct stat finfo;
1.8       paf        80:     if(fname && !stat(fname,&finfo) &&(f=open(fname,O_RDONLY
1.2       paf        81: #ifdef WIN32
                     82:                |O_TEXT
                     83: #endif
1.8       paf        84:                ))>=0) {
1.2       paf        85:                /*if(exclusive)
                     86:                        flock(f, LOCK_EX);*/
                     87: 
1.10      paf        88:                char *result=(char *)pool.malloc(finfo.st_size+1);
1.2       paf        89:                int read_size=read(f,result,finfo.st_size);
                     90:                if(read_size>=0 && read_size<=finfo.st_size) 
                     91:                        result[read_size]='\0';
                     92:                /*if(exclusive)
                     93:                        flock(f, LOCK_UN);*/
                     94:                close(f);
                     95:                return result;//prepare_config(result, remove_empty_lines);
                     96:     }
1.4       paf        97:        if(fail_on_read_problem)
1.11      paf        98:                PTHROW(0,0,
1.4       paf        99:                        0,
                    100:                        "use: can not read '%s' file", fname);
1.8       paf       101:     return 0;
                    102: }
                    103: 
1.18    ! paf       104: void file_write(Pool& pool, 
        !           105:                                const char *fname, 
        !           106:                                const char *data, size_t size, 
        !           107:                                bool exclusive) {
        !           108:        if(fname) {
        !           109:                int f;
        !           110:                if(access(fname, F_OK)!=0) {/*no*/
        !           111:                        if((f=open(fname,O_WRONLY|O_CREAT|_O_BINARY,0666))>0)
        !           112:                                close(f);
        !           113:                }
        !           114:                if(access(fname, R_OK|W_OK)==0) {
        !           115:                        if((f=open(fname,O_RDWR|_O_BINARY
        !           116: #ifdef WIN32
        !           117:                                |O_TRUNC
        !           118: #endif
        !           119:                                ,0666))>=0) {
        !           120:                                if(exclusive)
        !           121:                                        flock(f, LOCK_EX);
        !           122:                                
        !           123:                                if(size) write(f,data,size);
        !           124: #ifndef WIN32
        !           125:                                ftruncate(f,size);
        !           126: #endif
        !           127:                                if(exclusive)
        !           128:                                        flock(f, LOCK_UN);
        !           129:                                close(f);
        !           130:                                return;
        !           131:                        }
        !           132:                }
        !           133:        }
        !           134:        if(fname)
        !           135:                PTHROW(0, 0,
        !           136:                        0,
        !           137:                        "file_write('%s'): %s (#%d)", 
        !           138:                                fname, strerror(errno), errno);
        !           139:        else
        !           140:                PTHROW(0, 0,
        !           141:                        0,
        !           142:                        "file_write: no filename specified");
        !           143: }
        !           144: 
1.8       paf       145: char *getrow(char **row_ref, char delim) {
                    146:     char *result=*row_ref;
                    147:     if(result) {
                    148:                *row_ref=strchr(result, delim);
                    149:                if(*row_ref) 
                    150:                        *((*row_ref)++)=0; 
                    151:                else if(!*result) 
                    152:                        return 0;
                    153:     }
                    154:     return result;
                    155: }
                    156: 
                    157: char *lsplit(char *string_ref, char delim) {
                    158:     if(string_ref) {
                    159:                char *v=strchr(string_ref, delim);
                    160:                if(v) {
                    161:                        *v=0;
                    162:                        return v+1;
                    163:                }
                    164:     }
                    165:     return 0;
                    166: }
                    167: 
                    168: char *lsplit(char **string_ref, char delim) {
                    169:     char *result=*string_ref;
                    170:        char *next=lsplit(*string_ref, delim);
                    171:     *string_ref=next;
                    172:     return result;
1.9       paf       173: }
                    174: 
                    175: char *rsplit(char *string, char delim) {
1.18    ! paf       176:     if(string) {
1.9       paf       177:                char *v=strrchr(string, delim);
1.18    ! paf       178:                if(v) {
1.9       paf       179:                        *v=0;
                    180:                        return v+1;
                    181:                }
                    182:     }
                    183:     return NULL;       
1.10      paf       184: }
                    185: 
                    186: char *format(Pool& pool, double value, char *fmt) {
                    187:        char *result=(char *)pool.malloc(MAX_NUMBER);
                    188:        if(fmt)
                    189:                if(strpbrk(fmt, "diouxX"))
                    190:                        if(strpbrk(fmt, "ouxX"))
1.18    ! paf       191:                                snprintf(result, MAX_NUMBER, fmt,(uint)value );
1.10      paf       192:                        else
1.18    ! paf       193:                                snprintf(result, MAX_NUMBER, fmt,(int)value );
1.10      paf       194:                else
                    195:                        snprintf(result, MAX_NUMBER, fmt, value);
                    196:        else
1.18    ! paf       197:                snprintf(result, MAX_NUMBER, "%d",(int)value);
1.10      paf       198:        
                    199:        return result;
1.12      paf       200: }
                    201: 
1.13      paf       202: size_t stdout_write(const char *buf, size_t size) {
1.12      paf       203: #ifdef WIN32
                    204:        do{
                    205:                int chunk_written=fwrite(buf, 1, min(8*0x400, size), stdout);
                    206:                if(chunk_written<=0)
                    207:                        break;
                    208:                size-=chunk_written;
                    209:                buf+=chunk_written;
                    210:        } while(size>0);
                    211: 
                    212:        return size;
                    213: #else
1.13      paf       214:        return fwrite(buf, 1, size, stdout);
1.12      paf       215: #endif
1.2       paf       216: }
1.14      paf       217: 
                    218: const char *unescape_chars(Pool& pool, const char *cp, int len) {
                    219:        char *s=(char *)pool.malloc(len + 1);
                    220:        enum EscapeState {
                    221:                EscapeRest,
                    222:                EscapeFirst,
                    223:                EscapeSecond
                    224:        } escapeState=EscapeRest;
                    225:        int escapedValue=0;
                    226:        int srcPos=0;
                    227:        int dstPos=0;
                    228:        while(srcPos < len) {
                    229:                int ch=cp[srcPos];
                    230:                switch(escapeState) {
                    231:                        case EscapeRest:
                    232:                        if(ch=='%') {
                    233:                                escapeState=EscapeFirst;
                    234:                        } else if(ch=='+') {
                    235:                                s[dstPos++]=' ';
                    236:                        } else {
                    237:                                s[dstPos++]=ch; 
                    238:                        }
                    239:                        break;
                    240:                        case EscapeFirst:
                    241:                        escapedValue=hex_value[ch] << 4;        
                    242:                        escapeState=EscapeSecond;
                    243:                        break;
                    244:                        case EscapeSecond:
                    245:                        escapedValue +=hex_value[ch];
                    246:                        s[dstPos++]=escapedValue;
                    247:                        escapeState=EscapeRest;
                    248:                        break;
                    249:                }
                    250:                srcPos++;
                    251:        }
                    252:        s[dstPos]=0;
                    253:        return s;
                    254: }
                    255: 
1.17      paf       256: static void append_attribute_subattribute(const Hash::Key& akey, Hash::Val *avalue, 
1.14      paf       257:                                                                                  void *info) {
                    258:        if(akey==VALUE_NAME)
                    259:                return;
                    260: 
                    261:        // ...; charset=windows1251
                    262:        String *string=static_cast<String *>(info);
                    263:        string->APPEND_CONST("; ");
                    264:        string->append(akey, String::Untaint_lang::HEADER, true);
                    265:        string->APPEND_CONST("=");
                    266:        string->append(static_cast<Value *>(avalue)->as_string(), 
                    267:                String::Untaint_lang::HEADER, true);
                    268: }
                    269: const String& attributed_meaning_string(Value *meaning) {
                    270:        String &result=*new(meaning->pool()) String(meaning->pool());
                    271:        if(Hash *hash=meaning->get_hash()) {
                    272:                // $value(value) $subattribute(subattribute value)
                    273:                if(Value *value=static_cast<Value *>(hash->get(*value_name)))
                    274:                        result.append(value->as_string(), String::Untaint_lang::HEADER, true);
                    275: 
                    276:                hash->foreach(append_attribute_subattribute, &result);
                    277:        } else // result value
                    278:                result.append(meaning->as_string(), String::Untaint_lang::HEADER, true);
                    279: 
                    280:        return result;
                    281: }

E-mail: