-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cpp
154 lines (140 loc) · 6.55 KB
/
Config.cpp
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
#include <iostream>
#include <fstream>
#include <regex>
#include "Config.h"
Config::Config(std::string file) {
this->file_path = file;
// Set some defaults.
this->idle_time = 30.0;
this->brightness_idle = 1;
this->brightness_active = 50;
this->frequency_min = 88100000;
this->frequency_max = 107000000;
this->frequency_start = 88100000;
this->frequency_display_format = std::string("%.1f");
this->tune_kill_sleep_time = 5.0;
this->tune_increment_trans_time = 2.0;
this->tune_increment_normal = 100000;
this->tune_increment_fast = 1000000;
this->softfm_path = std::string("lib/ngsoftfm/build/softfm");
this->softfm_args = std::string("freq=%d gain=auto");
this->bookmark_margin = 20;
this->bookmark_cols = 7;
this->bookmark_separation = 5;
}
Config::~Config() {
}
void Config::save() {
std::ofstream configFile;
configFile.open(this->file_path);
configFile << "# Describes how long to wait on inactivity before changing" << std::endl;
configFile << "# the brightness level." << std::endl;
configFile << "idle_time=" << this->idle_time << std::endl;
configFile << "# A level of 0 means no backlight and 100 is full." << std::endl;
configFile << "brightness_idle=" << this->brightness_idle << std::endl;
configFile << "brightness_active=" << this->brightness_active << std::endl;
configFile << "# The min/max frequencies in Hz." << std::endl;
configFile << "frequency_min=" << this->frequency_min << std::endl;
configFile << "frequency_max=" << this->frequency_max << std::endl;
configFile << "frequency_start=" << this->frequency_start << std::endl;
configFile << "# How to display the frequency in MHz." << std::endl;
configFile << "frequency_display_format=" << this->frequency_display_format << std::endl;
configFile << "# How long to let the softfm program run before attempting" << std::endl;
configFile << "# to kill it in seconds." << std::endl;
configFile << "tune_kill_sleep_time=" << this->tune_kill_sleep_time << std::endl;
configFile << "# How long the tune button must be held before it" << std::endl;
configFile << "# transitions to the fast increment speed." << std::endl;
configFile << "tune_increment_trans_time=" << this->tune_increment_trans_time << std::endl;
configFile << "# The amount to increment in Hz." << std::endl;
configFile << "tune_increment_normal=" << this->tune_increment_normal << std::endl;
configFile << "tune_increment_fast=" << this->tune_increment_fast << std::endl;
configFile << "# The path to the softfm program." << std::endl;
configFile << "softfm_path=" << this->softfm_path << std::endl;
configFile << "# The arguments to pass to the softfm program specific to" << std::endl;
configFile << "# the RTL-SDR inputs; at a minimum it must have:" << std::endl;
configFile << "# 'freq=%d' for the correct frequency to be passed." << std::endl;
configFile << "softfm_args=" << this->softfm_args << std::endl;
configFile << "# How much (in pixels) to indent on both sides." << std::endl;
configFile << "bookmark_margin=" << this->bookmark_margin << std::endl;
configFile << "# How many bookmarked stations to show in one row." << std::endl;
configFile << "bookmark_cols=" << this->bookmark_cols << std::endl;
configFile << "# How many pixels to space between bookmarked stations." << std::endl;
configFile << "bookmark_separation=" << this->bookmark_separation << std::endl;
configFile.close();
}
bool Config::load() {
std::ifstream configFile;
configFile.open(this->file_path);
if (configFile.fail()) {
return false;
}
std::string line;
while (std::getline(configFile, line)) {
std::size_t pos = line.find('#');
if (pos == 0) {
// Ignore a commented line
continue;
}
pos = line.find('=');
if (pos == std::string::npos) {
// keep going, this line is malformed.
continue;
}
std::string attr_name = line.substr(0, pos);
std::string attr_value = line.substr(pos+1);
if (attr_name == "idle_time") {
this->idle_time = std::stod(attr_value);
} else if (attr_name == "brightness_idle") {
this->brightness_idle = std::stoi(attr_value);
} else if (attr_name == "brightness_active") {
this->brightness_active = std::stoi(attr_value);
} else if (attr_name == "frequency_min") {
this->frequency_min = std::stoi(attr_value);
} else if (attr_name == "frequency_max") {
this->frequency_max = std::stoi(attr_value);
} else if (attr_name == "frequency_start") {
this->frequency_start = std::stoi(attr_value);
} else if (attr_name == "frequency_display_format") {
this->frequency_display_format = attr_value;
} else if (attr_name == "tune_kill_sleep_time") {
this->tune_kill_sleep_time = std::stod(attr_value);
} else if (attr_name == "tune_increment_trans_time") {
this->tune_increment_trans_time = std::stod(attr_value);
} else if (attr_name == "tune_increment_normal") {
this->tune_increment_normal = std::stoi(attr_value);
} else if (attr_name == "tune_increment_fast") {
this->tune_increment_fast = std::stoi(attr_value);
} else if (attr_name == "softfm_path") {
this->softfm_path = attr_value;
} else if (attr_name == "softfm_args") {
this->softfm_args = attr_value;
} else if (attr_name == "bookmark_margin") {
this->bookmark_margin = std::stoi(attr_value);
} else if (attr_name == "bookmark_cols") {
this->bookmark_cols = std::stoi(attr_value);
} else if (attr_name == "bookmark_separation") {
this->bookmark_separation = std::stoi(attr_value);
} else {
std::cout << "No match for (" << attr_name << ") = (" << attr_value << ")" << std::endl;
}
}
configFile.close();
return true;
}
void Config::print_config() {
std::cout << "======Beg Config=====" << std::endl;
std::cout << "idle_time=" << this->idle_time << std::endl;
std::cout << "brightness_idle=" << this->brightness_idle << std::endl;
std::cout << "brightness_active=" << this->brightness_active << std::endl;
std::cout << "frequency_min=" << this->frequency_min << std::endl;
std::cout << "frequency_max=" << this->frequency_max << std::endl;
std::cout << "frequency_start=" << this->frequency_start << std::endl;
std::cout << "frequency_display_format=" << this->frequency_display_format << std::endl;
std::cout << "tune_kill_sleep_time=" << this->tune_kill_sleep_time << std::endl;
std::cout << "tune_increment_trans_time=" << this->tune_increment_trans_time << std::endl;
std::cout << "tune_increment_normal=" << this->tune_increment_normal << std::endl;
std::cout << "tune_increment_fast=" << this->tune_increment_fast << std::endl;
std::cout << "softfm_path=" << this->softfm_path << std::endl;
std::cout << "softfm_args=" << this->softfm_args << std::endl;
std::cout << "======End Config=====" << std::endl;
}