summaryrefslogtreecommitdiffstats
path: root/config_parser.hpp
blob: bf5cb2d825ca7084f7150a00a6ffe24f773d5c36 (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
#pragma once

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

namespace phosphor
{
namespace network
{
namespace config
{

using Section = std::string;
using KeyValues =  std::multimap<std::string, std::string>;
namespace fs = std::experimental::filesystem;

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 values associated with the key.
         */

        std::vector<std::string> 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 map of the key and value.
         */

        KeyValues 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, KeyValues> sections;
        fs::path filePath;
};

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