started working on simple config file parser
This commit is contained in:
parent
63a39cc9c8
commit
510b0ff02d
1 changed files with 40 additions and 0 deletions
40
io.cpp
Normal file
40
io.cpp
Normal 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());
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue