]> git.stg.codes - stg.git/blob - sgauth/settings_impl.cpp
Port to CMake, get rid of os_int.h.
[stg.git] / 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 "stg/common.h"
25 #include "stg/conffiles.h"
26 #include "settings_impl.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::ReadSettings()
42 {
43 CONFIGFILE cf(confFile);
44
45 if (cf.Error())
46     {
47     strError = "Cannot read file '" + confFile + "'";
48     return -1;
49     }
50
51 cf.ReadString("Login", &login, "/?--?--?*");
52 if (login == "/?--?--?*")
53     {
54     strError = "Parameter 'Login' not found.";
55     return -1;
56     }
57
58 cf.ReadString("Password", &password, "/?--?--?*");
59 if (login == "/?--?--?*")
60     {
61     strError = "Parameter 'Password' not found.";
62     return -1;
63     }
64
65 cf.ReadString("ServerName", &serverName, "?*?*?");
66 if (serverName == "?*?*?")
67     {
68     strError = "Parameter 'ServerName' not found.";
69     return -1;
70     }
71
72 std::string temp;
73 cf.ReadString("ListenWebIP", &temp, "127.0.0.1");
74 listenWebIP = inet_strington(temp);
75 if (listenWebIP == 0)
76     {
77     strError = "Parameter 'ListenWebIP' is not valid.";
78     return -1;
79     }
80
81 cf.ReadString("ServerPort", &temp, "5555");
82 if (ParseIntInRange(temp, 1, 65535, &port))
83     {
84     strError = "Parameter 'ServerPort' is not valid.";
85     return -1;
86     }
87
88 cf.ReadString("LocalName", &localName, "");
89
90 cf.ReadString("LocalPort", &temp, "0");
91 if (ParseIntInRange(temp, 0, 65535, &localPort))
92     {
93     strError = "Parameter 'LocalPort' is not valid.";
94     return -1;
95     }
96
97 cf.ReadString("RefreshPeriod", &temp, "5");
98 if (ParseIntInRange(temp, 1, 24*3600, &refreshPeriod))
99     {
100     strError = "Parameter 'RefreshPeriod' is not valid.";
101     return -1;
102     }
103
104 cf.ReadString("Reconnect", &temp, "yes");
105 if (ParseYesNo(temp, &reconnect))
106     {
107     strError = "Parameter 'Reconnect' is not valid.";
108     return -1;
109     }
110
111 cf.ReadString("Daemon", &temp, "yes");
112 if (ParseYesNo(temp, &daemon))
113     {
114     strError = "Parameter 'Daemon' is not valid.";
115     return -1;
116     }
117
118 cf.ReadString("ShowPid", &temp, "no");
119 if (ParseYesNo(temp, &showPid))
120     {
121     strError = "Parameter 'ShowPid' is not valid.";
122     return -1;
123     }
124
125 cf.ReadString("DisableWeb", &temp, "no");
126 if (ParseYesNo(temp, &noWeb))
127     {
128     strError = "Parameter 'DisableWeb' is not valid.";
129     return -1;
130     }
131
132 return 0;
133 }
134 //-----------------------------------------------------------------------------
135 void SETTINGS_IMPL::Print() const
136 {
137 std::cout << "Login = " << login << "\n"
138           << "Password = " << password << "\n"
139           << "Ip = " << serverName << "\n"
140           << "Port = " << port << "\n"
141           << "LocalPort = " << localPort << "\n"
142           << "ListenWebIP = " << inet_ntostring(listenWebIP) << "\n"
143           << "RefreshPeriod = " << refreshPeriod << "\n"
144           << "Daemon = " << daemon << "\n"
145           << "DisableWeb = " << noWeb << "\n"
146           << "Reconnect = " << reconnect << "\n"
147           << "ShowPid = " << showPid << std::endl;
148 }