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