-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathIniSettings.cpp
147 lines (126 loc) · 6.12 KB
/
IniSettings.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
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
// sktoolslib - common files for SK tools
// Copyright (C) 2013, 2017, 2020-2021 - Stefan Kueng
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "stdafx.h"
#include "IniSettings.h"
#include "StringUtils.h"
CIniSettings::CIniSettings()
{
}
CIniSettings::~CIniSettings()
{
Save();
}
CIniSettings& CIniSettings::Instance()
{
static CIniSettings instance;
return instance;
}
void CIniSettings::SetIniPath(const std::wstring& p)
{
if (p.empty())
{
wchar_t buf[MAX_PATH] = {0};
GetModuleFileName(nullptr, buf, _countof(buf));
m_iniPath = buf;
m_iniPath = m_iniPath.substr(0, m_iniPath.find_last_of('\\'));
m_iniPath += L"\\settings";
}
else
m_iniPath = p;
m_iniFile.LoadFile(m_iniPath.c_str());
}
void CIniSettings::Save() const
{
FILE* pFile = nullptr;
_wfopen_s(&pFile, m_iniPath.c_str(), L"wb");
m_iniFile.SaveFile(pFile);
fclose(pFile);
}
__int64 CIniSettings::GetInt64(LPCWSTR section, LPCWSTR key, __int64 defaultVal) const
{
_ASSERT(m_iniPath.size());
const wchar_t* v = m_iniFile.GetValue(section, key, nullptr);
if (v == nullptr)
return defaultVal;
return _wcstoi64(v, nullptr, 10);
}
void CIniSettings::SetInt64(LPCWSTR section, LPCWSTR key, __int64 value)
{
wchar_t val[100] = {0};
_i64tow_s(value, val, _countof(val), 10);
m_iniFile.SetValue(section, key, val);
}
LPCWSTR CIniSettings::GetString(LPCWSTR section, LPCWSTR key, LPCWSTR defaultVal /*= nullptr*/) const
{
_ASSERT(m_iniPath.size());
return m_iniFile.GetValue(section, key, defaultVal);
}
void CIniSettings::SetString(LPCWSTR section, LPCWSTR key, LPCWSTR value)
{
m_iniFile.SetValue(section, key, value);
}
void CIniSettings::RestoreWindowPos(LPCWSTR windowname, HWND hWnd, UINT showCmd) const
{
WINDOWPLACEMENT wpl = {0};
wpl.length = sizeof(WINDOWPLACEMENT);
wpl.flags = static_cast<UINT>(GetInt64(L"windowpos", CStringUtils::Format(L"%s_flags", windowname).c_str(), 0));
wpl.showCmd = static_cast<UINT>(GetInt64(L"windowpos", CStringUtils::Format(L"%s_showCmd", windowname).c_str(), -1));
wpl.ptMinPosition.x = static_cast<LONG>(GetInt64(L"windowpos", CStringUtils::Format(L"%s_ptMinPositionX", windowname).c_str(), 0));
wpl.ptMinPosition.y = static_cast<LONG>(GetInt64(L"windowpos", CStringUtils::Format(L"%s_ptMinPositionY", windowname).c_str(), 0));
wpl.ptMaxPosition.x = static_cast<LONG>(GetInt64(L"windowpos", CStringUtils::Format(L"%s_ptMaxPositionX", windowname).c_str(), 0));
wpl.ptMaxPosition.y = static_cast<LONG>(GetInt64(L"windowpos", CStringUtils::Format(L"%s_ptMaxPositionY", windowname).c_str(), 0));
wpl.rcNormalPosition.left = static_cast<LONG>(GetInt64(L"windowpos", CStringUtils::Format(L"%s_rcNormalPositionLeft", windowname).c_str(), 0));
wpl.rcNormalPosition.top = static_cast<LONG>(GetInt64(L"windowpos", CStringUtils::Format(L"%s_rcNormalPositionTop", windowname).c_str(), 0));
wpl.rcNormalPosition.right = static_cast<LONG>(GetInt64(L"windowpos", CStringUtils::Format(L"%s_rcNormalPositionRight", windowname).c_str(), 0));
wpl.rcNormalPosition.bottom = static_cast<LONG>(GetInt64(L"windowpos", CStringUtils::Format(L"%s_rcNormalPositionBottom", windowname).c_str(), 0));
if (wpl.showCmd != static_cast<UINT>(-1))
{
if (wpl.showCmd == SW_HIDE)
wpl.showCmd = SW_SHOWDEFAULT;
else if ((wpl.showCmd == SW_SHOWMINIMIZED) && (wpl.flags & WPF_RESTORETOMAXIMIZED))
wpl.showCmd = SW_SHOWMAXIMIZED;
else if ((wpl.showCmd == SW_SHOWMINIMIZED) || (wpl.showCmd == SW_MINIMIZE) || (wpl.showCmd == SW_SHOWMINNOACTIVE))
wpl.showCmd = SW_RESTORE;
if (showCmd)
wpl.showCmd = showCmd;
SetWindowPlacement(hWnd, &wpl);
}
else
ShowWindow(hWnd, SW_SHOW);
}
void CIniSettings::SaveWindowPos(LPCWSTR windowname, HWND hWnd)
{
WINDOWPLACEMENT wpl = {0};
wpl.length = sizeof(WINDOWPLACEMENT);
GetWindowPlacement(hWnd, &wpl);
SetInt64(L"windowpos", CStringUtils::Format(L"%s_flags", windowname).c_str(), wpl.flags);
SetInt64(L"windowpos", CStringUtils::Format(L"%s_showCmd", windowname).c_str(), wpl.showCmd);
SetInt64(L"windowpos", CStringUtils::Format(L"%s_ptMinPositionX", windowname).c_str(), wpl.ptMinPosition.x);
SetInt64(L"windowpos", CStringUtils::Format(L"%s_ptMinPositionY", windowname).c_str(), wpl.ptMinPosition.y);
SetInt64(L"windowpos", CStringUtils::Format(L"%s_ptMaxPositionX", windowname).c_str(), wpl.ptMaxPosition.x);
SetInt64(L"windowpos", CStringUtils::Format(L"%s_ptMaxPositionY", windowname).c_str(), wpl.ptMaxPosition.y);
SetInt64(L"windowpos", CStringUtils::Format(L"%s_rcNormalPositionLeft", windowname).c_str(), wpl.rcNormalPosition.left);
SetInt64(L"windowpos", CStringUtils::Format(L"%s_rcNormalPositionTop", windowname).c_str(), wpl.rcNormalPosition.top);
SetInt64(L"windowpos", CStringUtils::Format(L"%s_rcNormalPositionRight", windowname).c_str(), wpl.rcNormalPosition.right);
SetInt64(L"windowpos", CStringUtils::Format(L"%s_rcNormalPositionBottom", windowname).c_str(), wpl.rcNormalPosition.bottom);
}
void CIniSettings::Delete(LPCWSTR section, LPCWSTR key)
{
m_iniFile.Delete(section, key, true);
}
void CIniSettings::Reload()
{
m_iniFile.LoadFile(m_iniPath.c_str());
}