From 510b0ff02d2d87664113338b6802569ff465dcbd Mon Sep 17 00:00:00 2001 From: Yohai Meiron Date: Sun, 8 Mar 2020 00:59:44 -0500 Subject: [PATCH] started working on simple config file parser --- io.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 io.cpp diff --git a/io.cpp b/io.cpp new file mode 100644 index 0000000..b7894e6 --- /dev/null +++ b/io.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include + +int main() +{ + std::unordered_map dictionary; + std::string file_name = "phigrape.conf"; + std::ifstream file(file_name); + if (!file.good()) { + std::cout << "Not found!" << std::endl; + exit(1); + } + std::string str; + int line_number = 0; + while (std::getline(file, str)) { + line_number++; + auto pos = str.find('#'); + if (pos != std::string::npos) str = str.substr(0, pos); + pos = str.find_first_not_of(" \t"); + if (pos != std::string::npos) str = str.substr(pos, str.size()); + else continue; + pos = str.find_last_not_of(" \t"); + if (pos != std::string::npos) str = str.substr(0, pos+1); + if (str.size() == 0) continue; + pos = str.find_first_of(" \t"); + if (pos == std::string::npos) { + std::cerr << "Error: expected a key-value pair in line " << line_number << " of file " << file_name << std::endl; + exit(1); + } + std::string key = str.substr(0, pos); + pos = str.find_first_not_of(" \t", pos+1); + std::string val = str.substr(pos, str.size()); + dictionary[key] = val; + } + + printf("dictionary[\"more\"] = %s\n", dictionary["eps"].c_str()); +}