1 #pragma once 2 3 #include <filesystem> 4 #include <string> 5 #include <string_view> 6 #include <unordered_map> 7 #include <vector> 8 9 namespace phosphor 10 { 11 namespace network 12 { 13 namespace config 14 { 15 16 struct string_hash : public std::hash<std::string_view> 17 { 18 using is_transparent = void; 19 }; 20 21 using Key = std::string; 22 using Section = std::string; 23 using Value = std::string; 24 using ValueList = std::vector<Value>; 25 using KeyValuesMap = 26 std::unordered_map<Key, ValueList, string_hash, std::equal_to<>>; 27 using SectionMap = 28 std::unordered_map<Section, KeyValuesMap, string_hash, std::equal_to<>>; 29 30 namespace fs = std::filesystem; 31 32 class Parser 33 { 34 public: 35 Parser() = default; 36 37 /** @brief Constructor 38 * @param[in] filename - Absolute path of the file which will be parsed. 39 */ 40 41 Parser(const fs::path& filename); 42 43 /** @brief Get the values of the given key and section. 44 * @param[in] section - section name. 45 * @param[in] key - key to look for. 46 * @returns The ValueList or nullptr if no key + section exists. 47 */ 48 const ValueList& getValues(std::string_view section, 49 std::string_view key) const noexcept; 50 51 /** @brief Set the file name and parse it. 52 * @param[in] filename - Absolute path of the file. 53 */ 54 void setFile(const fs::path& filename); 55 56 private: 57 SectionMap sections; 58 }; 59 60 } // namespace config 61 } // namespace network 62 } // namespace phosphor 63