Annotation of parser3/src/main/pa_common.C, revision 1.45
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.45 ! paf 9: $Id: pa_common.C,v 1.44 2001/04/10 10:32:10 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>
1.41 paf 19: #include <ctype.h>
1.20 paf 20:
1.1 paf 21: #include "pa_common.h"
1.2 paf 22: #include "pa_types.h"
1.4 paf 23: #include "pa_exception.h"
1.14 paf 24: #include "pa_pool.h"
25: #include "pa_globals.h"
26: #include "pa_value.h"
27: #include "pa_hash.h"
28: #include "pa_string.h"
1.1 paf 29:
1.22 paf 30: #if _MSC_VER
1.1 paf 31:
32: int __vsnprintf(char *b, size_t s, const char *f, va_list l) {
33: int r=_vsnprintf(b, --s, f, l);
34: b[s]=0;
35: return r;
36: }
37: int __snprintf(char *b, size_t s, const char *f, ...) {
38: va_list l;
39: va_start(l, f);
40: int r=__vsnprintf(b, s, f, l);
41: va_end(l);
42: return r;
43: }
44:
45: #endif
1.2 paf 46:
1.18 paf 47:
1.28 paf 48: char *file_read_text(Pool& pool, const String& file_spec, bool fail_on_read_problem) {
1.34 paf 49: void *result;
50: size_t size;
1.35 paf 51: return file_read(pool, file_spec, result, size, true,
52: fail_on_read_problem)?(char *)result:0;
1.34 paf 53: }
54: bool file_read(Pool& pool, const String& file_spec,
1.44 paf 55: void*& data, size_t& read_size, bool as_text,
56: bool fail_on_read_problem,
57: size_t offset, size_t limit) {
58: const char *fname=file_spec.cstr(String::UL_FILE_NAME);
1.33 paf 59: int f;
1.2 paf 60: struct stat finfo;
1.33 paf 61:
62: // first open, next stat:
1.45 ! paf 63: // directory update of NTFS hard links performed on open.
1.33 paf 64: // ex:
65: // a.html:^test[] and b.html hardlink to a.html
66: // user inserts ! before ^test in a.html
67: // directory entry of b.html in NTFS not updated at once,
1.35 paf 68: // they delay update till open, so we would receive "!^test[" string
69: // if would do stat, next open.
1.34 paf 70: if(
71: (f=open(fname, O_RDONLY|(as_text?_O_TEXT:_O_BINARY)))>=0 &&
72: stat(fname, &finfo)==0) {
1.2 paf 73: /*if(exclusive)
74: flock(f, LOCK_EX);*/
1.44 paf 75: size_t max_size=limit?min(offset+limit, finfo.st_size)-offset:finfo.st_size;
76: data=pool.malloc(max_size+(as_text?1:0));
1.45 ! paf 77: if(offset)
! 78: lseek(f, offset, SEEK_SET);
1.44 paf 79: read_size=read(f, data, max_size);
1.2 paf 80: /*if(exclusive)
81: flock(f, LOCK_UN);*/
82: close(f);
1.32 paf 83:
1.44 paf 84: if(read_size>=0 && read_size<=max_size) {
1.34 paf 85: if(as_text)
1.44 paf 86: ((char *)data)[read_size]=0;
1.34 paf 87: } else
1.33 paf 88: PTHROW(0, 0,
89: &file_spec,
1.44 paf 90: "read failed: actually read %d bytes count not in [0..%lu] valid range",
91: read_size, (unsigned long)max_size); //never
1.32 paf 92:
1.34 paf 93: return true;//prepare_config(result, remove_empty_lines);
1.2 paf 94: }
1.4 paf 95: if(fail_on_read_problem)
1.33 paf 96: PTHROW(0, 0,
97: &file_spec,
1.45 ! paf 98: "read failed: %s (%d)", strerror(errno), errno);
1.34 paf 99: return false;
1.8 paf 100: }
101:
1.38 paf 102: /// @test mkdirs
1.18 paf 103: void file_write(Pool& pool,
1.28 paf 104: const String& file_spec,
1.34 paf 105: const void *data, size_t size,
1.33 paf 106: bool as_text/*,
1.20 paf 107: bool exclusive*/) {
1.44 paf 108: const char *fname=file_spec.cstr(String::UL_FILE_NAME);
1.28 paf 109: int f;
110: if(access(fname, F_OK)!=0) {/*no*/
1.33 paf 111: if((f=open(fname, O_WRONLY|O_CREAT|_O_BINARY, 0666))>0)
1.28 paf 112: close(f);
113: }
114: if(access(fname, R_OK|W_OK)==0) {
1.34 paf 115: int mode=O_RDWR|(as_text?_O_TEXT:_O_BINARY)
1.18 paf 116: #ifdef WIN32
1.28 paf 117: |O_TRUNC
1.18 paf 118: #endif
1.28 paf 119: ;
1.33 paf 120: if((f=open(fname, mode, 0666))>=0) {
1.28 paf 121: /*if(exclusive)
122: flock(f, LOCK_EX);*/
123:
1.33 paf 124: if(size) write(f, data, size);
1.18 paf 125: #ifndef WIN32
1.33 paf 126: ftruncate(f, size);
1.18 paf 127: #endif
1.28 paf 128: /*if(exclusive)
129: flock(f, LOCK_UN);*/
130: close(f);
131: return;
1.18 paf 132: }
133: }
1.33 paf 134: PTHROW(0, 0,
135: &file_spec,
1.45 ! paf 136: "write failed: %s (%d)", strerror(errno), errno);
1.30 paf 137: }
138:
139: void file_delete(Pool& pool, const String& file_spec) {
1.39 paf 140: if(unlink(file_spec.cstr(String::UL_FILE_NAME))!=0)
1.33 paf 141: PTHROW(0, 0,
142: &file_spec,
1.45 ! paf 143: "unlink failed: %s (%d)", strerror(errno), errno);
1.31 paf 144: }
145:
146: bool file_readable(const String& file_spec) {
1.39 paf 147: return access(file_spec.cstr(String::UL_FILE_NAME), R_OK)==0;
1.43 paf 148: }
149: bool file_executable(const String& file_spec) {
150: return access(file_spec.cstr(String::UL_FILE_NAME), X_OK)==0;
1.44 paf 151: }
152:
153: size_t file_size(const String& file_spec) {
154: Pool& pool=file_spec.pool();
155: const char *fname=file_spec.cstr(String::UL_FILE_NAME);
156: struct stat finfo;
157: if(stat(fname, &finfo)!=0)
158: PTHROW(0, 0,
159: &file_spec,
1.45 ! paf 160: "write failed: %s (%d)", strerror(errno), errno);
1.44 paf 161: return finfo.st_size;
1.18 paf 162: }
163:
1.8 paf 164: char *getrow(char **row_ref, char delim) {
165: char *result=*row_ref;
166: if(result) {
167: *row_ref=strchr(result, delim);
168: if(*row_ref)
169: *((*row_ref)++)=0;
170: else if(!*result)
171: return 0;
172: }
173: return result;
174: }
175:
1.23 paf 176: char *lsplit(char *string, char delim) {
177: if(string) {
178: char *v=strchr(string, delim);
1.8 paf 179: if(v) {
180: *v=0;
181: return v+1;
182: }
183: }
184: return 0;
185: }
186:
187: char *lsplit(char **string_ref, char delim) {
188: char *result=*string_ref;
189: char *next=lsplit(*string_ref, delim);
190: *string_ref=next;
191: return result;
1.9 paf 192: }
193:
194: char *rsplit(char *string, char delim) {
1.18 paf 195: if(string) {
1.9 paf 196: char *v=strrchr(string, delim);
1.18 paf 197: if(v) {
1.9 paf 198: *v=0;
199: return v+1;
200: }
201: }
202: return NULL;
1.10 paf 203: }
204:
1.37 paf 205: /// @todo less stupid type detection
1.10 paf 206: char *format(Pool& pool, double value, char *fmt) {
207: char *result=(char *)pool.malloc(MAX_NUMBER);
208: if(fmt)
209: if(strpbrk(fmt, "diouxX"))
210: if(strpbrk(fmt, "ouxX"))
1.33 paf 211: snprintf(result, MAX_NUMBER, fmt, (uint)value );
1.10 paf 212: else
1.33 paf 213: snprintf(result, MAX_NUMBER, fmt, (int)value );
1.10 paf 214: else
215: snprintf(result, MAX_NUMBER, fmt, value);
216: else
1.33 paf 217: snprintf(result, MAX_NUMBER, "%d", (int)value);
1.10 paf 218:
219: return result;
1.12 paf 220: }
221:
1.36 paf 222: size_t stdout_write(const void *buf, size_t size) {
1.12 paf 223: #ifdef WIN32
224: do{
225: int chunk_written=fwrite(buf, 1, min(8*0x400, size), stdout);
226: if(chunk_written<=0)
227: break;
228: size-=chunk_written;
1.36 paf 229: buf=((const char*)buf)+chunk_written;
1.12 paf 230: } while(size>0);
231:
232: return size;
233: #else
1.13 paf 234: return fwrite(buf, 1, size, stdout);
1.12 paf 235: #endif
1.2 paf 236: }
1.14 paf 237:
238: const char *unescape_chars(Pool& pool, const char *cp, int len) {
239: char *s=(char *)pool.malloc(len + 1);
240: enum EscapeState {
1.33 paf 241: EscapeRest,
242: EscapeFirst,
1.14 paf 243: EscapeSecond
244: } escapeState=EscapeRest;
245: int escapedValue=0;
246: int srcPos=0;
247: int dstPos=0;
248: while(srcPos < len) {
249: int ch=cp[srcPos];
250: switch(escapeState) {
251: case EscapeRest:
252: if(ch=='%') {
253: escapeState=EscapeFirst;
254: } else if(ch=='+') {
255: s[dstPos++]=' ';
256: } else {
257: s[dstPos++]=ch;
258: }
259: break;
260: case EscapeFirst:
261: escapedValue=hex_value[ch] << 4;
262: escapeState=EscapeSecond;
263: break;
264: case EscapeSecond:
265: escapedValue +=hex_value[ch];
266: s[dstPos++]=escapedValue;
267: escapeState=EscapeRest;
268: break;
269: }
270: srcPos++;
271: }
272: s[dstPos]=0;
273: return s;
274: }
275:
1.40 paf 276: struct Attributed_meaning_info {
277: String::Untaint_lang lang;
278: String *header;
279: };
1.17 paf 280: static void append_attribute_subattribute(const Hash::Key& akey, Hash::Val *avalue,
1.14 paf 281: void *info) {
282: if(akey==VALUE_NAME)
283: return;
284:
1.40 paf 285: Attributed_meaning_info& ami=*static_cast<Attributed_meaning_info *>(info);
286:
1.14 paf 287: // ...; charset=windows1251
1.40 paf 288: if(ami.header->size())
289: ami.header->APPEND_CONST("; ");
290: ami.header->append(akey, ami.lang, true);
291: ami.header->APPEND_CONST("=");
292: ami.header->append(static_cast<Value *>(avalue)->as_string(),
293: ami.lang, true);
1.14 paf 294: }
1.40 paf 295: const String& attributed_meaning_to_string(Value& meaning, String::Untaint_lang lang) {
1.20 paf 296: String &result=*new(meaning.pool()) String(meaning.pool());
297: if(Hash *hash=meaning.get_hash()) {
1.14 paf 298: // $value(value) $subattribute(subattribute value)
299: if(Value *value=static_cast<Value *>(hash->get(*value_name)))
1.40 paf 300: result.append(value->as_string(), lang, true);
1.14 paf 301:
1.40 paf 302: Attributed_meaning_info attributed_meaning_info={
303: lang,
304: &result
305: };
306: hash->for_each(append_attribute_subattribute, &attributed_meaning_info);
1.14 paf 307: } else // result value
1.40 paf 308: result.append(meaning.as_string(), lang, true);
1.14 paf 309:
310: return result;
1.24 paf 311: }
312:
313: #ifdef WIN32
314: void back_slashes_to_slashes(char *s) {
315: if(s)
316: for(; *s; s++)
317: if(*s=='\\')
318: *s='/';
319: }
1.42 paf 320: /*
321: void slashes_to_back_slashes(char *s) {
322: if(s)
323: for(; *s; s++)
324: if(*s=='/')
325: *s='\\';
326: }
327: */
1.24 paf 328: #endif
1.41 paf 329:
330: bool StrEqNc(const char *s1, const char *s2, bool strict) {
331: while(true) {
332: if(!(*s1)) {
333: if(!(*s2))
334: return true;
335: else
336: return !strict;
337: } else if(!(*s2))
338: return !strict;
339: if(isalpha(*s1)) {
340: if(tolower(*s1) !=tolower(*s2))
341: return false;
342: } else if((*s1) !=(*s2))
343: return false;
344: s1++;
345: s2++;
346: }
347: }
E-mail: