From dacd393fefeabafd1306533dd6c5a56e0ab347cc Mon Sep 17 00:00:00 2001 From: Florian Jung Date: Sun, 27 Feb 2011 18:48:35 +0100 Subject: Initial commit --- TConfig.cpp | 385 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 385 insertions(+) create mode 100644 TConfig.cpp (limited to 'TConfig.cpp') diff --git a/TConfig.cpp b/TConfig.cpp new file mode 100644 index 0000000..3ae4a84 --- /dev/null +++ b/TConfig.cpp @@ -0,0 +1,385 @@ +/* + * TConfig.cpp + * + * Copyright 2009 Florian + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + + +#include +#include +#include +#include + +#include "TConfig.h" +#include "myfuncs.h" + +using namespace std; + +TConfig::TConfig() +{ + +} + +TConfig::TConfig(string f) +{ + if (!loadconfig(f)) throw; +} + +bool TConfig::loadconfig(string f) +{ + int line_no=0; + bool fail; + int pos; + string varname; + string value; + bool add; + + errorlines.clear(); + + ifstream ifs; + ifs.open (f.c_str()); + if (ifs.good()) + { + string line; + while (getline(ifs,line)) + { + line_no++; + + if ((line=trim(removecomments(line)))!="") + { +// cout << "line #"< TConfig::get_errors() +{ + return errorlines; +} + +int TConfig::get_valid_integer(string name, int def) +{ + string path, leaf; + if (name.rfind('.')==string::npos) + { + path=""; leaf=name; + } + else + { + path=name.substr(0,name.rfind('.')+1); + leaf=name.substr(name.rfind('.')+1); + + while (!is_integer(path+leaf)) + { + if (path.rfind('.',path.length()-2)==string::npos) + { + path=""; + break; + } + else + { + path=path.substr(0,path.rfind('.',path.length()-2)+1); + } + } + } + + if (!is_integer(path+leaf)) + return def; + else + return get_integer(path+leaf); +} + +string TConfig::get_valid_string(string name, string def) +{ + string path, leaf; + if (name.rfind('.')==string::npos) + { + path=""; leaf=name; + } + else + { + path=name.substr(0,name.rfind('.')+1); + leaf=name.substr(name.rfind('.')+1); + + while (!is_stored(path+leaf)) + { + if (path.rfind('.',path.length()-2)==string::npos) + { + path=""; + break; + } + else + { + path=path.substr(0,path.rfind('.',path.length()-2)+1); + } + cout << "path="<