示例#1
0
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;
}
示例#2
0
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;
}