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