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 | |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 3 | #include <algorithm> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 4 | #include <fstream> |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 5 | #include <list> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 6 | #include <phosphor-logging/log.hpp> |
| 7 | #include <regex> |
| 8 | #include <string> |
| 9 | #include <unordered_map> |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 10 | |
| 11 | namespace phosphor |
| 12 | { |
| 13 | namespace network |
| 14 | { |
| 15 | namespace config |
| 16 | { |
| 17 | |
| 18 | using namespace phosphor::logging; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 19 | |
| 20 | Parser::Parser(const fs::path& filePath) |
| 21 | { |
| 22 | setFile(filePath); |
| 23 | } |
| 24 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 25 | std::tuple<ReturnCode, KeyValueMap> |
| 26 | Parser::getSection(const std::string& section) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 27 | { |
| 28 | auto it = sections.find(section); |
| 29 | if (it == sections.end()) |
| 30 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 31 | KeyValueMap keyValues; |
| 32 | return std::make_tuple(ReturnCode::SECTION_NOT_FOUND, |
| 33 | std::move(keyValues)); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 34 | } |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 35 | |
| 36 | return std::make_tuple(ReturnCode::SUCCESS, it->second); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 37 | } |
| 38 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 39 | std::tuple<ReturnCode, ValueList> Parser::getValues(const std::string& section, |
| 40 | const std::string& key) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 41 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 42 | ValueList values; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 43 | KeyValueMap keyValues{}; |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 44 | auto rc = ReturnCode::SUCCESS; |
| 45 | |
| 46 | std::tie(rc, keyValues) = getSection(section); |
| 47 | if (rc != ReturnCode::SUCCESS) |
| 48 | { |
| 49 | return std::make_tuple(rc, std::move(values)); |
| 50 | } |
| 51 | |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 52 | auto it = keyValues.find(key); |
| 53 | if (it == keyValues.end()) |
| 54 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 55 | return std::make_tuple(ReturnCode::KEY_NOT_FOUND, std::move(values)); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 56 | } |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 57 | |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 58 | for (; it != keyValues.end() && key == it->first; it++) |
| 59 | { |
| 60 | values.push_back(it->second); |
| 61 | } |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 62 | |
| 63 | return std::make_tuple(ReturnCode::SUCCESS, std::move(values)); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 64 | } |
| 65 | |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 66 | bool Parser::isValueExist(const std::string& section, const std::string& key, |
| 67 | const std::string& value) |
| 68 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 69 | auto rc = ReturnCode::SUCCESS; |
| 70 | ValueList values; |
| 71 | std::tie(rc, values) = getValues(section, key); |
| 72 | |
| 73 | if (rc != ReturnCode::SUCCESS) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 74 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 75 | return false; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 76 | } |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 77 | auto it = std::find(values.begin(), values.end(), value); |
| 78 | return it != std::end(values) ? true : false; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void Parser::setValue(const std::string& section, const std::string& key, |
| 82 | const std::string& value) |
| 83 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 84 | KeyValueMap values; |
Ratan Gupta | dea3ead | 2017-08-02 18:09:25 +0530 | [diff] [blame] | 85 | auto it = sections.find(section); |
| 86 | if (it != sections.end()) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 87 | { |
Ratan Gupta | dea3ead | 2017-08-02 18:09:25 +0530 | [diff] [blame] | 88 | values = std::move(it->second); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 89 | } |
Ratan Gupta | dea3ead | 2017-08-02 18:09:25 +0530 | [diff] [blame] | 90 | values.insert(std::make_pair(key, value)); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 91 | |
Ratan Gupta | dea3ead | 2017-08-02 18:09:25 +0530 | [diff] [blame] | 92 | if (it != sections.end()) |
| 93 | { |
| 94 | it->second = std::move(values); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 95 | } |
Ratan Gupta | dea3ead | 2017-08-02 18:09:25 +0530 | [diff] [blame] | 96 | else |
| 97 | { |
| 98 | sections.insert(std::make_pair(section, std::move(values))); |
| 99 | } |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | #if 0 |
| 103 | void Parser::print() |
| 104 | { |
| 105 | for (auto section : sections) |
| 106 | { |
| 107 | std::cout << "[" << section.first << "]\n\n"; |
| 108 | for (auto keyValue : section.second) |
| 109 | { |
| 110 | std::cout << keyValue.first << "=" << keyValue.second << "\n"; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | #endif |
| 115 | |
| 116 | void Parser::setFile(const fs::path& filePath) |
| 117 | { |
| 118 | this->filePath = filePath; |
Johnathan Mantey | e9d095d | 2021-07-07 10:20:31 -0700 | [diff] [blame] | 119 | std::fstream stream(filePath, std::fstream::in); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 120 | |
| 121 | if (!stream.is_open()) |
| 122 | { |
| 123 | return; |
| 124 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 125 | // clear all the section data. |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 126 | sections.clear(); |
| 127 | parse(stream); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 128 | } |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 129 | |
| 130 | void Parser::parse(std::istream& in) |
| 131 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 132 | static const std::regex commentRegex{R"x(\s*[;#])x"}; |
| 133 | static const std::regex sectionRegex{R"x(\s*\[([^\]]+)\])x"}; |
| 134 | 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] | 135 | std::string section; |
| 136 | std::smatch pieces; |
| 137 | for (std::string line; std::getline(in, line);) |
| 138 | { |
| 139 | if (line.empty() || std::regex_match(line, pieces, commentRegex)) |
| 140 | { |
| 141 | // skip comment lines and blank lines |
| 142 | } |
| 143 | else if (std::regex_match(line, pieces, sectionRegex)) |
| 144 | { |
| 145 | if (pieces.size() == 2) |
| 146 | { |
| 147 | section = pieces[1].str(); |
| 148 | } |
| 149 | } |
| 150 | else if (std::regex_match(line, pieces, valueRegex)) |
| 151 | { |
| 152 | if (pieces.size() == 3) |
| 153 | { |
| 154 | setValue(section, pieces[1].str(), pieces[2].str()); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 160 | } // namespace config |
| 161 | } // namespace network |
| 162 | } // namespace phosphor |