started working on simple config file parser

This commit is contained in:
Yohai Meiron 2020-03-08 00:59:44 -05:00
parent 63a39cc9c8
commit 510b0ff02d

40
io.cpp Normal file
View file

@ -0,0 +1,40 @@
#include <unordered_map>
#include <string>
#include <cstdio>
#include <iostream>
#include <fstream>
int main()
{
std::unordered_map<std::string,std::string> 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());
}