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

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

E-mail: