|
|
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.17 ! paf 8: $Id: pa_common.C,v 1.16 2001/03/19 17:42:15 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.7 paf 48: char *file_read(Pool& pool, const char *fname, bool fail_on_read_problem) {
1.2 paf 49: int f;
50: struct stat finfo;
1.8 paf 51: if(fname && !stat(fname,&finfo) &&(f=open(fname,O_RDONLY
1.2 paf 52: #ifdef WIN32
53: |O_TEXT
54: #endif
1.8 paf 55: ))>=0) {
1.2 paf 56: /*if(exclusive)
57: flock(f, LOCK_EX);*/
58:
1.10 paf 59: char *result=(char *)pool.malloc(finfo.st_size+1);
1.2 paf 60: int read_size=read(f,result,finfo.st_size);
61: if(read_size>=0 && read_size<=finfo.st_size)
62: result[read_size]='\0';
63: /*if(exclusive)
64: flock(f, LOCK_UN);*/
65: close(f);
66: return result;//prepare_config(result, remove_empty_lines);
67: }
1.4 paf 68: if(fail_on_read_problem)
1.11 paf 69: PTHROW(0,0,
1.4 paf 70: 0,
71: "use: can not read '%s' file", fname);
1.8 paf 72: return 0;
73: }
74:
75: char *getrow(char **row_ref, char delim) {
76: char *result=*row_ref;
77: if(result) {
78: *row_ref=strchr(result, delim);
79: if(*row_ref)
80: *((*row_ref)++)=0;
81: else if(!*result)
82: return 0;
83: }
84: return result;
85: }
86:
87: char *lsplit(char *string_ref, char delim) {
88: if(string_ref) {
89: char *v=strchr(string_ref, delim);
90: if(v) {
91: *v=0;
92: return v+1;
93: }
94: }
95: return 0;
96: }
97:
98: char *lsplit(char **string_ref, char delim) {
99: char *result=*string_ref;
100: char *next=lsplit(*string_ref, delim);
101: *string_ref=next;
102: return result;
1.9 paf 103: }
104:
105: char *rsplit(char *string, char delim) {
106: if(string){
107: char *v=strrchr(string, delim);
108: if (v){
109: *v=0;
110: return v+1;
111: }
112: }
113: return NULL;
1.10 paf 114: }
115:
116: char *format(Pool& pool, double value, char *fmt) {
117: char *result=(char *)pool.malloc(MAX_NUMBER);
118: if(fmt)
119: if(strpbrk(fmt, "diouxX"))
120: if(strpbrk(fmt, "ouxX"))
121: snprintf(result, MAX_NUMBER, fmt, (uint)value );
122: else
123: snprintf(result, MAX_NUMBER, fmt, (int)value );
124: else
125: snprintf(result, MAX_NUMBER, fmt, value);
126: else
127: snprintf(result, MAX_NUMBER, "%d", (int)value);
128:
129: return result;
1.12 paf 130: }
131:
1.13 paf 132: size_t stdout_write(const char *buf, size_t size) {
1.12 paf 133: #ifdef WIN32
134: do{
135: int chunk_written=fwrite(buf, 1, min(8*0x400, size), stdout);
136: if(chunk_written<=0)
137: break;
138: size-=chunk_written;
139: buf+=chunk_written;
140: } while(size>0);
141:
142: return size;
143: #else
1.13 paf 144: return fwrite(buf, 1, size, stdout);
1.12 paf 145: #endif
1.2 paf 146: }
1.14 paf 147:
148: const char *unescape_chars(Pool& pool, const char *cp, int len) {
149: char *s=(char *)pool.malloc(len + 1);
150: enum EscapeState {
151: EscapeRest,
152: EscapeFirst,
153: EscapeSecond
154: } escapeState=EscapeRest;
155: int escapedValue=0;
156: int srcPos=0;
157: int dstPos=0;
158: while(srcPos < len) {
159: int ch=cp[srcPos];
160: switch(escapeState) {
161: case EscapeRest:
162: if(ch=='%') {
163: escapeState=EscapeFirst;
164: } else if(ch=='+') {
165: s[dstPos++]=' ';
166: } else {
167: s[dstPos++]=ch;
168: }
169: break;
170: case EscapeFirst:
171: escapedValue=hex_value[ch] << 4;
172: escapeState=EscapeSecond;
173: break;
174: case EscapeSecond:
175: escapedValue +=hex_value[ch];
176: s[dstPos++]=escapedValue;
177: escapeState=EscapeRest;
178: break;
179: }
180: srcPos++;
181: }
182: s[dstPos]=0;
183: return s;
184: }
185:
1.17 ! paf 186: static void append_attribute_subattribute(const Hash::Key& akey, Hash::Val *avalue,
1.14 paf 187: void *info) {
188: if(akey==VALUE_NAME)
189: return;
190:
191: // ...; charset=windows1251
192: String *string=static_cast<String *>(info);
193: string->APPEND_CONST("; ");
194: string->append(akey, String::Untaint_lang::HEADER, true);
195: string->APPEND_CONST("=");
196: string->append(static_cast<Value *>(avalue)->as_string(),
197: String::Untaint_lang::HEADER, true);
198: }
199: const String& attributed_meaning_string(Value *meaning) {
200: String &result=*new(meaning->pool()) String(meaning->pool());
201: if(Hash *hash=meaning->get_hash()) {
202: // $value(value) $subattribute(subattribute value)
203: if(Value *value=static_cast<Value *>(hash->get(*value_name)))
204: result.append(value->as_string(), String::Untaint_lang::HEADER, true);
205:
206: hash->foreach(append_attribute_subattribute, &result);
207: } else // result value
208: result.append(meaning->as_string(), String::Untaint_lang::HEADER, true);
209:
210: return result;
211: }