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));
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 <<<<<<< Updated upstream
119 std::string parameter = Trim(line.substr(0, pos));
120 std::string value = Trim(line.substr(pos + 1));
122 std::string parameter = line.substr(0, pos);
123 std::string value = line.substr(pos + 1);
124 >>>>>>> Stashed changes
125 param_val[parameter] = value;
128 //---------------------------------------------------------------------------
129 CONFIGFILE::~CONFIGFILE()
133 //---------------------------------------------------------------------------
134 const std::string & CONFIGFILE::GetFileName() const
138 //---------------------------------------------------------------------------
139 int CONFIGFILE::Error() const
145 //---------------------------------------------------------------------------
146 int CONFIGFILE::ReadString(const std::string & param, std::string * val, const std::string & defaultVal) const
148 const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
150 if (it != param_val.end())
159 //---------------------------------------------------------------------------
160 void CONFIGFILE::WriteString(const std::string & param, const std::string &val)
162 param_val[param] = val;
165 //---------------------------------------------------------------------------
166 int CONFIGFILE::ReadTime(const std::string & param, time_t * val, time_t defaultVal) const
168 const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
170 if (it != param_val.end())
173 *val = strtol(it->second.c_str(), &res, 10);
176 *val = defaultVal; //Error!
185 //---------------------------------------------------------------------------
186 int CONFIGFILE::ReadInt(const std::string & param, int * val, int defaultVal) const
188 const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
190 if (it != param_val.end())
193 *val = static_cast<int>(strtol(it->second.c_str(), &res, 10));
196 *val = defaultVal; //Error!
205 //---------------------------------------------------------------------------
206 int CONFIGFILE::ReadUInt(const std::string & param, unsigned int * val, unsigned int defaultVal) const
208 const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
210 if (it != param_val.end())
213 *val = static_cast<unsigned int>(strtoul(it->second.c_str(), &res, 10));
216 *val = defaultVal; //Error!
225 //---------------------------------------------------------------------------
226 int CONFIGFILE::ReadLongInt(const std::string & param, long int * val, long int defaultVal) const
228 const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
230 if (it != param_val.end())
233 *val = strtol(it->second.c_str(), &res, 10);
236 *val = defaultVal; //Error!
245 //---------------------------------------------------------------------------
246 int CONFIGFILE::ReadULongInt(const std::string & param, unsigned long int * val, unsigned long int defaultVal) const
248 const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
250 if (it != param_val.end())
253 *val = strtoul(it->second.c_str(), &res, 10);
256 *val = defaultVal; //Error!
265 //---------------------------------------------------------------------------
266 int CONFIGFILE::ReadLongLongInt(const std::string & param, int64_t * val, int64_t defaultVal) const
268 const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
270 if (it != param_val.end())
273 *val = strtoll(it->second.c_str(), &res, 10);
276 *val = defaultVal; //Error!
285 //---------------------------------------------------------------------------
286 int CONFIGFILE::ReadULongLongInt(const std::string & param, uint64_t * val, uint64_t defaultVal) const
288 const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
290 if (it != param_val.end())
293 *val = strtoull(it->second.c_str(), &res, 10);
296 *val = defaultVal; //Error!
305 //---------------------------------------------------------------------------
306 int CONFIGFILE::ReadShortInt(const std::string & param, short int * val, short int defaultVal) const
308 const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
310 if (it != param_val.end())
313 *val = (short)strtol(it->second.c_str(), &res, 10);
316 *val = defaultVal; //Error!
325 //---------------------------------------------------------------------------
326 int CONFIGFILE::ReadUShortInt(const std::string & param, unsigned short int * val, unsigned short int defaultVal) const
328 const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
330 if (it != param_val.end())
333 *val = (short)strtoul(it->second.c_str(), &res, 10);
336 *val = defaultVal; //Error!
345 //---------------------------------------------------------------------------
346 void CONFIGFILE::WriteInt(const std::string & param, int64_t val)
349 snprintf(buf, sizeof(buf), "%lld", static_cast<long long int>(val));
350 param_val[param] = buf;
353 //---------------------------------------------------------------------------
354 int CONFIGFILE::ReadDouble(const std::string & param, double * val, double defaultVal) const
356 const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
358 if (it != param_val.end())
361 *val = strtod(it->second.c_str(), &res);
364 *val = defaultVal; //Error!
373 //---------------------------------------------------------------------------
374 void CONFIGFILE::WriteDouble(const std::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 std::ofstream f(path.c_str());
391 std::map<std::string, std::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 snprintf(pid, sizeof(pid), "%d", getpid());
410 if (Flush(fileName + "." + pid))
413 if (rename((fileName + "." + pid).c_str(), fileName.c_str()))
420 //---------------------------------------------------------------------------