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