-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.cpp
81 lines (70 loc) · 2.29 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
73
74
75
76
77
78
79
80
81
// Copyright (c) <2017> <Alexander Basov>
//
// Configuration file contains serialized representation of Device class
//
#include "config.h"
#include <iostream>
#include <fstream>
#include <algorithm>
#include "usb_device.h"
using std::string;
static inline bool isLineOK(const string &s) {
return (s.length() > 0
&& !std::all_of(s.begin(),s.end(),[](char c) { return std::isspace(c); })
&& (s[0] != '#'));
}
static inline bool equal(const string &s1, const string &s2) {
return s2.compare(0, s1.size(), s1) == 0;
}
// TODO: remove trailing spaces
// removes leading spaces
static inline string & trim(string &s)
{
s.erase(s.begin(),find_if_not(s.begin(),s.end(),[](int c){return isspace(c);}));
return s;
}
Device load(const string &configFile) {
std::ifstream inFile(configFile);
Device d;
if (inFile) {
std::string line;
while (std::getline(inFile, line)) {
if (isLineOK(line)) {
size_t eq = line.find('=');
if (eq == string::npos){
continue;
}
string param = line.substr(0, eq);
string value = line.substr(eq + 1, line.length());
value = trim(value);
if (!isLineOK(value)) {
std::cerr << "WARNING: No value is not set for '"
<< param << "'" << std::endl;
continue;
}
if (equal("manufacturer", param)) {
d.setManufacturer(value);
}
else if (equal("product", param)) {
d.setProduct(value);
}
else if (equal("serialNumber", param)) {
d.setSerialNumber(value);
}
else if (equal("idVendor", param)) {
d.setIdVendor(stoi(value, nullptr,16));
}
else if (equal("idProduct", param)) {
d.setIdPorduct(stoi(value, nullptr,16));
}
else {
std::cerr << "unknown parameter [" << param << "]" << std::endl;
}
}
}
}
else {
std::cerr << "Cannot open config file." << std::endl;
}
return d;
}