Repository navigation
Expand file tree
/
Copy pathIniReader.hpp
More file actions
143 lines (124 loc) · 3.6 KB
/
Copy pathIniReader.hpp
File metadata and controls
143 lines (124 loc) · 3.6 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
#pragma once
#include <fstream>
#include <string>
#include <type_traits>
#include "minIni.h"
class IniWriter
{
friend class IniReader;
private:
std::string m_szFileName;
public:
IniWriter() = default;
~IniWriter() = default;
IniWriter(const std::string& filename)
{
m_szFileName = filename;
}
virtual void SetFile(const std::string& filename) {};
bool WriteInteger(const std::string& section, const std::string& key, int value)
{
return ini_putl(section.c_str(), key.c_str(), value, m_szFileName.c_str()) != 0 ? true : false;
}
bool WriteFloat(const std::string& section, const std::string& key, float value)
{
return ini_putf(section.c_str(), key.c_str(), value, m_szFileName.c_str()) != 0 ? true : false;
}
bool WriteBoolean(const std::string& section, const std::string& key, bool value)
{
return ini_putbool(section.c_str(), key.c_str(), value, m_szFileName.c_str()) != 0 ? true : false;
}
bool WriteString(const std::string& section, const std::string& key, const std::string& str)
{
return ini_puts(section.c_str(), key.c_str(), str.c_str(), m_szFileName.c_str()) != 0 ? true : false;
}
template <typename T>
bool Write(const std::string& section, const std::string& key, const T& value)
{
if constexpr (std::is_same_v<T, bool> && sizeof(T) == 1)
{
return WriteBoolean(section, key, value);
}
else if constexpr (std::is_integral_v<T>)
{
return WriteInteger(section, key, value);
}
else if constexpr (std::is_floating_point_v<T>)
{
return WriteFloat(section, key, value);
}
else if constexpr (std::is_same_v<T, std::string>)
{
return WriteString(section, key, value);
}
else
{
return false;
}
}
template <typename T>
void operator()(const std::string& section, const std::string& key, const T& value)
{
Write<T>(section, key, value);
}
void WriteText(const std::string& text)
{
std::ofstream file(m_szFileName, std::ios::out | std::ios::app);
file << text << std::endl;
}
};
class IniReader : public IniWriter
{
public:
IniReader(const std::string& filename)
{
m_szFileName = filename;
}
void SetFile(const std::string& filename)
{
m_szFileName = filename;
}
int ReadInteger(const std::string& section, const std::string& key, int default_value = 0)
{
return ini_getl(section.c_str(), key.c_str(), default_value, m_szFileName.c_str());
}
float ReadFloat(const std::string& section, const std::string& key, float default_value = 0.0f)
{
return ini_getf(section.c_str(), key.c_str(), default_value, m_szFileName.c_str());
}
bool ReadBoolean(const std::string& section, const std::string& key, bool default_value = false)
{
return ini_getbool(section.c_str(), key.c_str(), default_value, m_szFileName.c_str());
}
std::string ReadString(const std::string& section, const std::string& key, const std::string& default_value = "")
{
char buffer[INI_BUFFERSIZE];
memset(buffer, 0, INI_BUFFERSIZE);
ini_gets(section.c_str(), key.c_str(), default_value.c_str(), buffer, INI_BUFFERSIZE, m_szFileName.c_str());
return std::string(buffer);
}
template <typename T>
T Read(const std::string& section, const std::string& key, const T& default_value)
{
if constexpr (std::is_same_v<T, bool> && sizeof(T) == 1)
{
return ReadBoolean(section, key, default_value);
}
else if constexpr (std::is_integral_v<T>)
{
return ReadInteger(section, key, default_value);
}
else if constexpr (std::is_floating_point_v<T>)
{
return ReadFloat(section, key, default_value);
}
else if constexpr (std::is_same_v<T, std::string>)
{
return ReadString(section, key, default_value);
}
else
{
return default_value;
}
}
};