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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
|
/*
* Settings.cpp
* ------------
* Purpose: Application setting handling framework.
* Notes : (currently none)
* Authors: Joern Heusipp
* OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#include "stdafx.h"
#include "Settings.h"
#include "mpt/binary/hex.hpp"
#include "../common/misc_util.h"
#include "../common/mptStringBuffer.h"
#include "Mptrack.h"
#include "Mainfrm.h"
#include <algorithm>
#include "../common/mptFileIO.h"
OPENMPT_NAMESPACE_BEGIN
mpt::ustring SettingValue::FormatTypeAsString() const
{
if(GetType() == SettingTypeNone)
{
return U_("nil");
}
mpt::ustring result;
switch(GetType())
{
case SettingTypeBool:
result += U_("bool");
break;
case SettingTypeInt:
result += U_("int");
break;
case SettingTypeFloat:
result += U_("float");
break;
case SettingTypeString:
result += U_("string");
break;
case SettingTypeBinary:
result += U_("binary");
break;
case SettingTypeNone:
default:
result += U_("nil");
break;
}
if(HasTypeTag() && !GetTypeTag().empty())
{
result += U_(":") + mpt::ToUnicode(mpt::Charset::ASCII, GetTypeTag());
}
return result;
}
mpt::ustring SettingValue::FormatValueAsString() const
{
switch(GetType())
{
case SettingTypeBool:
return mpt::ufmt::val(as<bool>());
break;
case SettingTypeInt:
return mpt::ufmt::val(as<int32>());
break;
case SettingTypeFloat:
return mpt::ufmt::val(as<double>());
break;
case SettingTypeString:
return as<mpt::ustring>();
break;
case SettingTypeBinary:
return mpt::encode_hex(mpt::as_span(as<std::vector<std::byte>>()));
break;
case SettingTypeNone:
default:
return mpt::ustring();
break;
}
}
void SettingValue::SetFromString(const AnyStringLocale &newVal)
{
switch(GetType())
{
case SettingTypeBool:
value = ConvertStrTo<bool>(newVal);
break;
case SettingTypeInt:
value = ConvertStrTo<int32>(newVal);
break;
case SettingTypeFloat:
value = ConvertStrTo<double>(newVal);
break;
case SettingTypeString:
value = newVal;
break;
case SettingTypeBinary:
value = mpt::decode_hex(newVal);
break;
case SettingTypeNone:
default:
break;
}
}
SettingValue SettingsContainer::BackendsReadSetting(const SettingPath &path, const SettingValue &def) const
{
return backend->ReadSetting(path, def);
}
void SettingsContainer::BackendsWriteSetting(const SettingPath &path, const SettingValue &val)
{
backend->WriteSetting(path, val);
}
void SettingsContainer::BackendsRemoveSetting(const SettingPath &path)
{
backend->RemoveSetting(path);
}
void SettingsContainer::BackendsRemoveSection(const mpt::ustring §ion)
{
backend->RemoveSection(section);
}
SettingValue SettingsContainer::ReadSetting(const SettingPath &path, const SettingValue &def) const
{
ASSERT(theApp.InGuiThread());
ASSERT(!CMainFrame::GetMainFrame() || (CMainFrame::GetMainFrame() && !CMainFrame::GetMainFrame()->InNotifyHandler())); // This is a slow path, use CachedSetting for stuff that is accessed in notify handler.
auto entry = map.find(path);
if(entry == map.end())
{
entry = map.insert(map.begin(), std::make_pair(path, SettingState(def).assign(BackendsReadSetting(path, def), false)));
}
return entry->second;
}
bool SettingsContainer::IsDefaultSetting(const SettingPath &path) const
{
ASSERT(theApp.InGuiThread());
ASSERT(!CMainFrame::GetMainFrame() || (CMainFrame::GetMainFrame() && !CMainFrame::GetMainFrame()->InNotifyHandler())); // This is a slow path, use CachedSetting for stuff that is accessed in notify handler.
auto entry = map.find(path);
if(entry == map.end())
{
return true;
}
return entry->second.IsDefault();
}
void SettingsContainer::WriteSetting(const SettingPath &path, const SettingValue &val, SettingFlushMode flushMode)
{
ASSERT(theApp.InGuiThread());
ASSERT(!CMainFrame::GetMainFrame() || (CMainFrame::GetMainFrame() && !CMainFrame::GetMainFrame()->InNotifyHandler())); // This is a slow path, use CachedSetting for stuff that is accessed in notify handler.
auto entry = map.find(path);
if(entry == map.end())
{
map[path] = val;
entry = map.find(path);
} else
{
entry->second = val;
}
NotifyListeners(path);
if(immediateFlush || flushMode == SettingWriteThrough)
{
BackendsWriteSetting(path, val);
entry->second.Clean();
}
}
void SettingsContainer::ForgetSetting(const SettingPath &path)
{
ASSERT(theApp.InGuiThread());
ASSERT(!CMainFrame::GetMainFrame() || (CMainFrame::GetMainFrame() && !CMainFrame::GetMainFrame()->InNotifyHandler())); // This is a slow path, use CachedSetting for stuff that is accessed in notify handler.
map.erase(path);
}
void SettingsContainer::ForgetAll()
{
ASSERT(theApp.InGuiThread());
ASSERT(!CMainFrame::GetMainFrame() || (CMainFrame::GetMainFrame() && !CMainFrame::GetMainFrame()->InNotifyHandler())); // This is a slow path, use CachedSetting for stuff that is accessed in notify handler.
map.clear();
}
void SettingsContainer::RemoveSetting(const SettingPath &path)
{
ASSERT(theApp.InGuiThread());
ASSERT(!CMainFrame::GetMainFrame() || (CMainFrame::GetMainFrame() && !CMainFrame::GetMainFrame()->InNotifyHandler())); // This is a slow path, use CachedSetting for stuff that is accessed in notify handler.
map.erase(path);
BackendsRemoveSetting(path);
}
void SettingsContainer::RemoveSection(const mpt::ustring §ion)
{
ASSERT(theApp.InGuiThread());
ASSERT(!CMainFrame::GetMainFrame() || (CMainFrame::GetMainFrame() && !CMainFrame::GetMainFrame()->InNotifyHandler())); // This is a slow path, use CachedSetting for stuff that is accessed in notify handler.
std::vector<SettingPath> pathsToRemove;
for(const auto &entry : map)
{
if(entry.first.GetSection() == section)
{
pathsToRemove.push_back(entry.first);
}
}
for(const auto &path : pathsToRemove)
{
map.erase(path);
}
BackendsRemoveSection(section);
}
void SettingsContainer::NotifyListeners(const SettingPath &path)
{
const auto entry = mapListeners.find(path);
if(entry != mapListeners.end())
{
for(auto &it : entry->second)
{
it->SettingChanged(path);
}
}
}
void SettingsContainer::WriteSettings()
{
ASSERT(theApp.InGuiThread());
ASSERT(!CMainFrame::GetMainFrame() || (CMainFrame::GetMainFrame() && !CMainFrame::GetMainFrame()->InNotifyHandler())); // This is a slow path, use CachedSetting for stuff that is accessed in notify handler.
for(auto &[path, value] : map)
{
if(value.IsDirty())
{
BackendsWriteSetting(path, value);
value.Clean();
}
}
}
void SettingsContainer::Flush()
{
ASSERT(theApp.InGuiThread());
ASSERT(!CMainFrame::GetMainFrame() || (CMainFrame::GetMainFrame() && !CMainFrame::GetMainFrame()->InNotifyHandler())); // This is a slow path, use CachedSetting for stuff that is accessed in notify handler.
WriteSettings();
}
void SettingsContainer::SetImmediateFlush(bool newImmediateFlush)
{
if(newImmediateFlush)
{
Flush();
}
immediateFlush = newImmediateFlush;
}
void SettingsContainer::Register(ISettingChanged *listener, const SettingPath &path)
{
mapListeners[path].insert(listener);
}
void SettingsContainer::UnRegister(ISettingChanged *listener, const SettingPath &path)
{
mapListeners[path].erase(listener);
}
SettingsContainer::~SettingsContainer()
{
WriteSettings();
}
SettingsContainer::SettingsContainer(ISettingsBackend *backend)
: backend(backend)
{
MPT_ASSERT(backend);
}
std::vector<std::byte> IniFileSettingsBackend::ReadSettingRaw(const SettingPath &path, const std::vector<std::byte> &def) const
{
std::vector<std::byte> result = def;
if(!mpt::in_range<UINT>(result.size()))
{
return result;
}
::GetPrivateProfileStruct(GetSection(path).c_str(), GetKey(path).c_str(), result.data(), static_cast<UINT>(result.size()), filename.AsNative().c_str());
return result;
}
mpt::ustring IniFileSettingsBackend::ReadSettingRaw(const SettingPath &path, const mpt::ustring &def) const
{
std::vector<TCHAR> buf(128);
while(::GetPrivateProfileString(GetSection(path).c_str(), GetKey(path).c_str(), mpt::ToWin(def).c_str(), buf.data(), static_cast<DWORD>(buf.size()), filename.AsNative().c_str()) == buf.size() - 1)
{
if(buf.size() == std::numeric_limits<DWORD>::max())
{
return def;
}
buf.resize(mpt::exponential_grow(buf.size(), std::numeric_limits<DWORD>::max()));
}
return mpt::ToUnicode(mpt::winstring(buf.data()));
}
double IniFileSettingsBackend::ReadSettingRaw(const SettingPath &path, double def) const
{
std::vector<TCHAR> buf(128);
while(::GetPrivateProfileString(GetSection(path).c_str(), GetKey(path).c_str(), mpt::tfmt::val(def).c_str(), buf.data(), static_cast<DWORD>(buf.size()), filename.AsNative().c_str()) == buf.size() - 1)
{
if(buf.size() == std::numeric_limits<DWORD>::max())
{
return def;
}
buf.resize(mpt::exponential_grow(buf.size(), std::numeric_limits<DWORD>::max()));
}
return ConvertStrTo<double>(mpt::winstring(buf.data()));
}
int32 IniFileSettingsBackend::ReadSettingRaw(const SettingPath &path, int32 def) const
{
return (int32)::GetPrivateProfileInt(GetSection(path).c_str(), GetKey(path).c_str(), (UINT)def, filename.AsNative().c_str());
}
bool IniFileSettingsBackend::ReadSettingRaw(const SettingPath &path, bool def) const
{
return ::GetPrivateProfileInt(GetSection(path).c_str(), GetKey(path).c_str(), def?1:0, filename.AsNative().c_str()) ? true : false;
}
void IniFileSettingsBackend::WriteSettingRaw(const SettingPath &path, const std::vector<std::byte> &val)
{
MPT_ASSERT(mpt::in_range<UINT>(val.size()));
::WritePrivateProfileStruct(GetSection(path).c_str(), GetKey(path).c_str(), (LPVOID)val.data(), static_cast<UINT>(val.size()), filename.AsNative().c_str());
}
void IniFileSettingsBackend::WriteSettingRaw(const SettingPath &path, const mpt::ustring &val)
{
::WritePrivateProfileString(GetSection(path).c_str(), GetKey(path).c_str(), mpt::ToWin(val).c_str(), filename.AsNative().c_str());
if(mpt::ToUnicode(mpt::Charset::Locale, mpt::ToCharset(mpt::Charset::Locale, val)) != val) // explicit round-trip
{
// Value is not representable in ANSI CP.
// Now check if the string got stored correctly.
if(ReadSettingRaw(path, mpt::ustring()) != val)
{
// The ini file is probably ANSI encoded.
ConvertToUnicode();
// Re-write non-ansi-representable value.
::WritePrivateProfileString(GetSection(path).c_str(), GetKey(path).c_str(), mpt::ToWin(val).c_str(), filename.AsNative().c_str());
}
}
}
void IniFileSettingsBackend::WriteSettingRaw(const SettingPath &path, double val)
{
::WritePrivateProfileString(GetSection(path).c_str(), GetKey(path).c_str(), mpt::tfmt::val(val).c_str(), filename.AsNative().c_str());
}
void IniFileSettingsBackend::WriteSettingRaw(const SettingPath &path, int32 val)
{
::WritePrivateProfileString(GetSection(path).c_str(), GetKey(path).c_str(), mpt::tfmt::val(val).c_str(), filename.AsNative().c_str());
}
void IniFileSettingsBackend::WriteSettingRaw(const SettingPath &path, bool val)
{
::WritePrivateProfileString(GetSection(path).c_str(), GetKey(path).c_str(), mpt::tfmt::val(val).c_str(), filename.AsNative().c_str());
}
void IniFileSettingsBackend::RemoveSettingRaw(const SettingPath &path)
{
::WritePrivateProfileString(GetSection(path).c_str(), GetKey(path).c_str(), NULL, filename.AsNative().c_str());
}
void IniFileSettingsBackend::RemoveSectionRaw(const mpt::ustring §ion)
{
::WritePrivateProfileSection(mpt::ToWin(section).c_str(), _T("\0"), filename.AsNative().c_str());
}
mpt::winstring IniFileSettingsBackend::GetSection(const SettingPath &path)
{
return mpt::ToWin(path.GetSection());
}
mpt::winstring IniFileSettingsBackend::GetKey(const SettingPath &path)
{
return mpt::ToWin(path.GetKey());
}
IniFileSettingsBackend::IniFileSettingsBackend(const mpt::PathString &filename)
: filename(filename)
{
return;
}
IniFileSettingsBackend::~IniFileSettingsBackend()
{
return;
}
static std::vector<char> ReadFile(const mpt::PathString &filename)
{
mpt::ifstream s(filename, std::ios::binary);
std::vector<char> result;
while(s)
{
char buf[4096];
s.read(buf, 4096);
std::streamsize count = s.gcount();
result.insert(result.end(), buf, buf + count);
}
return result;
}
static void WriteFileUTF16LE(const mpt::PathString &filename, const std::wstring &str)
{
static_assert(sizeof(wchar_t) == 2);
mpt::SafeOutputFile sinifile(filename, std::ios::binary, mpt::FlushMode::Full);
mpt::ofstream& inifile = sinifile;
const uint8 UTF16LE_BOM[] = { 0xff, 0xfe };
inifile.write(reinterpret_cast<const char*>(UTF16LE_BOM), 2);
inifile.write(reinterpret_cast<const char*>(str.c_str()), str.length() * sizeof(std::wstring::value_type));
}
void IniFileSettingsBackend::ConvertToUnicode(const mpt::ustring &backupTag)
{
// Force ini file to be encoded in UTF16.
// This causes WINAPI ini file functions to keep it in UTF16 encoding
// and thus support storing unicode strings uncorrupted.
// This is backwards compatible because even ANSI WINAPI behaves the
// same way in this case.
const std::vector<char> data = ReadFile(filename);
if(!data.empty() && IsTextUnicode(data.data(), mpt::saturate_cast<int>(data.size()), NULL))
{
return;
}
const mpt::PathString backupFilename = filename + mpt::PathString::FromUnicode(backupTag.empty() ? U_(".ansi.bak") : U_(".ansi.") + backupTag + U_(".bak"));
CopyFile(filename.AsNative().c_str(), backupFilename.AsNative().c_str(), FALSE);
WriteFileUTF16LE(filename, mpt::ToWide(mpt::Charset::Locale, mpt::buffer_cast<std::string>(data)));
}
SettingValue IniFileSettingsBackend::ReadSetting(const SettingPath &path, const SettingValue &def) const
{
switch(def.GetType())
{
case SettingTypeBool: return SettingValue(ReadSettingRaw(path, def.as<bool>()), def.GetTypeTag()); break;
case SettingTypeInt: return SettingValue(ReadSettingRaw(path, def.as<int32>()), def.GetTypeTag()); break;
case SettingTypeFloat: return SettingValue(ReadSettingRaw(path, def.as<double>()), def.GetTypeTag()); break;
case SettingTypeString: return SettingValue(ReadSettingRaw(path, def.as<mpt::ustring>()), def.GetTypeTag()); break;
case SettingTypeBinary: return SettingValue(ReadSettingRaw(path, def.as<std::vector<std::byte> >()), def.GetTypeTag()); break;
default: return SettingValue(); break;
}
}
void IniFileSettingsBackend::WriteSetting(const SettingPath &path, const SettingValue &val)
{
ASSERT(val.GetType() != SettingTypeNone);
switch(val.GetType())
{
case SettingTypeBool: WriteSettingRaw(path, val.as<bool>()); break;
case SettingTypeInt: WriteSettingRaw(path, val.as<int32>()); break;
case SettingTypeFloat: WriteSettingRaw(path, val.as<double>()); break;
case SettingTypeString: WriteSettingRaw(path, val.as<mpt::ustring>()); break;
case SettingTypeBinary: WriteSettingRaw(path, val.as<std::vector<std::byte> >()); break;
default: break;
}
}
void IniFileSettingsBackend::RemoveSetting(const SettingPath &path)
{
RemoveSettingRaw(path);
}
void IniFileSettingsBackend::RemoveSection(const mpt::ustring §ion)
{
RemoveSectionRaw(section);
}
IniFileSettingsContainer::IniFileSettingsContainer(const mpt::PathString &filename)
: IniFileSettingsBackend(filename)
, SettingsContainer(this)
{
return;
}
IniFileSettingsContainer::~IniFileSettingsContainer()
{
return;
}
DefaultSettingsContainer::DefaultSettingsContainer()
: IniFileSettingsContainer(theApp.GetConfigFileName())
{
return;
}
DefaultSettingsContainer::~DefaultSettingsContainer()
{
return;
}
OPENMPT_NAMESPACE_END
|