-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.cpp
72 lines (58 loc) · 1.54 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "config.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <algorithm>
#define R_NO_REMAP
#include <Rinternals.h>
// trim from start (in place)
inline void ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
return !std::isspace(ch);
}));
}
// trim from end (in place)
inline void rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
return !std::isspace(ch);
}).base(), s.end());
}
// trim from both ends (in place)
inline void trim(std::string &s) {
ltrim(s);
rtrim(s);
}
Config::Config(const fs::path& path) {
std::ifstream description_file(path);
if(!description_file) {
Rf_error("No configuration file %s\n", path.c_str());
}
std::string line;
while(std::getline(description_file, line)) {
std::istringstream is_line(line);
std::string key;
if(std::getline(is_line, key, '=')) {
std::string value;
if (key[0] == '#')
continue;
if (std::getline(is_line, value))
{
trim(key);
trim(value);
config[key] = value;
}
}
}
}
void Config::write(const fs::path& filename) {
std::ofstream file(filename,std::ofstream::out | std::ofstream::trunc);
if(!file) {
Rf_error("Impossible to open for write config file %s\n", filename.c_str());
}
for(auto& it : config) {
file << it.first << "=" << it.second << "\n";
}
file << std::flush;
}
Config::Config(std::unordered_map<std::string, std::string>&& config) : config(config){
}