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

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.40    ! paf         9:        $Id: pa_common.C,v 1.39 2001/04/03 17:01:03 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.34      paf        48:        void *result;
                     49:        size_t size;
1.35      paf        50:        return file_read(pool, file_spec, result, size, true, 
                     51:                fail_on_read_problem)?(char *)result:0;
1.34      paf        52: }
                     53: bool file_read(Pool& pool, const String& file_spec, 
                     54:                           void*& data, size_t& size, bool as_text,
                     55:                           bool fail_on_read_problem) {
1.39      paf        56:        char *fname=file_spec.cstr(String::UL_FILE_NAME);
1.33      paf        57:        int f;
1.2       paf        58:     struct stat finfo;
1.33      paf        59: 
                     60:        // first open, next stat:
                     61:        // directory update of NTFS symbolic links performed on open.
                     62:        // ex: 
                     63:        //   a.html:^test[] and b.html hardlink to a.html
                     64:        //   user inserts ! before ^test in a.html
                     65:        //   directory entry of b.html in NTFS not updated at once,
1.35      paf        66:        //   they delay update till open, so we would receive "!^test[" string
                     67:        //   if would do stat, next open.
1.34      paf        68:     if(
                     69:                (f=open(fname, O_RDONLY|(as_text?_O_TEXT:_O_BINARY)))>=0 && 
                     70:                stat(fname, &finfo)==0) {
1.2       paf        71:                /*if(exclusive)
                     72:                        flock(f, LOCK_EX);*/
1.34      paf        73:                data=pool.malloc(finfo.st_size+(as_text?1:0));
                     74:                size=read(f, data, finfo.st_size);
1.2       paf        75:                /*if(exclusive)
                     76:                        flock(f, LOCK_UN);*/
                     77:                close(f);
1.32      paf        78: 
1.34      paf        79:                if(size>=0 && size<=(size_t)finfo.st_size) {
                     80:                        if(as_text)
                     81:                                ((char *)data)[size]=0;
                     82:                } else
1.33      paf        83:                        PTHROW(0, 0, 
                     84:                                &file_spec, 
                     85:                                "read failed: actually read %d bytes count not in [0..%ul] valid range", 
1.35      paf        86:                                        size, (unsigned long)finfo.st_size); //never
1.32      paf        87:                
1.34      paf        88:                return true;//prepare_config(result, remove_empty_lines);
1.2       paf        89:     }
1.4       paf        90:        if(fail_on_read_problem)
1.33      paf        91:                PTHROW(0, 0, 
                     92:                        &file_spec, 
1.29      paf        93:                        "read failed: %s (#%d)", strerror(errno), errno);
1.34      paf        94:     return false;
1.8       paf        95: }
                     96: 
1.38      paf        97: /// @test mkdirs
1.18      paf        98: void file_write(Pool& pool, 
1.28      paf        99:                                const String& file_spec, 
1.34      paf       100:                                const void *data, size_t size, 
1.33      paf       101:                                bool as_text/*, 
1.20      paf       102:                                bool exclusive*/) {
1.39      paf       103:        char *fname=file_spec.cstr(String::UL_FILE_NAME);
1.28      paf       104:        int f;
                    105:        if(access(fname, F_OK)!=0) {/*no*/
1.33      paf       106:                if((f=open(fname, O_WRONLY|O_CREAT|_O_BINARY, 0666))>0)
1.28      paf       107:                        close(f);
                    108:        }
                    109:        if(access(fname, R_OK|W_OK)==0) {
1.34      paf       110:                int mode=O_RDWR|(as_text?_O_TEXT:_O_BINARY)
1.18      paf       111: #ifdef WIN32
1.28      paf       112:                        |O_TRUNC
1.18      paf       113: #endif
1.28      paf       114:                ;
1.33      paf       115:                if((f=open(fname, mode, 0666))>=0) {
1.28      paf       116:                        /*if(exclusive)
                    117:                                flock(f, LOCK_EX);*/
                    118:                        
1.33      paf       119:                        if(size) write(f, data, size);
1.18      paf       120: #ifndef WIN32
1.33      paf       121:                        ftruncate(f, size);
1.18      paf       122: #endif
1.28      paf       123:                        /*if(exclusive)
                    124:                                flock(f, LOCK_UN);*/
                    125:                        close(f);
                    126:                        return;
1.18      paf       127:                }
                    128:        }
1.33      paf       129:        PTHROW(0, 0, 
                    130:                &file_spec, 
1.29      paf       131:                "write failed: %s (#%d)", strerror(errno), errno);
1.30      paf       132: }
                    133: 
                    134: void file_delete(Pool& pool, const String& file_spec) {
1.39      paf       135:        if(unlink(file_spec.cstr(String::UL_FILE_NAME))!=0)
1.33      paf       136:                PTHROW(0, 0, 
                    137:                        &file_spec, 
1.30      paf       138:                        "unlink failed: %s (#%d)", strerror(errno), errno);
1.31      paf       139: }
                    140: 
                    141: bool file_readable(const String& file_spec) {
1.39      paf       142:     return access(file_spec.cstr(String::UL_FILE_NAME), R_OK)==0;
1.18      paf       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: 
1.23      paf       157: char *lsplit(char *string, char delim) {
                    158:     if(string) {
                    159:                char *v=strchr(string, delim);
1.8       paf       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: 
1.37      paf       186: /// @todo less stupid type detection
1.10      paf       187: char *format(Pool& pool, double value, char *fmt) {
                    188:        char *result=(char *)pool.malloc(MAX_NUMBER);
                    189:        if(fmt)
                    190:                if(strpbrk(fmt, "diouxX"))
                    191:                        if(strpbrk(fmt, "ouxX"))
1.33      paf       192:                                snprintf(result, MAX_NUMBER, fmt, (uint)value );
1.10      paf       193:                        else
1.33      paf       194:                                snprintf(result, MAX_NUMBER, fmt, (int)value );
1.10      paf       195:                else
                    196:                        snprintf(result, MAX_NUMBER, fmt, value);
                    197:        else
1.33      paf       198:                snprintf(result, MAX_NUMBER, "%d", (int)value);
1.10      paf       199:        
                    200:        return result;
1.12      paf       201: }
                    202: 
1.36      paf       203: size_t stdout_write(const void *buf, size_t size) {
1.12      paf       204: #ifdef WIN32
                    205:        do{
                    206:                int chunk_written=fwrite(buf, 1, min(8*0x400, size), stdout);
                    207:                if(chunk_written<=0)
                    208:                        break;
                    209:                size-=chunk_written;
1.36      paf       210:                buf=((const char*)buf)+chunk_written;
1.12      paf       211:        } while(size>0);
                    212: 
                    213:        return size;
                    214: #else
1.13      paf       215:        return fwrite(buf, 1, size, stdout);
1.12      paf       216: #endif
1.2       paf       217: }
1.14      paf       218: 
                    219: const char *unescape_chars(Pool& pool, const char *cp, int len) {
                    220:        char *s=(char *)pool.malloc(len + 1);
                    221:        enum EscapeState {
1.33      paf       222:                EscapeRest, 
                    223:                EscapeFirst, 
1.14      paf       224:                EscapeSecond
                    225:        } escapeState=EscapeRest;
                    226:        int escapedValue=0;
                    227:        int srcPos=0;
                    228:        int dstPos=0;
                    229:        while(srcPos < len) {
                    230:                int ch=cp[srcPos];
                    231:                switch(escapeState) {
                    232:                        case EscapeRest:
                    233:                        if(ch=='%') {
                    234:                                escapeState=EscapeFirst;
                    235:                        } else if(ch=='+') {
                    236:                                s[dstPos++]=' ';
                    237:                        } else {
                    238:                                s[dstPos++]=ch; 
                    239:                        }
                    240:                        break;
                    241:                        case EscapeFirst:
                    242:                        escapedValue=hex_value[ch] << 4;        
                    243:                        escapeState=EscapeSecond;
                    244:                        break;
                    245:                        case EscapeSecond:
                    246:                        escapedValue +=hex_value[ch];
                    247:                        s[dstPos++]=escapedValue;
                    248:                        escapeState=EscapeRest;
                    249:                        break;
                    250:                }
                    251:                srcPos++;
                    252:        }
                    253:        s[dstPos]=0;
                    254:        return s;
                    255: }
                    256: 
1.40    ! paf       257: struct Attributed_meaning_info {
        !           258:        String::Untaint_lang lang;
        !           259:        String *header;
        !           260: };
1.17      paf       261: static void append_attribute_subattribute(const Hash::Key& akey, Hash::Val *avalue, 
1.14      paf       262:                                                                                  void *info) {
                    263:        if(akey==VALUE_NAME)
                    264:                return;
                    265: 
1.40    ! paf       266:        Attributed_meaning_info& ami=*static_cast<Attributed_meaning_info *>(info);
        !           267: 
1.14      paf       268:        // ...; charset=windows1251
1.40    ! paf       269:        if(ami.header->size())
        !           270:                ami.header->APPEND_CONST("; ");
        !           271:        ami.header->append(akey, ami.lang, true);
        !           272:        ami.header->APPEND_CONST("=");
        !           273:        ami.header->append(static_cast<Value *>(avalue)->as_string(), 
        !           274:                ami.lang, true);
1.14      paf       275: }
1.40    ! paf       276: const String& attributed_meaning_to_string(Value& meaning, String::Untaint_lang lang) {
1.20      paf       277:        String &result=*new(meaning.pool()) String(meaning.pool());
                    278:        if(Hash *hash=meaning.get_hash()) {
1.14      paf       279:                // $value(value) $subattribute(subattribute value)
                    280:                if(Value *value=static_cast<Value *>(hash->get(*value_name)))
1.40    ! paf       281:                        result.append(value->as_string(), lang, true);
1.14      paf       282: 
1.40    ! paf       283:                Attributed_meaning_info attributed_meaning_info={
        !           284:                        lang,
        !           285:                        &result
        !           286:                };
        !           287:                hash->for_each(append_attribute_subattribute, &attributed_meaning_info);
1.14      paf       288:        } else // result value
1.40    ! paf       289:                result.append(meaning.as_string(), lang, true);
1.14      paf       290: 
                    291:        return result;
1.24      paf       292: }
                    293: 
                    294: #ifdef WIN32
                    295: void back_slashes_to_slashes(char *s) {
                    296:        if(s)
                    297:                for(; *s; s++)
                    298:                        if(*s=='\\')
                    299:                                *s='/';
                    300: }
                    301: #endif

E-mail: