Debian Codegraph @

src/engine/configEngine.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 "engine.h"
21 #include "../ta-log.h"
22 #include "../fileutils.h"
23
24 #ifdef _WIN32
25 #include "winStuff.h"
26 #define CONFIG_FILE "\\furnace.cfg"
27 #define LOG_FILE "\\furnace.log"
28 #define LAYOUT_INI "\\layout.ini"
29 #else
30 #ifdef __HAIKU__
31 #include <support/SupportDefs.h>
32 #include <storage/FindDirectory.h>
33 #endif
34 #include <unistd.h>
35 #include <pwd.h>
36 #include <sys/stat.h>
37 #define CONFIG_FILE "/furnace.cfg"
38 #define LOG_FILE "/furnace.log"
39 #define LAYOUT_INI "/layout.ini"
40 #endif
41
42 #ifdef IS_MOBILE
43 #ifdef HAVE_SDL2
44 #include <SDL.h>
45 #else
46 #error "Furnace mobile requires SDL2!"
47 #endif
48 #endif
49
50 void DivEngine::initConfDir() {
51 #ifdef _WIN32
52 // maybe move this function in here instead?
53 configPath=getWinConfigPath();
54 #elif defined(IS_MOBILE)
55 configPath=SDL_GetPrefPath("tildearrow","furnace");
56 #else
57 #ifdef __HAIKU__
58 char userSettingsDir[PATH_MAX];
59 status_t findUserDir = find_directory(B_USER_SETTINGS_DIRECTORY,0,true,userSettingsDir,PATH_MAX);
60 if (findUserDir==B_OK) {
61 configPath=userSettingsDir;
62 } else {
63 logW("unable to find/create user settings directory (%s)!",strerror(findUserDir));
64 configPath=".";
65 return;
66 }
67 #else
68 char* xdgConfigHome=getenv("XDG_CONFIG_HOME");
69 char* home=getenv("HOME");
70 if (xdgConfigHome) {
71 configPath=xdgConfigHome;
72 } else if (home==NULL) {
73 int uid=getuid();
74 struct passwd* entry=getpwuid(uid);
75 if (entry==NULL) {
76 logW("unable to determine home directory (%s)!",strerror(errno));
77 configPath=".";
78 return;
79 } else {
80 configPath=entry->pw_dir;
81 }
82 } else {
83 configPath=home;
84 }
85 #ifdef __APPLE__
86 configPath+="/Library/Application Support";
87 #else
88 if (xdgConfigHome==NULL) {
89 configPath+="/.config";
90 }
91 #endif // __APPLE__
92 #endif // __HAIKU__
93 #ifdef __APPLE__
94 configPath+="/Furnace";
95 #else
96 configPath+="/furnace";
97 #endif // __APPLE__
98 struct stat st;
99 std::string pathSep="/";
100 configPath+=pathSep;
101 size_t sepPos=configPath.find(pathSep,1);
102 while (sepPos!=std::string::npos) {
103 std::string subpath=configPath.substr(0,sepPos++);
104 if (stat(subpath.c_str(),&st)!=0) {
105 logI("creating config path element %s ...",subpath.c_str());
106 if (mkdir(subpath.c_str(),0755)!=0) {
107 logW("could not create config path element %s! (%s)",subpath.c_str(),strerror(errno));
108 configPath=".";
109 return;
110 }
111 }
112 sepPos=configPath.find(pathSep,sepPos);
113 }
114 configPath.resize(configPath.length()-pathSep.length());
115 #endif // _WIN32
116 }
117
118 bool DivEngine::saveConf() {
119 configFile=configPath+String(CONFIG_FILE);
120 return conf.save(configFile.c_str(),true);
121 }
122
123 bool DivEngine::loadConf() {
124 configFile=configPath+String(CONFIG_FILE);
125 return conf.loadFromFile(configFile.c_str(),true,true);
126 }
127
128 bool DivEngine::getConfBool(String key, bool fallback) {
129 return conf.getBool(key,fallback);
130 }
131
132 int DivEngine::getConfInt(String key, int fallback) {
133 return conf.getInt(key,fallback);
134 }
135
136 float DivEngine::getConfFloat(String key, float fallback) {
137 return conf.getFloat(key,fallback);
138 }
139
140 double DivEngine::getConfDouble(String key, double fallback) {
141 return conf.getDouble(key,fallback);
142 }
143
144 String DivEngine::getConfString(String key, String fallback) {
145 return conf.getString(key,fallback);
146 }
147
148 void DivEngine::setConf(String key, bool value) {
149 conf.set(key,value);
150 }
151
152 void DivEngine::setConf(String key, int value) {
153 conf.set(key,value);
154 }
155
156 void DivEngine::setConf(String key, float value) {
157 conf.set(key,value);
158 }
159
160 void DivEngine::setConf(String key, double value) {
161 conf.set(key,value);
162 }
163
164 void DivEngine::setConf(String key, const char* value) {
165 conf.set(key,value);
166 }
167
168 void DivEngine::setConf(String key, String value) {
169 conf.set(key,value);
170 }
171
172 bool DivEngine::hasConf(String key) {
173 return conf.has(key);
174 }
175
176 DivConfig& DivEngine::getConfObject() {
177 return conf;
178 }
179
180 void DivEngine::factoryReset() {
181 conf.clear();
182 String confPath=configPath+String(CONFIG_FILE);
183 String layoutPath=configPath+String(LAYOUT_INI);
184
185 for (int i=0; i<10; i++) {
186 String path=confPath;
187 if (i>0) path+=fmt::sprintf(".%d",i);
188 deleteFile(path.c_str());
189 }
190
191 for (int i=0; i<10; i++) {
192 String path=layoutPath;
193 if (i>0) path+=fmt::sprintf(".%d",i);
194 deleteFile(path.c_str());
195 }
196 }