120 AutoVal::operator std::string()
const {
124 AutoVal::operator double()
const {
125 return atof(m_value.c_str());
128 AutoVal::operator int()
const {
129 return atoi(m_value.c_str());
132 AutoVal::operator
unsigned int()
const {
133 return (
unsigned int) atoi(m_value.c_str());
136 AutoVal::operator bool()
const {
138 if (toLower(m_value)==
"on" || atoi(m_value.c_str()) == 1)
144 std::string result(source);
146 for (
unsigned int i=0; i<result.length(); i++) {
169 return read(std::string(configFile));
173 std::ifstream file(configFile.c_str());
175 if (!file || file.eof())
181 std::string inSection;
182 while (std::getline(file,line)) {
184 if (! line.length())
continue;
186 if (line[0] ==
'#')
continue;
187 line = truncate(line,
"#");
189 if (! line.length())
continue;
191 if (line[0] ==
'[') {
192 inSection=trim(line.substr(1,line.find(
']')-1));
196 istringstream lineStream(line);
199 insertValue(inSection,name,val);
204 void ConfigFile::insertValue(
const std::string& section,
const std::string& entry,
const std::string& thevalue) {
205 m_content[toLower(section)+
'/'+toLower(entry)]=AutoVal(thevalue);
208 const AutoVal&
ConfigFile::value(
const std::string& section,
const std::string& entry)
const {
210 std::map<std::string,AutoVal>::const_iterator ci =
211 m_content.find(toLower(section) +
'/' + toLower(entry));
212 if (ci == m_content.end())
throw "entry does not exist";
217 const AutoVal&
ConfigFile::value(
const std::string& section,
const std::string& entry,
double def) {
219 return value(section, entry);
220 }
catch(
const char *) {
221 return m_content.insert(std::make_pair(section+
'/'+entry, AutoVal(def))).first->second;
227 return value(section, entry);
228 }
catch(
const char *) {
229 return m_content.insert(std::make_pair(section+
'/'+entry,
AutoVal(def))).first->second;
233 const AutoVal&
ConfigFile::value(
const std::string& section,
const std::string& entry,
int def) {
235 return value(section, entry);
236 }
catch(
const char *) {
237 return m_content.insert(std::make_pair(section+
'/'+entry,
AutoVal(def))).first->second;
241 const AutoVal&
ConfigFile::value(
const std::string& section,
const std::string& entry,
unsigned int def) {
243 return value(section, entry);
244 }
catch(
const char *) {
245 return m_content.insert(std::make_pair(section+
'/'+entry,
AutoVal(def))).first->second;
249 const AutoVal&
ConfigFile::value(
const std::string& section,
const std::string& entry,
const std::string& def) {
251 return value(section, entry);
252 }
catch(
const char *) {
253 return m_content.insert(std::make_pair(section+
'/'+entry,
AutoVal(def))).first->second;
257 const AutoVal&
ConfigFile::value(
const std::string& section,
const std::string& entry,
const char* def) {
259 return value(section, entry);
260 }
catch(
const char *) {
261 return m_content.insert(std::make_pair(section+
'/'+entry,
AutoVal(def))).first->second;
267 for (std::map<std::string,AutoVal>::const_iterator it = m_content.begin();
268 it != m_content.end(); it++) {
269 out << (std::string) it->first <<
" " << (std::string)it->second << std::endl;
274 std::string
ConfigFile::trim(
const std::string& source,
char const* delims)
const {
275 std::string result(source);
276 std::string::size_type index = result.find_last_not_of(delims);
277 if(index != std::string::npos)
278 result.erase(++index);
280 index = result.find_first_not_of(delims);
281 if(index != std::string::npos)
282 result.erase(0, index);
289 std::string::size_type index = source.find_first_of(atChar);
293 else if(index == std::string::npos) {
297 source.substr(0,index);
301 std::string result(source);
303 for (
unsigned int i=0; i<result.length(); i++) {