1 #pragma once 2 3 #include <filesystem> 4 #include <map> 5 #include <string> 6 #include <tuple> 7 #include <unordered_map> 8 #include <vector> 9 10 namespace phosphor 11 { 12 namespace network 13 { 14 namespace config 15 { 16 17 using Section = std::string; 18 using KeyValueMap = std::multimap<std::string, std::string>; 19 using ValueList = std::vector<std::string>; 20 21 namespace fs = std::filesystem; 22 23 enum class ReturnCode 24 { 25 SUCCESS = 0x0, 26 SECTION_NOT_FOUND = 0x1, 27 KEY_NOT_FOUND = 0x2, 28 }; 29 30 class Parser 31 { 32 public: 33 Parser() = default; 34 35 /** @brief Constructor 36 * @param[in] fileName - Absolute path of the file which will be parsed. 37 */ 38 39 Parser(const fs::path& fileName); 40 41 /** @brief Get the values of the given key and section. 42 * @param[in] section - section name. 43 * @param[in] key - key to look for. 44 * @returns the tuple of return code and the 45 * values associated with the key. 46 */ 47 48 std::tuple<ReturnCode, ValueList> getValues(const std::string& section, 49 const std::string& key); 50 51 /** @brief Set the value of the given key and section. 52 * @param[in] section - section name. 53 * @param[in] key - key name. 54 * @param[in] value - value. 55 */ 56 57 void setValue(const std::string& section, const std::string& key, 58 const std::string& value); 59 60 /** @brief Set the file name and parse it. 61 * @param[in] fileName - Absolute path of the file. 62 */ 63 64 void setFile(const fs::path& fileName); 65 66 private: 67 /** @brief Parses the given file and fills the data. 68 * @param[in] stream - inputstream. 69 */ 70 71 void parse(std::istream& stream); 72 73 /** @brief Get all the key values of the given section. 74 * @param[in] section - section name. 75 * @returns the tuple of return code and the map of (key,value). 76 */ 77 78 std::tuple<ReturnCode, KeyValueMap> getSection(const std::string& section); 79 80 /** @brief checks that whether the value exist in the 81 * given section. 82 * @param[in] section - section name. 83 * @param[in] key - key name. 84 * @param[in] value - value. 85 * @returns true if exist otherwise false. 86 */ 87 88 bool isValueExist(const std::string& section, const std::string& key, 89 const std::string& value); 90 91 std::unordered_map<Section, KeyValueMap> sections; 92 fs::path filePath; 93 }; 94 95 } // namespace config 96 } // namespace network 97 } // namespace phosphor 98