2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Author : Boris Mikhailenko <stg34@ua.fm>
27 $Date: 2009/10/22 11:40:22 $
30 //---------------------------------------------------------------------------
37 #include "conffiles.h"
42 //---------------------------------------------------------------------------
43 bool StringCaseCmp(const string & str1, const string & str2)
45 return (strcasecmp(str1.c_str(), str2.c_str()) < 0);
47 //---------------------------------------------------------------------------
48 CONFIGFILE::CONFIGFILE(const string & fn)
49 : param_val(StringCaseCmp),
53 ifstream f(fileName.c_str());
54 //FILE * f = fopen(fileName.c_str(), "rt");
63 while (getline(f, line))
65 /*unsigned char c = fgetc(f);
74 size_t pos = line.find('#');
75 if (pos != string::npos)
78 if (line.find_first_not_of(" \t\r") == string::npos)
81 /*bool emptyLine = true;
82 for (unsigned int i = 0; i < line.size(); i++)
84 if (line[i] != ' ' && line[i] != '\t' && line[i] != '\n' && line[i] != '\r')
95 pos = line.find_first_of('=');
96 if (pos == string::npos)
102 string parameter = line.substr(0, pos);
103 string value = line.substr(pos + 1);
104 param_val[parameter] = value;
107 //---------------------------------------------------------------------------
108 CONFIGFILE::~CONFIGFILE()
111 //---------------------------------------------------------------------------
112 const string & CONFIGFILE::GetFileName() const
116 //---------------------------------------------------------------------------
117 int CONFIGFILE::Error()
123 //---------------------------------------------------------------------------
124 int CONFIGFILE::Flush()
126 ofstream f(fileName.c_str());
133 map<string, string>::const_iterator it = param_val.begin();
134 while (it != param_val.end())
136 f << it->first << "=" << it->second << endl;
144 /*//---------------------------------------------------------------------------
145 int CONFIGFILE::ReadString(const string & param, char * str, int * maxLen, const char * defaultVal) const
147 it = param_val.find(param);
148 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
150 if (it != param_val.end())
153 strncpy(str, param_val[param].c_str(), *maxLen);
154 *maxLen = param_val[param].size();
158 strncpy(str, defaultVal, *maxLen);
159 *maxLen = strlen(defaultVal);
162 //---------------------------------------------------------------------------
163 int CONFIGFILE::ReadString(const string & param, string * val, const string & defaultVal) const
165 const map<string, string>::const_iterator it(param_val.find(param));
166 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
168 if (it != param_val.end())
178 /*//---------------------------------------------------------------------------
179 int CONFIGFILE::WriteString(const string & param, const char * val)
181 WriteString(param, string(val));
184 //---------------------------------------------------------------------------
185 int CONFIGFILE::WriteString(const string & param, const string &val)
187 param_val[param] = val;
191 //---------------------------------------------------------------------------
192 int CONFIGFILE::ReadTime(const string & param, time_t * val, time_t defaultVal) const
194 const map<string, string>::const_iterator it(param_val.find(param));
196 if (it != param_val.end())
199 *val = strtol(it->second.c_str(), &res, 10);
202 *val = defaultVal; //Error!
211 //---------------------------------------------------------------------------
212 int CONFIGFILE::ReadInt(const string & param, int * val, int defaultVal) const
214 const map<string, string>::const_iterator it(param_val.find(param));
215 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
217 if (it != param_val.end())
221 *val = strtol(it->second.c_str(), &res, 10);
224 *val = defaultVal; //Error!
233 //---------------------------------------------------------------------------
234 int CONFIGFILE::ReadUInt(const string & param, unsigned int * val, unsigned int defaultVal) const
236 const map<string, string>::const_iterator it(param_val.find(param));
237 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
239 if (it != param_val.end())
243 *val = strtoul(it->second.c_str(), &res, 10);
246 *val = defaultVal; //Error!
255 //---------------------------------------------------------------------------
256 int CONFIGFILE::ReadLongInt(const string & param, long int * val, long int defaultVal) const
258 const map<string, string>::const_iterator it(param_val.find(param));
259 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
261 if (it != param_val.end())
265 *val = strtol(it->second.c_str(), &res, 10);
268 *val = defaultVal; //Error!
277 //---------------------------------------------------------------------------
278 int CONFIGFILE::ReadULongInt(const string & param, unsigned long int * val, unsigned long int defaultVal) const
280 const map<string, string>::const_iterator it(param_val.find(param));
281 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
283 if (it != param_val.end())
287 *val = strtoul(it->second.c_str(), &res, 10);
290 *val = defaultVal; //Error!
299 //---------------------------------------------------------------------------
300 int CONFIGFILE::ReadLongLongInt(const string & param, int64_t * val, int64_t defaultVal) const
302 const map<string, string>::const_iterator it(param_val.find(param));
303 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
305 if (it != param_val.end())
309 *val = strtoll(it->second.c_str(), &res, 10);
312 *val = defaultVal; //Error!
321 //---------------------------------------------------------------------------
322 int CONFIGFILE::ReadULongLongInt(const string & param, uint64_t * val, uint64_t defaultVal) const
324 const map<string, string>::const_iterator it(param_val.find(param));
325 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
327 if (it != param_val.end())
331 *val = strtoull(it->second.c_str(), &res, 10);
334 *val = defaultVal; //Error!
343 //---------------------------------------------------------------------------
344 int CONFIGFILE::ReadShortInt(const string & param, short int * val, short int defaultVal) const
346 const map<string, string>::const_iterator it(param_val.find(param));
347 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
349 if (it != param_val.end())
353 *val = (short)strtol(it->second.c_str(), &res, 10);
356 *val = defaultVal; //Error!
365 //---------------------------------------------------------------------------
366 int CONFIGFILE::ReadUShortInt(const string & param, unsigned short int * val, unsigned short int defaultVal) const
368 const map<string, string>::const_iterator it(param_val.find(param));
369 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
371 if (it != param_val.end())
375 *val = (short)strtoul(it->second.c_str(), &res, 10);
378 *val = defaultVal; //Error!
387 //---------------------------------------------------------------------------
388 int CONFIGFILE::WriteInt(const string & param, int64_t val)
391 //sprintf(s, "%lld", val);
393 param_val[param] = s;
397 //---------------------------------------------------------------------------
398 int CONFIGFILE::ReadDouble(const string & param, double * val, double defaultVal) const
400 const map<string, string>::const_iterator it(param_val.find(param));
401 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
403 if (it != param_val.end())
407 *val = strtod(it->second.c_str(), &res);
410 *val = defaultVal; //Error!
419 //---------------------------------------------------------------------------
420 int CONFIGFILE::WriteDouble(const string & param, double val)
423 sprintf(s, "%f", val);
424 param_val[param] = s;
428 //---------------------------------------------------------------------------