blob: 34b2a689eea81ff993d6e6c0655abc7a5af8025c [file] [log] [blame]
Ratan Guptaed123a32017-06-15 09:07:31 +05301#pragma once
2
Patrick Venture189d44e2018-07-09 12:30:59 -07003#include <experimental/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;
Ratan Guptac27170a2017-11-22 15:44:42 +053018using KeyValueMap = std::multimap<std::string, std::string>;
19using ValueList = std::vector<std::string>;
20
Ratan Guptaed123a32017-06-15 09:07:31 +053021namespace fs = std::experimental::filesystem;
22
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{
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 Guptac27170a2017-11-22 15:44:42 +053045 * @returns the tuple of return code and the
46 * values associated with the key.
Ratan Guptaed123a32017-06-15 09:07:31 +053047 */
48
Ratan Guptac27170a2017-11-22 15:44:42 +053049 std::tuple<ReturnCode, ValueList> getValues(
50 const std::string& section,
51 const std::string& key);
Ratan Guptaed123a32017-06-15 09:07:31 +053052
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 Guptac27170a2017-11-22 15:44:42 +053079 * @returns the tuple of return code and the map of (key,value).
Ratan Guptaed123a32017-06-15 09:07:31 +053080 */
81
Ratan Guptac27170a2017-11-22 15:44:42 +053082 std::tuple<ReturnCode, KeyValueMap> getSection(const std::string& section);
Ratan Guptaed123a32017-06-15 09:07:31 +053083
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 Guptac27170a2017-11-22 15:44:42 +053095 std::unordered_map<Section, KeyValueMap> sections;
Ratan Guptaed123a32017-06-15 09:07:31 +053096 fs::path filePath;
97};
98
99}//namespace config
100}//namespce network
101}//namespace phosphor