blob: 7ff20baf38ecfb304830124a1ba843b65f461e73 [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 IIIa520a392022-08-08 12:17:34 -070022namespace fs = std::filesystem;
23
24fs::path pathForIntfConf(const fs::path& dir, std::string_view intf);
25fs::path pathForIntfDev(const fs::path& dir, std::string_view intf);
26
William A. Kennington III25511a12022-08-04 16:32:28 -070027struct string_hash : public std::hash<std::string_view>
28{
29 using is_transparent = void;
30};
31
32using Key = std::string;
Ratan Guptaed123a32017-06-15 09:07:31 +053033using Section = std::string;
William A. Kennington III25511a12022-08-04 16:32:28 -070034using Value = std::string;
35using ValueList = std::vector<Value>;
36using KeyValuesMap =
37 std::unordered_map<Key, ValueList, string_hash, std::equal_to<>>;
38using SectionMap =
39 std::unordered_map<Section, KeyValuesMap, string_hash, std::equal_to<>>;
Ratan Guptac27170a2017-11-22 15:44:42 +053040
Ratan Guptaed123a32017-06-15 09:07:31 +053041class Parser
42{
Gunnar Mills57d9c502018-09-14 14:42:34 -050043 public:
44 Parser() = default;
Ratan Guptaed123a32017-06-15 09:07:31 +053045
Gunnar Mills57d9c502018-09-14 14:42:34 -050046 /** @brief Constructor
William A. Kennington III25511a12022-08-04 16:32:28 -070047 * @param[in] filename - Absolute path of the file which will be parsed.
Gunnar Mills57d9c502018-09-14 14:42:34 -050048 */
William A. Kennington III25511a12022-08-04 16:32:28 -070049 Parser(const fs::path& filename);
Ratan Guptaed123a32017-06-15 09:07:31 +053050
Gunnar Mills57d9c502018-09-14 14:42:34 -050051 /** @brief Get the values of the given key and section.
52 * @param[in] section - section name.
53 * @param[in] key - key to look for.
William A. Kennington III25511a12022-08-04 16:32:28 -070054 * @returns The ValueList or nullptr if no key + section exists.
Gunnar Mills57d9c502018-09-14 14:42:34 -050055 */
William A. Kennington III25511a12022-08-04 16:32:28 -070056 const ValueList& getValues(std::string_view section,
57 std::string_view key) const noexcept;
Ratan Guptaed123a32017-06-15 09:07:31 +053058
William A. Kennington IIIbc52d932022-08-18 16:34:02 -070059 /** @brief Determine if there were warnings parsing the file
60 * @return The number of parsing issues in the file
61 */
62 inline const std::vector<std::string>& getWarnings() const noexcept
63 {
64 return warnings;
65 }
66
William A. Kennington IIIa520a392022-08-08 12:17:34 -070067 /** @brief Get the filename last parsed successfully
68 * @return file path
69 */
70 inline const fs::path& getFilename() const noexcept
71 {
72 return filename;
73 }
74
Gunnar Mills57d9c502018-09-14 14:42:34 -050075 /** @brief Set the file name and parse it.
William A. Kennington III25511a12022-08-04 16:32:28 -070076 * @param[in] filename - Absolute path of the file.
Gunnar Mills57d9c502018-09-14 14:42:34 -050077 */
William A. Kennington III25511a12022-08-04 16:32:28 -070078 void setFile(const fs::path& filename);
Gunnar Mills57d9c502018-09-14 14:42:34 -050079
80 private:
William A. Kennington IIIa520a392022-08-08 12:17:34 -070081 fs::path filename;
William A. Kennington III25511a12022-08-04 16:32:28 -070082 SectionMap sections;
William A. Kennington IIIbc52d932022-08-18 16:34:02 -070083 std::vector<std::string> warnings;
Ratan Guptaed123a32017-06-15 09:07:31 +053084};
85
Gunnar Mills57d9c502018-09-14 14:42:34 -050086} // namespace config
87} // namespace network
88} // namespace phosphor