-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.cpp
49 lines (35 loc) · 873 Bytes
/
config.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
42
43
44
45
46
47
48
49
#include <fstream>
#include <iostream>
#include "config.hpp"
using namespace std;
Config::Config(int argc, char *argv[])
{
if(argc <= 1)
return; // Nothing to parse
ifstream f{argv[1]};
// Process the config file first
std::string line;
while(getline(f, line))
process(line);
// Then the command line arguments
if(argc >= 2)
{
for(size_t i = 2 ; i < argc ; ++i)
process(argv[i]);
}
// Print everything
for(auto it=_map.begin() ; it != _map.end() ; ++it)
cout << it->first << " = " << it->second << endl;
}
bool Config::process(std::string const& str)
{
if(str[0] == '#') // Comments
return true;
auto eq = str.find('=');
if(eq == str.npos)
return false;
auto key = str.substr(0, eq);
auto value = str.substr(eq + 1);
_map[key] = value;
return true;
}