phigrape/io.cpp
2020-03-15 21:35:53 -04:00

50 lines
1.6 KiB
C++

#include <unordered_map>
#include <string>
#include <cstdio>
#include <iostream>
#include <fstream>
std::string strip(const std::string str)
{
std::string str_new = str;
auto pos = str_new.find_first_not_of(" \t");
if (pos != std::string::npos) str_new = str_new.substr(pos, str_new.size());
pos = str_new.find_last_not_of(" \t");
if (pos != std::string::npos) str_new = str_new.substr(0, pos+1);
return str_new;
}
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);
str = strip(str);
if (str.size() == 0) continue;
pos = str.find_first_of("=");
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 = strip(str.substr(0, pos));
pos = str.find_first_not_of(" \t", pos+1);
std::string val = strip(str.substr(pos, str.size()));
dictionary[key] = val;
}
for (auto key_value : dictionary) {
auto key = key_value.first;
auto value = key_value.second;
printf("dictionary[\"%s\"] = \"%s\"\n", key.c_str(), value.c_str());
}
}