-
Notifications
You must be signed in to change notification settings - Fork 0
/
Properties.cpp
68 lines (57 loc) · 1.31 KB
/
Properties.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
#include <iostream>
#include <string>
#include <fstream>
#include <locale>
#include <cstdlib>
#include <cassert>
#include "Properties.h"
using namespace std;
void Properties :: load (string filename) {
ifstream in(filename.c_str());
assert(in);
int numline = 0;
while(in){
string line;
getline(in, line);
numline++;
line = trim(line);
if(line.size() > 0){
if(line[0] != '#'){
size_t pos = line.find('=');
if(pos != string::npos){
string a = line.substr(0,pos);
string b = line.substr(pos + 1);
propertyList[trim(a)] = trim(b);
}else{
cout << "Bad formatting of property in line " << numline << endl;
exit(EXIT_FAILURE);
}
}
}
}
in.close();
}
string Properties :: getProperty (string property) {
string value = propertyList[property];
if(value.empty())
propertyList.erase(property);
return value;
}
void Properties :: show () {
map<string, string>::iterator it;
for(it = propertyList.begin(); it != propertyList.end(); it++){
cout << (*it).first << " => " << (*it).second << endl;
}
}
string Properties :: trim (string& input){
string ret = input;
string::iterator p = ret.begin();
while(p != ret.end() && *p == ' '){
p = ret.erase(p);
}
p = ret.end();
while(p != ret.begin() && !isalnum(*(p-1))){
p = ret.erase(p - 1);
}
return ret;
}