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 "stg/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, string * val, const string & defaultVal) const
110 const map<string, string>::const_iterator it(param_val.find(param));
112 if (it != param_val.end())
121 //---------------------------------------------------------------------------
122 void CONFIGFILE::WriteString(const string & param, const string &val)
124 param_val[param] = val;
127 //---------------------------------------------------------------------------
128 int CONFIGFILE::ReadTime(const string & param, time_t * val, time_t defaultVal) const
130 const map<string, string>::const_iterator it(param_val.find(param));
132 if (it != param_val.end())
135 *val = strtol(it->second.c_str(), &res, 10);
138 *val = defaultVal; //Error!
147 //---------------------------------------------------------------------------
148 int CONFIGFILE::ReadInt(const string & param, int * val, int 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::ReadUInt(const string & param, unsigned int * val, unsigned int defaultVal) const
170 const map<string, string>::const_iterator it(param_val.find(param));
172 if (it != param_val.end())
175 *val = strtoul(it->second.c_str(), &res, 10);
178 *val = defaultVal; //Error!
187 //---------------------------------------------------------------------------
188 int CONFIGFILE::ReadLongInt(const string & param, long int * val, long int defaultVal) const
190 const map<string, string>::const_iterator it(param_val.find(param));
192 if (it != param_val.end())
195 *val = strtol(it->second.c_str(), &res, 10);
198 *val = defaultVal; //Error!
207 //---------------------------------------------------------------------------
208 int CONFIGFILE::ReadULongInt(const string & param, unsigned long int * val, unsigned long int defaultVal) const
210 const map<string, string>::const_iterator it(param_val.find(param));
212 if (it != param_val.end())
215 *val = strtoul(it->second.c_str(), &res, 10);
218 *val = defaultVal; //Error!
227 //---------------------------------------------------------------------------
228 int CONFIGFILE::ReadLongLongInt(const string & param, int64_t * val, int64_t defaultVal) const
230 const map<string, string>::const_iterator it(param_val.find(param));
232 if (it != param_val.end())
235 *val = strtoll(it->second.c_str(), &res, 10);
238 *val = defaultVal; //Error!
247 //---------------------------------------------------------------------------
248 int CONFIGFILE::ReadULongLongInt(const string & param, uint64_t * val, uint64_t defaultVal) const
250 const map<string, string>::const_iterator it(param_val.find(param));
252 if (it != param_val.end())
255 *val = strtoull(it->second.c_str(), &res, 10);
258 *val = defaultVal; //Error!
267 //---------------------------------------------------------------------------
268 int CONFIGFILE::ReadShortInt(const string & param, short int * val, short int defaultVal) const
270 const map<string, string>::const_iterator it(param_val.find(param));
272 if (it != param_val.end())
275 *val = (short)strtol(it->second.c_str(), &res, 10);
278 *val = defaultVal; //Error!
287 //---------------------------------------------------------------------------
288 int CONFIGFILE::ReadUShortInt(const string & param, unsigned short int * val, unsigned short int defaultVal) const
290 const map<string, string>::const_iterator it(param_val.find(param));
292 if (it != param_val.end())
295 *val = (short)strtoul(it->second.c_str(), &res, 10);
298 *val = defaultVal; //Error!
307 //---------------------------------------------------------------------------
308 void CONFIGFILE::WriteInt(const string & param, int64_t val)
311 snprintf(buf, sizeof(buf), "%lld", static_cast<long long int>(val));
312 param_val[param] = buf;
315 //---------------------------------------------------------------------------
316 int CONFIGFILE::ReadDouble(const string & param, double * val, double defaultVal) const
318 const map<string, string>::const_iterator it(param_val.find(param));
320 if (it != param_val.end())
323 *val = strtod(it->second.c_str(), &res);
326 *val = defaultVal; //Error!
335 //---------------------------------------------------------------------------
336 void CONFIGFILE::WriteDouble(const string & param, double val)
339 snprintf(s, 30, "%f", val);
340 param_val[param] = s;
343 //---------------------------------------------------------------------------
344 int CONFIGFILE::Flush(const std::string & path) const
346 ofstream f(path.c_str());
353 map<string, string>::const_iterator it = param_val.begin();
354 while (it != param_val.end())
356 f << it->first << "=" << it->second << "\n";
363 //---------------------------------------------------------------------------
364 int CONFIGFILE::Flush() const
370 snprintf(pid, sizeof(pid), "%d", getpid());
372 if (Flush(fileName + "." + pid))
375 if (rename((fileName + "." + pid).c_str(), fileName.c_str()))
382 //---------------------------------------------------------------------------