Menu

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

Download this file

203 lines (196 with data), 3.6 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
#include "global_config.h"
#include <stdio.h>
#include <ctype.h>
Global_Config global_config;
bool Global_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=='\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)) {
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 HTTP_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 HTTP_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 HTTP_Error(string("Unexpected charrecter")+(char)c);
}
}
return false;
}
void Global_Config::load(char const *fname)
{
if(loaded){
return;
}
FILE *f=fopen(fname,"r");
line_counter=1;
if(!f) {
throw HTTP_Error(string("Failed to open file:")+fname);
}
tocken_t T;
string key;
int state=0;
try{
while(get_tocken(f,T) && state != 5) {
switch(state) {
case 0: if(T.first != WORD) {
state=5;
}else{
key=T.second;
state=1;
}
break;
case 1: if(T.first != '.')
state=5;
else
state=2;
break;
case 2: if(T.first!=WORD){
state=5;
}else{
state=3;
key+='.';
key+=T.second;
}
break;
case 3: if(T.first!= '=')
state=5;
else
state=4;
break;
case 4: if(T.first==INT) {
long val=atol(T.second.c_str());
long_map.insert(pair<string,long>(key,val));
}
else if(T.first==DOUBLE) {
double val=atof(T.second.c_str());
double_map.insert(pair<string,double>(key,val));
}
else if(T.first==STR){
string_map.insert(pair<string,string>(key,T.second));
}
else {
state=5;
break;
}
state=0;
break;
}
}
if(state!=0) {
throw HTTP_Error("Parsing error");
}
}
catch (HTTP_Error &err){
fclose(f);
char stmp[32];
snprintf(stmp,32," at line %d",line_counter);
throw HTTP_Error(string(err.get())+stmp);
}
fclose(f);
loaded=true;
}
void Global_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) {
throw HTTP_Error("Configuration file not defined");
}
load(def_file);
}
MongoDB Logo MongoDB