]> git.stg.codes - stg.git/blob - stglibs/common_settings.lib/common_settings.cpp
Добавление исходников
[stg.git] / stglibs / common_settings.lib / common_settings.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: 29.03.2007
19  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
20  */
21
22 /*
23 $Revision: 1.2 $
24 $Date: 2007/04/07 13:29:07 $
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <string>
32
33 using namespace std;
34
35 #include "common_settings.h"
36 #include "common.h"
37
38 //-----------------------------------------------------------------------------
39 COMMON_SETTINGS::COMMON_SETTINGS()
40 {
41
42 }
43 //-----------------------------------------------------------------------------
44 COMMON_SETTINGS::~COMMON_SETTINGS()
45 {
46
47 }
48 //-----------------------------------------------------------------------------
49 int COMMON_SETTINGS::ParseYesNo(const string & value, bool * val)
50 {
51 if (0 == strcasecmp(value.c_str(), "yes"))
52     {
53     *val = true;
54     return 0;
55     }
56 if (0 == strcasecmp(value.c_str(), "no"))
57     {
58     *val = false;
59     return 0;
60     }
61
62 strError = "Incorrect value \'" + value + "\'.";
63 return -1;
64 }
65 //-----------------------------------------------------------------------------
66 int COMMON_SETTINGS::ParseInt(const string & value, int * val)
67 {
68 char *res;
69 *val = strtol(value.c_str(), &res, 10);
70 if (*res != 0)
71     {
72     strError = "Cannot convert \'" + value + "\' to integer.";
73     return -1;
74     }
75 return 0;
76 }
77 //-----------------------------------------------------------------------------
78 int COMMON_SETTINGS::ParseIntInRange(const string & value, int min, int max, int * val)
79 {
80 if (ParseInt(value, val) != 0)
81     return -1;
82
83 if (*val < min || *val > max)
84     {
85     strError = "Value \'" + value + "\' out of range.";
86     return -1;
87     }
88
89 return 0;
90 }
91 //-----------------------------------------------------------------------------
92 int COMMON_SETTINGS::ParseDouble(const std::string & value, double * val)
93 {
94 char *res;
95 *val = strtod(value.c_str(), &res);
96 if (*res != 0)
97     {
98     strError = "Cannot convert \'" + value + "\' to double.";
99     return -1;
100     }
101 return 0;
102 }
103 //-----------------------------------------------------------------------------
104 int COMMON_SETTINGS::ParseDoubleInRange(const std::string & value, double min, double max, double * val)
105 {
106 if (ParseDouble(value, val) != 0)
107     return -1;
108
109 if (*val < min || *val > max)
110     {
111     strError = "Value \'" + value + "\' out of range.";
112     return -1;
113     }
114
115 return 0;
116 }
117 //-----------------------------------------------------------------------------
118 string COMMON_SETTINGS::GetStrError() const
119 {
120 return strError;
121 }
122 //-----------------------------------------------------------------------------
123 int COMMON_SETTINGS::Reload ()
124 {
125 return ReadSettings();
126 }
127 //-----------------------------------------------------------------------------
128