-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.cpp
More file actions
133 lines (103 loc) · 3 KB
/
Copy pathsettings.cpp
File metadata and controls
133 lines (103 loc) · 3 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
#include "settings.h"
#include <fstream>
#include <sstream>
#include <algorithm>
static std::string Trim(std::string s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
return !std::isspace(ch);
}));
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
return !std::isspace(ch);
}).base(), s.end());
return s;
}
static std::string ToLower(std::string s)
{
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) {
return static_cast<char>(std::tolower(c));
});
return s;
}
static bool ParseBool(const std::string& value, bool defaultValue)
{
std::string v = ToLower(Trim(value));
if (v == "true" || v == "1" || v == "yes" || v == "on")
return true;
if (v == "false" || v == "0" || v == "no" || v == "off")
return false;
return defaultValue;
}
std::string RegionModeToString(RegionMode mode)
{
switch (mode)
{
case RegionMode::PAL:
return "pal";
case RegionMode::NTSC:
return "ntsc";
case RegionMode::Auto:
default:
return "auto";
}
}
RegionMode RegionModeFromString(const std::string& value)
{
std::string v = ToLower(Trim(value));
if (v == "pal")
return RegionMode::PAL;
if (v == "ntsc")
return RegionMode::NTSC;
return RegionMode::Auto;
}
O2EMSettings LoadSettings(const std::string& filename)
{
O2EMSettings settings;
std::ifstream file(filename);
if (!file.is_open())
{
SaveSettings(filename, settings);
return settings;
}
std::string line;
while (std::getline(file, line))
{
line = Trim(line);
if (line.empty() || line[0] == '#')
continue;
size_t equalsPos = line.find('=');
if (equalsPos == std::string::npos)
continue;
std::string key = Trim(line.substr(0, equalsPos));
std::string value = Trim(line.substr(equalsPos + 1));
key = ToLower(key);
if (key == "start_fullscreen")
{
#ifdef __EMSCRIPTEN__
settings.start_fullscreen = false;
#else
settings.start_fullscreen = ParseBool(value, settings.start_fullscreen);
#endif
}
else if (key == "region_mode")
{
settings.region_mode = RegionModeFromString(value);
}
else if (key == "scanlines")
{
settings.scanlines = ParseBool(value, settings.scanlines);
}
}
return settings;
}
void SaveSettings(const std::string& filename, const O2EMSettings& settings)
{
std::ofstream file(filename);
if (!file.is_open())
return;
file << "# O2EM-NG configuration\n";
file << "# Values: true/false, auto/pal/ntsc\n\n";
file << "start_fullscreen = " << (settings.start_fullscreen ? "true" : "false") << "\n";
file << "region_mode = " << RegionModeToString(settings.region_mode) << "\n";
file << "scanlines = " << (settings.scanlines ? "true" : "false") << "\n";
}