-
Notifications
You must be signed in to change notification settings - Fork 0
/
Presets.cpp
82 lines (69 loc) · 2.47 KB
/
Presets.cpp
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
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <Registry.hpp>
#include <RsUtil.h>
#include "Presets.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
const String BaseKey = "\\Software\\Roel Schroeven\\VolBalCon";
const String PresetsKey = "\\Software\\Roel Schroeven\\VolBalCon\\Presets";
using rsutil::JoinPaths;
using rsutil::PrintF;
void TPresets::Load()
{
std::auto_ptr<TRegistry> Registry(new TRegistry);
Registry->RootKey = HKEY_CURRENT_USER;
if (!Registry->OpenKeyReadOnly(PresetsKey))
return;
std::auto_ptr<TStrings> PresetNames(new TStringList);
Registry->GetKeyNames(PresetNames.get());
for (int i = 0; i < PresetNames->Count; ++i)
{
String Name = PresetNames->Strings[i];
if (!Registry->OpenKeyReadOnly(JoinPaths(PresetsKey, Name)))
continue;
TPreset Preset;
Preset.Master = 0.01 * Registry->ReadInteger("Master");
int ChannelIndex = 0;
while (true)
{
String ValueName = PrintF(L"Channel%d", ChannelIndex);
if (!Registry->ValueExists(ValueName))
break;
float ChannelVolume = 0.0;
try { ChannelVolume = 0.01 * Registry->ReadInteger(ValueName); }
catch (Exception &E) { }
Preset.Channels.push_back(ChannelVolume);
ChannelIndex += 1;
}
Presets[Name] = Preset;
}
}
static void DeletePresetsFromRegistry(TRegistry *Registry, const String &PresetsKey)
{
if (!Registry->OpenKey(PresetsKey, false))
return;
std::auto_ptr<TStrings> PresetNames(new TStringList);
Registry->GetKeyNames(PresetNames.get());
for (int i = 0; i < PresetNames->Count; ++i)
Registry->DeleteKey(JoinPaths(PresetsKey, PresetNames->Strings[i]));
}
void TPresets::Save()
{
std::auto_ptr<TRegistry> Registry(new TRegistry);
Registry->RootKey = HKEY_CURRENT_USER;
// First remove all presets to start from a clean slate
DeletePresetsFromRegistry(Registry.get(), PresetsKey);
// Write presets
for (std::map<String, TPreset>::const_iterator it = Presets.begin(); it != Presets.end(); ++it)
{
const String &PresetName = it->first;
const TPreset &Preset = it->second;
if (!Registry->OpenKey(JoinPaths(PresetsKey, PresetName), true))
continue;
Registry->WriteInteger(L"Master", 100 * Preset.Master);
for (unsigned i = 0; i < Preset.Channels.size(); ++i)
Registry->WriteInteger(PrintF(L"Channel%d", i), 100 * Preset.Channels[i]);
}
}