|
|
1.26 paf 1:
1.15 paf 2: /** @file
1.16 paf 3: Parser: commonly functions.
4:
1.8 paf 5: Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)
1.16 paf 6:
1.8 paf 7: Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)
1.5 paf 8:
1.27 ! paf 9: $Id: pa_common.C,v 1.26 2001/03/24 11:33:26 paf Exp $
1.1 paf 10: */
11:
1.22 paf 12: #include "pa_config_includes.h"
1.1 paf 13:
1.2 paf 14: #include <fcntl.h>
15: #include <sys/stat.h>
16: #include <io.h>
1.1 paf 17: #include <stdio.h>
1.20 paf 18: #include <errno.h>
19:
1.1 paf 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:
1.22 paf 29: #if _MSC_VER
1.1 paf 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.18 paf 46:
1.19 paf 47: char *file_read_text(Pool& pool, const char *fname, bool fail_on_read_problem) {
1.2 paf 48: int f;
49: struct stat finfo;
1.8 paf 50: if(fname && !stat(fname,&finfo) &&(f=open(fname,O_RDONLY
1.2 paf 51: #ifdef WIN32
52: |O_TEXT
53: #endif
1.8 paf 54: ))>=0) {
1.2 paf 55: /*if(exclusive)
56: flock(f, LOCK_EX);*/
57:
1.10 paf 58: char *result=(char *)pool.malloc(finfo.st_size+1);
1.2 paf 59: int read_size=read(f,result,finfo.st_size);
60: if(read_size>=0 && read_size<=finfo.st_size)
61: result[read_size]='\0';
62: /*if(exclusive)
63: flock(f, LOCK_UN);*/
64: close(f);
65: return result;//prepare_config(result, remove_empty_lines);
66: }
1.4 paf 67: if(fail_on_read_problem)
1.27 ! paf 68: if(fname)
! 69: PTHROW(0, 0,
! 70: 0,
! 71: "file_read('%s'): %s (#%d)",
! 72: fname, strerror(errno), errno);
! 73: else
! 74: PTHROW(0, 0,
! 75: 0,
! 76: "file_read: no filename specified");
1.8 paf 77: return 0;
78: }
79:
1.18 paf 80: void file_write(Pool& pool,
81: const char *fname,
82: const char *data, size_t size,
1.20 paf 83: bool as_text/*,
84: bool exclusive*/) {
1.18 paf 85: if(fname) {
86: int f;
87: if(access(fname, F_OK)!=0) {/*no*/
88: if((f=open(fname,O_WRONLY|O_CREAT|_O_BINARY,0666))>0)
89: close(f);
90: }
91: if(access(fname, R_OK|W_OK)==0) {
1.19 paf 92: int mode=O_RDWR
1.18 paf 93: #ifdef WIN32
94: |O_TRUNC
95: #endif
1.19 paf 96: ;
97: mode|=as_text?_O_TEXT:_O_BINARY;
98: if((f=open(fname,mode,0666))>=0) {
1.20 paf 99: /*if(exclusive)
100: flock(f, LOCK_EX);*/
1.18 paf 101:
102: if(size) write(f,data,size);
103: #ifndef WIN32
104: ftruncate(f,size);
105: #endif
1.20 paf 106: /*if(exclusive)
107: flock(f, LOCK_UN);*/
1.18 paf 108: close(f);
109: return;
110: }
111: }
112: }
113: if(fname)
114: PTHROW(0, 0,
115: 0,
116: "file_write('%s'): %s (#%d)",
117: fname, strerror(errno), errno);
118: else
119: PTHROW(0, 0,
120: 0,
121: "file_write: no filename specified");
122: }
123:
1.8 paf 124: char *getrow(char **row_ref, char delim) {
125: char *result=*row_ref;
126: if(result) {
127: *row_ref=strchr(result, delim);
128: if(*row_ref)
129: *((*row_ref)++)=0;
130: else if(!*result)
131: return 0;
132: }
133: return result;
134: }
135:
1.23 paf 136: char *lsplit(char *string, char delim) {
137: if(string) {
138: char *v=strchr(string, delim);
1.8 paf 139: if(v) {
140: *v=0;
141: return v+1;
142: }
143: }
144: return 0;
145: }
146:
147: char *lsplit(char **string_ref, char delim) {
148: char *result=*string_ref;
149: char *next=lsplit(*string_ref, delim);
150: *string_ref=next;
151: return result;
1.9 paf 152: }
153:
154: char *rsplit(char *string, char delim) {
1.18 paf 155: if(string) {
1.9 paf 156: char *v=strrchr(string, delim);
1.18 paf 157: if(v) {
1.9 paf 158: *v=0;
159: return v+1;
160: }
161: }
162: return NULL;
1.10 paf 163: }
164:
165: char *format(Pool& pool, double value, char *fmt) {
166: char *result=(char *)pool.malloc(MAX_NUMBER);
167: if(fmt)
168: if(strpbrk(fmt, "diouxX"))
169: if(strpbrk(fmt, "ouxX"))
1.18 paf 170: snprintf(result, MAX_NUMBER, fmt,(uint)value );
1.10 paf 171: else
1.18 paf 172: snprintf(result, MAX_NUMBER, fmt,(int)value );
1.10 paf 173: else
174: snprintf(result, MAX_NUMBER, fmt, value);
175: else
1.18 paf 176: snprintf(result, MAX_NUMBER, "%d",(int)value);
1.10 paf 177:
178: return result;
1.12 paf 179: }
180:
1.13 paf 181: size_t stdout_write(const char *buf, size_t size) {
1.12 paf 182: #ifdef WIN32
183: do{
184: int chunk_written=fwrite(buf, 1, min(8*0x400, size), stdout);
185: if(chunk_written<=0)
186: break;
187: size-=chunk_written;
188: buf+=chunk_written;
189: } while(size>0);
190:
191: return size;
192: #else
1.13 paf 193: return fwrite(buf, 1, size, stdout);
1.12 paf 194: #endif
1.2 paf 195: }
1.14 paf 196:
197: const char *unescape_chars(Pool& pool, const char *cp, int len) {
198: char *s=(char *)pool.malloc(len + 1);
199: enum EscapeState {
200: EscapeRest,
201: EscapeFirst,
202: EscapeSecond
203: } escapeState=EscapeRest;
204: int escapedValue=0;
205: int srcPos=0;
206: int dstPos=0;
207: while(srcPos < len) {
208: int ch=cp[srcPos];
209: switch(escapeState) {
210: case EscapeRest:
211: if(ch=='%') {
212: escapeState=EscapeFirst;
213: } else if(ch=='+') {
214: s[dstPos++]=' ';
215: } else {
216: s[dstPos++]=ch;
217: }
218: break;
219: case EscapeFirst:
220: escapedValue=hex_value[ch] << 4;
221: escapeState=EscapeSecond;
222: break;
223: case EscapeSecond:
224: escapedValue +=hex_value[ch];
225: s[dstPos++]=escapedValue;
226: escapeState=EscapeRest;
227: break;
228: }
229: srcPos++;
230: }
231: s[dstPos]=0;
232: return s;
233: }
234:
1.17 paf 235: static void append_attribute_subattribute(const Hash::Key& akey, Hash::Val *avalue,
1.14 paf 236: void *info) {
237: if(akey==VALUE_NAME)
238: return;
239:
240: // ...; charset=windows1251
241: String *string=static_cast<String *>(info);
242: string->APPEND_CONST("; ");
1.20 paf 243: string->append(akey, String::UL_HEADER, true);
1.14 paf 244: string->APPEND_CONST("=");
245: string->append(static_cast<Value *>(avalue)->as_string(),
1.20 paf 246: String::UL_HEADER, true);
1.14 paf 247: }
1.20 paf 248: const String& attributed_meaning_to_string(Value& meaning) {
249: String &result=*new(meaning.pool()) String(meaning.pool());
250: if(Hash *hash=meaning.get_hash()) {
1.14 paf 251: // $value(value) $subattribute(subattribute value)
252: if(Value *value=static_cast<Value *>(hash->get(*value_name)))
1.20 paf 253: result.append(value->as_string(), String::UL_HEADER, true);
1.14 paf 254:
1.25 paf 255: hash->for_each(append_attribute_subattribute, &result);
1.14 paf 256: } else // result value
1.20 paf 257: result.append(meaning.as_string(), String::UL_HEADER, true);
1.14 paf 258:
259: return result;
1.24 paf 260: }
261:
262: #ifdef WIN32
263: void back_slashes_to_slashes(char *s) {
264: if(s)
265: for(; *s; s++)
266: if(*s=='\\')
267: *s='/';
268: }
269: #endif