forked from sccn/liblsl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinireader.cpp
More file actions
41 lines (38 loc) · 1.53 KB
/
inireader.cpp
File metadata and controls
41 lines (38 loc) · 1.53 KB
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 "inireader.h"
#include <algorithm>
#include <cctype>
void INI::load(std::istream &infile) {
std::string line;
std::string section;
int linenr = 0;
while (std::getline(infile, line)) {
linenr++;
// Comment / empty line
if (line[0] == ';' || std::all_of(line.cbegin(), line.cend(), ::isspace)) continue;
// Section
if (line[0] == '[') {
auto closingbracket = std::find(line.cbegin(), line.cend(), ']');
if (closingbracket == line.cend())
throw std::runtime_error(
"No closing bracket ] found in line " + std::to_string(linenr));
section = (std::string(line.cbegin() + 1, closingbracket) += '.');
continue;
}
// Key / Value - Pair
auto eqpos = line.find('=');
if (eqpos == std::string::npos)
throw std::runtime_error("No Key-Value pair in line " + std::to_string(linenr));
const char *ws = " \t\r\n";
auto keybegin = line.find_first_not_of(ws), keyend = line.find_last_not_of(ws, eqpos - 1),
valbegin = line.find_first_not_of(ws, eqpos + 1), valend = line.find_last_not_of(ws);
if (keyend == std::string::npos)
throw std::runtime_error("Empty key in line " + std::to_string(linenr));
if (valbegin == std::string::npos || valend == eqpos)
throw std::runtime_error("Empty value in line " + std::to_string(linenr));
auto key = section;
key += line.substr(keybegin, keyend - keybegin + 1);
if (values.find(key) != values.end()) throw std::runtime_error("Duplicate key " + key);
values.insert(
std::make_pair(key, std::string(line.cbegin() + valbegin, line.cbegin() + valend + 1)));
}
}