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()
89 //---------------------------------------------------------------------------
90 const string & CONFIGFILE::GetFileName() const
94 //---------------------------------------------------------------------------
95 int CONFIGFILE::Error() const
101 //---------------------------------------------------------------------------
102 int CONFIGFILE::Flush() const
104 ofstream f(fileName.c_str());
111 map<string, string>::const_iterator it = param_val.begin();
112 while (it != param_val.end())
114 f << it->first << "=" << it->second << "\n";
122 /*//---------------------------------------------------------------------------
123 int CONFIGFILE::ReadString(const string & param, char * str, int * maxLen, const char * defaultVal) const
125 it = param_val.find(param);
126 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
128 if (it != param_val.end())
131 strncpy(str, param_val[param].c_str(), *maxLen);
132 *maxLen = param_val[param].size();
136 strncpy(str, defaultVal, *maxLen);
137 *maxLen = strlen(defaultVal);
140 //---------------------------------------------------------------------------
141 int CONFIGFILE::ReadString(const string & param, string * val, const string & defaultVal) const
143 const map<string, string>::const_iterator it(param_val.find(param));
144 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
146 if (it != param_val.end())
156 //---------------------------------------------------------------------------
157 void CONFIGFILE::WriteString(const string & param, const string &val)
159 param_val[param] = val;
161 //---------------------------------------------------------------------------
162 int CONFIGFILE::ReadTime(const string & param, time_t * val, time_t defaultVal) const
164 const map<string, string>::const_iterator it(param_val.find(param));
166 if (it != param_val.end())
169 *val = strtol(it->second.c_str(), &res, 10);
172 *val = defaultVal; //Error!
181 //---------------------------------------------------------------------------
182 int CONFIGFILE::ReadInt(const string & param, int * val, int defaultVal) const
184 const map<string, string>::const_iterator it(param_val.find(param));
185 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
187 if (it != param_val.end())
191 *val = strtol(it->second.c_str(), &res, 10);
194 *val = defaultVal; //Error!
203 //---------------------------------------------------------------------------
204 int CONFIGFILE::ReadUInt(const string & param, unsigned int * val, unsigned int defaultVal) const
206 const map<string, string>::const_iterator it(param_val.find(param));
207 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
209 if (it != param_val.end())
213 *val = strtoul(it->second.c_str(), &res, 10);
216 *val = defaultVal; //Error!
225 //---------------------------------------------------------------------------
226 int CONFIGFILE::ReadLongInt(const string & param, long int * val, long int defaultVal) const
228 const map<string, string>::const_iterator it(param_val.find(param));
229 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
231 if (it != param_val.end())
235 *val = strtol(it->second.c_str(), &res, 10);
238 *val = defaultVal; //Error!
247 //---------------------------------------------------------------------------
248 int CONFIGFILE::ReadULongInt(const string & param, unsigned long int * val, unsigned long int defaultVal) const
250 const map<string, string>::const_iterator it(param_val.find(param));
251 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
253 if (it != param_val.end())
257 *val = strtoul(it->second.c_str(), &res, 10);
260 *val = defaultVal; //Error!
269 //---------------------------------------------------------------------------
270 int CONFIGFILE::ReadLongLongInt(const string & param, int64_t * val, int64_t defaultVal) const
272 const map<string, string>::const_iterator it(param_val.find(param));
273 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
275 if (it != param_val.end())
279 *val = strtoll(it->second.c_str(), &res, 10);
282 *val = defaultVal; //Error!
291 //---------------------------------------------------------------------------
292 int CONFIGFILE::ReadULongLongInt(const string & param, uint64_t * val, uint64_t defaultVal) const
294 const map<string, string>::const_iterator it(param_val.find(param));
295 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
297 if (it != param_val.end())
301 *val = strtoull(it->second.c_str(), &res, 10);
304 *val = defaultVal; //Error!
313 //---------------------------------------------------------------------------
314 int CONFIGFILE::ReadShortInt(const string & param, short int * val, short int defaultVal) const
316 const map<string, string>::const_iterator it(param_val.find(param));
317 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
319 if (it != param_val.end())
323 *val = (short)strtol(it->second.c_str(), &res, 10);
326 *val = defaultVal; //Error!
335 //---------------------------------------------------------------------------
336 int CONFIGFILE::ReadUShortInt(const string & param, unsigned short int * val, unsigned short int defaultVal) const
338 const map<string, string>::const_iterator it(param_val.find(param));
339 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
341 if (it != param_val.end())
345 *val = (short)strtoul(it->second.c_str(), &res, 10);
348 *val = defaultVal; //Error!
357 //---------------------------------------------------------------------------
358 void CONFIGFILE::WriteInt(const string & param, int64_t val)
362 param_val[param] = s;
364 //---------------------------------------------------------------------------
365 int CONFIGFILE::ReadDouble(const string & param, double * val, double defaultVal) const
367 const map<string, string>::const_iterator it(param_val.find(param));
368 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
370 if (it != param_val.end())
374 *val = strtod(it->second.c_str(), &res);
377 *val = defaultVal; //Error!
386 //---------------------------------------------------------------------------
387 void CONFIGFILE::WriteDouble(const string & param, double val)
390 snprintf(s, 30, "%f", val);
391 param_val[param] = s;
393 //---------------------------------------------------------------------------