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, bool nook)
49 : param_val(StringCaseCmp),
53 ifstream f(fileName.c_str());
63 while (getline(f, line))
65 size_t pos = line.find('#');
66 if (pos != string::npos)
69 if (line.find_first_not_of(" \t\r") == string::npos)
72 pos = line.find_first_of('=');
73 if (pos == string::npos)
79 string parameter = line.substr(0, pos);
80 string value = line.substr(pos + 1);
81 param_val[parameter] = value;
84 //---------------------------------------------------------------------------
85 CONFIGFILE::~CONFIGFILE()
88 //---------------------------------------------------------------------------
89 const string & CONFIGFILE::GetFileName() const
93 //---------------------------------------------------------------------------
94 int CONFIGFILE::Error()
100 //---------------------------------------------------------------------------
101 int CONFIGFILE::Flush()
103 ofstream f(fileName.c_str());
110 map<string, string>::const_iterator it = param_val.begin();
111 while (it != param_val.end())
113 f << it->first << "=" << it->second << "\n";
121 /*//---------------------------------------------------------------------------
122 int CONFIGFILE::ReadString(const string & param, char * str, int * maxLen, const char * defaultVal) const
124 it = param_val.find(param);
125 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
127 if (it != param_val.end())
130 strncpy(str, param_val[param].c_str(), *maxLen);
131 *maxLen = param_val[param].size();
135 strncpy(str, defaultVal, *maxLen);
136 *maxLen = strlen(defaultVal);
139 //---------------------------------------------------------------------------
140 int CONFIGFILE::ReadString(const string & param, string * val, const string & defaultVal) const
142 const map<string, string>::const_iterator it(param_val.find(param));
143 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
145 if (it != param_val.end())
155 /*//---------------------------------------------------------------------------
156 int CONFIGFILE::WriteString(const string & param, const char * val)
158 WriteString(param, string(val));
161 //---------------------------------------------------------------------------
162 int CONFIGFILE::WriteString(const string & param, const string &val)
164 param_val[param] = val;
168 //---------------------------------------------------------------------------
169 int CONFIGFILE::ReadTime(const string & param, time_t * val, time_t defaultVal) const
171 const map<string, string>::const_iterator it(param_val.find(param));
173 if (it != param_val.end())
176 *val = strtol(it->second.c_str(), &res, 10);
179 *val = defaultVal; //Error!
188 //---------------------------------------------------------------------------
189 int CONFIGFILE::ReadInt(const string & param, int * val, int defaultVal) const
191 const map<string, string>::const_iterator it(param_val.find(param));
192 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
194 if (it != param_val.end())
198 *val = strtol(it->second.c_str(), &res, 10);
201 *val = defaultVal; //Error!
210 //---------------------------------------------------------------------------
211 int CONFIGFILE::ReadUInt(const string & param, unsigned int * val, unsigned int defaultVal) const
213 const map<string, string>::const_iterator it(param_val.find(param));
214 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
216 if (it != param_val.end())
220 *val = strtoul(it->second.c_str(), &res, 10);
223 *val = defaultVal; //Error!
232 //---------------------------------------------------------------------------
233 int CONFIGFILE::ReadLongInt(const string & param, long int * val, long int defaultVal) const
235 const map<string, string>::const_iterator it(param_val.find(param));
236 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
238 if (it != param_val.end())
242 *val = strtol(it->second.c_str(), &res, 10);
245 *val = defaultVal; //Error!
254 //---------------------------------------------------------------------------
255 int CONFIGFILE::ReadULongInt(const string & param, unsigned long int * val, unsigned long int defaultVal) const
257 const map<string, string>::const_iterator it(param_val.find(param));
258 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
260 if (it != param_val.end())
264 *val = strtoul(it->second.c_str(), &res, 10);
267 *val = defaultVal; //Error!
276 //---------------------------------------------------------------------------
277 int CONFIGFILE::ReadLongLongInt(const string & param, int64_t * val, int64_t defaultVal) const
279 const map<string, string>::const_iterator it(param_val.find(param));
280 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
282 if (it != param_val.end())
286 *val = strtoll(it->second.c_str(), &res, 10);
289 *val = defaultVal; //Error!
298 //---------------------------------------------------------------------------
299 int CONFIGFILE::ReadULongLongInt(const string & param, uint64_t * val, uint64_t defaultVal) const
301 const map<string, string>::const_iterator it(param_val.find(param));
302 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
304 if (it != param_val.end())
308 *val = strtoull(it->second.c_str(), &res, 10);
311 *val = defaultVal; //Error!
320 //---------------------------------------------------------------------------
321 int CONFIGFILE::ReadShortInt(const string & param, short int * val, short int defaultVal) const
323 const map<string, string>::const_iterator it(param_val.find(param));
324 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
326 if (it != param_val.end())
330 *val = (short)strtol(it->second.c_str(), &res, 10);
333 *val = defaultVal; //Error!
342 //---------------------------------------------------------------------------
343 int CONFIGFILE::ReadUShortInt(const string & param, unsigned short int * val, unsigned short int defaultVal) const
345 const map<string, string>::const_iterator it(param_val.find(param));
346 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
348 if (it != param_val.end())
352 *val = (short)strtoul(it->second.c_str(), &res, 10);
355 *val = defaultVal; //Error!
364 //---------------------------------------------------------------------------
365 int CONFIGFILE::WriteInt(const string & param, int64_t val)
368 //sprintf(s, "%lld", val);
370 param_val[param] = s;
374 //---------------------------------------------------------------------------
375 int CONFIGFILE::ReadDouble(const string & param, double * val, double defaultVal) const
377 const map<string, string>::const_iterator it(param_val.find(param));
378 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
380 if (it != param_val.end())
384 *val = strtod(it->second.c_str(), &res);
387 *val = defaultVal; //Error!
396 //---------------------------------------------------------------------------
397 int CONFIGFILE::WriteDouble(const string & param, double val)
400 sprintf(s, "%f", val);
401 param_val[param] = s;
405 //---------------------------------------------------------------------------