Repository navigation
Expand file tree
/
Copy pathcfg.cpp
More file actions
219 lines (186 loc) · 5.22 KB
/
Copy pathcfg.cpp
File metadata and controls
219 lines (186 loc) · 5.22 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
This file is part of libswirl
Config file crap
Supports various things, as virtual config entries and such crap
Works surprisingly well considering how old it is ...
*/
#include "license/bsd"
#define _CRT_SECURE_NO_DEPRECATE (1)
#include <errno.h>
#include "cfg.h"
#include "ini.h"
static string cfgPath;
static bool save_config = true;
static emucfg::ConfigFile cfgdb;
static string game_id;
static bool has_game_specific_config = false;
void savecfgf()
{
FILE* cfgfile = fopen(cfgPath.c_str(),"wt");
if (!cfgfile)
{
printf("Error: Unable to open file '%s' for saving\n", cfgPath.c_str());
}
else
{
cfgdb.save(cfgfile);
fclose(cfgfile);
}
}
void cfgSaveStr(const wchar * Section, const wchar * Key, const wchar * String)
{
const std::string section(Section);
const std::string key(Key);
const std::string value(String);
if (cfgHasGameSpecificConfig())
{
if (cfgdb.get(section, key, "") == value)
// Same value as main config: delete entry
cfgdb.delete_entry(game_id, key);
else
cfgdb.set(game_id, key, value);
}
else
cfgdb.set(section, key, value);
if(save_config)
{
savecfgf();
}
}
//New config code
/*
I want config to be really flexible .. so , here is the new implementation :
Functions :
cfgLoadInt : Load an int , if it does not exist save the default value to it and return it
cfgSaveInt : Save an int
cfgLoadStr : Load a str , if it does not exist save the default value to it and return it
cfgSaveStr : Save a str
cfgExists : Returns true if the Section:Key exists. If Key is null , it retuns true if Section exists
Config parameters can be read from the config file , and can be given at the command line
-cfg section:key=value -> defines a value at command line
If a cfgSave* is made on a value defined by command line , then the command line value is replaced by it
cfg values set by command line are not written to the cfg file , unless a cfgSave* is used
*/
///////////////////////////////
/*
** This will verify there is a working file @ ./szIniFn
** - if not present, it will write defaults
*/
void cfgSetPath(const char* path)
{
cfgPath = path;
}
bool cfgOpen()
{
if (get_writable_config_path("").empty())
// Config dir not set (android onboarding)
return false;
const char* filename = "/emu.cfg";
// cfgSetPath may have set an override already
if (cfgPath.empty())
cfgPath = "/media/fat/games/Dreamcast/emu.cfg" ;//get_writable_config_path(filename);
string config_path_read = cfgPath; //get_readonly_config_path(filename);
FILE* cfgfile = fopen(config_path_read.c_str(),"r");
if(cfgfile != NULL) {
cfgdb.parse(cfgfile);
fclose(cfgfile);
}
else
{
// Config file can't be opened
int error_code = errno;
printf("Warning: Unable to open the config file '%s' for reading (%s)\n", config_path_read.c_str(), strerror(error_code));
if (error_code == ENOENT || cfgPath != config_path_read)
{
// Config file didn't exist
printf("Creating new empty config file at '%s'\n", cfgPath.c_str());
savecfgf();
}
else
{
// There was some other error (may be a permissions problem or something like that)
save_config = false;
}
}
return true;
}
//Implementations of the interface :)
//Section must be set
//If key is 0 , it looks for the section
//0 : not found
//1 : found section , key was 0
//2 : found section & key
s32 cfgExists(const wchar * Section, const wchar * Key)
{
if(cfgdb.has_entry(string(Section), string(Key)))
{
return 2;
}
else
{
return (cfgdb.has_section(string(Section)) ? 1 : 0);
}
}
void cfgLoadStr(const wchar * Section, const wchar * Key, wchar * Return,const wchar* Default)
{
string value = cfgdb.get(Section, Key, Default);
// FIXME: Buffer overflow possible
strcpy(Return, value.c_str());
}
string cfgLoadStr(const wchar * Section, const wchar * Key, const wchar* Default)
{
std::string v = cfgdb.get(string(Section), string(Key), string(Default));
if (cfgHasGameSpecificConfig())
v = cfgdb.get(game_id, string(Key), v);
return v;
}
//These are helpers , mainly :)
void cfgSaveInt(const wchar * Section, const wchar * Key, s32 Int)
{
char str[32];
sprintf(str, "%d", Int);
cfgSaveStr(Section, Key, str);
}
s32 cfgLoadInt(const wchar * Section, const wchar * Key,s32 Default)
{
s32 v = cfgdb.get_int(string(Section), string(Key), Default);
if (cfgHasGameSpecificConfig())
v = cfgdb.get_int(game_id, string(Key), v);
return v;
}
void cfgSaveBool(const wchar * Section, const wchar * Key, bool BoolValue)
{
cfgSaveStr(Section, Key, BoolValue ? "yes" : "no");
}
bool cfgLoadBool(const wchar * Section, const wchar * Key,bool Default)
{
bool v = cfgdb.get_bool(string(Section), string(Key), Default);
if (cfgHasGameSpecificConfig())
v = cfgdb.get_bool(game_id, string(Key), v);
return v;
}
void cfgSetVirtual(const wchar * Section, const wchar * Key, const wchar * String)
{
cfgdb.set(string(Section), string(Key), string(String), true);
}
void cfgSetGameId(const char *id)
{
game_id = id;
}
const char *cfgGetGameId()
{
return game_id.c_str();
}
bool cfgHasGameSpecificConfig()
{
return has_game_specific_config || cfgdb.has_section(game_id);
}
void cfgMakeGameSpecificConfig()
{
has_game_specific_config = true;
}
void cfgDeleteGameSpecificConfig()
{
has_game_specific_config = false;
cfgdb.delete_section(game_id);
}