-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathConf.cpp
More file actions
234 lines (200 loc) · 5.15 KB
/
Conf.cpp
File metadata and controls
234 lines (200 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Copyright (C) 2015,2016,2017,2018,2020,2023,2025 by Jonathan Naylor G4KLX
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "Conf.h"
#include "Log.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
const int BUFFER_SIZE = 500;
enum class SECTION {
NONE,
GENERAL,
LOG,
APRS_IS,
MQTT
};
CConf::CConf(const std::string& file) :
m_file(file),
m_callsign(),
m_debug(false),
m_daemon(false),
m_logDisplayLevel(0U),
m_logMQTTLevel(0U),
m_aprsServer(),
m_aprsPort(0U),
m_aprsPassword(),
m_mqttAddress("127.0.0.1"),
m_mqttPort(1883U),
m_mqttKeepalive(60U),
m_mqttName("aprs-gateway"),
m_mqttAuthEnabled(false),
m_mqttUsername(),
m_mqttPassword()
{
}
CConf::~CConf()
{
}
bool CConf::read()
{
FILE* fp = ::fopen(m_file.c_str(), "rt");
if (fp == nullptr) {
::fprintf(stderr, "Couldn't open the .ini file - %s\n", m_file.c_str());
return false;
}
SECTION section = SECTION::NONE;
char buffer[BUFFER_SIZE];
while (::fgets(buffer, BUFFER_SIZE, fp) != nullptr) {
if (buffer[0U] == '#')
continue;
if (buffer[0U] == '[') {
if (::strncmp(buffer, "[General]", 9U) == 0)
section = SECTION::GENERAL;
else if (::strncmp(buffer, "[Log]", 5U) == 0)
section = SECTION::LOG;
else if (::strncmp(buffer, "[APRS-IS]", 9U) == 0)
section = SECTION::APRS_IS;
else if (::strncmp(buffer, "[MQTT]", 6U) == 0)
section = SECTION::MQTT;
else
section = SECTION::NONE;
continue;
}
char* key = ::strtok(buffer, " \t=\r\n");
if (key == nullptr)
continue;
char* value = ::strtok(nullptr, "\r\n");
if (value == nullptr)
continue;
// Remove quotes from the value
size_t len = ::strlen(value);
if (len > 1U && *value == '"' && value[len - 1U] == '"') {
value[len - 1U] = '\0';
value++;
} else {
char *p;
// if value is not quoted, remove after # (to make comment)
if ((p = strchr(value, '#')) != nullptr)
*p = '\0';
// remove trailing tab/space
for (p = value + strlen(value) - 1U; p >= value && (*p == '\t' || *p == ' '); p--)
*p = '\0';
}
if (section == SECTION::GENERAL) {
if (::strcmp(key, "Callsign") == 0) {
// Convert the callsign to upper case
for (unsigned int i = 0U; value[i] != 0; i++)
value[i] = ::toupper(value[i]);
m_callsign = value;
} else if (::strcmp(key, "Debug") == 0)
m_debug = ::atoi(value) == 1;
else if (::strcmp(key, "Daemon") == 0)
m_daemon = ::atoi(value) == 1;
} else if (section == SECTION::LOG) {
if (::strcmp(key, "MQTTLevel") == 0)
m_logMQTTLevel = (unsigned int)::atoi(value);
else if (::strcmp(key, "DisplayLevel") == 0)
m_logDisplayLevel = (unsigned int)::atoi(value);
} else if (section == SECTION::APRS_IS) {
if (::strcmp(key, "Server") == 0)
m_aprsServer = value;
else if (::strcmp(key, "Port") == 0)
m_aprsPort = (unsigned short)::atoi(value);
else if (::strcmp(key, "Password") == 0)
m_aprsPassword = value;
} else if (section == SECTION::MQTT) {
if (::strcmp(key, "Address") == 0)
m_mqttAddress = value;
else if (::strcmp(key, "Port") == 0)
m_mqttPort = (unsigned short)::atoi(value);
else if (::strcmp(key, "Keepalive") == 0)
m_mqttKeepalive = (unsigned int)::atoi(value);
else if (::strcmp(key, "Name") == 0)
m_mqttName = value;
else if (::strcmp(key, "Auth") == 0)
m_mqttAuthEnabled = ::atoi(value) == 1;
else if (::strcmp(key, "Username") == 0)
m_mqttUsername = value;
else if (::strcmp(key, "Password") == 0)
m_mqttPassword = value;
}
}
::fclose(fp);
return true;
}
std::string CConf::getCallsign() const
{
return m_callsign;
}
bool CConf::getDebug() const
{
return m_debug;
}
bool CConf::getDaemon() const
{
return m_daemon;
}
std::string CConf::getAPRSServer() const
{
return m_aprsServer;
}
unsigned short CConf::getAPRSPort() const
{
return m_aprsPort;
}
std::string CConf::getAPRSPassword() const
{
return m_aprsPassword;
}
unsigned int CConf::getLogDisplayLevel() const
{
return m_logDisplayLevel;
}
unsigned int CConf::getLogMQTTLevel() const
{
return m_logMQTTLevel;
}
std::string CConf::getMQTTAddress() const
{
return m_mqttAddress;
}
unsigned short CConf::getMQTTPort() const
{
return m_mqttPort;
}
unsigned int CConf::getMQTTKeepalive() const
{
return m_mqttKeepalive;
}
std::string CConf::getMQTTName() const
{
return m_mqttName;
}
bool CConf::getMQTTAuthEnabled() const
{
return m_mqttAuthEnabled;
}
std::string CConf::getMQTTUsername() const
{
return m_mqttUsername;
}
std::string CConf::getMQTTPassword() const
{
return m_mqttPassword;
}