-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathini_parser.cpp
93 lines (76 loc) · 2.01 KB
/
ini_parser.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
#include "pch.h"
#include "ini_parser.h"
/*
-- ----------------------------------------------------------------------
-- Project: DLL creation
--
-- Author: Hamza Qureshi
--
-- File name: INI_Parser.h
--
-- Rev.: 1.0
-- Creation date: DEC 2023
--
-- ---------------------------------------------------------------------
Purpose of this module: MAIN MODULE
----------Main body--------------
Comments:
WARININGS
---------------------------------------------------------------------------
*/
// ini_parser.cpp : implementation file
//
bool INIParser::load(const std::string& filename)
{
std::ifstream file(filename);
if (!file.is_open())
{
std::cerr << "Failed to open the INI file: " << filename << std::endl;
return false;
}
std::string line;
std::string currentSection;
while (std::getline(file, line))
{
parseLine(line, currentSection);
}
file.close();
return true;
}
std::string INIParser::getValue(const std::string& section, const std::string& key) const
{
auto sectionIter = iniData.find(section);
if (sectionIter != iniData.end()) {
auto keyIter = sectionIter->second.find(key);
if (keyIter != sectionIter->second.end())
{
return keyIter->second;
}
}
return ""; // Return empty string if the section or key is not found
}
void INIParser::parseLine(const std::string& line, std::string& currentSection)
{
std::istringstream iss(line);
std::string token;
iss >> std::ws >> token;
if (token.empty() || token[0] == ';')
{
// Ignore empty lines and comments
return;
}
if (token[0] == '[' && token.back() == ']') {
// Section header
currentSection = token.substr(1, token.size() - 2);
}
else {
// Key-value pair
size_t equalsPos = token.find('=');
if (equalsPos != std::string::npos)
{
std::string key = token.substr(0, equalsPos);
std::string value = token.substr(equalsPos + 1);
iniData[currentSection][key] = value;
}
}
}