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.
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.
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
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
24 #include "stg/common.h"
25 #include "stg/conffiles.h"
26 #include "settings_impl.h"
28 SETTINGS_IMPL::SETTINGS_IMPL()
37 confFile("/etc/sgauth.conf")
40 //-----------------------------------------------------------------------------
41 int SETTINGS_IMPL::ParseYesNo(const string & value, bool * val)
43 if (0 == strcasecmp(value.c_str(), "yes"))
48 if (0 == strcasecmp(value.c_str(), "no"))
54 strError = "Incorrect value \'" + value + "\'.";
57 //-----------------------------------------------------------------------------
58 int SETTINGS_IMPL::ParseInt(const string & value, int * val)
60 if (str2x<int>(value, *val))
62 strError = "Cannot convert \'" + value + "\' to integer.";
67 //-----------------------------------------------------------------------------
68 int SETTINGS_IMPL::ParseUnsigned(const string & value, unsigned * val)
70 if (str2x<unsigned>(value, *val))
72 strError = "Cannot convert \'" + value + "\' to unsigned integer.";
77 //-----------------------------------------------------------------------------
78 int SETTINGS_IMPL::ParseIntInRange(const string & value, int min, int max, int * val)
80 if (ParseInt(value, val) != 0)
83 if (*val < min || *val > max)
85 strError = "Value \'" + value + "\' out of range.";
91 //-----------------------------------------------------------------------------
92 int SETTINGS_IMPL::ParseUnsignedInRange(const string & value, unsigned min, unsigned max, unsigned * val)
94 if (ParseUnsigned(value, val) != 0)
97 if (*val < min || *val > max)
99 strError = "Value \'" + value + "\' out of range.";
105 //-----------------------------------------------------------------------------
106 int SETTINGS_IMPL::ReadSettings()
108 CONFIGFILE cf(confFile);
112 strError = "Cannot read file '" + confFile + "'";
116 cf.ReadString("Login", &login, "/?--?--?*");
117 if (login == "/?--?--?*")
119 strError = "Parameter 'Login' not found.";
123 cf.ReadString("Password", &password, "/?--?--?*");
124 if (login == "/?--?--?*")
126 strError = "Parameter 'Password' not found.";
130 cf.ReadString("ServerName", &serverName, "?*?*?");
131 if (serverName == "?*?*?")
133 strError = "Parameter 'ServerName' not found.";
138 cf.ReadString("ListenWebIP", &temp, "127.0.0.1");
139 listenWebIP = inet_strington(temp);
140 if (listenWebIP == 0)
142 strError = "Parameter 'ListenWebIP' is not valid.";
146 cf.ReadString("ServerPort", &temp, "5555");
147 if (ParseIntInRange(temp, 1, 65535, &port))
149 strError = "Parameter 'ServerPort' is not valid.";
153 cf.ReadString("LocalPort", &temp, "0");
154 if (ParseIntInRange(temp, 0, 65535, &localPort))
156 strError = "Parameter 'LocalPort' is not valid.";
160 cf.ReadString("RefreshPeriod", &temp, "5");
161 if (ParseIntInRange(temp, 1, 24*3600, &refreshPeriod))
163 strError = "Parameter 'RefreshPeriod' is not valid.";
167 cf.ReadString("Reconnect", &temp, "yes");
168 if (ParseYesNo(temp, &reconnect))
170 strError = "Parameter 'Reconnect' is not valid.";
174 cf.ReadString("Daemon", &temp, "yes");
175 if (ParseYesNo(temp, &daemon))
177 strError = "Parameter 'Daemon' is not valid.";
181 cf.ReadString("ShowPid", &temp, "no");
182 if (ParseYesNo(temp, &showPid))
184 strError = "Parameter 'ShowPid' is not valid.";
188 cf.ReadString("DisableWeb", &temp, "no");
189 if (ParseYesNo(temp, &noWeb))
191 strError = "Parameter 'DisableWeb' is not valid.";
197 //-----------------------------------------------------------------------------
198 void SETTINGS_IMPL::Print() const
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;