/* * This file is part of libximu * * Copyright(c) sschulz techfak.uni-bielefeld.de * http://opensource.cit-ec.de/projects/libximu * * This file may be licensed under the terms of the * GNU Lesser General Public License Version 3 (the ``LGPL''), * or (at your option) any later version. * * Software distributed under the License is distributed * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either * express or implied. See the LGPL for the specific language * governing rights and limitations. * * You should have received a copy of the LGPL along with this * program. If not, go to http://www.gnu.org/licenses/lgpl.html * or write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * The development of this software was supported by the * Excellence Cluster EXC 277 Cognitive Interaction Technology. * The Excellence Cluster EXC 277 is a grant of the Deutsche * Forschungsgemeinschaft (DFG) in the context of the German * Excellence Initiative. */ #include "tinyconfig.h" void TinyConfig::add_variable(string name, bool *var, bool default_value){ *var = default_value; config_map_bool.insert(pair(name, var)); } void TinyConfig::add_variable(string name, double *var, double default_value){ *var = default_value; config_map_double.insert(pair(name, var)); } void TinyConfig::add_variable(string name, int *var, int default_value){ *var = default_value; config_map_int.insert(pair(name, var)); } void TinyConfig::add_variable(string name, vector *var, vector default_value){ *var = default_value; config_map_vec_int.insert(pair*>(name, var)); } void TinyConfig::add_variable(string name, string *var, string default_value){ *var = default_value; config_map_string.insert(pair(name, var)); } void TinyConfig::add_variable(string name, Point2f *var, Point2f default_value){ *var = default_value; config_map_point2f.insert(pair(name, var)); } void TinyConfig::add_variable(string name, vector *var, vector default_value){ *var = default_value; config_map_vec_point2f.insert(pair* >(name, var)); } void TinyConfig::add_variable(string name, Point2d *var, Point2d default_value){ *var = default_value; config_map_point2d.insert(pair(name, var)); } TinyConfig::TinyConfig(){ // printf("NEW CFG\n"); } TinyConfig::~TinyConfig(){ } void TinyConfig::load(string filename){ printf("> loading config from '%s'\n",filename.c_str()); ifstream in(filename.c_str(), ifstream::in); if (!in.is_open()){ printf("> ERROR can't read configfile '%s'. aborted\n",filename.c_str()); exit(1); } string line; string res; int id; while(getline(in, line)){ string::size_type i = line.find_first_not_of ( " \t\n\v" ); //printf("> got '%s'\n",line.c_str()); if ((i != string::npos) && (line[i] == '#')){ //ignore comments }else{ //decode keyword string keyword = parser_extract_keyword(line); string type = parser_extract_type(line); string value = parser_extract_arg(line); //try to decode bools: if (type == "bool"){ if (DEBUG_CONFIG_PARSER) printf("> checking bool type for keyword '%s'\n",keyword.c_str()); config_map_bool_t::iterator it = config_map_bool.find(keyword); if (it != config_map_bool.end()){ if (value == "1"){ *(it->second) = true; }else{ *(it->second) = false; } if (DEBUG_CONFIG_PARSER) printf("> %s = %s\n",keyword.c_str(),(*(it->second))?"true":"false"); } }else if(type == "double"){ if (DEBUG_CONFIG_PARSER) printf("> checking double type for keyword '%s'\n",keyword.c_str()); config_map_double_t::iterator it = config_map_double.find(keyword); if (it != config_map_double.end()){ if (value == ""){ *(it->second) = 0.0; }else{ *(it->second) = atof(value.c_str()); } } }else if(type == "int"){ if (DEBUG_CONFIG_PARSER) printf("> checking int type for keyword '%s'\n",keyword.c_str()); config_map_int_t::iterator it = config_map_int.find(keyword); if (it != config_map_int.end()){ if (value == ""){ *(it->second) = 0; }else{ *(it->second) = atoi(value.c_str()); } } }else if(type == "vector"){ if (DEBUG_CONFIG_PARSER) printf("> checking vector type for keyword '%s'\n",keyword.c_str()); config_map_vec_int_t::iterator it = config_map_vec_int.find(keyword); if (it != config_map_vec_int.end()){ vector *vect = (it->second); if (DEBUG_CONFIG_PARSER) printf("val %s\n",value.c_str()); if (value == ""){ vect->clear(); //*(it->second)->clear(); // = vector; }else{ vect->clear(); //extract values: int pos_col = value.find(":"); while(pos_col != string::npos){ string match = value.substr(0,pos_col); value = value.substr(pos_col+1); if (DEBUG_CONFIG_PARSER) printf("> matching '%s'\n",match.c_str()); int val = atoi(match.c_str()); vect->push_back(val); pos_col = value.find(":"); } } } }else if(type == "string"){ if (DEBUG_CONFIG_PARSER) printf("> checking string type for keyword '%s'\n",keyword.c_str()); config_map_string_t::iterator it = config_map_string.find(keyword); if (it != config_map_string.end()){ if (value == ""){ *(it->second) = "none"; }else{ *(it->second) = value.c_str(); } } }else if(type == "point2f"){ if (DEBUG_CONFIG_PARSER) printf("> checking point2f type for keyword '%s'\n",keyword.c_str()); config_map_point2f_t::iterator it = config_map_point2f.find(keyword); if (it != config_map_point2f.end()){ if (value == ""){ *(it->second) = Point2f(0,0); }else{ //extract values: int pos = value.find(","); string str_x = value.substr(0,pos); string str_y = value.substr(pos+1); float f_x = atof(str_x.c_str()); float f_y = atof(str_y.c_str()); if (DEBUG_CONFIG_PARSER) printf("%f %f\n",f_x,f_y); *(it->second) = Point2f(f_x,f_y); //value.c_str(); } } }else if(type == "vector"){ if (DEBUG_CONFIG_PARSER) printf("> checking vector type for keyword '%s'\n",keyword.c_str()); config_map_vec_point2f_t::iterator it = config_map_vec_point2f.find(keyword); if (it != config_map_vec_point2f.end()){ vector *vect = (it->second); if (DEBUG_CONFIG_PARSER) printf("val %s\n",value.c_str()); if (value == ""){ vect->clear(); //*(it->second)->clear(); // = vector; }else{ vect->clear(); //extract values: int pos_col = value.find(":"); while(pos_col != string::npos){ string match = value.substr(0,pos_col); value = value.substr(pos_col+1); if (DEBUG_CONFIG_PARSER) printf("> matching '%s'\n",match.c_str()); int pos = match.find(","); string str_x = match.substr(0,pos); string str_y = match.substr(pos+1); float f_x = atof(str_x.c_str()); float f_y = atof(str_y.c_str()); vect->push_back(Point2f(f_x,f_y)); pos_col = value.find(":"); } } } }else if(type == "point2d"){ if (DEBUG_CONFIG_PARSER) printf("> checking point2d type for keyword '%s'\n",keyword.c_str()); config_map_point2d_t::iterator it = config_map_point2d.find(keyword); if (it != config_map_point2d.end()){ if (value == ""){ *(it->second) = Point2d(0,0); }else{ //extract values: int pos = value.find(","); string str_x = value.substr(0,pos); string str_y = value.substr(pos+1); double f_x = atof(str_x.c_str()); double f_y = atof(str_y.c_str()); if (DEBUG_CONFIG_PARSER) printf("%f %f\n",f_x,f_y); *(it->second) = Point2d(f_x,f_y); //value.c_str(); } } }else if(type == "vector"){ if (DEBUG_CONFIG_PARSER) printf("> checking vector type for keyword '%s'\n",keyword.c_str()); config_map_vec_point2d_t::iterator it = config_map_vec_point2d.find(keyword); if (it != config_map_vec_point2d.end()){ vector *vect = (it->second); if (DEBUG_CONFIG_PARSER) printf("val %s\n",value.c_str()); if (value == ""){ vect->clear(); //*(it->second)->clear(); // = vector; }else{ vect->clear(); //extract values: int pos_col = value.find(":"); while(pos_col != string::npos){ string match = value.substr(0,pos_col); value = value.substr(pos_col+1); if (DEBUG_CONFIG_PARSER) printf("> matching '%s'\n",match.c_str()); int pos = match.find(","); string str_x = match.substr(0,pos); string str_y = match.substr(pos+1); double f_x = atof(str_x.c_str()); double f_y = atof(str_y.c_str()); vect->push_back(Point2d(f_x,f_y)); pos_col = value.find(":"); } } } } } } } string TinyConfig::get_formated_date(){ struct tm *current; time_t now; time(&now); current = localtime(&now); char buf[256]; snprintf(buf, 256, "%4d.%02d.%02d - %02d:%02d:%02d", 1900+current->tm_year, current->tm_mon, current->tm_mday,current->tm_hour,current->tm_min,current->tm_sec); return string(buf); } void TinyConfig::save(string filename){ // printf("size = %d\n",config_map_bool.size()); printf("> saving config to '%s'\n",filename.c_str()); FILE *fp = fopen(filename.c_str(), "w"); if (fp){ //info: fprintf(fp, "#CONFIG FILE\n"); fprintf(fp, "#written on %s\n",get_formated_date().c_str()); //store bools config_map_bool_t::iterator it_bool = config_map_bool.begin(); while(it_bool != config_map_bool.end()){ fprintf(fp, "%s[bool] = %d\n", it_bool->first.c_str(),(*it_bool->second)?1:0); it_bool++; } //store doubles config_map_double_t::iterator it_double = config_map_double.begin(); while(it_double != config_map_double.end()){ fprintf(fp, "%s[double] = %f\n", it_double->first.c_str(),(*it_double->second)); it_double++; } //store ints config_map_int_t::iterator it_int = config_map_int.begin(); while(it_int != config_map_int.end()){ fprintf(fp, "%s[int] = %d\n", it_int->first.c_str(),(*it_int->second)); it_int++; } //store vector config_map_vec_int_t::iterator it_vec_int = config_map_vec_int.begin(); while(it_vec_int != config_map_vec_int.end()){ vector vect = *it_vec_int->second; vector::iterator it2; fprintf(fp, "%s[vector] = ", it_vec_int->first.c_str()); for(it2 = vect.begin(); it2first.c_str(),(*it_string->second).c_str()); it_string++; } //store point2fs config_map_point2f_t::iterator it_point2f = config_map_point2f.begin(); while(it_point2f != config_map_point2f.end()){ fprintf(fp, "%s[point2f] = %f,%f\n", it_point2f->first.c_str(),(*it_point2f->second).x,(*it_point2f->second).y); it_point2f++; } //store vector point2f config_map_vec_point2f_t::iterator it_vec_point2f = config_map_vec_point2f.begin(); while(it_vec_point2f != config_map_vec_point2f.end()){ vector vect = *it_vec_point2f->second; vector::iterator it2; fprintf(fp, "%s[vector] = ", it_vec_point2f->first.c_str()); for(it2 = vect.begin(); it2x, it2->y); } fprintf(fp, "\n"); // printf("SIZE %d\n",vect.size()); it_vec_point2f++; } //store point2ds config_map_point2d_t::iterator it_point2d = config_map_point2d.begin(); while(it_point2d != config_map_point2d.end()){ fprintf(fp, "%s[point2d] = %f,%f\n", it_point2d->first.c_str(),(*it_point2d->second).x,(*it_point2d->second).y); it_point2d++; } //store vector point2d config_map_vec_point2d_t::iterator it_vec_point2d = config_map_vec_point2d.begin(); while(it_vec_point2d != config_map_vec_point2d.end()){ vector vect = *it_vec_point2d->second; vector::iterator it2; fprintf(fp, "%s[vector] = ", it_vec_point2d->first.c_str()); for(it2 = vect.begin(); it2x, it2->y); } fprintf(fp, "\n"); // printf("SIZE %d\n",vect.size()); it_vec_point2d++; } fclose(fp); }else{ printf("> ERROR: can not write to config file!\n"); } } string TinyConfig::parser_extract_keyword(string in){ unsigned int pos = in.find_first_of("[= \t\n"); string res = in.substr(0, pos); return res; } string TinyConfig::parser_extract_type(string in){ unsigned int pos = in.find_first_of("["); if (pos == string::npos){ return ""; } string type = in.substr(pos); if (DEBUG_CONFIG_PARSER) printf("type %s\n",type.c_str()); unsigned int pos2 = type.find_first_of("]"); string res = type.substr(1, pos2-1); if (DEBUG_CONFIG_PARSER) printf("type %s\n",res.c_str()); return res; } string TinyConfig::parser_extract_arg(string in){ unsigned int pos = 0; if (DEBUG_CONFIG_PARSER) printf("extract_arg('%s')\n",in.c_str()); pos = in.find_first_of("=", pos); if (pos != string::npos){ //ok, scan to "non space" if (DEBUG_CONFIG_PARSER) printf("found = at pos %d\n",pos); //skip "=" pos++; pos = in.find_first_not_of(" \t\n\v", pos); if ((pos != string::npos) && (pos != -1)){ //ok, read rest of line: int pos_end = in.find_last_not_of(" \t\n\v"); if (DEBUG_CONFIG_PARSER) printf("result is '%s' %d--->%d\n",in.substr(pos,pos_end-pos+1).c_str(),pos,pos_end); return in.substr(pos, pos_end-pos+1); } } return ""; }