blob: 30651628599606c1ed857c572c36c6fb9ee699b5 [file] [log] [blame]
Ratan Guptaed123a32017-06-15 09:07:31 +05301#pragma once
2
Manojkiran Edaa879baa2020-06-13 14:39:08 +05303#include <filesystem>
Patrick Venture189d44e2018-07-09 12:30:59 -07004#include <string>
William A. Kennington III25511a12022-08-04 16:32:28 -07005#include <string_view>
Ratan Guptaed123a32017-06-15 09:07:31 +05306#include <unordered_map>
7#include <vector>
Ratan Guptaed123a32017-06-15 09:07:31 +05308
9namespace phosphor
10{
11namespace network
12{
13namespace config
14{
15
William A. Kennington III25511a12022-08-04 16:32:28 -070016struct string_hash : public std::hash<std::string_view>
17{
18 using is_transparent = void;
19};
20
21using Key = std::string;
Ratan Guptaed123a32017-06-15 09:07:31 +053022using Section = std::string;
William A. Kennington III25511a12022-08-04 16:32:28 -070023using Value = std::string;
24using ValueList = std::vector<Value>;
25using KeyValuesMap =
26 std::unordered_map<Key, ValueList, string_hash, std::equal_to<>>;
27using SectionMap =
28 std::unordered_map<Section, KeyValuesMap, string_hash, std::equal_to<>>;
Ratan Guptac27170a2017-11-22 15:44:42 +053029
Manojkiran Edaa879baa2020-06-13 14:39:08 +053030namespace fs = std::filesystem;
Ratan Guptaed123a32017-06-15 09:07:31 +053031
32class Parser
33{
Gunnar Mills57d9c502018-09-14 14:42:34 -050034 public:
35 Parser() = default;
Ratan Guptaed123a32017-06-15 09:07:31 +053036
Gunnar Mills57d9c502018-09-14 14:42:34 -050037 /** @brief Constructor
William A. Kennington III25511a12022-08-04 16:32:28 -070038 * @param[in] filename - Absolute path of the file which will be parsed.
Gunnar Mills57d9c502018-09-14 14:42:34 -050039 */
Ratan Guptaed123a32017-06-15 09:07:31 +053040
William A. Kennington III25511a12022-08-04 16:32:28 -070041 Parser(const fs::path& filename);
Ratan Guptaed123a32017-06-15 09:07:31 +053042
Gunnar Mills57d9c502018-09-14 14:42:34 -050043 /** @brief Get the values of the given key and section.
44 * @param[in] section - section name.
45 * @param[in] key - key to look for.
William A. Kennington III25511a12022-08-04 16:32:28 -070046 * @returns The ValueList or nullptr if no key + section exists.
Gunnar Mills57d9c502018-09-14 14:42:34 -050047 */
William A. Kennington III25511a12022-08-04 16:32:28 -070048 const ValueList& getValues(std::string_view section,
49 std::string_view key) const noexcept;
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 */
Gunnar Mills57d9c502018-09-14 14:42:34 -050056 void setValue(const std::string& section, const std::string& key,
57 const std::string& value);
Ratan Guptaed123a32017-06-15 09:07:31 +053058
Gunnar Mills57d9c502018-09-14 14:42:34 -050059 /** @brief Set the file name and parse it.
William A. Kennington III25511a12022-08-04 16:32:28 -070060 * @param[in] filename - Absolute path of the file.
Gunnar Mills57d9c502018-09-14 14:42:34 -050061 */
William A. Kennington III25511a12022-08-04 16:32:28 -070062 void setFile(const fs::path& filename);
Gunnar Mills57d9c502018-09-14 14:42:34 -050063
64 private:
William A. Kennington III25511a12022-08-04 16:32:28 -070065 SectionMap sections;
Ratan Guptaed123a32017-06-15 09:07:31 +053066};
67
Gunnar Mills57d9c502018-09-14 14:42:34 -050068} // namespace config
69} // namespace network
70} // namespace phosphor