blob: 51ef55bb70ad6fafba06818ce7c2170b333096ec [file] [log] [blame]
Ratan Guptaed123a32017-06-15 09:07:31 +05301#include "config_parser.hpp"
2#include "xyz/openbmc_project/Common/error.hpp"
3#include <phosphor-logging/log.hpp>
4#include <phosphor-logging/elog-errors.hpp>
5
6#include <fstream>
7#include <string>
8#include <algorithm>
9#include <unordered_map>
10#include <regex>
11#include <list>
12
13namespace phosphor
14{
15namespace network
16{
17namespace config
18{
19
20using namespace phosphor::logging;
21using namespace sdbusplus::xyz::openbmc_project::Common::Error;
22
23Parser::Parser(const fs::path& filePath)
24{
25 setFile(filePath);
26}
27
28
29KeyValues Parser::getSection(const std::string& section)
30{
31 auto it = sections.find(section);
32 if (it == sections.end())
33 {
34 log<level::ERR>("ConfigParser: Section not found",
35 entry("SECTION=%s",section));
36 elog<InternalFailure>();
37 }
38 return it->second;
39}
40
41std::vector<std::string> Parser::getValues(const std::string& section,
42 const std::string& key)
43{
44 std::vector<std::string> values;
45 auto keyValues = getSection(section);
46 auto it = keyValues.find(key);
47 if (it == keyValues.end())
48 {
49 log<level::ERR>("ConfigParser: Key not found",
50 entry("KEY=%s",key));
51 elog<InternalFailure>();
52 }
53 for (; it != keyValues.end() && key == it->first; it++)
54 {
55 values.push_back(it->second);
56 }
57 return values;
58}
59
60
61bool Parser::isValueExist(const std::string& section, const std::string& key,
62 const std::string& value)
63{
64 try
65 {
66 auto values = getValues(section, key);
67 auto it = std::find(values.begin(), values.end(), value);
68 return it != std::end(values) ? true : false;
69 }
70 catch (InternalFailure& e)
71 {
72 commit<InternalFailure>();
73 }
74 return false;
75}
76
77void Parser::setValue(const std::string& section, const std::string& key,
78 const std::string& value)
79{
80 KeyValues values;
81 try
82 {
83 values = getSection(section);
84 }
85 catch (InternalFailure& e)
86 {
87 // don't commit the error.
88
89 }
90 values.emplace(key, value);
91 sections.emplace(section, values);
92}
93
94#if 0
95void Parser::print()
96{
97 for (auto section : sections)
98 {
99 std::cout << "[" << section.first << "]\n\n";
100 for (auto keyValue : section.second)
101 {
102 std::cout << keyValue.first << "=" << keyValue.second << "\n";
103 }
104 }
105}
106#endif
107
108void Parser::setFile(const fs::path& filePath)
109{
110 this->filePath = filePath;
111 std::fstream stream;
112 stream.open(filePath.string(), std::fstream::in);
113
114 if (!stream.is_open())
115 {
116 return;
117 }
118 //clear all the section data.
119 sections.clear();
120 parse(stream);
121 stream.close();
122 }
123
124void Parser::parse(std::istream& in)
125{
126 static const std::regex commentRegex
127 {
128 R"x(\s*[;#])x"
129 };
130 static const std::regex sectionRegex
131 {
132 R"x(\s*\[([^\]]+)\])x"
133 };
134 static const std::regex valueRegex
135 {
136 R"x(\s*(\S[^ \t=]*)\s*=\s*(\S+)\s*$)x"
137 };
138 std::string section;
139 std::smatch pieces;
140 for (std::string line; std::getline(in, line);)
141 {
142 if (line.empty() || std::regex_match(line, pieces, commentRegex))
143 {
144 // skip comment lines and blank lines
145 }
146 else if (std::regex_match(line, pieces, sectionRegex))
147 {
148 if (pieces.size() == 2)
149 {
150 section = pieces[1].str();
151 }
152 }
153 else if (std::regex_match(line, pieces, valueRegex))
154 {
155 if (pieces.size() == 3)
156 {
157 setValue(section, pieces[1].str(), pieces[2].str());
158 }
159 }
160 }
161}
162
163}//namespace config
164}//namespace network
165}//namespace phosphor