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