forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathini_file.cpp
More file actions
254 lines (206 loc) · 6.29 KB
/
ini_file.cpp
File metadata and controls
254 lines (206 loc) · 6.29 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Aseprite
// Copyright (C) 2001-2016 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/ini_file.h"
#include "app/resource_finder.h"
#include "base/fs.h"
#include "base/split_string.h"
#include "base/string.h"
#include "cfg/cfg.h"
#ifdef __APPLE__
#include "she/logger.h"
#include "she/system.h"
#endif
#ifndef _WIN32
#include "base/fs.h"
#endif
#include <cstdlib>
#include <vector>
namespace app {
using namespace gfx;
static std::string g_configFilename;
static std::vector<cfg::CfgFile*> g_configs;
ConfigModule::ConfigModule()
{
ResourceFinder rf;
rf.includeUserDir("aseprite.ini");
// getFirstOrCreateDefault() will create the Aseprite directory
// inside the OS configuration folder (~/.config/aseprite/, etc.).
std::string fn = rf.getFirstOrCreateDefault();
#ifdef __APPLE__
// On OS X we migrate from ~/.config/aseprite/* -> "~/Library/Application Support/Aseprite/*"
if (!base::is_file(fn)) {
try {
std::string new_dir = base::get_file_path(fn);
// Now we try to move all old configuration files into the new
// directory.
ResourceFinder old_rf;
old_rf.includeHomeDir(".config/aseprite/aseprite.ini");
std::string old_config_fn = old_rf.defaultFilename();
if (base::is_file(old_config_fn)) {
std::string old_dir = base::get_file_path(old_config_fn);
for (std::string old_fn : base::list_files(old_dir)) {
std::string from = base::join_path(old_dir, old_fn);
std::string to = base::join_path(new_dir, old_fn);
base::move_file(from, to);
}
base::remove_directory(old_dir);
}
}
// Something failed
catch (const std::exception& ex) {
std::string err = "Error in configuration migration: ";
err += ex.what();
auto system = she::instance();
if (system && system->logger())
system->logger()->logError(err.c_str());
}
}
#elif !defined(_WIN32)
// On Linux we migrate the old configuration file name
// (.asepriterc -> ~/.config/aseprite/aseprite.ini)
{
ResourceFinder old_rf;
old_rf.includeHomeDir(".asepriterc");
std::string old_fn = old_rf.defaultFilename();
if (base::is_file(old_fn))
base::move_file(old_fn, fn);
}
#endif
set_config_file(fn.c_str());
g_configFilename = fn;
}
ConfigModule::~ConfigModule()
{
flush_config_file();
for (auto cfg : g_configs)
delete cfg;
g_configs.clear();
}
//////////////////////////////////////////////////////////////////////
// Allegro-like API to handle .ini configuration files
void push_config_state()
{
g_configs.push_back(new cfg::CfgFile());
}
void pop_config_state()
{
ASSERT(!g_configs.empty());
delete g_configs.back();
g_configs.erase(--g_configs.end());
}
void flush_config_file()
{
ASSERT(!g_configs.empty());
g_configs.back()->save();
}
void set_config_file(const char* filename)
{
if (g_configs.empty())
g_configs.push_back(new cfg::CfgFile());
g_configs.back()->load(filename);
}
std::string main_config_filename()
{
return g_configFilename;
}
const char* get_config_string(const char* section, const char* name, const char* value)
{
return g_configs.back()->getValue(section, name, value);
}
void set_config_string(const char* section, const char* name, const char* value)
{
g_configs.back()->setValue(section, name, value);
}
int get_config_int(const char* section, const char* name, int value)
{
return g_configs.back()->getIntValue(section, name, value);
}
void set_config_int(const char* section, const char* name, int value)
{
g_configs.back()->setIntValue(section, name, value);
}
float get_config_float(const char* section, const char* name, float value)
{
return (float)g_configs.back()->getDoubleValue(section, name, (float)value);
}
void set_config_float(const char* section, const char* name, float value)
{
g_configs.back()->setDoubleValue(section, name, (float)value);
}
double get_config_double(const char* section, const char* name, double value)
{
return g_configs.back()->getDoubleValue(section, name, value);
}
void set_config_double(const char* section, const char* name, double value)
{
g_configs.back()->setDoubleValue(section, name, value);
}
bool get_config_bool(const char* section, const char* name, bool value)
{
return g_configs.back()->getBoolValue(section, name, value);
}
void set_config_bool(const char* section, const char* name, bool value)
{
g_configs.back()->setBoolValue(section, name, value);
}
Point get_config_point(const char* section, const char* name, const Point& point)
{
Point point2(point);
const char* value = get_config_string(section, name, "");
if (value) {
std::vector<std::string> parts;
base::split_string(value, parts, " ");
if (parts.size() == 2) {
point2.x = strtol(parts[0].c_str(), NULL, 10);
point2.y = strtol(parts[1].c_str(), NULL, 10);
}
}
return point2;
}
void set_config_point(const char* section, const char* name, const Point& point)
{
char buf[128];
sprintf(buf, "%d %d", point.x, point.y);
set_config_string(section, name, buf);
}
Rect get_config_rect(const char* section, const char* name, const Rect& rect)
{
Rect rect2(rect);
const char* value = get_config_string(section, name, "");
if (value) {
std::vector<std::string> parts;
base::split_string(value, parts, " ");
if (parts.size() == 4) {
rect2.x = strtol(parts[0].c_str(), NULL, 10);
rect2.y = strtol(parts[1].c_str(), NULL, 10);
rect2.w = strtol(parts[2].c_str(), NULL, 10);
rect2.h = strtol(parts[3].c_str(), NULL, 10);
}
}
return rect2;
}
void set_config_rect(const char* section, const char* name, const Rect& rect)
{
char buf[128];
sprintf(buf, "%d %d %d %d", rect.x, rect.y, rect.w, rect.h);
set_config_string(section, name, buf);
}
app::Color get_config_color(const char* section, const char* name, const app::Color& value)
{
return app::Color::fromString(get_config_string(section, name, value.toString().c_str()));
}
void set_config_color(const char* section, const char* name, const app::Color& value)
{
set_config_string(section, name, value.toString().c_str());
}
void del_config_value(const char* section, const char* name)
{
g_configs.back()->deleteValue(section, name);
}
} // namespace app