config_parser: Cleanups and modern c++ standards
This was exposing many functions and semantics that are unused by the
application. The goal is to simplify the interface and convert to using
types like `string_view` and referenceable lists where possible.
Change-Id: I4cba6326f9a96a943d384165e656f8589f931959
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/config_parser.hpp b/src/config_parser.hpp
index 8af4a34..3065162 100644
--- a/src/config_parser.hpp
+++ b/src/config_parser.hpp
@@ -1,9 +1,8 @@
#pragma once
#include <filesystem>
-#include <map>
#include <string>
-#include <tuple>
+#include <string_view>
#include <unordered_map>
#include <vector>
@@ -14,82 +13,56 @@
namespace config
{
+struct string_hash : public std::hash<std::string_view>
+{
+ using is_transparent = void;
+};
+
+using Key = std::string;
using Section = std::string;
-using KeyValueMap = std::multimap<std::string, std::string>;
-using ValueList = std::vector<std::string>;
+using Value = std::string;
+using ValueList = std::vector<Value>;
+using KeyValuesMap =
+ std::unordered_map<Key, ValueList, string_hash, std::equal_to<>>;
+using SectionMap =
+ std::unordered_map<Section, KeyValuesMap, string_hash, std::equal_to<>>;
namespace fs = std::filesystem;
-enum class ReturnCode
-{
- SUCCESS = 0x0,
- SECTION_NOT_FOUND = 0x1,
- KEY_NOT_FOUND = 0x2,
-};
-
class Parser
{
public:
Parser() = default;
/** @brief Constructor
- * @param[in] fileName - Absolute path of the file which will be parsed.
+ * @param[in] filename - Absolute path of the file which will be parsed.
*/
- Parser(const fs::path& fileName);
+ 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 tuple of return code and the
- * values associated with the key.
+ * @returns The ValueList or nullptr if no key + section exists.
*/
-
- std::tuple<ReturnCode, ValueList> getValues(const std::string& section,
- const std::string& key);
+ const ValueList& getValues(std::string_view section,
+ std::string_view key) const noexcept;
/** @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.
+ * @param[in] filename - Absolute path of the file.
*/
-
- void setFile(const fs::path& fileName);
+ 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 tuple of return code and the map of (key,value).
- */
-
- std::tuple<ReturnCode, KeyValueMap> 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, KeyValueMap> sections;
- fs::path filePath;
+ SectionMap sections;
};
} // namespace config