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

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.29    ! paf         9:        $Id: pa_common.C,v 1.28 2001/03/24 15:58:00 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.2       paf        48:     int f;
                     49:     struct stat finfo;
1.28      paf        50:        char *fname=file_spec.cstr();
                     51:     if(!stat(fname,&finfo) &&(f=open(fname,O_RDONLY
1.2       paf        52: #ifdef WIN32
                     53:                |O_TEXT
                     54: #endif
1.8       paf        55:                ))>=0) {
1.2       paf        56:                /*if(exclusive)
                     57:                        flock(f, LOCK_EX);*/
                     58: 
1.10      paf        59:                char *result=(char *)pool.malloc(finfo.st_size+1);
1.2       paf        60:                int read_size=read(f,result,finfo.st_size);
                     61:                if(read_size>=0 && read_size<=finfo.st_size) 
                     62:                        result[read_size]='\0';
                     63:                /*if(exclusive)
                     64:                        flock(f, LOCK_UN);*/
                     65:                close(f);
                     66:                return result;//prepare_config(result, remove_empty_lines);
                     67:     }
1.4       paf        68:        if(fail_on_read_problem)
1.28      paf        69:                PTHROW(0, 0,
                     70:                        &file_spec,
1.29    ! paf        71:                        "read failed: %s (#%d)", strerror(errno), errno);
1.8       paf        72:     return 0;
                     73: }
                     74: 
1.18      paf        75: void file_write(Pool& pool, 
1.28      paf        76:                                const String& file_spec, 
1.18      paf        77:                                const char *data, size_t size, 
1.20      paf        78:                                bool as_text/*,
                     79:                                bool exclusive*/) {
1.28      paf        80:        char *fname=file_spec.cstr();
                     81:        int f;
                     82:        if(access(fname, F_OK)!=0) {/*no*/
                     83:                if((f=open(fname,O_WRONLY|O_CREAT|_O_BINARY,0666))>0)
                     84:                        close(f);
                     85:        }
                     86:        if(access(fname, R_OK|W_OK)==0) {
                     87:                int mode=O_RDWR
1.18      paf        88: #ifdef WIN32
1.28      paf        89:                        |O_TRUNC
1.18      paf        90: #endif
1.28      paf        91:                ;
                     92:                mode|=as_text?_O_TEXT:_O_BINARY;
                     93:                if((f=open(fname,mode,0666))>=0) {
                     94:                        /*if(exclusive)
                     95:                                flock(f, LOCK_EX);*/
                     96:                        
                     97:                        if(size) write(f,data,size);
1.18      paf        98: #ifndef WIN32
1.28      paf        99:                        ftruncate(f,size);
1.18      paf       100: #endif
1.28      paf       101:                        /*if(exclusive)
                    102:                                flock(f, LOCK_UN);*/
                    103:                        close(f);
                    104:                        return;
1.18      paf       105:                }
                    106:        }
1.28      paf       107:        PTHROW(0, 0,
                    108:                &file_spec,
1.29    ! paf       109:                "write failed: %s (#%d)", strerror(errno), errno);
1.18      paf       110: }
                    111: 
1.8       paf       112: char *getrow(char **row_ref, char delim) {
                    113:     char *result=*row_ref;
                    114:     if(result) {
                    115:                *row_ref=strchr(result, delim);
                    116:                if(*row_ref) 
                    117:                        *((*row_ref)++)=0; 
                    118:                else if(!*result) 
                    119:                        return 0;
                    120:     }
                    121:     return result;
                    122: }
                    123: 
1.23      paf       124: char *lsplit(char *string, char delim) {
                    125:     if(string) {
                    126:                char *v=strchr(string, delim);
1.8       paf       127:                if(v) {
                    128:                        *v=0;
                    129:                        return v+1;
                    130:                }
                    131:     }
                    132:     return 0;
                    133: }
                    134: 
                    135: char *lsplit(char **string_ref, char delim) {
                    136:     char *result=*string_ref;
                    137:        char *next=lsplit(*string_ref, delim);
                    138:     *string_ref=next;
                    139:     return result;
1.9       paf       140: }
                    141: 
                    142: char *rsplit(char *string, char delim) {
1.18      paf       143:     if(string) {
1.9       paf       144:                char *v=strrchr(string, delim);
1.18      paf       145:                if(v) {
1.9       paf       146:                        *v=0;
                    147:                        return v+1;
                    148:                }
                    149:     }
                    150:     return NULL;       
1.10      paf       151: }
                    152: 
                    153: char *format(Pool& pool, double value, char *fmt) {
                    154:        char *result=(char *)pool.malloc(MAX_NUMBER);
                    155:        if(fmt)
                    156:                if(strpbrk(fmt, "diouxX"))
                    157:                        if(strpbrk(fmt, "ouxX"))
1.18      paf       158:                                snprintf(result, MAX_NUMBER, fmt,(uint)value );
1.10      paf       159:                        else
1.18      paf       160:                                snprintf(result, MAX_NUMBER, fmt,(int)value );
1.10      paf       161:                else
                    162:                        snprintf(result, MAX_NUMBER, fmt, value);
                    163:        else
1.18      paf       164:                snprintf(result, MAX_NUMBER, "%d",(int)value);
1.10      paf       165:        
                    166:        return result;
1.12      paf       167: }
                    168: 
1.13      paf       169: size_t stdout_write(const char *buf, size_t size) {
1.12      paf       170: #ifdef WIN32
                    171:        do{
                    172:                int chunk_written=fwrite(buf, 1, min(8*0x400, size), stdout);
                    173:                if(chunk_written<=0)
                    174:                        break;
                    175:                size-=chunk_written;
                    176:                buf+=chunk_written;
                    177:        } while(size>0);
                    178: 
                    179:        return size;
                    180: #else
1.13      paf       181:        return fwrite(buf, 1, size, stdout);
1.12      paf       182: #endif
1.2       paf       183: }
1.14      paf       184: 
                    185: const char *unescape_chars(Pool& pool, const char *cp, int len) {
                    186:        char *s=(char *)pool.malloc(len + 1);
                    187:        enum EscapeState {
                    188:                EscapeRest,
                    189:                EscapeFirst,
                    190:                EscapeSecond
                    191:        } escapeState=EscapeRest;
                    192:        int escapedValue=0;
                    193:        int srcPos=0;
                    194:        int dstPos=0;
                    195:        while(srcPos < len) {
                    196:                int ch=cp[srcPos];
                    197:                switch(escapeState) {
                    198:                        case EscapeRest:
                    199:                        if(ch=='%') {
                    200:                                escapeState=EscapeFirst;
                    201:                        } else if(ch=='+') {
                    202:                                s[dstPos++]=' ';
                    203:                        } else {
                    204:                                s[dstPos++]=ch; 
                    205:                        }
                    206:                        break;
                    207:                        case EscapeFirst:
                    208:                        escapedValue=hex_value[ch] << 4;        
                    209:                        escapeState=EscapeSecond;
                    210:                        break;
                    211:                        case EscapeSecond:
                    212:                        escapedValue +=hex_value[ch];
                    213:                        s[dstPos++]=escapedValue;
                    214:                        escapeState=EscapeRest;
                    215:                        break;
                    216:                }
                    217:                srcPos++;
                    218:        }
                    219:        s[dstPos]=0;
                    220:        return s;
                    221: }
                    222: 
1.17      paf       223: static void append_attribute_subattribute(const Hash::Key& akey, Hash::Val *avalue, 
1.14      paf       224:                                                                                  void *info) {
                    225:        if(akey==VALUE_NAME)
                    226:                return;
                    227: 
                    228:        // ...; charset=windows1251
                    229:        String *string=static_cast<String *>(info);
                    230:        string->APPEND_CONST("; ");
1.20      paf       231:        string->append(akey, String::UL_HEADER, true);
1.14      paf       232:        string->APPEND_CONST("=");
                    233:        string->append(static_cast<Value *>(avalue)->as_string(), 
1.20      paf       234:                String::UL_HEADER, true);
1.14      paf       235: }
1.20      paf       236: const String& attributed_meaning_to_string(Value& meaning) {
                    237:        String &result=*new(meaning.pool()) String(meaning.pool());
                    238:        if(Hash *hash=meaning.get_hash()) {
1.14      paf       239:                // $value(value) $subattribute(subattribute value)
                    240:                if(Value *value=static_cast<Value *>(hash->get(*value_name)))
1.20      paf       241:                        result.append(value->as_string(), String::UL_HEADER, true);
1.14      paf       242: 
1.25      paf       243:                hash->for_each(append_attribute_subattribute, &result);
1.14      paf       244:        } else // result value
1.20      paf       245:                result.append(meaning.as_string(), String::UL_HEADER, true);
1.14      paf       246: 
                    247:        return result;
1.24      paf       248: }
                    249: 
                    250: #ifdef WIN32
                    251: void back_slashes_to_slashes(char *s) {
                    252:        if(s)
                    253:                for(; *s; s++)
                    254:                        if(*s=='\\')
                    255:                                *s='/';
                    256: }
                    257: #endif

E-mail: