]> 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 Flush();
88 }
89 //---------------------------------------------------------------------------
90 const string & CONFIGFILE::GetFileName() const
91 {
92 return fileName;
93 }
94 //---------------------------------------------------------------------------
95 int CONFIGFILE::Error() const
96 {
97 int e = error;
98 error = 0;
99 return e;
100 }
101 //---------------------------------------------------------------------------
102 int CONFIGFILE::Flush() const
103 {
104 ofstream f(fileName.c_str());
105 if (!f.is_open())
106     {
107     error = EIO;
108     return EIO;
109     }
110
111 map<string, string>::const_iterator it = param_val.begin();
112 while (it != param_val.end())
113     {
114     f << it->first << "=" << it->second << "\n";
115     ++it;
116     }
117
118 f.close();
119
120 return 0;
121 }
122 /*//---------------------------------------------------------------------------
123 int CONFIGFILE::ReadString(const string & param, char * str, int * maxLen, const char * defaultVal) const
124 {
125 it = param_val.find(param);
126 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
127
128 if (it != param_val.end())
129     {
130     // þÔÏ-ÔÏ ÓÔÏÉÔ
131     strncpy(str, param_val[param].c_str(), *maxLen);
132     *maxLen = param_val[param].size();
133     return 0;
134     }
135
136 strncpy(str, defaultVal, *maxLen);
137 *maxLen = strlen(defaultVal);
138 return -1;
139 }*/
140 //---------------------------------------------------------------------------
141 int CONFIGFILE::ReadString(const string & param, string * val, const string & defaultVal) const
142 {
143 const map<string, string>::const_iterator it(param_val.find(param));
144 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
145
146 if (it != param_val.end())
147     {
148     // þÔÏ-ÔÏ ÓÔÏÉÔ
149     *val = it->second;
150     return 0;
151     }
152
153 *val = defaultVal;
154 return -1;
155 }
156 //---------------------------------------------------------------------------
157 void CONFIGFILE::WriteString(const string & param, const string &val)
158 {
159 param_val[param] = val;
160 }
161 //---------------------------------------------------------------------------
162 int CONFIGFILE::ReadTime(const string & param, time_t * val, time_t defaultVal) const
163 {
164 const map<string, string>::const_iterator it(param_val.find(param));
165
166 if (it != param_val.end())
167     {
168     char *res;
169     *val = strtol(it->second.c_str(), &res, 10);
170     if (*res != 0)
171         {
172         *val = defaultVal; //Error!
173         return EINVAL;
174         }
175     return 0;
176     }
177
178 *val = defaultVal;
179 return -1;
180 }
181 //---------------------------------------------------------------------------
182 int CONFIGFILE::ReadInt(const string & param, int * val, int defaultVal) const
183 {
184 const map<string, string>::const_iterator it(param_val.find(param));
185 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
186
187 if (it != param_val.end())
188     {
189     // þÔÏ-ÔÏ ÓÔÏÉÔ
190     char *res;
191     *val = strtol(it->second.c_str(), &res, 10);
192     if (*res != 0)
193         {
194         *val = defaultVal; //Error!
195         return EINVAL;
196         }
197     return 0;
198     }
199
200 *val = defaultVal;
201 return -1;
202 }
203 //---------------------------------------------------------------------------
204 int CONFIGFILE::ReadUInt(const string & param, unsigned int * val, unsigned int defaultVal) const
205 {
206 const map<string, string>::const_iterator it(param_val.find(param));
207 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
208
209 if (it != param_val.end())
210     {
211     // þÔÏ-ÔÏ ÓÔÏÉÔ
212     char *res;
213     *val = strtoul(it->second.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::ReadLongInt(const string & param, long int * val, long int defaultVal) const
227 {
228 const map<string, string>::const_iterator it(param_val.find(param));
229 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
230
231 if (it != param_val.end())
232     {
233     // þÔÏ-ÔÏ ÓÔÏÉÔ
234     char *res;
235     *val = strtol(it->second.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::ReadULongInt(const string & param, unsigned long int * val, unsigned long int defaultVal) const
249 {
250 const map<string, string>::const_iterator it(param_val.find(param));
251 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
252
253 if (it != param_val.end())
254     {
255     // þÔÏ-ÔÏ ÓÔÏÉÔ
256     char *res;
257     *val = strtoul(it->second.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::ReadLongLongInt(const string & param, int64_t * val, int64_t defaultVal) const
271 {
272 const map<string, string>::const_iterator it(param_val.find(param));
273 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
274
275 if (it != param_val.end())
276     {
277     // þÔÏ-ÔÏ ÓÔÏÉÔ
278     char *res;
279     *val = strtoll(it->second.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::ReadULongLongInt(const string & param, uint64_t * val, uint64_t defaultVal) const
293 {
294 const map<string, string>::const_iterator it(param_val.find(param));
295 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
296
297 if (it != param_val.end())
298     {
299     // þÔÏ-ÔÏ ÓÔÏÉÔ
300     char *res;
301     *val = strtoull(it->second.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::ReadShortInt(const string & param, short int * val, short int defaultVal) const
315 {
316 const map<string, string>::const_iterator it(param_val.find(param));
317 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
318
319 if (it != param_val.end())
320     {
321     // þÔÏ-ÔÏ ÓÔÏÉÔ
322     char *res;
323     *val = (short)strtol(it->second.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::ReadUShortInt(const string & param, unsigned short int * val, unsigned short int defaultVal) const
337 {
338 const map<string, string>::const_iterator it(param_val.find(param));
339 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
340
341 if (it != param_val.end())
342     {
343     // þÔÏ-ÔÏ ÓÔÏÉÔ
344     char *res;
345     *val = (short)strtoul(it->second.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 void CONFIGFILE::WriteInt(const string & param, int64_t val)
359 {
360 string s;
361 x2str(val, s);
362 param_val[param] = s;
363 }
364 //---------------------------------------------------------------------------
365 int CONFIGFILE::ReadDouble(const string & param, double * val, double defaultVal) const
366 {
367 const map<string, string>::const_iterator it(param_val.find(param));
368 // îÁÛÌÉ ÎÕÖÎÕÀ ÐÅÒÅÍÅÎÎÕÀ
369
370 if (it != param_val.end())
371     {
372     // þÔÏ-ÔÏ ÓÔÏÉÔ
373     char *res;
374     *val = strtod(it->second.c_str(), &res);
375     if (*res != 0)
376         {
377         *val = defaultVal; //Error!
378         return EINVAL;
379         }
380     return 0;
381     }
382
383 *val = defaultVal;
384 return -1;
385 }
386 //---------------------------------------------------------------------------
387 void CONFIGFILE::WriteDouble(const string & param, double val)
388 {
389 char s[30];
390 snprintf(s, 30, "%f", val);
391 param_val[param] = s;
392 }
393 //---------------------------------------------------------------------------