From 840be643145a8314c305c1a63e0dce96bb0ece42 Mon Sep 17 00:00:00 2001 From: tildearrow Date: Fri, 18 Feb 2022 13:11:04 -0500 Subject: [PATCH] split config management --- CMakeLists.txt | 1 + src/engine/config.cpp | 163 ++++++++++++++++++++++++++++++++++++++++++ src/engine/engine.cpp | 142 ------------------------------------ 3 files changed, 164 insertions(+), 142 deletions(-) create mode 100644 src/engine/config.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e3e27ab..c5d8d6e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -265,6 +265,7 @@ src/engine/platform/ym2610Interface.cpp src/engine/blip_buf.c src/engine/safeReader.cpp src/engine/safeWriter.cpp +src/engine/config.cpp src/engine/dispatchContainer.cpp src/engine/engine.cpp src/engine/fileOps.cpp diff --git a/src/engine/config.cpp b/src/engine/config.cpp new file mode 100644 index 00000000..f7444e3c --- /dev/null +++ b/src/engine/config.cpp @@ -0,0 +1,163 @@ +/** + * Furnace Tracker - multi-system chiptune tracker + * Copyright (C) 2021-2022 tildearrow and contributors + * + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "engine.h" +#include "../ta-log.h" +#include "../fileutils.h" +#include + +#ifdef _WIN32 +#define CONFIG_FILE "\\furnace.cfg" +#else +#define CONFIG_FILE "/furnace.cfg" +#endif + +bool DivEngine::saveConf() { + configFile=configPath+String(CONFIG_FILE); + FILE* f=ps_fopen(configFile.c_str(),"wb"); + if (f==NULL) { + logW("could not write config file! %s\n",strerror(errno)); + return false; + } + for (auto& i: conf) { + String toWrite=fmt::sprintf("%s=%s\n",i.first,i.second); + if (fwrite(toWrite.c_str(),1,toWrite.size(),f)!=toWrite.size()) { + logW("could not write config file! %s\n",strerror(errno)); + fclose(f); + return false; + } + } + fclose(f); + return true; +} + +bool DivEngine::loadConf() { + char line[4096]; + configFile=configPath+String(CONFIG_FILE); + FILE* f=ps_fopen(configFile.c_str(),"rb"); + if (f==NULL) { + logI("creating default config.\n"); + return saveConf(); + } + logI("loading config.\n"); + while (!feof(f)) { + String key=""; + String value=""; + bool keyOrValue=false; + if (fgets(line,4095,f)==NULL) { + break; + } + for (char* i=line; *i; i++) { + if (*i=='\n') continue; + if (keyOrValue) { + value+=*i; + } else { + if (*i=='=') { + keyOrValue=true; + } else { + key+=*i; + } + } + } + if (keyOrValue) { + conf[key]=value; + } + } + fclose(f); + return true; +} + +bool DivEngine::getConfBool(String key, bool fallback) { + try { + String val=conf.at(key); + if (val=="true") { + return true; + } else if (val=="false") { + return false; + } + } catch (std::out_of_range& e) { + } + return fallback; +} + +int DivEngine::getConfInt(String key, int fallback) { + try { + String val=conf.at(key); + int ret=std::stoi(val); + return ret; + } catch (std::out_of_range& e) { + } catch (std::invalid_argument& e) { + } + return fallback; +} + +float DivEngine::getConfFloat(String key, float fallback) { + try { + String val=conf.at(key); + float ret=std::stof(val); + return ret; + } catch (std::out_of_range& e) { + } catch (std::invalid_argument& e) { + } + return fallback; +} + +double DivEngine::getConfDouble(String key, double fallback) { + try { + String val=conf.at(key); + double ret=std::stod(val); + return ret; + } catch (std::out_of_range& e) { + } catch (std::invalid_argument& e) { + } + return fallback; +} + +String DivEngine::getConfString(String key, String fallback) { + try { + String val=conf.at(key); + return val; + } catch (std::out_of_range& e) { + } + return fallback; +} + +void DivEngine::setConf(String key, bool value) { + if (value) { + conf[key]="true"; + } else { + conf[key]="false"; + } +} + +void DivEngine::setConf(String key, int value) { + conf[key]=fmt::sprintf("%d",value); +} + +void DivEngine::setConf(String key, float value) { + conf[key]=fmt::sprintf("%f",value); +} + +void DivEngine::setConf(String key, double value) { + conf[key]=fmt::sprintf("%f",value); +} + +void DivEngine::setConf(String key, String value) { + conf[key]=value; +} diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index ce8bcc7c..438a965c 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -39,8 +39,6 @@ #include #include -constexpr int MASTER_CLOCK_PREC=(sizeof(void*)==8)?8:0; - void process(void* u, float** in, float** out, int inChans, int outChans, unsigned int size) { ((DivEngine*)u)->nextBuf(in,out,inChans,outChans,size); } @@ -435,146 +433,6 @@ void DivEngine::notifyWaveChange(int wave) { isBusy.unlock(); } -#ifdef _WIN32 -#define CONFIG_FILE "\\furnace.cfg" -#else -#define CONFIG_FILE "/furnace.cfg" -#endif - -bool DivEngine::saveConf() { - configFile=configPath+String(CONFIG_FILE); - FILE* f=ps_fopen(configFile.c_str(),"wb"); - if (f==NULL) { - logW("could not write config file! %s\n",strerror(errno)); - return false; - } - for (auto& i: conf) { - String toWrite=fmt::sprintf("%s=%s\n",i.first,i.second); - if (fwrite(toWrite.c_str(),1,toWrite.size(),f)!=toWrite.size()) { - logW("could not write config file! %s\n",strerror(errno)); - fclose(f); - return false; - } - } - fclose(f); - return true; -} - -bool DivEngine::loadConf() { - char line[4096]; - configFile=configPath+String(CONFIG_FILE); - FILE* f=ps_fopen(configFile.c_str(),"rb"); - if (f==NULL) { - logI("creating default config.\n"); - return saveConf(); - } - logI("loading config.\n"); - while (!feof(f)) { - String key=""; - String value=""; - bool keyOrValue=false; - if (fgets(line,4095,f)==NULL) { - break; - } - for (char* i=line; *i; i++) { - if (*i=='\n') continue; - if (keyOrValue) { - value+=*i; - } else { - if (*i=='=') { - keyOrValue=true; - } else { - key+=*i; - } - } - } - if (keyOrValue) { - conf[key]=value; - } - } - fclose(f); - return true; -} - -bool DivEngine::getConfBool(String key, bool fallback) { - try { - String val=conf.at(key); - if (val=="true") { - return true; - } else if (val=="false") { - return false; - } - } catch (std::out_of_range& e) { - } - return fallback; -} - -int DivEngine::getConfInt(String key, int fallback) { - try { - String val=conf.at(key); - int ret=std::stoi(val); - return ret; - } catch (std::out_of_range& e) { - } catch (std::invalid_argument& e) { - } - return fallback; -} - -float DivEngine::getConfFloat(String key, float fallback) { - try { - String val=conf.at(key); - float ret=std::stof(val); - return ret; - } catch (std::out_of_range& e) { - } catch (std::invalid_argument& e) { - } - return fallback; -} - -double DivEngine::getConfDouble(String key, double fallback) { - try { - String val=conf.at(key); - double ret=std::stod(val); - return ret; - } catch (std::out_of_range& e) { - } catch (std::invalid_argument& e) { - } - return fallback; -} - -String DivEngine::getConfString(String key, String fallback) { - try { - String val=conf.at(key); - return val; - } catch (std::out_of_range& e) { - } - return fallback; -} - -void DivEngine::setConf(String key, bool value) { - if (value) { - conf[key]="true"; - } else { - conf[key]="false"; - } -} - -void DivEngine::setConf(String key, int value) { - conf[key]=fmt::sprintf("%d",value); -} - -void DivEngine::setConf(String key, float value) { - conf[key]=fmt::sprintf("%f",value); -} - -void DivEngine::setConf(String key, double value) { - conf[key]=fmt::sprintf("%f",value); -} - -void DivEngine::setConf(String key, String value) { - conf[key]=value; -} - // ADPCM code attribution: https://wiki.neogeodev.org/index.php?title=ADPCM_codecs static short adSteps[49]={