Skip to content

Instantly share code, notes, and snippets.

@desulaid
Created January 24, 2019 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save desulaid/57ece8da37930135fdf5c461df0e6edc to your computer and use it in GitHub Desktop.
Save desulaid/57ece8da37930135fdf5c461df0e6edc to your computer and use it in GitHub Desktop.
#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