blob: 9b203c2922480e077323aaea5dc80eb9b6ba588a [file] [log] [blame]
Ratan Guptaed123a32017-06-15 09:07:31 +05301#pragma once
2
Manojkiran Edaa879baa2020-06-13 14:39:08 +05303#include <filesystem>
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -07004#include <functional>
William A. Kennington III150753f2022-08-05 15:20:54 -07005#include <optional>
Patrick Venture189d44e2018-07-09 12:30:59 -07006#include <string>
William A. Kennington III25511a12022-08-04 16:32:28 -07007#include <string_view>
Ratan Guptaed123a32017-06-15 09:07:31 +05308#include <unordered_map>
9#include <vector>
Ratan Guptaed123a32017-06-15 09:07:31 +053010
11namespace phosphor
12{
13namespace network
14{
15namespace config
16{
17
William A. Kennington III150753f2022-08-05 15:20:54 -070018/** @brief Compare in (case insensitive) vs expected (sensitive) */
19bool icaseeq(std::string_view in, std::string_view expected) noexcept;
20/** @brief Turns a systemd bool string into a c++ bool */
21std::optional<bool> parseBool(std::string_view in) noexcept;
22
William A. Kennington IIIa520a392022-08-08 12:17:34 -070023namespace fs = std::filesystem;
24
25fs::path pathForIntfConf(const fs::path& dir, std::string_view intf);
26fs::path pathForIntfDev(const fs::path& dir, std::string_view intf);
27
William A. Kennington III25511a12022-08-04 16:32:28 -070028struct string_hash : public std::hash<std::string_view>
29{
30 using is_transparent = void;
31};
32
33using Key = std::string;
Ratan Guptaed123a32017-06-15 09:07:31 +053034using Section = std::string;
William A. Kennington III25511a12022-08-04 16:32:28 -070035using Value = std::string;
36using ValueList = std::vector<Value>;
37using KeyValuesMap =
38 std::unordered_map<Key, ValueList, string_hash, std::equal_to<>>;
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -070039using KeyValuesMapList = std::vector<KeyValuesMap>;
40using SectionMapInt =
41 std::unordered_map<Section, KeyValuesMapList, string_hash, std::equal_to<>>;
42
43class SectionMap : public SectionMapInt
44{
45 public:
46 const std::string* getLastValueString(std::string_view section,
47 std::string_view key) const noexcept;
48 inline auto getValues(std::string_view section, std::string_view key,
49 auto&& conv) const
50 {
51 std::vector<std::invoke_result_t<decltype(conv), const Value&>> values;
52 auto sit = find(section);
53 if (sit == end())
54 {
55 return values;
56 }
57 for (const auto& secv : sit->second)
58 {
59 auto kit = secv.find(key);
60 if (kit == secv.end())
61 {
62 continue;
63 }
64 for (auto v : kit->second)
65 {
66 values.push_back(conv(v));
67 }
68 }
69 return values;
70 }
71 std::vector<std::string> getValueStrings(std::string_view section,
72 std::string_view key) const;
73};
Ratan Guptac27170a2017-11-22 15:44:42 +053074
Ratan Guptaed123a32017-06-15 09:07:31 +053075class Parser
76{
Gunnar Mills57d9c502018-09-14 14:42:34 -050077 public:
78 Parser() = default;
Ratan Guptaed123a32017-06-15 09:07:31 +053079
Gunnar Mills57d9c502018-09-14 14:42:34 -050080 /** @brief Constructor
William A. Kennington III25511a12022-08-04 16:32:28 -070081 * @param[in] filename - Absolute path of the file which will be parsed.
Gunnar Mills57d9c502018-09-14 14:42:34 -050082 */
William A. Kennington III25511a12022-08-04 16:32:28 -070083 Parser(const fs::path& filename);
Ratan Guptaed123a32017-06-15 09:07:31 +053084
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -070085 /** @brief Retrieve the map of all values in the file */
86 inline const SectionMap& getMap() const noexcept
87 {
88 return sections;
89 }
Ratan Guptaed123a32017-06-15 09:07:31 +053090
William A. Kennington IIIbc52d932022-08-18 16:34:02 -070091 /** @brief Determine if there were warnings parsing the file
92 * @return The number of parsing issues in the file
93 */
94 inline const std::vector<std::string>& getWarnings() const noexcept
95 {
96 return warnings;
97 }
98
William A. Kennington IIIa520a392022-08-08 12:17:34 -070099 /** @brief Get the filename last parsed successfully
100 * @return file path
101 */
102 inline const fs::path& getFilename() const noexcept
103 {
104 return filename;
105 }
106
Gunnar Mills57d9c502018-09-14 14:42:34 -0500107 /** @brief Set the file name and parse it.
William A. Kennington III25511a12022-08-04 16:32:28 -0700108 * @param[in] filename - Absolute path of the file.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500109 */
William A. Kennington III25511a12022-08-04 16:32:28 -0700110 void setFile(const fs::path& filename);
Gunnar Mills57d9c502018-09-14 14:42:34 -0500111
112 private:
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700113 fs::path filename;
William A. Kennington III25511a12022-08-04 16:32:28 -0700114 SectionMap sections;
William A. Kennington IIIbc52d932022-08-18 16:34:02 -0700115 std::vector<std::string> warnings;
Ratan Guptaed123a32017-06-15 09:07:31 +0530116};
117
Gunnar Mills57d9c502018-09-14 14:42:34 -0500118} // namespace config
119} // namespace network
120} // namespace phosphor