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 std::string TrimL(std::string val)
50 size_t pos = val.find_first_not_of(" \t");
51 if (pos == std::string::npos)
53 val.erase(val.begin(), val.end());
61 //---------------------------------------------------------------------------
62 std::string TrimR(std::string val)
64 size_t pos = val.find_last_not_of(" \t");
65 if (pos != std::string::npos)
71 //---------------------------------------------------------------------------
72 std::string Trim(std::string val)
74 return TrimR(TrimL(val));
76 //---------------------------------------------------------------------------
79 //---------------------------------------------------------------------------
80 bool StringCaseCmp(const std::string & str1, const std::string & str2)
82 return (strcasecmp(str1.c_str(), str2.c_str()) < 0);
84 //---------------------------------------------------------------------------
85 CONFIGFILE::CONFIGFILE(const std::string & fn, bool nook)
86 : param_val(StringCaseCmp),
91 std::ifstream f(fileName.c_str());
101 while (getline(f, line))
103 size_t pos = line.find('#');
104 if (pos != std::string::npos)
107 if (line.find_first_not_of(" \t\r") == std::string::npos)
110 pos = line.find_first_of('=');
111 if (pos == std::string::npos)
117 std::string parameter = Trim(line.substr(0, pos));
118 std::string value = Trim(line.substr(pos + 1));
119 param_val[parameter] = value;
122 //---------------------------------------------------------------------------
123 CONFIGFILE::~CONFIGFILE()
127 //---------------------------------------------------------------------------
128 const std::string & CONFIGFILE::GetFileName() const
132 //---------------------------------------------------------------------------
133 int CONFIGFILE::Error() const
139 //---------------------------------------------------------------------------
140 int CONFIGFILE::ReadString(const std::string & param, std::string * val, const std::string & defaultVal) const
142 const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
144 if (it != param_val.end())
153 //---------------------------------------------------------------------------
154 void CONFIGFILE::WriteString(const std::string & param, const std::string &val)
156 param_val[param] = val;
159 //---------------------------------------------------------------------------
160 int CONFIGFILE::ReadTime(const std::string & param, time_t * val, time_t defaultVal) const
162 const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
164 if (it != param_val.end())
167 *val = strtol(it->second.c_str(), &res, 10);
170 *val = defaultVal; //Error!
179 //---------------------------------------------------------------------------
180 int CONFIGFILE::ReadInt(const std::string & param, int * val, int defaultVal) const
182 const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
184 if (it != param_val.end())
187 *val = strtol(it->second.c_str(), &res, 10);
190 *val = defaultVal; //Error!
199 //---------------------------------------------------------------------------
200 int CONFIGFILE::ReadUInt(const std::string & param, unsigned int * val, unsigned int defaultVal) const
202 const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
204 if (it != param_val.end())
207 *val = strtoul(it->second.c_str(), &res, 10);
210 *val = defaultVal; //Error!
219 //---------------------------------------------------------------------------
220 int CONFIGFILE::ReadLongInt(const std::string & param, long int * val, long int defaultVal) const
222 const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
224 if (it != param_val.end())
227 *val = strtol(it->second.c_str(), &res, 10);
230 *val = defaultVal; //Error!
239 //---------------------------------------------------------------------------
240 int CONFIGFILE::ReadULongInt(const std::string & param, unsigned long int * val, unsigned long int defaultVal) const
242 const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
244 if (it != param_val.end())
247 *val = strtoul(it->second.c_str(), &res, 10);
250 *val = defaultVal; //Error!
259 //---------------------------------------------------------------------------
260 int CONFIGFILE::ReadLongLongInt(const std::string & param, int64_t * val, int64_t defaultVal) const
262 const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
264 if (it != param_val.end())
267 *val = strtoll(it->second.c_str(), &res, 10);
270 *val = defaultVal; //Error!
279 //---------------------------------------------------------------------------
280 int CONFIGFILE::ReadULongLongInt(const std::string & param, uint64_t * val, uint64_t defaultVal) const
282 const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
284 if (it != param_val.end())
287 *val = strtoull(it->second.c_str(), &res, 10);
290 *val = defaultVal; //Error!
299 //---------------------------------------------------------------------------
300 int CONFIGFILE::ReadShortInt(const std::string & param, short int * val, short int defaultVal) const
302 const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
304 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 std::string & param, unsigned short int * val, unsigned short int defaultVal) const
322 const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
324 if (it != param_val.end())
327 *val = (short)strtoul(it->second.c_str(), &res, 10);
330 *val = defaultVal; //Error!
339 //---------------------------------------------------------------------------
340 void CONFIGFILE::WriteInt(const std::string & param, int64_t val)
343 snprintf(buf, sizeof(buf), "%lld", static_cast<long long int>(val));
344 param_val[param] = buf;
347 //---------------------------------------------------------------------------
348 int CONFIGFILE::ReadDouble(const std::string & param, double * val, double defaultVal) const
350 const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
352 if (it != param_val.end())
355 *val = strtod(it->second.c_str(), &res);
358 *val = defaultVal; //Error!
367 //---------------------------------------------------------------------------
368 void CONFIGFILE::WriteDouble(const std::string & param, double val)
371 snprintf(s, 30, "%f", val);
372 param_val[param] = s;
375 //---------------------------------------------------------------------------
376 int CONFIGFILE::Flush(const std::string & path) const
378 std::ofstream f(path.c_str());
385 std::map<std::string, std::string>::const_iterator it = param_val.begin();
386 while (it != param_val.end())
388 f << it->first << "=" << it->second << "\n";
395 //---------------------------------------------------------------------------
396 int CONFIGFILE::Flush() const
402 snprintf(pid, sizeof(pid), "%d", getpid());
404 if (Flush(fileName + "." + pid))
407 if (rename((fileName + "." + pid).c_str(), fileName.c_str()))
414 //---------------------------------------------------------------------------