Improved the new-style config file and its reader

This commit is contained in:
Yohai Meiron 2020-03-15 21:35:53 -04:00
parent 90f070d1fb
commit cd282cc406
3 changed files with 119 additions and 21 deletions

28
io.cpp
View file

@ -4,6 +4,16 @@
#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;
@ -19,22 +29,22 @@ int main()
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);
str = strip(str);
if (str.size() == 0) continue;
pos = str.find_first_of(" \t");
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 = str.substr(0, pos);
std::string key = strip(str.substr(0, pos));
pos = str.find_first_not_of(" \t", pos+1);
std::string val = str.substr(pos, str.size());
std::string val = strip(str.substr(pos, str.size()));
dictionary[key] = val;
}
printf("dictionary[\"more\"] = %s\n", dictionary["eps"].c_str());
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());
}
}