|
|
1.1 paf 1: /*
1.5 paf 2: Parser
1.8 paf 3: Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)
4: Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)
1.5 paf 5:
1.14 ! paf 6: $Id: pa_common.C,v 1.13 2001/03/18 14:45:27 paf Exp $
1.1 paf 7: */
8:
9: #ifdef HAVE_CONFIG_H
10: # include "pa_config.h"
11: #endif
12:
1.2 paf 13: #include <fcntl.h>
14: #include <sys/types.h>
15: #include <sys/stat.h>
16: #include <io.h>
1.1 paf 17: #include <stdio.h>
1.8 paf 18: #include <string.h>
1.1 paf 19:
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:
29: #ifdef WIN32
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.7 paf 46: char *file_read(Pool& pool, const char *fname, bool fail_on_read_problem) {
1.2 paf 47: int f;
48: struct stat finfo;
1.8 paf 49: if(fname && !stat(fname,&finfo) &&(f=open(fname,O_RDONLY
1.2 paf 50: #ifdef WIN32
51: |O_TEXT
52: #endif
1.8 paf 53: ))>=0) {
1.2 paf 54: /*if(exclusive)
55: flock(f, LOCK_EX);*/
56:
1.10 paf 57: char *result=(char *)pool.malloc(finfo.st_size+1);
1.2 paf 58: int read_size=read(f,result,finfo.st_size);
59: if(read_size>=0 && read_size<=finfo.st_size)
60: result[read_size]='\0';
61: /*if(exclusive)
62: flock(f, LOCK_UN);*/
63: close(f);
64: return result;//prepare_config(result, remove_empty_lines);
65: }
1.4 paf 66: if(fail_on_read_problem)
1.11 paf 67: PTHROW(0,0,
1.4 paf 68: 0,
69: "use: can not read '%s' file", fname);
1.8 paf 70: return 0;
71: }
72:
73: char *getrow(char **row_ref, char delim) {
74: char *result=*row_ref;
75: if(result) {
76: *row_ref=strchr(result, delim);
77: if(*row_ref)
78: *((*row_ref)++)=0;
79: else if(!*result)
80: return 0;
81: }
82: return result;
83: }
84:
85: char *lsplit(char *string_ref, char delim) {
86: if(string_ref) {
87: char *v=strchr(string_ref, delim);
88: if(v) {
89: *v=0;
90: return v+1;
91: }
92: }
93: return 0;
94: }
95:
96: char *lsplit(char **string_ref, char delim) {
97: char *result=*string_ref;
98: char *next=lsplit(*string_ref, delim);
99: *string_ref=next;
100: return result;
1.9 paf 101: }
102:
103: char *rsplit(char *string, char delim) {
104: if(string){
105: char *v=strrchr(string, delim);
106: if (v){
107: *v=0;
108: return v+1;
109: }
110: }
111: return NULL;
1.10 paf 112: }
113:
114: char *format(Pool& pool, double value, char *fmt) {
115: char *result=(char *)pool.malloc(MAX_NUMBER);
116: if(fmt)
117: if(strpbrk(fmt, "diouxX"))
118: if(strpbrk(fmt, "ouxX"))
119: snprintf(result, MAX_NUMBER, fmt, (uint)value );
120: else
121: snprintf(result, MAX_NUMBER, fmt, (int)value );
122: else
123: snprintf(result, MAX_NUMBER, fmt, value);
124: else
125: snprintf(result, MAX_NUMBER, "%d", (int)value);
126:
127: return result;
1.12 paf 128: }
129:
1.13 paf 130: size_t stdout_write(const char *buf, size_t size) {
1.12 paf 131: #ifdef WIN32
132: do{
133: int chunk_written=fwrite(buf, 1, min(8*0x400, size), stdout);
134: if(chunk_written<=0)
135: break;
136: size-=chunk_written;
137: buf+=chunk_written;
138: } while(size>0);
139:
140: return size;
141: #else
1.13 paf 142: return fwrite(buf, 1, size, stdout);
1.12 paf 143: #endif
1.2 paf 144: }
1.14 ! paf 145:
! 146: const char *unescape_chars(Pool& pool, const char *cp, int len) {
! 147: char *s=(char *)pool.malloc(len + 1);
! 148: enum EscapeState {
! 149: EscapeRest,
! 150: EscapeFirst,
! 151: EscapeSecond
! 152: } escapeState=EscapeRest;
! 153: int escapedValue=0;
! 154: int srcPos=0;
! 155: int dstPos=0;
! 156: while(srcPos < len) {
! 157: int ch=cp[srcPos];
! 158: switch(escapeState) {
! 159: case EscapeRest:
! 160: if(ch=='%') {
! 161: escapeState=EscapeFirst;
! 162: } else if(ch=='+') {
! 163: s[dstPos++]=' ';
! 164: } else {
! 165: s[dstPos++]=ch;
! 166: }
! 167: break;
! 168: case EscapeFirst:
! 169: escapedValue=hex_value[ch] << 4;
! 170: escapeState=EscapeSecond;
! 171: break;
! 172: case EscapeSecond:
! 173: escapedValue +=hex_value[ch];
! 174: s[dstPos++]=escapedValue;
! 175: escapeState=EscapeRest;
! 176: break;
! 177: }
! 178: srcPos++;
! 179: }
! 180: s[dstPos]=0;
! 181: return s;
! 182: }
! 183:
! 184: static void append_attribute_subattribute(const Hash::Key& akey, Hash::Value *avalue,
! 185: void *info) {
! 186: if(akey==VALUE_NAME)
! 187: return;
! 188:
! 189: // ...; charset=windows1251
! 190: String *string=static_cast<String *>(info);
! 191: string->APPEND_CONST("; ");
! 192: string->append(akey, String::Untaint_lang::HEADER, true);
! 193: string->APPEND_CONST("=");
! 194: string->append(static_cast<Value *>(avalue)->as_string(),
! 195: String::Untaint_lang::HEADER, true);
! 196: }
! 197: const String& attributed_meaning_string(Value *meaning) {
! 198: String &result=*new(meaning->pool()) String(meaning->pool());
! 199: if(Hash *hash=meaning->get_hash()) {
! 200: // $value(value) $subattribute(subattribute value)
! 201: if(Value *value=static_cast<Value *>(hash->get(*value_name)))
! 202: result.append(value->as_string(), String::Untaint_lang::HEADER, true);
! 203:
! 204: hash->foreach(append_attribute_subattribute, &result);
! 205: } else // result value
! 206: result.append(meaning->as_string(), String::Untaint_lang::HEADER, true);
! 207:
! 208: return result;
! 209: }