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