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