5 IniFile::IniFile(const wchar_t* path
)
7 CHandle
file(CreateFile(path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0));
8 if (file
== INVALID_HANDLE_VALUE
)
9 throw std::runtime_error("ini file not found");
11 DWORD size
= GetFileSize(file
, NULL
);
12 std::vector
<BYTE
> data(size
);
14 if (!ReadFile(file
, data
.data(), (DWORD
)data
.size(), &size
, NULL
))
15 throw std::runtime_error("failed to read ini file");
17 Parse(data
.data(), size
);
20 IniFile::IniFile(HMODULE hImage
, DWORD resId
)
22 auto data
= ExtractDataFromResource(hImage
, resId
);
23 Parse(data
.first
, data
.second
);
26 const wchar_t* GetEol(const wchar_t* p
, const wchar_t* end
)
28 while (p
< end
&& *p
!= L
'\n')
33 void IniFile::Parse(LPCVOID data
, size_t size
)
35 LPCWSTR begin
= (LPCWSTR
)data
;
36 LPCWSTR end
= begin
+ size
/ sizeof(*begin
);
41 Section
* currentSection
= nullptr;
42 CString
* varToAppend
= nullptr;
46 const wchar_t* eol
= GetEol(begin
, end
);
50 CStringW
line(begin
, int(eol
- begin
));
53 line
.Trim(L
" \t\r\n");
57 bool appendNext
= line
.Right(1) == L
"\\";
59 line
.Delete(line
.GetLength() - 1);
62 varToAppend
= nullptr;
66 if (line
.IsEmpty() || line
[0] == L
';')
69 if (line
[0] == L
'[' && line
[line
.GetLength() - 1] == L
']')
71 currentSection
= &m_sections
[line
.Trim(L
"[]")];
78 int equalPos
= line
.Find(L
'=');
79 if (equalPos
== -1 || equalPos
== 0)
81 CStringW var
= line
.Left(equalPos
);
82 CStringW text
= line
.Mid(equalPos
+ 1);
83 if (text
.Right(1) == L
"\\")
85 text
.Delete(text
.GetLength() - 1);
86 varToAppend
= &(*currentSection
)[var
];
89 (*currentSection
)[var
] = text
;
93 CStringW
IniFile::GetString(const wchar_t* section
, const wchar_t* variable
)
95 auto sec
= m_sections
.find(section
);
96 if (sec
!= m_sections
.end())
98 auto var
= sec
->second
.find(variable
);
99 if (var
!= sec
->second
.end())