Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 1 | #include "config_parser.hpp" |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 2 | |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 3 | #include <fstream> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 4 | #include <regex> |
| 5 | #include <string> |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 6 | |
| 7 | namespace phosphor |
| 8 | { |
| 9 | namespace network |
| 10 | { |
| 11 | namespace config |
| 12 | { |
| 13 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 14 | Parser::Parser(const fs::path& filename) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 15 | { |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 16 | setFile(filename); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 17 | } |
| 18 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 19 | const ValueList& Parser::getValues(std::string_view section, |
| 20 | std::string_view key) const noexcept |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 21 | { |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 22 | static const ValueList empty; |
| 23 | auto sit = sections.find(section); |
| 24 | if (sit == sections.end()) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 25 | { |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 26 | return empty; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 27 | } |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 28 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 29 | auto kit = sit->second.find(key); |
| 30 | if (kit == sit->second.end()) |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 31 | { |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 32 | return empty; |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 33 | } |
| 34 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 35 | return kit->second; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void Parser::setValue(const std::string& section, const std::string& key, |
| 39 | const std::string& value) |
| 40 | { |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 41 | auto sit = sections.find(section); |
| 42 | if (sit == sections.end()) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 43 | { |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 44 | std::tie(sit, std::ignore) = sections.emplace(section, KeyValuesMap{}); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 45 | } |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 46 | auto kit = sit->second.find(key); |
| 47 | if (kit == sit->second.end()) |
Ratan Gupta | dea3ead | 2017-08-02 18:09:25 +0530 | [diff] [blame] | 48 | { |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 49 | std::tie(kit, std::ignore) = sit->second.emplace(key, ValueList{}); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 50 | } |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 51 | kit->second.push_back(value); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 52 | } |
| 53 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 54 | void Parser::setFile(const fs::path& filename) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 55 | { |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 56 | std::fstream stream(filename, std::fstream::in); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 57 | if (!stream.is_open()) |
| 58 | { |
| 59 | return; |
| 60 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 61 | // clear all the section data. |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 62 | sections.clear(); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 63 | 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 Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 66 | std::string section; |
| 67 | std::smatch pieces; |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 68 | for (std::string line; std::getline(stream, line);) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 69 | { |
| 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 Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 91 | } // namespace config |
| 92 | } // namespace network |
| 93 | } // namespace phosphor |