-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIniFile.cpp
More file actions
116 lines (93 loc) · 2.98 KB
/
IniFile.cpp
File metadata and controls
116 lines (93 loc) · 2.98 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
/*
Copyright (c) 2024-2025 Toksisitee. All rights reserved.
This work is licensed under the terms of the MIT license.
For a copy, refer to license.md or https://opensource.org/licenses/MIT
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
*/
#include <windows.h>
#include <stdexcept>
#include <fstream>
#include "IniFile.h"
namespace Util
{
namespace FileSystem
{
void RemoveFileSpec( char* pszPath );
}
}
CIniFile g_IniFile( "config.ini" );
const std::string CIniFile::m_sSection = "Settings";
const std::unordered_map<EIniSetting, std::string> CIniFile::m_mapSettingKeys = {
{EIniSetting::ExportDirectory, "ExportDirectory"} ,
{EIniSetting::PopulousDirectory, "PopulousDirectory"} ,
};
CIniFile::CIniFile( const std::string& sConfigName ) : m_sFilePath( sConfigName ) {}
void CIniFile::Initialize()
{
char szPath[MAX_PATH];
GetModuleFileNameA( NULL, szPath, MAX_PATH );
Util::FileSystem::RemoveFileSpec( szPath );
m_sFilePath = std::string( szPath ) + "\\" + m_sFilePath;
std::ifstream file( m_sFilePath );
if ( !file.good() ) {
std::ofstream outFile( m_sFilePath );
if ( outFile.is_open() ) {
outFile << "[" << m_sSection << "]\n";
outFile.close();
}
}
}
std::string CIniFile::GetKey( EIniSetting eSetting )
{
auto it = m_mapSettingKeys.find( eSetting );
if ( it != m_mapSettingKeys.end() ) {
return it->second;
}
throw std::runtime_error( "Invalid setting key." );
}
std::string CIniFile::GetString( EIniSetting eSetting, const std::string& sDefault )
{
char szBuffer[256];
GetPrivateProfileStringA( m_sSection.c_str(), GetKey( eSetting ).c_str(), sDefault.c_str(), szBuffer, sizeof( szBuffer ), m_sFilePath.c_str() );
return std::string( szBuffer );
}
void CIniFile::SetString( EIniSetting eSetting, const std::string& sValue )
{
WritePrivateProfileStringA( m_sSection.c_str(), GetKey( eSetting ).c_str(), sValue.c_str(), m_sFilePath.c_str() );
}
int CIniFile::GetInt( EIniSetting eSetting, int nDefault )
{
return GetPrivateProfileIntA( m_sSection.c_str(), GetKey( eSetting ).c_str(), nDefault, m_sFilePath.c_str() );
}
void CIniFile::SetInt( EIniSetting eSetting, int nValue )
{
SetString( eSetting, std::to_string( nValue ) );
}
bool CIniFile::GetBool( EIniSetting eSetting, bool bDefault )
{
return GetInt( eSetting, bDefault ? 1 : 0 ) != 0;
}
void CIniFile::SetBool( EIniSetting eSetting, bool bValue )
{
SetInt( eSetting, bValue ? 1 : 0 );
}
float CIniFile::GetFloat( EIniSetting eSetting, float fDefault )
{
std::string strValue = GetString( eSetting, std::to_string( fDefault ) );
try {
return std::stof( strValue );
}
catch ( const std::exception& ) {
return fDefault;
}
}
void CIniFile::SetFloat( EIniSetting eSetting, float fValue )
{
SetString( eSetting, std::to_string( fValue ) );
}
void CIniFile::ResetSection()
{
WritePrivateProfileStringA( m_sSection.c_str(), nullptr, nullptr, m_sFilePath.c_str() );
}