summaryrefslogtreecommitdiffstats
path: root/config_parser.hpp
blob: 164d3c35617767f2202cb551a7a1cd8f5bd0eb44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#pragma once

#include <experimental/filesystem>
#include <map>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

namespace phosphor
{
namespace network
{
namespace config
{

using Section = std::string;
using KeyValueMap = std::multimap<std::string, std::string>;
using ValueList = std::vector<std::string>;

namespace fs = std::experimental::filesystem;

enum class ReturnCode
{
    SUCCESS = 0x0,
    SECTION_NOT_FOUND = 0x1,
    KEY_NOT_FOUND = 0x2,
};

class Parser
{
  public:
    Parser() = default;

    /** @brief Constructor
     *  @param[in] fileName - Absolute path of the file which will be parsed.
     */

    Parser(const fs::path& fileName);

    /** @brief Get the values of the given key and section.
     *  @param[in] section - section name.
     *  @param[in] key - key to look for.
     *  @returns the tuple of return code and the
     *           values associated with the key.
     */

    std::tuple<ReturnCode, ValueList> getValues(const std::string& section,
                                                const std::string& key);

    /** @brief Set the value of the given key and section.
     *  @param[in] section - section name.
     *  @param[in] key - key name.
     *  @param[in] value - value.
     */

    void setValue(const std::string& section, const std::string& key,
                  const std::string& value);

    /** @brief Set the file name and parse it.
     *  @param[in] fileName - Absolute path of the file.
     */

    void setFile(const fs::path& fileName);

  private:
    /** @brief Parses the given file and fills the data.
     *  @param[in] stream - inputstream.
     */

    void parse(std::istream& stream);

    /** @brief Get all the key values of the given section.
     *  @param[in] section - section name.
     *  @returns the tuple of return code and the map of (key,value).
     */

    std::tuple<ReturnCode, KeyValueMap> getSection(const std::string& section);

    /** @brief checks that whether the value exist in the
     *         given section.
     *  @param[in] section - section name.
     *  @param[in] key - key name.
     *  @param[in] value - value.
     *  @returns true if exist otherwise false.
     */

    bool isValueExist(const std::string& section, const std::string& key,
                      const std::string& value);

    std::unordered_map<Section, KeyValueMap> sections;
    fs::path filePath;
};

} // namespace config
} // namespace network
} // namespace phosphor
OpenPOWER on IntegriCloud