forked from openbmc/phosphor-networkd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_parser.cpp
164 lines (144 loc) · 3.86 KB
/
config_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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "config_parser.hpp"
#include <algorithm>
#include <fstream>
#include <list>
#include <phosphor-logging/log.hpp>
#include <regex>
#include <string>
#include <unordered_map>
namespace phosphor
{
namespace network
{
namespace config
{
using namespace phosphor::logging;
Parser::Parser(const fs::path& filePath)
{
setFile(filePath);
}
std::tuple<ReturnCode, KeyValueMap>
Parser::getSection(const std::string& section)
{
auto it = sections.find(section);
if (it == sections.end())
{
KeyValueMap keyValues;
return std::make_tuple(ReturnCode::SECTION_NOT_FOUND,
std::move(keyValues));
}
return std::make_tuple(ReturnCode::SUCCESS, it->second);
}
std::tuple<ReturnCode, ValueList> Parser::getValues(const std::string& section,
const std::string& key)
{
ValueList values;
KeyValueMap keyValues{};
auto rc = ReturnCode::SUCCESS;
std::tie(rc, keyValues) = getSection(section);
if (rc != ReturnCode::SUCCESS)
{
return std::make_tuple(rc, std::move(values));
}
auto it = keyValues.find(key);
if (it == keyValues.end())
{
return std::make_tuple(ReturnCode::KEY_NOT_FOUND, std::move(values));
}
for (; it != keyValues.end() && key == it->first; it++)
{
values.push_back(it->second);
}
return std::make_tuple(ReturnCode::SUCCESS, std::move(values));
}
bool Parser::isValueExist(const std::string& section, const std::string& key,
const std::string& value)
{
auto rc = ReturnCode::SUCCESS;
ValueList values;
std::tie(rc, values) = getValues(section, key);
if (rc != ReturnCode::SUCCESS)
{
return false;
}
auto it = std::find(values.begin(), values.end(), value);
return it != std::end(values) ? true : false;
}
void Parser::setValue(const std::string& section, const std::string& key,
const std::string& value)
{
KeyValueMap values;
auto it = sections.find(section);
if (it != sections.end())
{
values = std::move(it->second);
}
values.insert(std::make_pair(key, value));
if (it != sections.end())
{
it->second = std::move(values);
}
else
{
sections.insert(std::make_pair(section, std::move(values)));
}
}
#if 0
void Parser::print()
{
for (auto section : sections)
{
std::cout << "[" << section.first << "]\n\n";
for (auto keyValue : section.second)
{
std::cout << keyValue.first << "=" << keyValue.second << "\n";
}
}
}
#endif
void Parser::setFile(const fs::path& filePath)
{
this->filePath = filePath;
std::fstream stream;
stream.open(filePath.string(), std::fstream::in);
if (!stream.is_open())
{
return;
}
// clear all the section data.
sections.clear();
parse(stream);
stream.close();
}
void Parser::parse(std::istream& in)
{
static const std::regex commentRegex{R"x(\s*[;#])x"};
static const std::regex sectionRegex{R"x(\s*\[([^\]]+)\])x"};
static const std::regex valueRegex{R"x(\s*(\S[^ \t=]*)\s*=\s*(\S+)\s*$)x"};
std::string section;
std::smatch pieces;
for (std::string line; std::getline(in, line);)
{
if (line.empty() || std::regex_match(line, pieces, commentRegex))
{
// skip comment lines and blank lines
}
else if (std::regex_match(line, pieces, sectionRegex))
{
if (pieces.size() == 2)
{
section = pieces[1].str();
}
}
else if (std::regex_match(line, pieces, valueRegex))
{
if (pieces.size() == 3)
{
setValue(section, pieces[1].str(), pieces[2].str());
}
}
}
}
} // namespace config
} // namespace network
} // namespace phosphor