Menu

[r679]: / framework / trunk / global_config.cpp  Maximize  Restore  History

Download this file

256 lines (246 with data), 4.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include "global_config.h"
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
namespace cppcms {
bool cppcms_config::get_tocken(FILE *f,tocken_t &T)
{
int c;
while((c=fgetc(f))!=EOF) {
if(c=='.') {
T.first='.';
return true;
}
else if(c=='{') {
T.first='{';
return true;
}
else if(c=='}') {
T.first='}';
return true;
}
else if(c=='=') {
T.first='=';
return true;
}
else if(c=='\n') {
line_counter++;
continue;
}
else if(c==' ' || c=='\r' || c=='\t') {
continue;
}
else if(isalpha(c)) {
T.second="";
T.second.reserve(32);
T.second+=(char)c;
while((c=fgetc(f))!=EOF && (isalnum(c) || c=='_')) {
T.second+=(char)c;
}
if(c!=EOF){
ungetc(c,f);
}
T.first=WORD;
return true;
}
else if(isdigit(c) || c=='-') {
T.second="";
T.second.reserve(32);
T.second+=(char)c;
T.first=INT;
while((c=fgetc(f))!=EOF && isdigit(c)) {
T.second+=(char)c;
}
if(c=='.') {
T.second+='.';
T.first=DOUBLE;
while((c=fgetc(f))!=EOF && isdigit(c)) {
T.second+=(char)c;
}
}
if(T.second=="-" || T.second=="." || T.second=="-.") {
throw cppcms_error("Illegal charrecters");
}
if(c!=EOF) {
ungetc(c,f);
}
return true;
}
else if(c=='\"') {
T.first=STR;
T.second="";
T.second.reserve(128);
for(;;){
c=fgetc(f);
if(c=='\\'){
if((c=fgetc(f))=='\"' ) {
T.second+='"';
continue;
}
else {
T.second+='\\';
}
}
if(c==EOF){
throw cppcms_error("Unexpected EOF ");
}
if(c=='\n') line_counter++;
if(c=='\"') {
return true;
}
T.second+=(char)c;
}
}
else if(c=='#' || c==';'){
while((c=fgetc(f))!=EOF) {
if(c=='\n'){
line_counter++;
break;
}
}
if(c==EOF) {
return false;
}
}
else {
throw cppcms_error(string("Unexpected charrecter")+(char)c);
}
}
return false;
}
void cppcms_config::load(char const *fname)
{
if(loaded){
return;
}
FILE *f=fopen(fname,"r");
line_counter=1;
if(!f) {
throw cppcms_error(string("Failed to open file:")+fname);
}
tocken_t T;
string key;
int state=0;
try{
while(get_tocken(f,T) && state != -1) {
switch(state) {
case 0: if(T.first != WORD) {
state=-1;
}else{
key=T.second;
state=1;
}
break;
case 1: if(T.first != '.')
state=-1;
else
state=2;
break;
case 2: if(T.first!=WORD){
state=-1;
}else{
state=3;
key+='.';
key+=T.second;
}
break;
case 3: if(T.first!= '=')
state=-1;
else
state=4;
break;
case 4: if(T.first=='{') {
state=5;
break;
}
if(T.first==INT) {
int val=atol(T.second.c_str());
data[key]=val;
}
else if(T.first==DOUBLE) {
double val=atof(T.second.c_str());
data[key]=val;
}
else if(T.first==STR){
data[key]=T.second;
}
else {
state=-1;
break;
}
state=0;
break;
case 5:
if(T.first==INT || T.first==DOUBLE || T.first==STR) {
int fp=T.first;
vector<int> vl;
vector<double> vd;
vector<string> vs;
do {
if(T.first=='}') {
state=0;
}
else if(T.first==fp){
switch(T.first) {
case INT: vl.push_back(atol(T.second.c_str())); break;
case DOUBLE: vd.push_back(atof(T.second.c_str())); break;
case STR: vs.push_back(T.second); break;
}
}
else {
state=-1;
}
}while(state==5 && get_tocken(f,T));
if(state==0) {
switch(fp) {
case INT: data[key]=vl; break;
case DOUBLE: data[key]=vd; break;
case STR: data[key]=vs; break;
};
}
}
else
state=-1;
break;
}
}
if(state!=0) {
throw cppcms_error("Parsing error");
}
}
catch (cppcms_error &err){
fclose(f);
char stmp[32];
snprintf(stmp,32," at line %d",line_counter);
throw cppcms_error(string(err.what())+stmp);
}
fclose(f);
loaded=true;
}
void cppcms_config::load(int argc,char *argv[],char const *def)
{
if(loaded) {
return;
}
char const *def_file=def;
int i;
for(i=1;i<argc;i++) {
if(strncmp(argv[i],"--config=",9)==0) {
def_file=argv[i]+9;
break;
}
else if(strcmp(argv[i],"-c")==0 && i+1<argc) {
def_file=argv[i+1];
break;
}
}
if(def_file==NULL) {
def_file=getenv("CPPCMS_CONFIG");
}
if(def_file==NULL) {
throw cppcms_error("Configuration file not defined");
}
load(def_file);
}
}
MongoDB Logo MongoDB