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; |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 18 | using KeyValueMap = std::multimap<std::string, std::string>; |
| 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 | { |
| 32 | public: |
| 33 | |
| 34 | Parser() = default; |
| 35 | |
| 36 | /** @brief Constructor |
| 37 | * @param[in] fileName - Absolute path of the file which will be parsed. |
| 38 | */ |
| 39 | |
| 40 | Parser(const fs::path& fileName); |
| 41 | |
| 42 | /** @brief Get the values of the given key and section. |
| 43 | * @param[in] section - section name. |
| 44 | * @param[in] key - key to look for. |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 45 | * @returns the tuple of return code and the |
| 46 | * values associated with the key. |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 47 | */ |
| 48 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 49 | std::tuple<ReturnCode, ValueList> getValues( |
| 50 | const std::string& section, |
| 51 | const std::string& key); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 52 | |
| 53 | /** @brief Set the value of the given key and section. |
| 54 | * @param[in] section - section name. |
| 55 | * @param[in] key - key name. |
| 56 | * @param[in] value - value. |
| 57 | */ |
| 58 | |
| 59 | void setValue(const std::string& section, const std::string& key, |
| 60 | const std::string& value); |
| 61 | |
| 62 | |
| 63 | /** @brief Set the file name and parse it. |
| 64 | * @param[in] fileName - Absolute path of the file. |
| 65 | */ |
| 66 | |
| 67 | void setFile(const fs::path& fileName); |
| 68 | |
| 69 | private: |
| 70 | |
| 71 | /** @brief Parses the given file and fills the data. |
| 72 | * @param[in] stream - inputstream. |
| 73 | */ |
| 74 | |
| 75 | void parse(std::istream& stream); |
| 76 | |
| 77 | /** @brief Get all the key values of the given section. |
| 78 | * @param[in] section - section name. |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 79 | * @returns the tuple of return code and the map of (key,value). |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 80 | */ |
| 81 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 82 | std::tuple<ReturnCode, KeyValueMap> getSection(const std::string& section); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 83 | |
| 84 | /** @brief checks that whether the value exist in the |
| 85 | * given section. |
| 86 | * @param[in] section - section name. |
| 87 | * @param[in] key - key name. |
| 88 | * @param[in] value - value. |
| 89 | * @returns true if exist otherwise false. |
| 90 | */ |
| 91 | |
| 92 | bool isValueExist(const std::string& section, const std::string& key, |
| 93 | const std::string& value); |
| 94 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 95 | std::unordered_map<Section, KeyValueMap> sections; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 96 | fs::path filePath; |
| 97 | }; |
| 98 | |
| 99 | }//namespace config |
| 100 | }//namespce network |
| 101 | }//namespace phosphor |