blob: a6b7e3649523236e5f69dd6c8a06b9410f2ac993 [file] [log] [blame]
Ratan Guptaed123a32017-06-15 09:07:31 +05301#include "config_parser.hpp"
Ratan Guptaed123a32017-06-15 09:07:31 +05302
Patrick Venture189d44e2018-07-09 12:30:59 -07003#include <fstream>
Patrick Venture189d44e2018-07-09 12:30:59 -07004#include <regex>
5#include <string>
Ratan Guptaed123a32017-06-15 09:07:31 +05306
7namespace phosphor
8{
9namespace network
10{
11namespace config
12{
13
William A. Kennington III25511a12022-08-04 16:32:28 -070014Parser::Parser(const fs::path& filename)
Ratan Guptaed123a32017-06-15 09:07:31 +053015{
William A. Kennington III25511a12022-08-04 16:32:28 -070016 setFile(filename);
Ratan Guptaed123a32017-06-15 09:07:31 +053017}
18
William A. Kennington III25511a12022-08-04 16:32:28 -070019const ValueList& Parser::getValues(std::string_view section,
20 std::string_view key) const noexcept
Ratan Guptaed123a32017-06-15 09:07:31 +053021{
William A. Kennington III25511a12022-08-04 16:32:28 -070022 static const ValueList empty;
23 auto sit = sections.find(section);
24 if (sit == sections.end())
Ratan Guptaed123a32017-06-15 09:07:31 +053025 {
William A. Kennington III25511a12022-08-04 16:32:28 -070026 return empty;
Ratan Guptaed123a32017-06-15 09:07:31 +053027 }
Ratan Guptac27170a2017-11-22 15:44:42 +053028
William A. Kennington III25511a12022-08-04 16:32:28 -070029 auto kit = sit->second.find(key);
30 if (kit == sit->second.end())
Ratan Guptac27170a2017-11-22 15:44:42 +053031 {
William A. Kennington III25511a12022-08-04 16:32:28 -070032 return empty;
Ratan Guptac27170a2017-11-22 15:44:42 +053033 }
34
William A. Kennington III25511a12022-08-04 16:32:28 -070035 return kit->second;
Ratan Guptaed123a32017-06-15 09:07:31 +053036}
37
38void Parser::setValue(const std::string& section, const std::string& key,
39 const std::string& value)
40{
William A. Kennington III25511a12022-08-04 16:32:28 -070041 auto sit = sections.find(section);
42 if (sit == sections.end())
Ratan Guptaed123a32017-06-15 09:07:31 +053043 {
William A. Kennington III25511a12022-08-04 16:32:28 -070044 std::tie(sit, std::ignore) = sections.emplace(section, KeyValuesMap{});
Ratan Guptaed123a32017-06-15 09:07:31 +053045 }
William A. Kennington III25511a12022-08-04 16:32:28 -070046 auto kit = sit->second.find(key);
47 if (kit == sit->second.end())
Ratan Guptadea3ead2017-08-02 18:09:25 +053048 {
William A. Kennington III25511a12022-08-04 16:32:28 -070049 std::tie(kit, std::ignore) = sit->second.emplace(key, ValueList{});
Ratan Guptaed123a32017-06-15 09:07:31 +053050 }
William A. Kennington III25511a12022-08-04 16:32:28 -070051 kit->second.push_back(value);
Ratan Guptaed123a32017-06-15 09:07:31 +053052}
53
William A. Kennington III25511a12022-08-04 16:32:28 -070054void Parser::setFile(const fs::path& filename)
Ratan Guptaed123a32017-06-15 09:07:31 +053055{
William A. Kennington III25511a12022-08-04 16:32:28 -070056 std::fstream stream(filename, std::fstream::in);
Ratan Guptaed123a32017-06-15 09:07:31 +053057 if (!stream.is_open())
58 {
59 return;
60 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050061 // clear all the section data.
Ratan Guptaed123a32017-06-15 09:07:31 +053062 sections.clear();
Gunnar Mills57d9c502018-09-14 14:42:34 -050063 static const std::regex commentRegex{R"x(\s*[;#])x"};
64 static const std::regex sectionRegex{R"x(\s*\[([^\]]+)\])x"};
65 static const std::regex valueRegex{R"x(\s*(\S[^ \t=]*)\s*=\s*(\S+)\s*$)x"};
Ratan Guptaed123a32017-06-15 09:07:31 +053066 std::string section;
67 std::smatch pieces;
William A. Kennington III25511a12022-08-04 16:32:28 -070068 for (std::string line; std::getline(stream, line);)
Ratan Guptaed123a32017-06-15 09:07:31 +053069 {
70 if (line.empty() || std::regex_match(line, pieces, commentRegex))
71 {
72 // skip comment lines and blank lines
73 }
74 else if (std::regex_match(line, pieces, sectionRegex))
75 {
76 if (pieces.size() == 2)
77 {
78 section = pieces[1].str();
79 }
80 }
81 else if (std::regex_match(line, pieces, valueRegex))
82 {
83 if (pieces.size() == 3)
84 {
85 setValue(section, pieces[1].str(), pieces[2].str());
86 }
87 }
88 }
89}
90
Gunnar Mills57d9c502018-09-14 14:42:34 -050091} // namespace config
92} // namespace network
93} // namespace phosphor