|
|
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.4! paf 8: $Id: pa_common.C,v 1.19.2.3 2001/03/20 18:24:44 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: mode|=as_text?_O_TEXT:_O_BINARY;
100: if((f=open(fname,mode,0666))>=0) {
1.19.2.3 paf 101: /*if(exclusive)
102: flock(f, LOCK_EX);*/
1.18 paf 103:
104: if(size) write(f,data,size);
105: #ifndef WIN32
106: ftruncate(f,size);
107: #endif
1.19.2.3 paf 108: /*if(exclusive)
109: flock(f, LOCK_UN);*/
1.18 paf 110: close(f);
111: return;
112: }
113: }
114: }
115: if(fname)
116: PTHROW(0, 0,
117: 0,
118: "file_write('%s'): %s (#%d)",
119: fname, strerror(errno), errno);
120: else
121: PTHROW(0, 0,
122: 0,
123: "file_write: no filename specified");
124: }
125:
1.8 paf 126: char *getrow(char **row_ref, char delim) {
127: char *result=*row_ref;
128: if(result) {
129: *row_ref=strchr(result, delim);
130: if(*row_ref)
131: *((*row_ref)++)=0;
132: else if(!*result)
133: return 0;
134: }
135: return result;
136: }
137:
138: char *lsplit(char *string_ref, char delim) {
139: if(string_ref) {
140: char *v=strchr(string_ref, delim);
141: if(v) {
142: *v=0;
143: return v+1;
144: }
145: }
146: return 0;
147: }
148:
149: char *lsplit(char **string_ref, char delim) {
150: char *result=*string_ref;
151: char *next=lsplit(*string_ref, delim);
152: *string_ref=next;
153: return result;
1.9 paf 154: }
155:
156: char *rsplit(char *string, char delim) {
1.18 paf 157: if(string) {
1.9 paf 158: char *v=strrchr(string, delim);
1.18 paf 159: if(v) {
1.9 paf 160: *v=0;
161: return v+1;
162: }
163: }
164: return NULL;
1.10 paf 165: }
166:
167: char *format(Pool& pool, double value, char *fmt) {
168: char *result=(char *)pool.malloc(MAX_NUMBER);
169: if(fmt)
170: if(strpbrk(fmt, "diouxX"))
171: if(strpbrk(fmt, "ouxX"))
1.18 paf 172: snprintf(result, MAX_NUMBER, fmt,(uint)value );
1.10 paf 173: else
1.18 paf 174: snprintf(result, MAX_NUMBER, fmt,(int)value );
1.10 paf 175: else
176: snprintf(result, MAX_NUMBER, fmt, value);
177: else
1.18 paf 178: snprintf(result, MAX_NUMBER, "%d",(int)value);
1.10 paf 179:
180: return result;
1.12 paf 181: }
182:
1.13 paf 183: size_t stdout_write(const char *buf, size_t size) {
1.12 paf 184: #ifdef WIN32
185: do{
186: int chunk_written=fwrite(buf, 1, min(8*0x400, size), stdout);
187: if(chunk_written<=0)
188: break;
189: size-=chunk_written;
190: buf+=chunk_written;
191: } while(size>0);
192:
193: return size;
194: #else
1.13 paf 195: return fwrite(buf, 1, size, stdout);
1.12 paf 196: #endif
1.2 paf 197: }
1.14 paf 198:
199: const char *unescape_chars(Pool& pool, const char *cp, int len) {
200: char *s=(char *)pool.malloc(len + 1);
201: enum EscapeState {
202: EscapeRest,
203: EscapeFirst,
204: EscapeSecond
205: } escapeState=EscapeRest;
206: int escapedValue=0;
207: int srcPos=0;
208: int dstPos=0;
209: while(srcPos < len) {
210: int ch=cp[srcPos];
211: switch(escapeState) {
212: case EscapeRest:
213: if(ch=='%') {
214: escapeState=EscapeFirst;
215: } else if(ch=='+') {
216: s[dstPos++]=' ';
217: } else {
218: s[dstPos++]=ch;
219: }
220: break;
221: case EscapeFirst:
222: escapedValue=hex_value[ch] << 4;
223: escapeState=EscapeSecond;
224: break;
225: case EscapeSecond:
226: escapedValue +=hex_value[ch];
227: s[dstPos++]=escapedValue;
228: escapeState=EscapeRest;
229: break;
230: }
231: srcPos++;
232: }
233: s[dstPos]=0;
234: return s;
235: }
236:
1.17 paf 237: static void append_attribute_subattribute(const Hash::Key& akey, Hash::Val *avalue,
1.14 paf 238: void *info) {
239: if(akey==VALUE_NAME)
240: return;
241:
242: // ...; charset=windows1251
243: String *string=static_cast<String *>(info);
244: string->APPEND_CONST("; ");
1.19.2.1 paf 245: string->append(akey, String::UL_HEADER, true);
1.14 paf 246: string->APPEND_CONST("=");
247: string->append(static_cast<Value *>(avalue)->as_string(),
1.19.2.1 paf 248: String::UL_HEADER, true);
1.14 paf 249: }
250: const String& attributed_meaning_string(Value *meaning) {
251: String &result=*new(meaning->pool()) String(meaning->pool());
252: if(Hash *hash=meaning->get_hash()) {
253: // $value(value) $subattribute(subattribute value)
254: if(Value *value=static_cast<Value *>(hash->get(*value_name)))
1.19.2.1 paf 255: result.append(value->as_string(), String::UL_HEADER, true);
1.14 paf 256:
257: hash->foreach(append_attribute_subattribute, &result);
258: } else // result value
1.19.2.1 paf 259: result.append(meaning->as_string(), String::UL_HEADER, true);
1.14 paf 260:
261: return result;
262: }