-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnergyMonitorConfig.cpp
97 lines (75 loc) · 2.37 KB
/
EnergyMonitorConfig.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "EnergyMonitorConfig.h"
#include <fstream>
#include <algorithm>
#include <cctype>
using namespace std;
namespace
{
const string cConfigFilename = "energymonitor.conf";
const int cMaxLineSize = 128;
const char cCommentCharacter = '#';
const string cSplitCharacters = " \t";
const string cPictureOutputConfigName = "PictureOutput1";
}
bool EnergyMonitorConfig::isTestMode() const
{
return !(getPictureOutputFilename().empty());
}
string EnergyMonitorConfig::getPictureOutputFilename() const
{
return getValue(cPictureOutputConfigName);
}
string EnergyMonitorConfig::getValue(const string& key) const
{
string keyLower(key);
transform(keyLower.begin(), keyLower.end(), keyLower.begin(), ::tolower);
TConfigValuesMap::const_iterator iter = myConfigValues.find(keyLower);
return (iter != myConfigValues.end()) ? iter->second : string();
}
void EnergyMonitorConfig::readConfig()
{
ifstream configFile(cConfigFilename.c_str());
if (configFile.is_open())
{
while (!configFile.eof())
{
char line[cMaxLineSize + 1];
line[cMaxLineSize] = '\0';
configFile.getline(line, cMaxLineSize);
string key, value;
if (splitLine(line, key, value))
{
transform(key.begin(), key.end(), key.begin(), ::tolower);
myConfigValues[key] = value;
}
}
}
}
bool EnergyMonitorConfig::splitLine(const string& line, string& key, string& value)
{
size_t keyStartPos = line.find_first_not_of(cSplitCharacters);
size_t keyEndPos = string::npos;
if (keyStartPos != string::npos)
{
keyEndPos = line.find_first_of(cSplitCharacters, keyStartPos);
}
size_t valueStartPos = string::npos;
if (keyEndPos != string::npos)
{
valueStartPos = line.find_first_not_of(cSplitCharacters, keyEndPos);
}
size_t valueEndPos = string::npos;
if (valueStartPos != string::npos)
{
valueEndPos = line.find_last_not_of(cSplitCharacters);
}
bool result = false;
if (valueEndPos != string::npos && valueEndPos >= valueStartPos &&
line.at(keyStartPos) != cCommentCharacter)
{
key = line.substr(keyStartPos, keyEndPos - keyStartPos);
value = line.substr(valueStartPos, valueEndPos - valueStartPos + 1);
result = true;
}
return result;
}