Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 3 | #include <experimental/filesystem> |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 4 | #include <map> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 5 | #include <string> |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 6 | #include <tuple> |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 7 | #include <unordered_map> |
| 8 | #include <vector> |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 9 | |
| 10 | namespace phosphor |
| 11 | { |
| 12 | namespace network |
| 13 | { |
| 14 | namespace config |
| 15 | { |
| 16 | |
| 17 | using Section = std::string; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 18 | using KeyValueMap = std::multimap<std::string, std::string>; |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 19 | using ValueList = std::vector<std::string>; |
| 20 | |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 21 | namespace fs = std::experimental::filesystem; |
| 22 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 23 | enum class ReturnCode |
| 24 | { |
| 25 | SUCCESS = 0x0, |
| 26 | SECTION_NOT_FOUND = 0x1, |
| 27 | KEY_NOT_FOUND = 0x2, |
| 28 | }; |
| 29 | |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 30 | class Parser |
| 31 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 32 | public: |
| 33 | Parser() = default; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 34 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 35 | /** @brief Constructor |
| 36 | * @param[in] fileName - Absolute path of the file which will be parsed. |
| 37 | */ |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 38 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 39 | Parser(const fs::path& fileName); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 40 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 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 | */ |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 47 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 48 | std::tuple<ReturnCode, ValueList> getValues(const std::string& section, |
| 49 | const std::string& key); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 50 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 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 | */ |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 56 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 57 | void setValue(const std::string& section, const std::string& key, |
| 58 | const std::string& value); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 59 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 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, |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 89 | const std::string& value); |
| 90 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 91 | std::unordered_map<Section, KeyValueMap> sections; |
| 92 | fs::path filePath; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 93 | }; |
| 94 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 95 | } // namespace config |
| 96 | } // namespace network |
| 97 | } // namespace phosphor |