half-assed work

This commit is contained in:
tildearrow 2023-04-04 15:27:36 -05:00
parent 5026f62713
commit 3a84512486
2 changed files with 10 additions and 0 deletions

View file

@ -24,20 +24,24 @@
#include <fmt/printf.h> #include <fmt/printf.h>
bool DivConfig::save(const char* path) { bool DivConfig::save(const char* path) {
logD("opening config for write: %s",path);
FILE* f=ps_fopen(path,"wb"); FILE* f=ps_fopen(path,"wb");
if (f==NULL) { if (f==NULL) {
logW("could not write config file! %s",strerror(errno)); logW("could not write config file! %s",strerror(errno));
reportError(fmt::sprintf("could not write config file! %s",strerror(errno)));
return false; return false;
} }
for (auto& i: conf) { for (auto& i: conf) {
String toWrite=fmt::sprintf("%s=%s\n",i.first,i.second); String toWrite=fmt::sprintf("%s=%s\n",i.first,i.second);
if (fwrite(toWrite.c_str(),1,toWrite.size(),f)!=toWrite.size()) { if (fwrite(toWrite.c_str(),1,toWrite.size(),f)!=toWrite.size()) {
logW("could not write config file! %s",strerror(errno)); logW("could not write config file! %s",strerror(errno));
reportError(fmt::sprintf("could not write config file! %s",strerror(errno)));
fclose(f); fclose(f);
return false; return false;
} }
} }
fclose(f); fclose(f);
logD("config file written successfully.");
return true; return true;
} }
@ -81,12 +85,16 @@ void DivConfig::parseLine(const char* line) {
bool DivConfig::loadFromFile(const char* path, bool createOnFail) { bool DivConfig::loadFromFile(const char* path, bool createOnFail) {
char line[4096]; char line[4096];
logD("opening config for read: %s",path);
FILE* f=ps_fopen(path,"rb"); FILE* f=ps_fopen(path,"rb");
if (f==NULL) { if (f==NULL) {
logD("config does not exist");
if (createOnFail) { if (createOnFail) {
logI("creating default config."); logI("creating default config.");
showWarning(fmt::sprintf("Creating default config: %s",strerror(errno));
return save(path); return save(path);
} else { } else {
showWarning(fmt::sprintf("COULD NOT LOAD CONFIG %s",strerror(errno));
return false; return false;
} }
} }

View file

@ -66,4 +66,6 @@ struct TAParam {
func(f) {} func(f) {}
}; };
void reportError(String what);
#endif #endif