]> 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
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)
49     : param_val(StringCaseCmp),
50       fileName(fn),
51       error(0)
52 {
53 ifstream f(fileName.c_str());
54 //FILE * f = fopen(fileName.c_str(), "rt");
55
56 if (!f)
57     {
58     error = -1;
59     return;
60     }
61
62 string line;
63 while (getline(f, line))
64     {
65     /*unsigned char c = fgetc(f);
66     while (!feof(f))
67         {
68         if (c == '\n')
69             break;
70         line.push_back(c);
71         c = fgetc(f);
72         }*/
73
74     size_t pos = line.find('#');
75     if (pos != string::npos)
76         line.resize(pos);
77
78     if (line.find_first_not_of(" \t\r") == string::npos)
79         continue;
80
81     /*bool emptyLine = true;
82     for (unsigned int i = 0; i < line.size(); i++)
83         {
84         if (line[i] != ' ' && line[i] != '\t' && line[i] != '\n' && line[i] != '\r')
85             {
86             emptyLine = false;
87             break;
88             }
89         }
90     if (emptyLine)
91         {
92         continue;
93         }*/
94
95     pos = line.find_first_of('=');
96     if (pos == string::npos)
97         {
98         error = -1;
99         return;
100         }
101
102     string parameter = line.substr(0, pos);
103     string value = line.substr(pos + 1);
104     param_val[parameter] = value;
105     }
106 }
107 //---------------------------------------------------------------------------
108 CONFIGFILE::~CONFIGFILE()
109 {
110 }
111 //---------------------------------------------------------------------------
112 const string & CONFIGFILE::GetFileName() const
113 {
114 return fileName;
115 }
116 //---------------------------------------------------------------------------
117 int CONFIGFILE::Error()
118 {
119 int e = error;
120 error = 0;
121 return e;
122 }
123 //---------------------------------------------------------------------------
124 int CONFIGFILE::Flush()
125 {
126 ofstream f(fileName.c_str());
127 if (!f.is_open())
128     {
129     error = EIO;
130     return EIO;
131     }
132
133 map<string, string>::const_iterator it = param_val.begin();
134 while (it != param_val.end())
135     {
136     f << it->first << "=" << it->second << endl;
137     it++;
138     }
139
140 f.close();
141
142 return 0;
143 }
144 /*//---------------------------------------------------------------------------
145 int CONFIGFILE::ReadString(const string & param, char * str, int * maxLen, const char * defaultVal) const
146 {
147 it = param_val.find(param);
148 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
149
150 if (it != param_val.end())
151     {
152     // þÔÏ-ÔÏ ÓÔÏÉÔ
153     strncpy(str, param_val[param].c_str(), *maxLen);
154     *maxLen = param_val[param].size();
155     return 0;
156     }
157
158 strncpy(str, defaultVal, *maxLen);
159 *maxLen = strlen(defaultVal);
160 return -1;
161 }*/
162 //---------------------------------------------------------------------------
163 int CONFIGFILE::ReadString(const string & param, string * val, const string & defaultVal) const
164 {
165 const map<string, string>::const_iterator it(param_val.find(param));
166 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
167
168 if (it != param_val.end())
169     {
170     // þÔÏ-ÔÏ ÓÔÏÉÔ
171     *val = it->second;
172     return 0;
173     }
174
175 *val = defaultVal;
176 return -1;
177 }
178 /*//---------------------------------------------------------------------------
179 int CONFIGFILE::WriteString(const string & param, const char * val)
180 {
181 WriteString(param, string(val));
182 return 0;
183 }*/
184 //---------------------------------------------------------------------------
185 int CONFIGFILE::WriteString(const string & param, const string &val)
186 {
187 param_val[param] = val;
188 Flush();
189 return 0;
190 }
191 //---------------------------------------------------------------------------
192 int CONFIGFILE::ReadTime(const string & param, time_t * val, time_t defaultVal) const
193 {
194 const map<string, string>::const_iterator it(param_val.find(param));
195
196 if (it != param_val.end())
197     {
198     char *res;
199     *val = strtol(it->second.c_str(), &res, 10);
200     if (*res != 0)
201         {
202         *val = defaultVal; //Error!
203         return EINVAL;
204         }
205     return 0;
206     }
207
208 *val = defaultVal;
209 return -1;
210 }
211 //---------------------------------------------------------------------------
212 int CONFIGFILE::ReadInt(const string & param, int * val, int defaultVal) const
213 {
214 const map<string, string>::const_iterator it(param_val.find(param));
215 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
216
217 if (it != param_val.end())
218     {
219     // þÔÏ-ÔÏ ÓÔÏÉÔ
220     char *res;
221     *val = strtol(it->second.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::ReadUInt(const string & param, unsigned int * val, unsigned int defaultVal) const
235 {
236 const map<string, string>::const_iterator it(param_val.find(param));
237 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
238
239 if (it != param_val.end())
240     {
241     // þÔÏ-ÔÏ ÓÔÏÉÔ
242     char *res;
243     *val = strtoul(it->second.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::ReadLongInt(const string & param, long int * val, long int defaultVal) const
257 {
258 const map<string, string>::const_iterator it(param_val.find(param));
259 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
260
261 if (it != param_val.end())
262     {
263     // þÔÏ-ÔÏ ÓÔÏÉÔ
264     char *res;
265     *val = strtol(it->second.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::ReadULongInt(const string & param, unsigned long int * val, unsigned long int defaultVal) const
279 {
280 const map<string, string>::const_iterator it(param_val.find(param));
281 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
282
283 if (it != param_val.end())
284     {
285     // þÔÏ-ÔÏ ÓÔÏÉÔ
286     char *res;
287     *val = strtoul(it->second.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::ReadLongLongInt(const string & param, int64_t * val, int64_t defaultVal) const
301 {
302 const map<string, string>::const_iterator it(param_val.find(param));
303 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
304
305 if (it != param_val.end())
306     {
307     // þÔÏ-ÔÏ ÓÔÏÉÔ
308     char *res;
309     *val = strtoll(it->second.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::ReadULongLongInt(const string & param, uint64_t * val, uint64_t defaultVal) const
323 {
324 const map<string, string>::const_iterator it(param_val.find(param));
325 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
326
327 if (it != param_val.end())
328     {
329     // þÔÏ-ÔÏ ÓÔÏÉÔ
330     char *res;
331     *val = strtoull(it->second.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::ReadShortInt(const string & param, short int * val, short int defaultVal) const
345 {
346 const map<string, string>::const_iterator it(param_val.find(param));
347 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
348
349 if (it != param_val.end())
350     {
351     // þÔÏ-ÔÏ ÓÔÏÉÔ
352     char *res;
353     *val = (short)strtol(it->second.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::ReadUShortInt(const string & param, unsigned short int * val, unsigned short int defaultVal) const
367 {
368 const map<string, string>::const_iterator it(param_val.find(param));
369 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
370
371 if (it != param_val.end())
372     {
373     // þÔÏ-ÔÏ ÓÔÏÉÔ
374     char *res;
375     *val = (short)strtoul(it->second.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::WriteInt(const string & param, int64_t val)
389 {
390 string s;
391 //sprintf(s, "%lld", val);
392 x2str(val, s);
393 param_val[param] = s;
394 Flush();
395 return 0;
396 }
397 //---------------------------------------------------------------------------
398 int CONFIGFILE::ReadDouble(const string & param, double * val, double defaultVal) const
399 {
400 const map<string, string>::const_iterator it(param_val.find(param));
401 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
402
403 if (it != param_val.end())
404     {
405     // þÔÏ-ÔÏ ÓÔÏÉÔ
406     char *res;
407     *val = strtod(it->second.c_str(), &res);
408     if (*res != 0)
409         {
410         *val = defaultVal; //Error!
411         return EINVAL;
412         }
413     return 0;
414     }
415
416 *val = defaultVal;
417 return -1;
418 }
419 //---------------------------------------------------------------------------
420 int CONFIGFILE::WriteDouble(const string & param, double val)
421 {
422 char s[30];
423 sprintf(s, "%f", val);
424 param_val[param] = s;
425 Flush();
426 return 0;
427 }
428 //---------------------------------------------------------------------------