blob: 75ef4a76aa44451ead08b14fd9992ea4fd519bad [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 III150753f2022-08-05 15:20:54 -07004#include <optional>
Patrick Venture189d44e2018-07-09 12:30:59 -07005#include <string>
William A. Kennington III25511a12022-08-04 16:32:28 -07006#include <string_view>
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
William A. Kennington III150753f2022-08-05 15:20:54 -070017/** @brief Compare in (case insensitive) vs expected (sensitive) */
18bool icaseeq(std::string_view in, std::string_view expected) noexcept;
19/** @brief Turns a systemd bool string into a c++ bool */
20std::optional<bool> parseBool(std::string_view in) noexcept;
21
William A. Kennington III25511a12022-08-04 16:32:28 -070022struct string_hash : public std::hash<std::string_view>
23{
24 using is_transparent = void;
25};
26
27using Key = std::string;
Ratan Guptaed123a32017-06-15 09:07:31 +053028using Section = std::string;
William A. Kennington III25511a12022-08-04 16:32:28 -070029using Value = std::string;
30using ValueList = std::vector<Value>;
31using KeyValuesMap =
32 std::unordered_map<Key, ValueList, string_hash, std::equal_to<>>;
33using SectionMap =
34 std::unordered_map<Section, KeyValuesMap, string_hash, std::equal_to<>>;
Ratan Guptac27170a2017-11-22 15:44:42 +053035
Manojkiran Edaa879baa2020-06-13 14:39:08 +053036namespace fs = std::filesystem;
Ratan Guptaed123a32017-06-15 09:07:31 +053037
38class Parser
39{
Gunnar Mills57d9c502018-09-14 14:42:34 -050040 public:
41 Parser() = default;
Ratan Guptaed123a32017-06-15 09:07:31 +053042
Gunnar Mills57d9c502018-09-14 14:42:34 -050043 /** @brief Constructor
William A. Kennington III25511a12022-08-04 16:32:28 -070044 * @param[in] filename - Absolute path of the file which will be parsed.
Gunnar Mills57d9c502018-09-14 14:42:34 -050045 */
Ratan Guptaed123a32017-06-15 09:07:31 +053046
William A. Kennington III25511a12022-08-04 16:32:28 -070047 Parser(const fs::path& filename);
Ratan Guptaed123a32017-06-15 09:07:31 +053048
Gunnar Mills57d9c502018-09-14 14:42:34 -050049 /** @brief Get the values of the given key and section.
50 * @param[in] section - section name.
51 * @param[in] key - key to look for.
William A. Kennington III25511a12022-08-04 16:32:28 -070052 * @returns The ValueList or nullptr if no key + section exists.
Gunnar Mills57d9c502018-09-14 14:42:34 -050053 */
William A. Kennington III25511a12022-08-04 16:32:28 -070054 const ValueList& getValues(std::string_view section,
55 std::string_view key) const noexcept;
Ratan Guptaed123a32017-06-15 09:07:31 +053056
William A. Kennington IIIbc52d932022-08-18 16:34:02 -070057 /** @brief Determine if there were warnings parsing the file
58 * @return The number of parsing issues in the file
59 */
60 inline const std::vector<std::string>& getWarnings() const noexcept
61 {
62 return warnings;
63 }
64
Gunnar Mills57d9c502018-09-14 14:42:34 -050065 /** @brief Set the file name and parse it.
William A. Kennington III25511a12022-08-04 16:32:28 -070066 * @param[in] filename - Absolute path of the file.
Gunnar Mills57d9c502018-09-14 14:42:34 -050067 */
William A. Kennington III25511a12022-08-04 16:32:28 -070068 void setFile(const fs::path& filename);
Gunnar Mills57d9c502018-09-14 14:42:34 -050069
70 private:
William A. Kennington III25511a12022-08-04 16:32:28 -070071 SectionMap sections;
William A. Kennington IIIbc52d932022-08-18 16:34:02 -070072 std::vector<std::string> warnings;
Ratan Guptaed123a32017-06-15 09:07:31 +053073};
74
Gunnar Mills57d9c502018-09-14 14:42:34 -050075} // namespace config
76} // namespace network
77} // namespace phosphor