blob: 8af4a3483e45b5e281b987ab9c8df5fba9462665 [file] [log] [blame]
Ratan Guptaed123a32017-06-15 09:07:31 +05301#pragma once
2
Manojkiran Edaa879baa2020-06-13 14:39:08 +05303#include <filesystem>
Ratan Guptaed123a32017-06-15 09:07:31 +05304#include <map>
Patrick Venture189d44e2018-07-09 12:30:59 -07005#include <string>
Ratan Guptac27170a2017-11-22 15:44:42 +05306#include <tuple>
Ratan Guptaed123a32017-06-15 09:07:31 +05307#include <unordered_map>
8#include <vector>
Ratan Guptaed123a32017-06-15 09:07:31 +05309
10namespace phosphor
11{
12namespace network
13{
14namespace config
15{
16
17using Section = std::string;
Gunnar Mills57d9c502018-09-14 14:42:34 -050018using KeyValueMap = std::multimap<std::string, std::string>;
Ratan Guptac27170a2017-11-22 15:44:42 +053019using ValueList = std::vector<std::string>;
20
Manojkiran Edaa879baa2020-06-13 14:39:08 +053021namespace fs = std::filesystem;
Ratan Guptaed123a32017-06-15 09:07:31 +053022
Ratan Guptac27170a2017-11-22 15:44:42 +053023enum class ReturnCode
24{
25 SUCCESS = 0x0,
26 SECTION_NOT_FOUND = 0x1,
27 KEY_NOT_FOUND = 0x2,
28};
29
Ratan Guptaed123a32017-06-15 09:07:31 +053030class Parser
31{
Gunnar Mills57d9c502018-09-14 14:42:34 -050032 public:
33 Parser() = default;
Ratan Guptaed123a32017-06-15 09:07:31 +053034
Gunnar Mills57d9c502018-09-14 14:42:34 -050035 /** @brief Constructor
36 * @param[in] fileName - Absolute path of the file which will be parsed.
37 */
Ratan Guptaed123a32017-06-15 09:07:31 +053038
Gunnar Mills57d9c502018-09-14 14:42:34 -050039 Parser(const fs::path& fileName);
Ratan Guptaed123a32017-06-15 09:07:31 +053040
Gunnar Mills57d9c502018-09-14 14:42:34 -050041 /** @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 Guptaed123a32017-06-15 09:07:31 +053047
Gunnar Mills57d9c502018-09-14 14:42:34 -050048 std::tuple<ReturnCode, ValueList> getValues(const std::string& section,
49 const std::string& key);
Ratan Guptaed123a32017-06-15 09:07:31 +053050
Gunnar Mills57d9c502018-09-14 14:42:34 -050051 /** @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 Guptaed123a32017-06-15 09:07:31 +053056
Gunnar Mills57d9c502018-09-14 14:42:34 -050057 void setValue(const std::string& section, const std::string& key,
58 const std::string& value);
Ratan Guptaed123a32017-06-15 09:07:31 +053059
Gunnar Mills57d9c502018-09-14 14:42:34 -050060 /** @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 Guptaed123a32017-06-15 09:07:31 +053089 const std::string& value);
90
Gunnar Mills57d9c502018-09-14 14:42:34 -050091 std::unordered_map<Section, KeyValueMap> sections;
92 fs::path filePath;
Ratan Guptaed123a32017-06-15 09:07:31 +053093};
94
Gunnar Mills57d9c502018-09-14 14:42:34 -050095} // namespace config
96} // namespace network
97} // namespace phosphor