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 //---------------------------------------------------------------------------
33 #include <sys/types.h>
36 #include <cerrno> // E*
43 #include "conffiles.h"
48 //---------------------------------------------------------------------------
49 bool StringCaseCmp(const string & str1, const string & str2)
51 return (strcasecmp(str1.c_str(), str2.c_str()) < 0);
53 //---------------------------------------------------------------------------
54 CONFIGFILE::CONFIGFILE(const string & fn, bool nook)
55 : param_val(StringCaseCmp),
60 ifstream f(fileName.c_str());
70 while (getline(f, line))
72 size_t pos = line.find('#');
73 if (pos != string::npos)
76 if (line.find_first_not_of(" \t\r") == string::npos)
79 pos = line.find_first_of('=');
80 if (pos == string::npos)
86 string parameter = line.substr(0, pos);
87 string value = line.substr(pos + 1);
88 param_val[parameter] = value;
91 //---------------------------------------------------------------------------
92 CONFIGFILE::~CONFIGFILE()
96 //---------------------------------------------------------------------------
97 const string & CONFIGFILE::GetFileName() const
101 //---------------------------------------------------------------------------
102 int CONFIGFILE::Error() const
108 /*//---------------------------------------------------------------------------
109 int CONFIGFILE::ReadString(const string & param, char * str, int * maxLen, const char * defaultVal) const
111 it = param_val.find(param);
112 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
114 if (it != param_val.end())
117 strncpy(str, param_val[param].c_str(), *maxLen);
118 *maxLen = param_val[param].size();
122 strncpy(str, defaultVal, *maxLen);
123 *maxLen = strlen(defaultVal);
126 //---------------------------------------------------------------------------
127 int CONFIGFILE::ReadString(const string & param, string * val, const string & defaultVal) const
129 const map<string, string>::const_iterator it(param_val.find(param));
130 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
132 if (it != param_val.end())
142 //---------------------------------------------------------------------------
143 void CONFIGFILE::WriteString(const string & param, const string &val)
145 param_val[param] = val;
148 //---------------------------------------------------------------------------
149 int CONFIGFILE::ReadTime(const string & param, time_t * val, time_t defaultVal) const
151 const map<string, string>::const_iterator it(param_val.find(param));
153 if (it != param_val.end())
156 *val = strtol(it->second.c_str(), &res, 10);
159 *val = defaultVal; //Error!
168 //---------------------------------------------------------------------------
169 int CONFIGFILE::ReadInt(const string & param, int * val, int defaultVal) const
171 const map<string, string>::const_iterator it(param_val.find(param));
172 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
174 if (it != param_val.end())
178 *val = strtol(it->second.c_str(), &res, 10);
181 *val = defaultVal; //Error!
190 //---------------------------------------------------------------------------
191 int CONFIGFILE::ReadUInt(const string & param, unsigned int * val, unsigned int defaultVal) const
193 const map<string, string>::const_iterator it(param_val.find(param));
194 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
196 if (it != param_val.end())
200 *val = strtoul(it->second.c_str(), &res, 10);
203 *val = defaultVal; //Error!
212 //---------------------------------------------------------------------------
213 int CONFIGFILE::ReadLongInt(const string & param, long int * val, long int defaultVal) const
215 const map<string, string>::const_iterator it(param_val.find(param));
216 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
218 if (it != param_val.end())
222 *val = strtol(it->second.c_str(), &res, 10);
225 *val = defaultVal; //Error!
234 //---------------------------------------------------------------------------
235 int CONFIGFILE::ReadULongInt(const string & param, unsigned long int * val, unsigned long int defaultVal) const
237 const map<string, string>::const_iterator it(param_val.find(param));
238 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
240 if (it != param_val.end())
244 *val = strtoul(it->second.c_str(), &res, 10);
247 *val = defaultVal; //Error!
256 //---------------------------------------------------------------------------
257 int CONFIGFILE::ReadLongLongInt(const string & param, int64_t * val, int64_t defaultVal) const
259 const map<string, string>::const_iterator it(param_val.find(param));
260 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
262 if (it != param_val.end())
266 *val = strtoll(it->second.c_str(), &res, 10);
269 *val = defaultVal; //Error!
278 //---------------------------------------------------------------------------
279 int CONFIGFILE::ReadULongLongInt(const string & param, uint64_t * val, uint64_t defaultVal) const
281 const map<string, string>::const_iterator it(param_val.find(param));
282 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
284 if (it != param_val.end())
288 *val = strtoull(it->second.c_str(), &res, 10);
291 *val = defaultVal; //Error!
300 //---------------------------------------------------------------------------
301 int CONFIGFILE::ReadShortInt(const string & param, short int * val, short int defaultVal) const
303 const map<string, string>::const_iterator it(param_val.find(param));
304 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
306 if (it != param_val.end())
310 *val = (short)strtol(it->second.c_str(), &res, 10);
313 *val = defaultVal; //Error!
322 //---------------------------------------------------------------------------
323 int CONFIGFILE::ReadUShortInt(const string & param, unsigned short int * val, unsigned short int defaultVal) const
325 const map<string, string>::const_iterator it(param_val.find(param));
326 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
328 if (it != param_val.end())
332 *val = (short)strtoul(it->second.c_str(), &res, 10);
335 *val = defaultVal; //Error!
344 //---------------------------------------------------------------------------
345 void CONFIGFILE::WriteInt(const string & param, int64_t val)
349 param_val[param] = s;
352 //---------------------------------------------------------------------------
353 int CONFIGFILE::ReadDouble(const string & param, double * val, double defaultVal) const
355 const map<string, string>::const_iterator it(param_val.find(param));
356 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
358 if (it != param_val.end())
362 *val = strtod(it->second.c_str(), &res);
365 *val = defaultVal; //Error!
374 //---------------------------------------------------------------------------
375 void CONFIGFILE::WriteDouble(const string & param, double val)
378 snprintf(s, 30, "%f", val);
379 param_val[param] = s;
382 //---------------------------------------------------------------------------
383 int CONFIGFILE::Flush(const std::string & path) const
385 ofstream f(path.c_str());
392 map<string, string>::const_iterator it = param_val.begin();
393 while (it != param_val.end())
395 f << it->first << "=" << it->second << "\n";
402 //---------------------------------------------------------------------------
403 int CONFIGFILE::Flush() const
409 x2str(getpid(), pid);
411 if (Flush(fileName + "." + pid))
414 if (rename((fileName + "." + pid).c_str(), fileName.c_str()))
421 //---------------------------------------------------------------------------