-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.cpp
48 lines (45 loc) · 1.32 KB
/
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
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <config.h>
#include <cstring>
// Stuff to do with Config files
ConfigFile Config::readConfigFile(std::string file) {
// Setup Vars and open file
std::ifstream ifile(file);
// If file could not be opened, bail with a default config
if(!ifile.is_open()) {
ConfigFile out;
out.values["failed"] = "true";
return out;
}
return readFromStream(ifile);
}
ConfigFile Config::readFromData(char* data) {
std::istringstream ss(std::string(data, strlen(data)));
return readFromStream(ss);
}
ConfigFile Config::readFromStream(std::istream& stream) {
ConfigFile out;
std::string line;
// Loop over every line
while(std::getline(stream, line)) {
if(line == "") { continue; }
// Convert line into a stream for easier handling
std::istringstream is_line(line);
std::string name;
// Get setting name
if(std::getline(is_line, name, '=')) {
std::string value;
// Get setting value
if(std::getline(is_line, value)) {
out.values[name] = value;
}
} else {
std::cerr << "found invalid config line \"" << line <<"\"; attempting to ignore it. this might cause problems\n";
}
}
return out;
}