]> git.stg.codes - stg.git/blob - projects/sgauth/settings_impl.cpp
SETTINGS definition and implementation extracted from main.cpp to
[stg.git] / projects / sgauth / settings_impl.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  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 #include <iostream>
22 #include <cstring>
23
24 #include "settings_impl.h"
25 #include "common.h"
26 #include "conffiles.h"
27
28 SETTINGS_IMPL::SETTINGS_IMPL()
29     : port(0),
30       localPort(0),
31       listenWebIP(0),
32       refreshPeriod(0),
33       daemon(false),
34       noWeb(false),
35       reconnect(false),
36       showPid(false),
37       confFile("/etc/sgauth.conf")
38 {
39 }
40 //-----------------------------------------------------------------------------
41 int SETTINGS_IMPL::ParseYesNo(const string & value, bool * val)
42 {
43 if (0 == strcasecmp(value.c_str(), "yes"))
44     {
45     *val = true;
46     return 0;
47     }
48 if (0 == strcasecmp(value.c_str(), "no"))
49     {
50     *val = false;
51     return 0;
52     }
53
54 strError = "Incorrect value \'" + value + "\'.";
55 return -1;
56 }
57 //-----------------------------------------------------------------------------
58 int SETTINGS_IMPL::ParseInt(const string & value, int * val)
59 {
60 if (str2x<int>(value, *val))
61     {
62     strError = "Cannot convert \'" + value + "\' to integer.";
63     return -1;
64     }
65 return 0;
66 }
67 //-----------------------------------------------------------------------------
68 int SETTINGS_IMPL::ParseUnsigned(const string & value, unsigned * val)
69 {
70 if (str2x<unsigned>(value, *val))
71     {
72     strError = "Cannot convert \'" + value + "\' to unsigned integer.";
73     return -1;
74     }
75 return 0;
76 }
77 //-----------------------------------------------------------------------------
78 int SETTINGS_IMPL::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 SETTINGS_IMPL::ParseUnsignedInRange(const string & value, unsigned min, unsigned max, unsigned * val)
93 {
94 if (ParseUnsigned(value, val) != 0)
95     return -1;
96
97 if (*val < min || *val > max)
98     {
99     strError = "Value \'" + value + "\' out of range.";
100     return -1;
101     }
102
103 return 0;
104 }
105 //-----------------------------------------------------------------------------
106 int SETTINGS_IMPL::ReadSettings()
107 {
108 CONFIGFILE cf(confFile);
109
110 if (cf.Error())
111     {
112     strError = "Cannot read file '" + confFile + "'";
113     return -1;
114     }
115
116 cf.ReadString("Login", &login, "/?--?--?*");
117 if (login == "/?--?--?*")
118     {
119     strError = "Parameter 'Login' not found.";
120     return -1;
121     }
122
123 cf.ReadString("Password", &password, "/?--?--?*");
124 if (login == "/?--?--?*")
125     {
126     strError = "Parameter 'Password' not found.";
127     return -1;
128     }
129
130 cf.ReadString("ServerName", &serverName, "?*?*?");
131 if (serverName == "?*?*?")
132     {
133     strError = "Parameter 'ServerName' not found.";
134     return -1;
135     }
136
137 std::string temp;
138 cf.ReadString("ListenWebIP", &temp, "127.0.0.1");
139 listenWebIP = inet_strington(temp);
140 if (listenWebIP == 0)
141     {
142     strError = "Parameter 'ListenWebIP' is not valid.";
143     return -1;
144     }
145
146 cf.ReadString("ServerPort", &temp, "5555");
147 if (ParseIntInRange(temp, 1, 65535, &port))
148     {
149     strError = "Parameter 'ServerPort' is not valid.";
150     return -1;
151     }
152
153 cf.ReadString("LocalPort", &temp, "0");
154 if (ParseIntInRange(temp, 0, 65535, &localPort))
155     {
156     strError = "Parameter 'LocalPort' is not valid.";
157     return -1;
158     }
159
160 cf.ReadString("RefreshPeriod", &temp, "5");
161 if (ParseIntInRange(temp, 1, 24*3600, &refreshPeriod))
162     {
163     strError = "Parameter 'RefreshPeriod' is not valid.";
164     return -1;
165     }
166
167 cf.ReadString("Reconnect", &temp, "yes");
168 if (ParseYesNo(temp, &reconnect))
169     {
170     strError = "Parameter 'Reconnect' is not valid.";
171     return -1;
172     }
173
174 cf.ReadString("Daemon", &temp, "yes");
175 if (ParseYesNo(temp, &daemon))
176     {
177     strError = "Parameter 'Daemon' is not valid.";
178     return -1;
179     }
180
181 cf.ReadString("ShowPid", &temp, "no");
182 if (ParseYesNo(temp, &showPid))
183     {
184     strError = "Parameter 'ShowPid' is not valid.";
185     return -1;
186     }
187
188 cf.ReadString("DisableWeb", &temp, "no");
189 if (ParseYesNo(temp, &noWeb))
190     {
191     strError = "Parameter 'DisableWeb' is not valid.";
192     return -1;
193     }
194
195 return 0;
196 }
197 //-----------------------------------------------------------------------------
198 void SETTINGS_IMPL::Print() const
199 {
200 std::cout << "Login = " << login << "\n"
201           << "Password = " << password << "\n"
202           << "Ip = " << serverName << "\n"
203           << "Port = " << port << "\n"
204           << "LocalPort = " << localPort << "\n"
205           << "ListenWebIP = " << inet_ntostring(listenWebIP) << "\n"
206           << "RefreshPeriod = " << refreshPeriod << "\n"
207           << "Daemon = " << daemon << "\n"
208           << "DisableWeb = " << noWeb << "\n"
209           << "Reconnect = " << reconnect << "\n"
210           << "ShowPid = " << showPid << std::endl;
211 }