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

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

E-mail: