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 //---------------------------------------------------------------------------
38 #include "conffiles.h"
43 //---------------------------------------------------------------------------
44 bool StringCaseCmp(const string & str1, const string & str2)
46 return (strcasecmp(str1.c_str(), str2.c_str()) < 0);
48 //---------------------------------------------------------------------------
49 CONFIGFILE::CONFIGFILE(const string &fn):
50 param_val(StringCaseCmp)
53 f = fopen(fn.c_str(), "rt");
64 string line, parameter, value;
72 line.erase(line.begin(), line.end());
85 if (pos != string::npos)
89 for (unsigned int i = 0; i < line.size(); i++)
91 if (line[i] != ' ' && line[i] != '\t' && line[i] != '\n' && line[i] != '\r')
102 pos = line.find("=");
103 if (pos == string::npos)
107 //printf("%s find(=) error\n", __FILE__);
110 parameter = line.substr(0, pos);
111 //transform(parameter.begin(), parameter.end(), parameter.begin(), tolower);
112 value = line.substr(pos + 1);
113 //cout << parameter << "==" << value << endl;
114 param_val[parameter] = value;
115 //cout << parameter << "==" << param_val[parameter] << endl;
120 //---------------------------------------------------------------------------
121 CONFIGFILE::~CONFIGFILE()
125 //---------------------------------------------------------------------------
126 const string & CONFIGFILE::GetFileName() const
130 //---------------------------------------------------------------------------
131 int CONFIGFILE::Error()
137 //---------------------------------------------------------------------------
138 int CONFIGFILE::Flush()
140 fstream f(fileName.c_str(), ios::out);
147 it = param_val.begin();
148 while (it != param_val.end())
150 f << it->first << "=" << it->second << endl;
158 //---------------------------------------------------------------------------
159 int CONFIGFILE::ReadString(const string & param, char * str, int * maxLen, const char * defaultVal) const
161 it = param_val.find(param);
162 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
164 if (it != param_val.end())
167 strncpy(str, param_val[param].c_str(), *maxLen);
168 *maxLen = param_val[param].size();
172 strncpy(str, defaultVal, *maxLen);
173 *maxLen = strlen(defaultVal);
176 //---------------------------------------------------------------------------
177 int CONFIGFILE::ReadString(const string & param, string * val, const string & defaultVal) const
179 it = param_val.find(param);
180 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
182 if (it != param_val.end())
185 *val = param_val[param];
192 //---------------------------------------------------------------------------
193 int CONFIGFILE::WriteString(const string & param, const char * val)
195 WriteString(param, string(val));
198 //---------------------------------------------------------------------------
199 int CONFIGFILE::WriteString(const string & param, const string &val)
201 param_val[param] = val;
205 //---------------------------------------------------------------------------
206 int CONFIGFILE::ReadTime(const string & param, time_t * val, time_t defaultVal) const
208 it = param_val.find(param);
210 if (it != param_val.end())
213 *val = strtol(param_val[param].c_str(), &res, 10);
216 *val = defaultVal; //Error!
225 //---------------------------------------------------------------------------
226 int CONFIGFILE::ReadInt(const string & param, int * val, int defaultVal) const
228 it = param_val.find(param);
229 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
231 if (it != param_val.end())
235 *val = strtol(param_val[param].c_str(), &res, 10);
238 *val = defaultVal; //Error!
247 //---------------------------------------------------------------------------
248 int CONFIGFILE::ReadUInt(const string & param, unsigned int * val, unsigned int defaultVal) const
250 it = param_val.find(param);
251 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
253 if (it != param_val.end())
257 *val = strtoul(param_val[param].c_str(), &res, 10);
260 *val = defaultVal; //Error!
269 //---------------------------------------------------------------------------
270 int CONFIGFILE::ReadLongInt(const string & param, long int * val, long int defaultVal) const
272 it = param_val.find(param);
273 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
275 if (it != param_val.end())
279 *val = strtol(param_val[param].c_str(), &res, 10);
282 *val = defaultVal; //Error!
291 //---------------------------------------------------------------------------
292 int CONFIGFILE::ReadULongInt(const string & param, unsigned long int * val, unsigned long int defaultVal) const
294 it = param_val.find(param);
295 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
297 if (it != param_val.end())
301 *val = strtoul(param_val[param].c_str(), &res, 10);
304 *val = defaultVal; //Error!
313 //---------------------------------------------------------------------------
314 int CONFIGFILE::ReadLongLongInt(const string & param, int64_t * val, int64_t defaultVal) const
316 it = param_val.find(param);
317 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
319 if (it != param_val.end())
323 *val = strtoll(param_val[param].c_str(), &res, 10);
326 *val = defaultVal; //Error!
335 //---------------------------------------------------------------------------
336 int CONFIGFILE::ReadULongLongInt(const string & param, uint64_t * val, uint64_t defaultVal) const
338 it = param_val.find(param);
339 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
341 if (it != param_val.end())
345 *val = strtoull(param_val[param].c_str(), &res, 10);
348 *val = defaultVal; //Error!
357 //---------------------------------------------------------------------------
358 int CONFIGFILE::ReadShortInt(const string & param, short int * val, short int defaultVal) const
360 it = param_val.find(param);
361 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
363 if (it != param_val.end())
367 *val = (short)strtol(param_val[param].c_str(), &res, 10);
370 *val = defaultVal; //Error!
379 //---------------------------------------------------------------------------
380 int CONFIGFILE::ReadUShortInt(const string & param, unsigned short int * val, unsigned short int defaultVal) const
382 it = param_val.find(param);
383 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
385 if (it != param_val.end())
389 *val = (short)strtoul(param_val[param].c_str(), &res, 10);
392 *val = defaultVal; //Error!
401 //---------------------------------------------------------------------------
402 int CONFIGFILE::WriteInt(const string & param, int64_t val)
405 //sprintf(s, "%lld", val);
407 param_val[param] = s;
411 //---------------------------------------------------------------------------
412 int CONFIGFILE::ReadDouble(const string & param, double * val, double defaultVal) const
414 it = param_val.find(param);
415 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
417 if (it != param_val.end())
421 *val = strtod(param_val[param].c_str(), &res);
424 //cout << param << "=" << param_val[param] << " Error!!!\n";
425 *val = defaultVal; //Error!
431 //cout << "îÉÞÅÇÏ ÎÅÔ!!!\n";
436 //---------------------------------------------------------------------------
437 int CONFIGFILE::WriteDouble(const string & param, double val)
440 sprintf(s, "%f", val);
441 param_val[param] = s;
445 //---------------------------------------------------------------------------