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),
59 ifstream f(fileName.c_str());
69 while (getline(f, line))
71 size_t pos = line.find('#');
72 if (pos != string::npos)
75 if (line.find_first_not_of(" \t\r") == string::npos)
78 pos = line.find_first_of('=');
79 if (pos == string::npos)
85 string parameter = line.substr(0, pos);
86 string value = line.substr(pos + 1);
87 param_val[parameter] = value;
90 //---------------------------------------------------------------------------
91 CONFIGFILE::~CONFIGFILE()
95 //---------------------------------------------------------------------------
96 const string & CONFIGFILE::GetFileName() const
100 //---------------------------------------------------------------------------
101 int CONFIGFILE::Error() const
107 /*//---------------------------------------------------------------------------
108 int CONFIGFILE::ReadString(const string & param, char * str, int * maxLen, const char * defaultVal) const
110 it = param_val.find(param);
111 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
113 if (it != param_val.end())
116 strncpy(str, param_val[param].c_str(), *maxLen);
117 *maxLen = param_val[param].size();
121 strncpy(str, defaultVal, *maxLen);
122 *maxLen = strlen(defaultVal);
125 //---------------------------------------------------------------------------
126 int CONFIGFILE::ReadString(const string & param, string * val, const string & defaultVal) const
128 const map<string, string>::const_iterator it(param_val.find(param));
129 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
131 if (it != param_val.end())
141 //---------------------------------------------------------------------------
142 void CONFIGFILE::WriteString(const string & param, const string &val)
144 param_val[param] = val;
147 //---------------------------------------------------------------------------
148 int CONFIGFILE::ReadTime(const string & param, time_t * val, time_t defaultVal) const
150 const map<string, string>::const_iterator it(param_val.find(param));
152 if (it != param_val.end())
155 *val = strtol(it->second.c_str(), &res, 10);
158 *val = defaultVal; //Error!
167 //---------------------------------------------------------------------------
168 int CONFIGFILE::ReadInt(const string & param, int * val, int defaultVal) const
170 const map<string, string>::const_iterator it(param_val.find(param));
171 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
173 if (it != param_val.end())
177 *val = strtol(it->second.c_str(), &res, 10);
180 *val = defaultVal; //Error!
189 //---------------------------------------------------------------------------
190 int CONFIGFILE::ReadUInt(const string & param, unsigned int * val, unsigned int defaultVal) const
192 const map<string, string>::const_iterator it(param_val.find(param));
193 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
195 if (it != param_val.end())
199 *val = strtoul(it->second.c_str(), &res, 10);
202 *val = defaultVal; //Error!
211 //---------------------------------------------------------------------------
212 int CONFIGFILE::ReadLongInt(const string & param, long int * val, long int defaultVal) const
214 const map<string, string>::const_iterator it(param_val.find(param));
215 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
217 if (it != param_val.end())
221 *val = strtol(it->second.c_str(), &res, 10);
224 *val = defaultVal; //Error!
233 //---------------------------------------------------------------------------
234 int CONFIGFILE::ReadULongInt(const string & param, unsigned long int * val, unsigned long int defaultVal) const
236 const map<string, string>::const_iterator it(param_val.find(param));
237 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
239 if (it != param_val.end())
243 *val = strtoul(it->second.c_str(), &res, 10);
246 *val = defaultVal; //Error!
255 //---------------------------------------------------------------------------
256 int CONFIGFILE::ReadLongLongInt(const string & param, int64_t * val, int64_t defaultVal) const
258 const map<string, string>::const_iterator it(param_val.find(param));
259 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
261 if (it != param_val.end())
265 *val = strtoll(it->second.c_str(), &res, 10);
268 *val = defaultVal; //Error!
277 //---------------------------------------------------------------------------
278 int CONFIGFILE::ReadULongLongInt(const string & param, uint64_t * val, uint64_t defaultVal) const
280 const map<string, string>::const_iterator it(param_val.find(param));
281 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
283 if (it != param_val.end())
287 *val = strtoull(it->second.c_str(), &res, 10);
290 *val = defaultVal; //Error!
299 //---------------------------------------------------------------------------
300 int CONFIGFILE::ReadShortInt(const string & param, short int * val, short int defaultVal) const
302 const map<string, string>::const_iterator it(param_val.find(param));
303 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
305 if (it != param_val.end())
309 *val = (short)strtol(it->second.c_str(), &res, 10);
312 *val = defaultVal; //Error!
321 //---------------------------------------------------------------------------
322 int CONFIGFILE::ReadUShortInt(const string & param, unsigned short int * val, unsigned short int defaultVal) const
324 const map<string, string>::const_iterator it(param_val.find(param));
325 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
327 if (it != param_val.end())
331 *val = (short)strtoul(it->second.c_str(), &res, 10);
334 *val = defaultVal; //Error!
343 //---------------------------------------------------------------------------
344 void CONFIGFILE::WriteInt(const string & param, int64_t val)
348 param_val[param] = s;
351 //---------------------------------------------------------------------------
352 int CONFIGFILE::ReadDouble(const string & param, double * val, double defaultVal) const
354 const map<string, string>::const_iterator it(param_val.find(param));
355 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
357 if (it != param_val.end())
361 *val = strtod(it->second.c_str(), &res);
364 *val = defaultVal; //Error!
373 //---------------------------------------------------------------------------
374 void CONFIGFILE::WriteDouble(const string & param, double val)
377 snprintf(s, 30, "%f", val);
378 param_val[param] = s;
381 //---------------------------------------------------------------------------
382 int CONFIGFILE::Flush(const std::string & path) const
384 ofstream f(path.c_str());
391 map<string, string>::const_iterator it = param_val.begin();
392 while (it != param_val.end())
394 f << it->first << "=" << it->second << "\n";
401 //---------------------------------------------------------------------------
402 int CONFIGFILE::Flush() const
408 x2str(getpid(), pid);
410 if (Flush(fileName + "." + pid))
413 if (rename((fileName + "." + pid).c_str(), fileName.c_str()))
420 //---------------------------------------------------------------------------