Skip to content

Commit 24ec5c7

Browse files
committed
Fix
1 parent e637c41 commit 24ec5c7

File tree

4 files changed

+699
-0
lines changed

4 files changed

+699
-0
lines changed

Source/IniReader/IniReader.h

Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
#ifndef INIREADER_H
2+
#define INIREADER_H
3+
#include "ini_parser.hpp"
4+
#include <string>
5+
#include <string_view>
6+
#include <Windows.h>
7+
8+
/*
9+
* String comparision functions, with case sensitive option
10+
*/
11+
12+
using std::strcmp;
13+
14+
inline int strcmp(const char* str1, const char* str2, bool csensitive)
15+
{
16+
return (csensitive ? ::strcmp(str1, str2) : ::_stricmp(str1, str2));
17+
}
18+
19+
inline int strcmp(const char* str1, const char* str2, size_t num, bool csensitive)
20+
{
21+
return (csensitive ? ::strncmp(str1, str2, num) : ::_strnicmp(str1, str2, num));
22+
}
23+
24+
inline int compare(const std::string& str1, const std::string& str2, bool case_sensitive)
25+
{
26+
if (str1.length() == str2.length())
27+
return strcmp(str1.c_str(), str2.c_str(), case_sensitive);
28+
return (str1.length() < str2.length() ? -1 : 1);
29+
}
30+
31+
inline int compare(const std::string& str1, const std::string& str2, size_t num, bool case_sensitive)
32+
{
33+
if (str1.length() == str2.length())
34+
return strcmp(str1.c_str(), str2.c_str(), num, case_sensitive);
35+
return (str1.length() < str2.length() ? -1 : 1);
36+
}
37+
38+
inline int compare(const char* str1, const char* str2, bool case_sensitive)
39+
{
40+
return strcmp(str1, str2, case_sensitive);
41+
}
42+
43+
inline int compare(const char* str1, const char* str2, size_t num, bool case_sensitive)
44+
{
45+
return strcmp(str1, str2, num, case_sensitive);
46+
}
47+
48+
inline bool starts_with(const char* str, const char* prefix, bool case_sensitive)
49+
{
50+
while (*prefix)
51+
{
52+
bool equal;
53+
if (case_sensitive)
54+
equal = (*str++ == *prefix++);
55+
else
56+
equal = (::tolower(*str++) == ::tolower(*prefix++));
57+
58+
if (!equal) return false;
59+
}
60+
return true;
61+
}
62+
63+
inline bool ends_with(const char* str, const char* prefix, bool case_sensitive)
64+
{
65+
auto str2 = &str[strlen(str) - 1];
66+
auto prefix2 = &prefix[strlen(prefix) - 1];
67+
68+
while (prefix2 >= prefix)
69+
{
70+
bool equal;
71+
if (case_sensitive)
72+
equal = (*str2-- == *prefix2--);
73+
else
74+
equal = (::tolower(*str2--) == ::tolower(*prefix2--));
75+
76+
if (!equal) return false;
77+
}
78+
return true;
79+
}
80+
81+
class CIniReader
82+
{
83+
private:
84+
std::string m_szFileName;
85+
86+
public:
87+
linb::ini data;
88+
89+
CIniReader()
90+
{
91+
SetIniPath("");
92+
}
93+
94+
CIniReader(std::string_view szFileName)
95+
{
96+
SetIniPath(szFileName);
97+
}
98+
99+
CIniReader(std::stringstream& ini_mem)
100+
{
101+
data.load_file(ini_mem);
102+
}
103+
104+
bool operator==(CIniReader& ir)
105+
{
106+
if (data.size() != ir.data.size())
107+
return false;
108+
109+
for (auto& section : data)
110+
{
111+
for (auto& key : data.at(section.first))
112+
{
113+
if (key.second != ir.data.at(section.first)[key.first])
114+
return false;
115+
}
116+
}
117+
return true;
118+
}
119+
120+
bool operator!=(CIniReader& ir)
121+
{
122+
return !(*this == ir);
123+
}
124+
125+
bool CompareBySections(CIniReader& ir)
126+
{
127+
if (data.size() != ir.data.size())
128+
return false;
129+
130+
for (auto& section : data)
131+
{
132+
if (ir.data.find(section.first) == ir.data.end())
133+
return false;
134+
135+
if (section.second.size() != ir.data.find(section.first)->second.size())
136+
return false;
137+
138+
if (section.first != ir.data.find(section.first)->first)
139+
return false;
140+
}
141+
return true;
142+
}
143+
144+
bool CompareByValues(CIniReader& ir)
145+
{
146+
return *this == ir;
147+
}
148+
149+
const std::string& GetIniPath()
150+
{
151+
return m_szFileName;
152+
}
153+
154+
void SetIniPath()
155+
{
156+
SetIniPath("");
157+
}
158+
159+
void SetIniPath(std::string_view szFileName)
160+
{
161+
char buffer[MAX_PATH];
162+
HMODULE hm = NULL;
163+
GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR)&ends_with, &hm);
164+
GetModuleFileNameA(hm, buffer, sizeof(buffer));
165+
std::string modulePath = buffer;
166+
167+
if (szFileName.find(':') != std::string_view::npos)
168+
{
169+
m_szFileName = szFileName;
170+
}
171+
else if (szFileName.length() == 0)
172+
{
173+
m_szFileName = modulePath.substr(0, modulePath.find_last_of('.')) + ".ini";
174+
}
175+
else
176+
{
177+
m_szFileName = modulePath.substr(0, modulePath.rfind('\\') + 1) + szFileName.data();
178+
}
179+
180+
data.load_file(m_szFileName);
181+
}
182+
183+
int ReadInteger(std::string_view szSection, std::string_view szKey, int iDefaultValue)
184+
{
185+
auto str = data.get(szSection.data(), szKey.data(), std::to_string(iDefaultValue));
186+
return std::stoi(str, nullptr, starts_with(str.c_str(), "0x", false) ? 16 : 10);
187+
}
188+
189+
float ReadFloat(std::string_view szSection, std::string_view szKey, float fltDefaultValue)
190+
{
191+
return (float)atof(data.get(szSection.data(), szKey.data(), std::to_string(fltDefaultValue)).c_str());
192+
}
193+
194+
bool ReadBoolean(std::string_view szSection, std::string_view szKey, bool bolDefaultValue)
195+
{
196+
auto val = data.get(szSection.data(), szKey.data(), "");
197+
if (!val.empty())
198+
{
199+
if (val.size() == 1)
200+
return val.front() != '0';
201+
else
202+
return compare(val, "false", false);
203+
}
204+
return bolDefaultValue;
205+
}
206+
207+
std::string ReadString(std::string_view szSection, std::string_view szKey, std::string_view szDefaultValue)
208+
{
209+
auto s = data.get(szSection.data(), szKey.data(), szDefaultValue.data());
210+
211+
if (!s.empty())
212+
{
213+
if (s.at(0) == '\"' || s.at(0) == '\'')
214+
s.erase(0, 1);
215+
216+
if (s.at(s.size() - 1) == '\"' || s.at(s.size() - 1) == '\'')
217+
s.erase(s.size() - 1);
218+
}
219+
220+
return s;
221+
}
222+
223+
void WriteInteger(std::string_view szSection, std::string_view szKey, int iValue, bool useparser = false)
224+
{
225+
if (useparser)
226+
{
227+
data.set(szSection.data(), szKey.data(), std::to_string(iValue));
228+
data.write_file(m_szFileName);
229+
}
230+
else
231+
{
232+
char szValue[255];
233+
_snprintf_s(szValue, 255, "%s%d", " ", iValue);
234+
WritePrivateProfileStringA(szSection.data(), szKey.data(), szValue, m_szFileName.c_str());
235+
}
236+
}
237+
238+
void WriteFloat(std::string_view szSection, std::string_view szKey, float fltValue, bool useparser = false)
239+
{
240+
if (useparser)
241+
{
242+
data.set(szSection.data(), szKey.data(), std::to_string(fltValue));
243+
data.write_file(m_szFileName);
244+
}
245+
else
246+
{
247+
char szValue[255];
248+
_snprintf_s(szValue, 255, "%s%f", " ", fltValue);
249+
WritePrivateProfileStringA(szSection.data(), szKey.data(), szValue, m_szFileName.c_str());
250+
}
251+
}
252+
253+
void WriteBoolean(std::string_view szSection, std::string_view szKey, bool bolValue, bool useparser = false)
254+
{
255+
if (useparser)
256+
{
257+
data.set(szSection.data(), szKey.data(), std::to_string(bolValue));
258+
data.write_file(m_szFileName);
259+
}
260+
else
261+
{
262+
char szValue[255];
263+
_snprintf_s(szValue, 255, "%s%s", " ", bolValue ? "True" : "False");
264+
WritePrivateProfileStringA(szSection.data(), szKey.data(), szValue, m_szFileName.c_str());
265+
}
266+
}
267+
268+
void WriteString(std::string_view szSection, std::string_view szKey, std::string_view szValue, bool useparser = false)
269+
{
270+
if (useparser)
271+
{
272+
data.set(szSection.data(), szKey.data(), szValue.data());
273+
data.write_file(m_szFileName);
274+
}
275+
else
276+
{
277+
WritePrivateProfileStringA(szSection.data(), szKey.data(), szValue.data(), m_szFileName.c_str());
278+
}
279+
}
280+
};
281+
282+
#endif //INIREADER_H

Source/IniReader/LICENSE.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2013-2015 Denilson das Mercês Amorim <[email protected]>
3+
*
4+
* This software is provided 'as-is', without any express or implied
5+
* warranty. In no event will the authors be held liable for any damages
6+
* arising from the use of this software.
7+
*
8+
* Permission is granted to anyone to use this software for any purpose,
9+
* including commercial applications, and to alter it and redistribute it
10+
* freely, subject to the following restrictions:
11+
*
12+
* 1. The origin of this software must not be misrepresented; you must not
13+
* claim that you wrote the original software. If you use this software
14+
* in a product, an acknowledgment in the product documentation would be
15+
* appreciated but is not required.
16+
*
17+
* 2. Altered source versions must be plainly marked as such, and must not be
18+
* misrepresented as being the original software.
19+
*
20+
* 3. This notice may not be removed or altered from any source
21+
* distribution.
22+
*
23+
*/

0 commit comments

Comments
 (0)