-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_config.h
More file actions
145 lines (114 loc) · 4.7 KB
/
json_config.h
File metadata and controls
145 lines (114 loc) · 4.7 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
/*
* json_config.h - Simple JSON configuration parser
*
* A minimal JSON parser for configuration files.
* Handles basic key-value pairs with string and integer values.
*/
#ifndef JSON_CONFIG_H
#define JSON_CONFIG_H
#include <string>
#include <fstream>
#include <iostream>
#include <cctype>
class JsonConfig {
public:
// NTRIP settings
std::string ntrip_host;
int ntrip_port = 2101;
std::string ntrip_mountpoint;
std::string ntrip_user;
std::string ntrip_password;
// Serial port settings
std::string serial_port;
int serial_baud = 9600;
// TCP server settings (for forwarding serial data)
int tcp_port = 0; // 0 = disabled
// Other settings
int gga_interval_sec = 10;
bool log_serial = true; // Log raw serial data to a timestamped .log file
bool parse(const std::string& filename) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Error: Cannot open config file: " << filename << std::endl;
return false;
}
std::string content((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
ntrip_host = get_string(content, "ntrip_host");
ntrip_port = get_int(content, "ntrip_port", 2101);
ntrip_mountpoint = get_string(content, "ntrip_mountpoint");
ntrip_user = get_string(content, "ntrip_user");
ntrip_password = get_string(content, "ntrip_password");
serial_port = get_string(content, "serial_port");
serial_baud = get_int(content, "serial_baud", 9600);
tcp_port = get_int(content, "tcp_port", 0);
gga_interval_sec = get_int(content, "gga_interval_sec", 10);
log_serial = get_bool(content, "log_serial", true);
return validate();
}
bool validate() const {
if (ntrip_host.empty()) {
std::cerr << "Error: ntrip_host required\n";
return false;
}
if (ntrip_mountpoint.empty()) {
std::cerr << "Error: ntrip_mountpoint required\n";
return false;
}
if (serial_port.empty()) {
std::cerr << "Error: serial_port required\n";
return false;
}
return true;
}
void print() const {
std::cout << "Configuration:\n"
<< " NTRIP: " << ntrip_host << ":" << ntrip_port << "/" << ntrip_mountpoint << "\n"
<< " Serial: " << serial_port << " @ " << serial_baud << " baud\n";
if (tcp_port > 0) {
std::cout << " TCP Server: port " << tcp_port << "\n";
}
std::cout << " GGA Interval: " << gga_interval_sec << "s\n"
<< " Serial Logging: " << (log_serial ? "enabled" : "disabled") << "\n";
}
// Static helper methods for parsing JSON - can be used externally
static std::string get_string(const std::string& json, const std::string& key) {
std::string search = "\"" + key + "\"";
size_t pos = json.find(search);
if (pos == std::string::npos) return "";
pos = json.find(':', pos);
if (pos == std::string::npos) return "";
pos = json.find('"', pos);
if (pos == std::string::npos) return "";
size_t end = json.find('"', pos + 1);
if (end == std::string::npos) return "";
return json.substr(pos + 1, end - pos - 1);
}
static int get_int(const std::string& json, const std::string& key, int def = 0) {
std::string search = "\"" + key + "\"";
size_t pos = json.find(search);
if (pos == std::string::npos) return def;
pos = json.find(':', pos);
if (pos == std::string::npos) return def;
pos++;
while (pos < json.size() && (json[pos] == ' ' || json[pos] == '\t')) pos++;
std::string num;
while (pos < json.size() && (std::isdigit(json[pos]) || json[pos] == '-')) {
num += json[pos++];
}
return num.empty() ? def : std::stoi(num);
}
static bool get_bool(const std::string& json, const std::string& key, bool def = false) {
std::string search = "\"" + key + "\"";
size_t pos = json.find(search);
if (pos == std::string::npos) return def;
pos = json.find(':', pos);
if (pos == std::string::npos) return def;
pos++;
while (pos < json.size() && (json[pos] == ' ' || json[pos] == '\t')) pos++;
if (json.substr(pos, 4) == "true") return true;
if (json.substr(pos, 5) == "false") return false;
return def;
}
};
#endif // JSON_CONFIG_H