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*
44 #include "stg/conffiles.h"
48 //---------------------------------------------------------------------------
49 std::string TrimL(std::string val)
51 size_t pos = val.find_first_not_of(" \t");
52 if (pos == std::string::npos)
54 val.erase(val.begin(), val.end());
62 //---------------------------------------------------------------------------
63 std::string TrimR(std::string val)
65 size_t pos = val.find_last_not_of(" \t");
66 if (pos != std::string::npos)
72 //---------------------------------------------------------------------------
73 std::string Trim(const std::string& val)
75 return TrimR(TrimL(val));
77 //---------------------------------------------------------------------------
78 } // namespace anonymous
80 //---------------------------------------------------------------------------
81 bool StringCaseCmp(const std::string & str1, const std::string & str2)
83 return (strcasecmp(str1.c_str(), str2.c_str()) < 0);
85 //---------------------------------------------------------------------------
86 CONFIGFILE::CONFIGFILE(const std::string & fn, bool nook)
87 : param_val(StringCaseCmp),
92 std::ifstream f(fileName.c_str());
102 while (getline(f, line))
104 size_t pos = line.find('#');
105 if (pos != std::string::npos)
108 if (line.find_first_not_of(" \t\r") == std::string::npos)
111 pos = line.find_first_of('=');
112 if (pos == std::string::npos)
118 std::string parameter = Trim(line.substr(0, pos));
119 std::string value = Trim(line.substr(pos + 1));
120 param_val[parameter] = value;
123 //---------------------------------------------------------------------------
124 CONFIGFILE::~CONFIGFILE()
128 //---------------------------------------------------------------------------
129 const std::string & CONFIGFILE::GetFileName() const
133 //---------------------------------------------------------------------------
134 int CONFIGFILE::Error() const
140 //---------------------------------------------------------------------------
141 int CONFIGFILE::ReadString(const std::string & param, std::string * val, const std::string & defaultVal) const
143 const std::map<std::string, std::string, StringCaseCmp_t>::const_iterator it(param_val.find(param));
145 if (it != param_val.end())
154 //---------------------------------------------------------------------------
155 void CONFIGFILE::WriteString(const std::string & param, const std::string &val)
157 param_val[param] = val;
160 //---------------------------------------------------------------------------
161 int CONFIGFILE::ReadTime(const std::string & param, time_t * val, time_t defaultVal) const
163 const std::map<std::string, std::string, StringCaseCmp_t>::const_iterator it(param_val.find(param));
165 if (it != param_val.end())
168 *val = strtol(it->second.c_str(), &res, 10);
171 *val = defaultVal; //Error!
180 //---------------------------------------------------------------------------
181 int CONFIGFILE::ReadInt(const std::string & param, int * val, int defaultVal) const
183 const std::map<std::string, std::string, StringCaseCmp_t>::const_iterator it(param_val.find(param));
185 if (it != param_val.end())
188 *val = static_cast<int>(strtol(it->second.c_str(), &res, 10));
191 *val = defaultVal; //Error!
200 //---------------------------------------------------------------------------
201 int CONFIGFILE::ReadUInt(const std::string & param, unsigned int * val, unsigned int defaultVal) const
203 const std::map<std::string, std::string, StringCaseCmp_t>::const_iterator it(param_val.find(param));
205 if (it != param_val.end())
208 *val = static_cast<unsigned int>(strtoul(it->second.c_str(), &res, 10));
211 *val = defaultVal; //Error!
220 //---------------------------------------------------------------------------
221 int CONFIGFILE::ReadLongInt(const std::string & param, long int * val, long int defaultVal) const
223 const std::map<std::string, std::string, StringCaseCmp_t>::const_iterator it(param_val.find(param));
225 if (it != param_val.end())
228 *val = strtol(it->second.c_str(), &res, 10);
231 *val = defaultVal; //Error!
240 //---------------------------------------------------------------------------
241 int CONFIGFILE::ReadULongInt(const std::string & param, unsigned long int * val, unsigned long int defaultVal) const
243 const std::map<std::string, std::string, StringCaseCmp_t>::const_iterator it(param_val.find(param));
245 if (it != param_val.end())
248 *val = strtoul(it->second.c_str(), &res, 10);
251 *val = defaultVal; //Error!
260 //---------------------------------------------------------------------------
261 int CONFIGFILE::ReadLongLongInt(const std::string & param, int64_t * val, int64_t defaultVal) const
263 const std::map<std::string, std::string, StringCaseCmp_t>::const_iterator it(param_val.find(param));
265 if (it != param_val.end())
268 *val = strtoll(it->second.c_str(), &res, 10);
271 *val = defaultVal; //Error!
280 //---------------------------------------------------------------------------
281 int CONFIGFILE::ReadULongLongInt(const std::string & param, uint64_t * val, uint64_t defaultVal) const
283 const std::map<std::string, std::string, StringCaseCmp_t>::const_iterator it(param_val.find(param));
285 if (it != param_val.end())
288 *val = strtoull(it->second.c_str(), &res, 10);
291 *val = defaultVal; //Error!
300 //---------------------------------------------------------------------------
301 int CONFIGFILE::ReadShortInt(const std::string & param, short int * val, short int defaultVal) const
303 const std::map<std::string, std::string, StringCaseCmp_t>::const_iterator it(param_val.find(param));
305 if (it != param_val.end())
308 *val = (short)strtol(it->second.c_str(), &res, 10);
311 *val = defaultVal; //Error!
320 //---------------------------------------------------------------------------
321 int CONFIGFILE::ReadUShortInt(const std::string & param, unsigned short int * val, unsigned short int defaultVal) const
323 const std::map<std::string, std::string, StringCaseCmp_t>::const_iterator it(param_val.find(param));
325 if (it != param_val.end())
328 *val = (short)strtoul(it->second.c_str(), &res, 10);
331 *val = defaultVal; //Error!
340 //---------------------------------------------------------------------------
341 void CONFIGFILE::WriteInt(const std::string & param, int64_t val)
344 snprintf(buf, sizeof(buf), "%lld", static_cast<long long int>(val));
345 param_val[param] = buf;
348 //---------------------------------------------------------------------------
349 void CONFIGFILE::WriteTime(const std::string & param, time_t val)
351 std::stringstream ss;
353 param_val[param] = ss.str();
356 //---------------------------------------------------------------------------
357 int CONFIGFILE::ReadDouble(const std::string & param, double * val, double defaultVal) const
359 const std::map<std::string, std::string, StringCaseCmp_t>::const_iterator it(param_val.find(param));
361 if (it != param_val.end())
364 *val = strtod(it->second.c_str(), &res);
367 *val = defaultVal; //Error!
376 //---------------------------------------------------------------------------
377 void CONFIGFILE::WriteDouble(const std::string & param, double val)
380 snprintf(s, 30, "%f", val);
381 param_val[param] = s;
384 //---------------------------------------------------------------------------
385 int CONFIGFILE::Flush(const std::string & path) const
387 std::ofstream f(path.c_str());
394 std::map<std::string, std::string, StringCaseCmp_t>::const_iterator it = param_val.begin();
395 while (it != param_val.end())
397 f << it->first << "=" << it->second << "\n";
404 //---------------------------------------------------------------------------
405 int CONFIGFILE::Flush() const
411 snprintf(pid, sizeof(pid), "%d", getpid());
413 if (Flush(fileName + "." + pid))
416 if (rename((fileName + "." + pid).c_str(), fileName.c_str()))
423 //---------------------------------------------------------------------------