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*
42 #include "conffiles.h"
47 //---------------------------------------------------------------------------
48 bool StringCaseCmp(const string & str1, const string & str2)
50 return (strcasecmp(str1.c_str(), str2.c_str()) < 0);
52 //---------------------------------------------------------------------------
53 CONFIGFILE::CONFIGFILE(const string & fn, bool nook)
54 : param_val(StringCaseCmp),
58 ifstream f(fileName.c_str());
68 while (getline(f, line))
70 size_t pos = line.find('#');
71 if (pos != string::npos)
74 if (line.find_first_not_of(" \t\r") == string::npos)
77 pos = line.find_first_of('=');
78 if (pos == string::npos)
84 string parameter = line.substr(0, pos);
85 string value = line.substr(pos + 1);
86 param_val[parameter] = value;
89 //---------------------------------------------------------------------------
90 CONFIGFILE::~CONFIGFILE()
94 //---------------------------------------------------------------------------
95 const string & CONFIGFILE::GetFileName() const
99 //---------------------------------------------------------------------------
100 int CONFIGFILE::Error() const
106 /*//---------------------------------------------------------------------------
107 int CONFIGFILE::ReadString(const string & param, char * str, int * maxLen, const char * defaultVal) const
109 it = param_val.find(param);
110 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
112 if (it != param_val.end())
115 strncpy(str, param_val[param].c_str(), *maxLen);
116 *maxLen = param_val[param].size();
120 strncpy(str, defaultVal, *maxLen);
121 *maxLen = strlen(defaultVal);
124 //---------------------------------------------------------------------------
125 int CONFIGFILE::ReadString(const string & param, string * val, const string & defaultVal) const
127 const map<string, string>::const_iterator it(param_val.find(param));
128 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
130 if (it != param_val.end())
140 //---------------------------------------------------------------------------
141 void CONFIGFILE::WriteString(const string & param, const string &val)
143 param_val[param] = val;
145 //---------------------------------------------------------------------------
146 int CONFIGFILE::ReadTime(const string & param, time_t * val, time_t defaultVal) const
148 const map<string, string>::const_iterator it(param_val.find(param));
150 if (it != param_val.end())
153 *val = strtol(it->second.c_str(), &res, 10);
156 *val = defaultVal; //Error!
165 //---------------------------------------------------------------------------
166 int CONFIGFILE::ReadInt(const string & param, int * val, int defaultVal) const
168 const map<string, string>::const_iterator it(param_val.find(param));
169 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
171 if (it != param_val.end())
175 *val = strtol(it->second.c_str(), &res, 10);
178 *val = defaultVal; //Error!
187 //---------------------------------------------------------------------------
188 int CONFIGFILE::ReadUInt(const string & param, unsigned int * val, unsigned int defaultVal) const
190 const map<string, string>::const_iterator it(param_val.find(param));
191 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
193 if (it != param_val.end())
197 *val = strtoul(it->second.c_str(), &res, 10);
200 *val = defaultVal; //Error!
209 //---------------------------------------------------------------------------
210 int CONFIGFILE::ReadLongInt(const string & param, long int * val, long int defaultVal) const
212 const map<string, string>::const_iterator it(param_val.find(param));
213 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
215 if (it != param_val.end())
219 *val = strtol(it->second.c_str(), &res, 10);
222 *val = defaultVal; //Error!
231 //---------------------------------------------------------------------------
232 int CONFIGFILE::ReadULongInt(const string & param, unsigned long int * val, unsigned long int defaultVal) const
234 const map<string, string>::const_iterator it(param_val.find(param));
235 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
237 if (it != param_val.end())
241 *val = strtoul(it->second.c_str(), &res, 10);
244 *val = defaultVal; //Error!
253 //---------------------------------------------------------------------------
254 int CONFIGFILE::ReadLongLongInt(const string & param, int64_t * val, int64_t defaultVal) const
256 const map<string, string>::const_iterator it(param_val.find(param));
257 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
259 if (it != param_val.end())
263 *val = strtoll(it->second.c_str(), &res, 10);
266 *val = defaultVal; //Error!
275 //---------------------------------------------------------------------------
276 int CONFIGFILE::ReadULongLongInt(const string & param, uint64_t * val, uint64_t defaultVal) const
278 const map<string, string>::const_iterator it(param_val.find(param));
279 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
281 if (it != param_val.end())
285 *val = strtoull(it->second.c_str(), &res, 10);
288 *val = defaultVal; //Error!
297 //---------------------------------------------------------------------------
298 int CONFIGFILE::ReadShortInt(const string & param, short int * val, short int defaultVal) const
300 const map<string, string>::const_iterator it(param_val.find(param));
301 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
303 if (it != param_val.end())
307 *val = (short)strtol(it->second.c_str(), &res, 10);
310 *val = defaultVal; //Error!
319 //---------------------------------------------------------------------------
320 int CONFIGFILE::ReadUShortInt(const string & param, unsigned short int * val, unsigned short int defaultVal) const
322 const map<string, string>::const_iterator it(param_val.find(param));
323 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
325 if (it != param_val.end())
329 *val = (short)strtoul(it->second.c_str(), &res, 10);
332 *val = defaultVal; //Error!
341 //---------------------------------------------------------------------------
342 void CONFIGFILE::WriteInt(const string & param, int64_t val)
346 param_val[param] = s;
348 //---------------------------------------------------------------------------
349 int CONFIGFILE::ReadDouble(const string & param, double * val, double defaultVal) const
351 const map<string, string>::const_iterator it(param_val.find(param));
352 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
354 if (it != param_val.end())
358 *val = strtod(it->second.c_str(), &res);
361 *val = defaultVal; //Error!
370 //---------------------------------------------------------------------------
371 void CONFIGFILE::WriteDouble(const string & param, double val)
374 snprintf(s, 30, "%f", val);
375 param_val[param] = s;
377 //---------------------------------------------------------------------------
378 int CONFIGFILE::Flush(const std::string & path) const
380 ofstream f(path.c_str());
387 map<string, string>::const_iterator it = param_val.begin();
388 while (it != param_val.end())
390 f << it->first << "=" << it->second << "\n";
397 //---------------------------------------------------------------------------
398 int CONFIGFILE::Flush() const
401 x2str(getpid(), pid);
403 if (Flush(fileName + "." + pid))
406 if (rename((fileName + "." + pid).c_str(), fileName.c_str()))
411 //---------------------------------------------------------------------------