Debian Codegraph @

src/engine/config.cpp

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "config.h"
21 #include "../ta-log.h"
22 #include "../baseutils.h"
23 #include "../fileutils.h"
24 #include <fmt/printf.h>
25
26 #define REDUNDANCY_NUM_ATTEMPTS 5
27 #define CHECK_BUF_SIZE 8192
28
29 bool DivConfig::save(const char* path, bool redundancy) {
30 if (redundancy) {
31 char oldPath[4096];
32 char newPath[4096];
33
34 if (fileExists(path)==1) {
35 logD("rotating config files...");
36 for (int i=4; i>=0; i--) {
37 if (i>0) {
38 snprintf(oldPath,4095,"%s.%d",path,i);
39 } else {
40 strncpy(oldPath,path,4095);
41 }
42 snprintf(newPath,4095,"%s.%d",path,i+1);
43
44 if (i>=4) {
45 logV("remove %s",oldPath);
46 deleteFile(oldPath);
47 } else {
48 logV("move %s to %s",oldPath,newPath);
49 moveFiles(oldPath,newPath);
50 }
51 }
52 }
53 }
54 logD("opening config for write: %s",path);
55 FILE* f=ps_fopen(path,"wb");
56 if (f==NULL) {
57 logW("could not write config file! %s",strerror(errno));
58 reportError(fmt::sprintf("could not write config file! %s",strerror(errno)));
59 return false;
60 }
61 for (auto& i: conf) {
62 String toWrite=fmt::sprintf("%s=%s\n",i.first,i.second);
63 if (fwrite(toWrite.c_str(),1,toWrite.size(),f)!=toWrite.size()) {
64 logW("could not write config file! %s",strerror(errno));
65 reportError(fmt::sprintf("could not write config file! %s",strerror(errno)));
66 logV("removing config file");
67 fclose(f);
68 deleteFile(path);
69 return false;
70 }
71 }
72 fclose(f);
73 logD("config file written successfully.");
74 return true;
75 }
76
77 String DivConfig::toString() {
78 String ret;
79 for (auto& i: conf) {
80 ret+=fmt::sprintf("%s=%s\n",i.first,i.second);
81 }
82 return ret;
83 }
84
85 String DivConfig::toBase64() {
86 String data=toString();
87 return taEncodeBase64(data);
88 }
89
90 const std::map<String,String>& DivConfig::configMap() {
91 return conf;
92 }
93
94 void DivConfig::parseLine(const char* line) {
95 String key="";
96 String value="";
97 bool keyOrValue=false;
98 for (const char* i=line; *i; i++) {
99 if (*i=='\r') continue;
100 if (*i=='\n') continue;
101 if (keyOrValue) {
102 value+=*i;
103 } else {
104 if (*i=='=') {
105 keyOrValue=true;
106 } else {
107 key+=*i;
108 }
109 }
110 }
111 if (keyOrValue) {
112 conf[key]=value;
113 }
114 }
115
116 bool DivConfig::loadFromFile(const char* path, bool createOnFail, bool redundancy) {
117 char line[4096];
118 String lineStr;
119 lineStr.reserve(4096);
120 logD("opening config for read: %s",path);
121
122 FILE* f=NULL;
123
124 if (redundancy) {
125 unsigned char* readBuf=new unsigned char[CHECK_BUF_SIZE];
126 size_t readBufLen=0;
127 for (int i=0; i<REDUNDANCY_NUM_ATTEMPTS; i++) {
128 bool viable=false;
129 if (i>0) {
130 snprintf(line,4095,"%s.%d",path,i);
131 } else {
132 strncpy(line,path,4095);
133 }
134 logV("trying: %s",line);
135
136 // try to open config
137 f=ps_fopen(line,"rb");
138 // check whether we could open it
139 if (f==NULL) {
140 logV("fopen(): %s",strerror(errno));
141 continue;
142 }
143
144 // check whether there's something
145 while (!feof(f)) {
146 readBufLen=fread(readBuf,1,CHECK_BUF_SIZE,f);
147 if (ferror(f)) {
148 logV("fread(): %s",strerror(errno));
149 break;
150 }
151
152 for (size_t j=0; j<readBufLen; j++) {
153 if (readBuf[j]==0) {
154 viable=false;
155 logW("a zero?");
156 break;
157 }
158 if (readBuf[j]!='\r' && readBuf[j]!='\n' && readBuf[j]!=' ') {
159 viable=true;
160 }
161 }
162
163 if (viable) break;
164 }
165
166 // there's something
167 if (viable) {
168 if (fseek(f,0,SEEK_SET)==-1) {
169 logV("fseek(): %s",strerror(errno));
170 viable=false;
171 } else {
172 break;
173 }
174 }
175
176 // close it (because there's nothing)
177 fclose(f);
178 f=NULL;
179 }
180 delete[] readBuf;
181
182 // we couldn't read at all
183 if (f==NULL) {
184 logD("config does not exist");
185 if (createOnFail) {
186 logI("creating default config.");
187 //reportError(fmt::sprintf("Creating default config: %s",strerror(errno)));
188 return save(path,redundancy);
189 } else {
190 reportError(fmt::sprintf("COULD NOT LOAD CONFIG %s",strerror(errno)));
191 return false;
192 }
193 }
194 } else {
195 f=ps_fopen(path,"rb");
196 if (f==NULL) {
197 logD("config does not exist");
198 if (createOnFail) {
199 logI("creating default config.");
200 //reportError(fmt::sprintf("Creating default config: %s",strerror(errno)));
201 return save(path);
202 } else {
203 reportError(fmt::sprintf("COULD NOT LOAD CONFIG %s",strerror(errno)));
204 return false;
205 }
206 }
207 }
208
209
210 logI("loading config.");
211 while (!feof(f)) {
212 if (fgets(line,4095,f)==NULL) {
213 break;
214 }
215 lineStr+=line;
216 if (!lineStr.empty() && !feof(f)) {
217 if (lineStr[lineStr.size()-1]!='\n') {
218 continue;
219 }
220 }
221 parseLine(lineStr.c_str());
222 lineStr="";
223 lineStr.reserve(4096);
224 }
225 logD("end of file (%s)",strerror(errno));
226 fclose(f);
227 return true;
228 }
229
230 bool DivConfig::loadFromMemory(const char* buf) {
231 String line;
232 const char* readPos=buf;
233 while (*readPos) {
234 line+=*readPos;
235 readPos++;
236 if ((*readPos)=='\n' || (*readPos)==0) {
237 parseLine(line.c_str());
238 line="";
239 }
240 }
241 return true;
242 }
243
244 bool DivConfig::loadFromBase64(const char* buf) {
245 String data=taDecodeBase64(buf);
246 return loadFromMemory(data.c_str());
247 }
248
249 bool DivConfig::getBool(String key, bool fallback) const {
250 auto val=conf.find(key);
251 if (val!=conf.cend()) {
252 if (val->second=="true") {
253 return true;
254 } else if (val->second=="false") {
255 return false;
256 } else {
257 try {
258 int ret=std::stoi(val->second);
259 return (ret!=0);
260 } catch (std::out_of_range& e) {
261 } catch (std::invalid_argument& e) {
262 }
263 }
264 }
265 return fallback;
266 }
267
268 int DivConfig::getInt(String key, int fallback) const {
269 auto val=conf.find(key);
270 if (val!=conf.cend()) {
271 try {
272 int ret=std::stoi(val->second);
273 return ret;
274 } catch (std::out_of_range& e) {
275 } catch (std::invalid_argument& e) {
276 }
277 }
278 return fallback;
279 }
280
281 float DivConfig::getFloat(String key, float fallback) const {
282 auto val=conf.find(key);
283 if (val!=conf.cend()) {
284 try {
285 float ret=std::stof(val->second);
286 return ret;
287 } catch (std::out_of_range& e) {
288 } catch (std::invalid_argument& e) {
289 }
290 }
291 return fallback;
292 }
293
294 double DivConfig::getDouble(String key, double fallback) const {
295 auto val=conf.find(key);
296 if (val!=conf.cend()) {
297 try {
298 double ret=std::stod(val->second);
299 return ret;
300 } catch (std::out_of_range& e) {
301 } catch (std::invalid_argument& e) {
302 }
303 }
304 return fallback;
305 }
306
307 String DivConfig::getString(String key, String fallback) const {
308 auto val=conf.find(key);
309 if (val!=conf.cend()) {
310 return val->second;
311 }
312 return fallback;
313 }
314
315 std::vector<int> DivConfig::getIntList(String key, std::initializer_list<int> fallback) const {
316 String next;
317 std::vector<int> ret;
318 auto val=conf.find(key);
319 if (val!=conf.cend()) {
320 try {
321 for (char i: val->second) {
322 if (i==',') {
323 int num=std::stoi(next);
324 ret.push_back(num);
325 next="";
326 } else {
327 next+=i;
328 }
329 }
330 if (!next.empty()) {
331 int num=std::stoi(next);
332 ret.push_back(num);
333 }
334
335 return ret;
336 } catch (std::out_of_range& e) {
337 } catch (std::invalid_argument& e) {
338 }
339 }
340 return fallback;
341 }
342
343 bool DivConfig::has(String key) const {
344 auto val=conf.find(key);
345 return (val!=conf.cend());
346 }
347
348 void DivConfig::set(String key, bool value) {
349 if (value) {
350 conf[key]="true";
351 } else {
352 conf[key]="false";
353 }
354 }
355
356 void DivConfig::set(String key, int value) {
357 conf[key]=fmt::sprintf("%d",value);
358 }
359
360 void DivConfig::set(String key, float value) {
361 conf[key]=fmt::sprintf("%f",value);
362 }
363
364 void DivConfig::set(String key, double value) {
365 conf[key]=fmt::sprintf("%f",value);
366 }
367
368 void DivConfig::set(String key, const char* value) {
369 conf[key]=String(value);
370 }
371
372 void DivConfig::set(String key, String value) {
373 conf[key]=value;
374 }
375
376 void DivConfig::set(String key, const std::vector<int>& value) {
377 String val;
378 bool comma=false;
379 for (int i: value) {
380 if (comma) val+=',';
381 val+=fmt::sprintf("%d",i);
382 comma=true;
383 }
384 conf[key]=val;
385 }
386
387 bool DivConfig::remove(String key) {
388 return conf.erase(key);
389 }
390
391 void DivConfig::clear() {
392 conf.clear();
393 }