-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_parser.cpp
42 lines (36 loc) · 1.14 KB
/
config_parser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "config_parser.h"
#include <string>
#include <complex>
#include <vector>
#include <fstream>
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
map<string,string> load_config_file(const string& file_path, const string& sep)
{
ifstream file(file_path);
map<string,string> output;
// Load the file
if(file.is_open()){
string line;
while(getline(file,line)){
// Remove white space
line.erase(remove_if(line.begin(),line.end(), ::isspace),line.end());
if(line[0] == '#' || line.empty())
continue;
// Erase the comments
size_t commentPos = line.find("#");
if(commentPos != string::npos)
line.erase(line.begin()+commentPos,line.end());
// Find the name-value pairs.
size_t delimiterPos = line.find(sep);
string name = line.substr(0,delimiterPos);
string value = line.substr(delimiterPos+sep.length());
output[name] = value;
}
} else {
cerr << "Couldn't open the parameter file for reading.\n";
}
return output;
}