Created
January 24, 2019 18:25
-
-
Save desulaid/57ece8da37930135fdf5c461df0e6edc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef INI_HPP | |
#define INI_HPP | |
#include <map> | |
#include <regex> | |
#include <string> | |
#include <fstream> | |
#include <iostream> | |
class ini_document { | |
public: | |
ini_document(std::string path) { | |
file.open(path); | |
if (!file.is_open()) { | |
return; | |
} | |
const std::regex re(TEMPLATE_KEY_VALUE); | |
std::string line; | |
while (std::getline(file, line)) { | |
std::smatch match; | |
if (std::regex_match(line, match, re)) { | |
content.insert(content.begin(), | |
std::pair<std::string, std::string>(match[2], match[4])); | |
} | |
} | |
} | |
private: | |
std::ifstream file; | |
/* | |
* <key> = "<value>" | |
* <key> = <value> | |
* <key> = <value> | |
* <key>=<value> | |
*/ | |
constexpr static char | |
*TEMPLATE_KEY_VALUE = "^((\\w+)\\ *\\=\\ *)(\\\"{0,1}(\\w+)\\\"{0,1})"; | |
/* | |
* All values from the file are saved as a string. | |
*/ | |
using content_t = std::map<std::string, std::string>; | |
content_t content; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment