]> git.stg.codes - stg.git/blob - stglibs/conffiles.lib/conffiles.cpp
Мелкие правки в реализации CONFIGFILE
[stg.git] / stglibs / conffiles.lib / conffiles.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Date: 27.10.2002
19  */
20
21 /*
22  *    Author : Boris Mikhailenko <stg34@ua.fm>
23  */
24
25  /*
26  $Revision: 1.5 $
27  $Date: 2009/10/22 11:40:22 $
28  */
29
30 //---------------------------------------------------------------------------
31 #include <cerrno>
32 #include <cstring>
33 #include <cstdlib>
34
35 #include <fstream>
36
37 #include "conffiles.h"
38 #include "common.h"
39
40 using namespace std;
41
42 //---------------------------------------------------------------------------
43 bool StringCaseCmp(const string & str1, const string & str2)
44 {
45 return (strcasecmp(str1.c_str(), str2.c_str()) < 0);
46 }
47 //---------------------------------------------------------------------------
48 CONFIGFILE::CONFIGFILE(const string & fn, bool nook)
49     : param_val(StringCaseCmp),
50       fileName(fn),
51       error(0)
52 {
53 ifstream f(fileName.c_str());
54
55 if (!f)
56     {
57     if (!nook)
58         error = -1;
59     return;
60     }
61
62 string line;
63 while (getline(f, line))
64     {
65     size_t pos = line.find('#');
66     if (pos != string::npos)
67         line.resize(pos);
68
69     if (line.find_first_not_of(" \t\r") == string::npos)
70         continue;
71
72     pos = line.find_first_of('=');
73     if (pos == string::npos)
74         {
75         error = -1;
76         return;
77         }
78
79     string parameter = line.substr(0, pos);
80     string value = line.substr(pos + 1);
81     param_val[parameter] = value;
82     }
83 }
84 //---------------------------------------------------------------------------
85 CONFIGFILE::~CONFIGFILE()
86 {
87 }
88 //---------------------------------------------------------------------------
89 const string & CONFIGFILE::GetFileName() const
90 {
91 return fileName;
92 }
93 //---------------------------------------------------------------------------
94 int CONFIGFILE::Error()
95 {
96 int e = error;
97 error = 0;
98 return e;
99 }
100 //---------------------------------------------------------------------------
101 int CONFIGFILE::Flush()
102 {
103 ofstream f(fileName.c_str());
104 if (!f.is_open())
105     {
106     error = EIO;
107     return EIO;
108     }
109
110 map<string, string>::const_iterator it = param_val.begin();
111 while (it != param_val.end())
112     {
113     f << it->first << "=" << it->second << "\n";
114     it++;
115     }
116
117 f.close();
118
119 return 0;
120 }
121 /*//---------------------------------------------------------------------------
122 int CONFIGFILE::ReadString(const string & param, char * str, int * maxLen, const char * defaultVal) const
123 {
124 it = param_val.find(param);
125 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
126
127 if (it != param_val.end())
128     {
129     // þÔÏ-ÔÏ ÓÔÏÉÔ
130     strncpy(str, param_val[param].c_str(), *maxLen);
131     *maxLen = param_val[param].size();
132     return 0;
133     }
134
135 strncpy(str, defaultVal, *maxLen);
136 *maxLen = strlen(defaultVal);
137 return -1;
138 }*/
139 //---------------------------------------------------------------------------
140 int CONFIGFILE::ReadString(const string & param, string * val, const string & defaultVal) const
141 {
142 const map<string, string>::const_iterator it(param_val.find(param));
143 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
144
145 if (it != param_val.end())
146     {
147     // þÔÏ-ÔÏ ÓÔÏÉÔ
148     *val = it->second;
149     return 0;
150     }
151
152 *val = defaultVal;
153 return -1;
154 }
155 /*//---------------------------------------------------------------------------
156 int CONFIGFILE::WriteString(const string & param, const char * val)
157 {
158 WriteString(param, string(val));
159 return 0;
160 }*/
161 //---------------------------------------------------------------------------
162 int CONFIGFILE::WriteString(const string & param, const string &val)
163 {
164 param_val[param] = val;
165 Flush();
166 return 0;
167 }
168 //---------------------------------------------------------------------------
169 int CONFIGFILE::ReadTime(const string & param, time_t * val, time_t defaultVal) const
170 {
171 const map<string, string>::const_iterator it(param_val.find(param));
172
173 if (it != param_val.end())
174     {
175     char *res;
176     *val = strtol(it->second.c_str(), &res, 10);
177     if (*res != 0)
178         {
179         *val = defaultVal; //Error!
180         return EINVAL;
181         }
182     return 0;
183     }
184
185 *val = defaultVal;
186 return -1;
187 }
188 //---------------------------------------------------------------------------
189 int CONFIGFILE::ReadInt(const string & param, int * val, int defaultVal) const
190 {
191 const map<string, string>::const_iterator it(param_val.find(param));
192 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
193
194 if (it != param_val.end())
195     {
196     // þÔÏ-ÔÏ ÓÔÏÉÔ
197     char *res;
198     *val = strtol(it->second.c_str(), &res, 10);
199     if (*res != 0)
200         {
201         *val = defaultVal; //Error!
202         return EINVAL;
203         }
204     return 0;
205     }
206
207 *val = defaultVal;
208 return -1;
209 }
210 //---------------------------------------------------------------------------
211 int CONFIGFILE::ReadUInt(const string & param, unsigned int * val, unsigned int defaultVal) const
212 {
213 const map<string, string>::const_iterator it(param_val.find(param));
214 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
215
216 if (it != param_val.end())
217     {
218     // þÔÏ-ÔÏ ÓÔÏÉÔ
219     char *res;
220     *val = strtoul(it->second.c_str(), &res, 10);
221     if (*res != 0)
222         {
223         *val = defaultVal; //Error!
224         return EINVAL;
225         }
226     return 0;
227     }
228
229 *val = defaultVal;
230 return -1;
231 }
232 //---------------------------------------------------------------------------
233 int CONFIGFILE::ReadLongInt(const string & param, long int * val, long int defaultVal) const
234 {
235 const map<string, string>::const_iterator it(param_val.find(param));
236 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
237
238 if (it != param_val.end())
239     {
240     // þÔÏ-ÔÏ ÓÔÏÉÔ
241     char *res;
242     *val = strtol(it->second.c_str(), &res, 10);
243     if (*res != 0)
244         {
245         *val = defaultVal; //Error!
246         return EINVAL;
247         }
248     return 0;
249     }
250
251 *val = defaultVal;
252 return -1;
253 }
254 //---------------------------------------------------------------------------
255 int CONFIGFILE::ReadULongInt(const string & param, unsigned long int * val, unsigned long int defaultVal) const
256 {
257 const map<string, string>::const_iterator it(param_val.find(param));
258 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
259
260 if (it != param_val.end())
261     {
262     // þÔÏ-ÔÏ ÓÔÏÉÔ
263     char *res;
264     *val = strtoul(it->second.c_str(), &res, 10);
265     if (*res != 0)
266         {
267         *val = defaultVal; //Error!
268         return EINVAL;
269         }
270     return 0;
271     }
272
273 *val = defaultVal;
274 return -1;
275 }
276 //---------------------------------------------------------------------------
277 int CONFIGFILE::ReadLongLongInt(const string & param, int64_t * val, int64_t defaultVal) const
278 {
279 const map<string, string>::const_iterator it(param_val.find(param));
280 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
281
282 if (it != param_val.end())
283     {
284     // þÔÏ-ÔÏ ÓÔÏÉÔ
285     char *res;
286     *val = strtoll(it->second.c_str(), &res, 10);
287     if (*res != 0)
288         {
289         *val = defaultVal; //Error!
290         return EINVAL;
291         }
292     return 0;
293     }
294
295 *val = defaultVal;
296 return -1;
297 }
298 //---------------------------------------------------------------------------
299 int CONFIGFILE::ReadULongLongInt(const string & param, uint64_t * val, uint64_t defaultVal) const
300 {
301 const map<string, string>::const_iterator it(param_val.find(param));
302 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
303
304 if (it != param_val.end())
305     {
306     // þÔÏ-ÔÏ ÓÔÏÉÔ
307     char *res;
308     *val = strtoull(it->second.c_str(), &res, 10);
309     if (*res != 0)
310         {
311         *val = defaultVal; //Error!
312         return EINVAL;
313         }
314     return 0;
315     }
316
317 *val = defaultVal;
318 return -1;
319 }
320 //---------------------------------------------------------------------------
321 int CONFIGFILE::ReadShortInt(const string & param, short int * val, short int defaultVal) const
322 {
323 const map<string, string>::const_iterator it(param_val.find(param));
324 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
325
326 if (it != param_val.end())
327     {
328     // þÔÏ-ÔÏ ÓÔÏÉÔ
329     char *res;
330     *val = (short)strtol(it->second.c_str(), &res, 10);
331     if (*res != 0)
332         {
333         *val = defaultVal; //Error!
334         return EINVAL;
335         }
336     return 0;
337     }
338
339 *val = defaultVal;
340 return -1;
341 }
342 //---------------------------------------------------------------------------
343 int CONFIGFILE::ReadUShortInt(const string & param, unsigned short int * val, unsigned short int defaultVal) const
344 {
345 const map<string, string>::const_iterator it(param_val.find(param));
346 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
347
348 if (it != param_val.end())
349     {
350     // þÔÏ-ÔÏ ÓÔÏÉÔ
351     char *res;
352     *val = (short)strtoul(it->second.c_str(), &res, 10);
353     if (*res != 0)
354         {
355         *val = defaultVal; //Error!
356         return EINVAL;
357         }
358     return 0;
359     }
360
361 *val = defaultVal;
362 return -1;
363 }
364 //---------------------------------------------------------------------------
365 int CONFIGFILE::WriteInt(const string & param, int64_t val)
366 {
367 string s;
368 //sprintf(s, "%lld", val);
369 x2str(val, s);
370 param_val[param] = s;
371 Flush();
372 return 0;
373 }
374 //---------------------------------------------------------------------------
375 int CONFIGFILE::ReadDouble(const string & param, double * val, double defaultVal) const
376 {
377 const map<string, string>::const_iterator it(param_val.find(param));
378 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
379
380 if (it != param_val.end())
381     {
382     // þÔÏ-ÔÏ ÓÔÏÉÔ
383     char *res;
384     *val = strtod(it->second.c_str(), &res);
385     if (*res != 0)
386         {
387         *val = defaultVal; //Error!
388         return EINVAL;
389         }
390     return 0;
391     }
392
393 *val = defaultVal;
394 return -1;
395 }
396 //---------------------------------------------------------------------------
397 int CONFIGFILE::WriteDouble(const string & param, double val)
398 {
399 char s[30];
400 sprintf(s, "%f", val);
401 param_val[param] = s;
402 Flush();
403 return 0;
404 }
405 //---------------------------------------------------------------------------