|
|
1.1 paf 1: /*
1.5 paf 2: Parser
1.8 paf 3: Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)
4: Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)
1.5 paf 5:
1.11 ! paf 6: $Id: pa_common.C,v 1.10 2001/03/12 21:54:20 paf Exp $
1.1 paf 7: */
8:
9: #ifdef HAVE_CONFIG_H
10: # include "pa_config.h"
11: #endif
12:
1.2 paf 13: #include <fcntl.h>
14: #include <sys/types.h>
15: #include <sys/stat.h>
16: #include <io.h>
1.1 paf 17: #include <stdio.h>
1.8 paf 18: #include <string.h>
1.1 paf 19:
20: #include "pa_common.h"
1.2 paf 21: #include "pa_types.h"
1.4 paf 22: #include "pa_exception.h"
1.1 paf 23:
24: #ifdef WIN32
25:
26: int __vsnprintf(char *b, size_t s, const char *f, va_list l) {
27: int r=_vsnprintf(b, --s, f, l);
28: b[s]=0;
29: return r;
30: }
31: int __snprintf(char *b, size_t s, const char *f, ...) {
32: va_list l;
33: va_start(l, f);
34: int r=__vsnprintf(b, s, f, l);
35: va_end(l);
36: return r;
37: }
38:
39: #endif
1.2 paf 40:
1.7 paf 41: char *file_read(Pool& pool, const char *fname, bool fail_on_read_problem) {
1.2 paf 42: int f;
43: struct stat finfo;
1.8 paf 44: if(fname && !stat(fname,&finfo) &&(f=open(fname,O_RDONLY
1.2 paf 45: #ifdef WIN32
46: |O_TEXT
47: #endif
1.8 paf 48: ))>=0) {
1.2 paf 49: /*if(exclusive)
50: flock(f, LOCK_EX);*/
51:
1.10 paf 52: char *result=(char *)pool.malloc(finfo.st_size+1);
1.2 paf 53: int read_size=read(f,result,finfo.st_size);
54: if(read_size>=0 && read_size<=finfo.st_size)
55: result[read_size]='\0';
56: /*if(exclusive)
57: flock(f, LOCK_UN);*/
58: close(f);
59: return result;//prepare_config(result, remove_empty_lines);
60: }
1.4 paf 61: if(fail_on_read_problem)
1.11 ! paf 62: PTHROW(0,0,
1.4 paf 63: 0,
64: "use: can not read '%s' file", fname);
1.8 paf 65: return 0;
66: }
67:
68: char *getrow(char **row_ref, char delim) {
69: char *result=*row_ref;
70: if(result) {
71: *row_ref=strchr(result, delim);
72: if(*row_ref)
73: *((*row_ref)++)=0;
74: else if(!*result)
75: return 0;
76: }
77: return result;
78: }
79:
80: char *lsplit(char *string_ref, char delim) {
81: if(string_ref) {
82: char *v=strchr(string_ref, delim);
83: if(v) {
84: *v=0;
85: return v+1;
86: }
87: }
88: return 0;
89: }
90:
91: char *lsplit(char **string_ref, char delim) {
92: char *result=*string_ref;
93: char *next=lsplit(*string_ref, delim);
94: *string_ref=next;
95: return result;
1.9 paf 96: }
97:
98: char *rsplit(char *string, char delim) {
99: if(string){
100: char *v=strrchr(string, delim);
101: if (v){
102: *v=0;
103: return v+1;
104: }
105: }
106: return NULL;
1.10 paf 107: }
108:
109: char *format(Pool& pool, double value, char *fmt) {
110: char *result=(char *)pool.malloc(MAX_NUMBER);
111: if(fmt)
112: if(strpbrk(fmt, "diouxX"))
113: if(strpbrk(fmt, "ouxX"))
114: snprintf(result, MAX_NUMBER, fmt, (uint)value );
115: else
116: snprintf(result, MAX_NUMBER, fmt, (int)value );
117: else
118: snprintf(result, MAX_NUMBER, fmt, value);
119: else
120: snprintf(result, MAX_NUMBER, "%d", (int)value);
121:
122: return result;
1.2 paf 123: }