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 | |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 3 | #include <stdplus/exception.hpp> |
| 4 | #include <stdplus/fd/create.hpp> |
| 5 | #include <stdplus/fd/line.hpp> |
| 6 | #include <utility> |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 7 | |
| 8 | namespace phosphor |
| 9 | { |
| 10 | namespace network |
| 11 | { |
| 12 | namespace config |
| 13 | { |
| 14 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 15 | Parser::Parser(const fs::path& filename) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 16 | { |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 17 | setFile(filename); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 18 | } |
| 19 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 20 | const ValueList& Parser::getValues(std::string_view section, |
| 21 | std::string_view key) const noexcept |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 22 | { |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 23 | static const ValueList empty; |
| 24 | auto sit = sections.find(section); |
| 25 | if (sit == sections.end()) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 26 | { |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 27 | return empty; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 28 | } |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 29 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 30 | auto kit = sit->second.find(key); |
| 31 | if (kit == sit->second.end()) |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 32 | { |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 33 | return empty; |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 34 | } |
| 35 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 36 | return kit->second; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 37 | } |
| 38 | |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 39 | inline bool isspace(char c) noexcept |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 40 | { |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 41 | return c == ' ' || c == '\t'; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 42 | } |
| 43 | |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 44 | inline bool iscomment(char c) noexcept |
| 45 | { |
| 46 | return c == '#' || c == ';'; |
| 47 | } |
| 48 | |
| 49 | static void removePadding(std::string_view& str) noexcept |
| 50 | { |
| 51 | size_t idx = str.size(); |
| 52 | for (; idx > 0 && isspace(str[idx - 1]); idx--) |
| 53 | ; |
| 54 | str.remove_suffix(str.size() - idx); |
| 55 | |
| 56 | idx = 0; |
| 57 | for (; idx < str.size() && isspace(str[idx]); idx++) |
| 58 | ; |
| 59 | str.remove_prefix(idx); |
| 60 | } |
| 61 | |
| 62 | struct Parse |
| 63 | { |
| 64 | SectionMap sections; |
| 65 | KeyValuesMap* section = nullptr; |
| 66 | |
| 67 | void pumpSection(std::string_view line) |
| 68 | { |
| 69 | auto cpos = line.find(']'); |
| 70 | auto s = line.substr(0, cpos); |
| 71 | auto it = sections.find(s); |
| 72 | if (it == sections.end()) |
| 73 | { |
| 74 | std::tie(it, std::ignore) = |
| 75 | sections.emplace(Section(s), KeyValuesMap{}); |
| 76 | } |
| 77 | section = &it->second; |
| 78 | } |
| 79 | |
| 80 | void pumpKV(std::string_view line) |
| 81 | { |
| 82 | auto epos = line.find('='); |
| 83 | if (epos == line.npos) |
| 84 | { |
| 85 | return; |
| 86 | } |
| 87 | if (section == nullptr) |
| 88 | { |
| 89 | return; |
| 90 | } |
| 91 | auto k = line.substr(0, epos); |
| 92 | removePadding(k); |
| 93 | auto v = line.substr(epos + 1); |
| 94 | removePadding(v); |
| 95 | |
| 96 | auto it = section->find(k); |
| 97 | if (it == section->end()) |
| 98 | { |
| 99 | std::tie(it, std::ignore) = section->emplace(Key(k), ValueList{}); |
| 100 | } |
| 101 | it->second.emplace_back(v); |
| 102 | } |
| 103 | |
| 104 | void pump(std::string_view line) |
| 105 | { |
| 106 | for (size_t i = 0; i < line.size(); ++i) |
| 107 | { |
| 108 | auto c = line[i]; |
| 109 | if (iscomment(c)) |
| 110 | { |
| 111 | return; |
| 112 | } |
| 113 | else if (c == '[') |
| 114 | { |
| 115 | return pumpSection(line.substr(i + 1)); |
| 116 | } |
| 117 | else if (!isspace(c)) |
| 118 | { |
| 119 | return pumpKV(line.substr(i)); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | }; |
| 124 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 125 | void Parser::setFile(const fs::path& filename) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 126 | { |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 127 | Parse parse; |
| 128 | |
| 129 | try |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 130 | { |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 131 | auto fd = stdplus::fd::open(filename.c_str(), |
| 132 | stdplus::fd::OpenAccess::ReadOnly); |
| 133 | stdplus::fd::LineReader reader(fd); |
| 134 | while (true) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 135 | { |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 136 | parse.pump(*reader.readLine()); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 137 | } |
| 138 | } |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 139 | catch (...) |
| 140 | { |
| 141 | // TODO: Pass exceptions once callers can handle them |
| 142 | } |
| 143 | |
| 144 | this->sections = std::move(parse.sections); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 145 | } |
| 146 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 147 | } // namespace config |
| 148 | } // namespace network |
| 149 | } // namespace phosphor |