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