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 | #include <phosphor-logging/log.hpp> |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 3 | |
| 4 | #include <fstream> |
| 5 | #include <string> |
| 6 | #include <algorithm> |
| 7 | #include <unordered_map> |
| 8 | #include <regex> |
| 9 | #include <list> |
| 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 | |
| 25 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 26 | std::tuple<ReturnCode, KeyValueMap> 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; |
| 43 | KeyValueMap keyValues {}; |
| 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 | |
| 66 | |
| 67 | bool Parser::isValueExist(const std::string& section, const std::string& key, |
| 68 | const std::string& value) |
| 69 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 70 | auto rc = ReturnCode::SUCCESS; |
| 71 | ValueList values; |
| 72 | std::tie(rc, values) = getValues(section, key); |
| 73 | |
| 74 | if (rc != ReturnCode::SUCCESS) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 75 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 76 | return false; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 77 | } |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 78 | auto it = std::find(values.begin(), values.end(), value); |
| 79 | return it != std::end(values) ? true : false; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void Parser::setValue(const std::string& section, const std::string& key, |
| 83 | const std::string& value) |
| 84 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 85 | KeyValueMap values; |
Ratan Gupta | dea3ead | 2017-08-02 18:09:25 +0530 | [diff] [blame] | 86 | auto it = sections.find(section); |
| 87 | if (it != sections.end()) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 88 | { |
Ratan Gupta | dea3ead | 2017-08-02 18:09:25 +0530 | [diff] [blame] | 89 | values = std::move(it->second); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 90 | } |
Ratan Gupta | dea3ead | 2017-08-02 18:09:25 +0530 | [diff] [blame] | 91 | values.insert(std::make_pair(key, value)); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 92 | |
Ratan Gupta | dea3ead | 2017-08-02 18:09:25 +0530 | [diff] [blame] | 93 | if (it != sections.end()) |
| 94 | { |
| 95 | it->second = std::move(values); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 96 | } |
Ratan Gupta | dea3ead | 2017-08-02 18:09:25 +0530 | [diff] [blame] | 97 | else |
| 98 | { |
| 99 | sections.insert(std::make_pair(section, std::move(values))); |
| 100 | } |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | #if 0 |
| 104 | void Parser::print() |
| 105 | { |
| 106 | for (auto section : sections) |
| 107 | { |
| 108 | std::cout << "[" << section.first << "]\n\n"; |
| 109 | for (auto keyValue : section.second) |
| 110 | { |
| 111 | std::cout << keyValue.first << "=" << keyValue.second << "\n"; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | #endif |
| 116 | |
| 117 | void Parser::setFile(const fs::path& filePath) |
| 118 | { |
| 119 | this->filePath = filePath; |
| 120 | std::fstream stream; |
| 121 | stream.open(filePath.string(), std::fstream::in); |
| 122 | |
| 123 | if (!stream.is_open()) |
| 124 | { |
| 125 | return; |
| 126 | } |
| 127 | //clear all the section data. |
| 128 | sections.clear(); |
| 129 | parse(stream); |
| 130 | stream.close(); |
| 131 | } |
| 132 | |
| 133 | void Parser::parse(std::istream& in) |
| 134 | { |
| 135 | static const std::regex commentRegex |
| 136 | { |
| 137 | R"x(\s*[;#])x" |
| 138 | }; |
| 139 | static const std::regex sectionRegex |
| 140 | { |
| 141 | R"x(\s*\[([^\]]+)\])x" |
| 142 | }; |
| 143 | static const std::regex valueRegex |
| 144 | { |
| 145 | R"x(\s*(\S[^ \t=]*)\s*=\s*(\S+)\s*$)x" |
| 146 | }; |
| 147 | std::string section; |
| 148 | std::smatch pieces; |
| 149 | for (std::string line; std::getline(in, line);) |
| 150 | { |
| 151 | if (line.empty() || std::regex_match(line, pieces, commentRegex)) |
| 152 | { |
| 153 | // skip comment lines and blank lines |
| 154 | } |
| 155 | else if (std::regex_match(line, pieces, sectionRegex)) |
| 156 | { |
| 157 | if (pieces.size() == 2) |
| 158 | { |
| 159 | section = pieces[1].str(); |
| 160 | } |
| 161 | } |
| 162 | else if (std::regex_match(line, pieces, valueRegex)) |
| 163 | { |
| 164 | if (pieces.size() == 3) |
| 165 | { |
| 166 | setValue(section, pieces[1].str(), pieces[2].str()); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | }//namespace config |
| 173 | }//namespace network |
| 174 | }//namespace phosphor |