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 //---------------------------------------------------------------------------
36 #include "conffiles.h"
41 //---------------------------------------------------------------------------
42 bool StringCaseCmp(const string & str1, const string & str2)
44 return (strcasecmp(str1.c_str(), str2.c_str()) < 0);
46 //---------------------------------------------------------------------------
47 CONFIGFILE::CONFIGFILE(const string &fn):
48 param_val(StringCaseCmp)
51 f = fopen(fn.c_str(), "rt");
62 string line, parameter, value;
70 line.erase(line.begin(), line.end());
83 if (pos != string::npos)
87 for (unsigned int i = 0; i < line.size(); i++)
89 if (line[i] != ' ' && line[i] != '\t' && line[i] != '\n' && line[i] != '\r')
100 pos = line.find("=");
101 if (pos == string::npos)
105 //printf("%s find(=) error\n", __FILE__);
108 parameter = line.substr(0, pos);
109 //transform(parameter.begin(), parameter.end(), parameter.begin(), tolower);
110 value = line.substr(pos + 1);
111 //cout << parameter << "==" << value << endl;
112 param_val[parameter] = value;
113 //cout << parameter << "==" << param_val[parameter] << endl;
118 //---------------------------------------------------------------------------
119 CONFIGFILE::~CONFIGFILE()
123 //---------------------------------------------------------------------------
124 const string & CONFIGFILE::GetFileName() const
128 //---------------------------------------------------------------------------
129 int CONFIGFILE::Error()
135 //---------------------------------------------------------------------------
136 int CONFIGFILE::FindParameter(const string ¶meter, string * value) const
138 it = param_val.find(parameter);
139 if (it == param_val.end())
142 *value = param_val[parameter];
145 //---------------------------------------------------------------------------
146 int CONFIGFILE::Flush()
148 fstream f(fileName.c_str(), ios::out);
155 it = param_val.begin();
156 while (it != param_val.end())
158 f << it->first << "=" << it->second << endl;
166 //---------------------------------------------------------------------------
167 int CONFIGFILE::ReadString(const string & param, char * str, int * maxLen, const char * defaultVal) const
169 it = param_val.find(param);
170 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
172 if (it != param_val.end())
175 strncpy(str, param_val[param].c_str(), *maxLen);
176 *maxLen = param_val[param].size();
180 strncpy(str, defaultVal, *maxLen);
181 *maxLen = strlen(defaultVal);
184 //---------------------------------------------------------------------------
185 int CONFIGFILE::ReadString(const string & param, string * val, const string & defaultVal) const
187 it = param_val.find(param);
188 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
190 if (it != param_val.end())
193 *val = param_val[param];
200 //---------------------------------------------------------------------------
201 int CONFIGFILE::WriteString(const string & param, const char * val)
203 WriteString(param, string(val));
206 //---------------------------------------------------------------------------
207 int CONFIGFILE::WriteString(const string & param, const string &val)
209 param_val[param] = val;
213 //---------------------------------------------------------------------------
214 int CONFIGFILE::ReadTime(const string & param, time_t * val, time_t defaultVal) const
216 it = param_val.find(param);
218 if (it != param_val.end())
221 *val = strtol(param_val[param].c_str(), &res, 10);
224 *val = defaultVal; //Error!
233 //---------------------------------------------------------------------------
234 int CONFIGFILE::ReadInt(const string & param, int * val, int defaultVal) const
236 it = param_val.find(param);
237 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
239 if (it != param_val.end())
243 *val = strtol(param_val[param].c_str(), &res, 10);
246 *val = defaultVal; //Error!
255 //---------------------------------------------------------------------------
256 int CONFIGFILE::ReadUInt(const string & param, unsigned int * val, unsigned int defaultVal) const
258 it = param_val.find(param);
259 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
261 if (it != param_val.end())
265 *val = strtoul(param_val[param].c_str(), &res, 10);
268 *val = defaultVal; //Error!
277 //---------------------------------------------------------------------------
278 int CONFIGFILE::ReadLongInt(const string & param, long int * val, long int defaultVal) const
280 it = param_val.find(param);
281 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
283 if (it != param_val.end())
287 *val = strtol(param_val[param].c_str(), &res, 10);
290 *val = defaultVal; //Error!
299 //---------------------------------------------------------------------------
300 int CONFIGFILE::ReadULongInt(const string & param, unsigned long int * val, unsigned long int defaultVal) const
302 it = param_val.find(param);
303 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
305 if (it != param_val.end())
309 *val = strtoul(param_val[param].c_str(), &res, 10);
312 *val = defaultVal; //Error!
321 //---------------------------------------------------------------------------
322 int CONFIGFILE::ReadLongLongInt(const string & param, int64_t * val, int64_t defaultVal) const
324 it = param_val.find(param);
325 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
327 if (it != param_val.end())
331 *val = strtoll(param_val[param].c_str(), &res, 10);
334 *val = defaultVal; //Error!
343 //---------------------------------------------------------------------------
344 int CONFIGFILE::ReadULongLongInt(const string & param, uint64_t * val, uint64_t defaultVal) const
346 it = param_val.find(param);
347 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
349 if (it != param_val.end())
353 *val = strtoull(param_val[param].c_str(), &res, 10);
356 *val = defaultVal; //Error!
365 //---------------------------------------------------------------------------
366 int CONFIGFILE::ReadShortInt(const string & param, short int * val, short int defaultVal) const
368 it = param_val.find(param);
369 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
371 if (it != param_val.end())
375 *val = (short)strtol(param_val[param].c_str(), &res, 10);
378 *val = defaultVal; //Error!
387 //---------------------------------------------------------------------------
388 int CONFIGFILE::ReadUShortInt(const string & param, unsigned short int * val, unsigned short int defaultVal) const
390 it = param_val.find(param);
391 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
393 if (it != param_val.end())
397 *val = (short)strtoul(param_val[param].c_str(), &res, 10);
400 *val = defaultVal; //Error!
409 //---------------------------------------------------------------------------
410 int CONFIGFILE::WriteInt(const string & param, int64_t val)
413 //sprintf(s, "%lld", val);
415 param_val[param] = s;
419 //---------------------------------------------------------------------------
420 int CONFIGFILE::ReadDouble(const string & param, double * val, double defaultVal) const
422 it = param_val.find(param);
423 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
425 if (it != param_val.end())
429 *val = strtod(param_val[param].c_str(), &res);
432 //cout << param << "=" << param_val[param] << " Error!!!\n";
433 *val = defaultVal; //Error!
439 //cout << "îÉÞÅÇÏ ÎÅÔ!!!\n";
444 //---------------------------------------------------------------------------
445 int CONFIGFILE::WriteDouble(const string & param, double val)
448 sprintf(s, "%f", val);
449 param_val[param] = s;
453 //---------------------------------------------------------------------------