-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigParser.cpp
34 lines (29 loc) · 1.04 KB
/
ConfigParser.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
#include <fstream>
#include <algorithm>
#include "ConfigParser.h"
std::map<std::string, std::string> ConfigParser::parse(const std::string& filename) {
std::ifstream file(filename);
if (!file.is_open()) {
throw std::runtime_error("Error opening file: " + filename);
}
std::map<std::string, std::string> configMap;
std::string line;
while (std::getline(file, line)) {
trim(line);
if (!line.empty()) {
auto pos = line.find(':');
if (pos != std::string::npos) {
std::string key = line.substr(0, pos);
std::string value = line.substr(pos + 1);
trim(key);
trim(value);
configMap[key] = value;
}
}
}
return configMap;
}
void ConfigParser::trim(std::string& s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { return !std::isspace(ch); }));
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { return !std::isspace(ch); }).base(), s.end());
}