forked from albertz/commandergenius
-
Notifications
You must be signed in to change notification settings - Fork 250
/
options.cpp
183 lines (144 loc) · 4.61 KB
/
options.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/***************************************************************************
alienBlaster
Copyright (C) 2004
Paul Grathwohl, Arne Hormann, Daniel Kuehn, Soenke Schwardt
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
***************************************************************************/
using namespace std;
#include "options.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <sstream>
Options::Options(const string lFilename) {
if (lFilename == "") {
cout << "Options: no filename given!" << endl;
filename = string("config");
} else {
// save the filename for later use
filename = lFilename;
}
// open the file for reading
ifstream inputFile (filename.c_str(), ios::in);
// TODO FIXME ordentliches Filelocking machen
// fcntl(inputFile.rdbuf()->fd(), F_SETLKW, fileLock(F_RDLCK));
if (inputFile.good()) {
// while still data there
while (!inputFile.eof()) {
string newLine;
getline( inputFile, newLine );
// search delimiter
unsigned int pos = newLine.find("=");
// comment line or no delimiter found
if (( newLine[0] != '#' ) && ( pos != string::npos )) {
string key = newLine.substr(0,pos);
string val = newLine.substr(pos+1);
keymap[ key ] = val;
}
}
}
else {
cout << "Options: Error while opening " << filename << endl;
}
// TODO FIXME ordentliches Filelocking machen
// fcntl(inputFile.rdbuf()->fd(), F_SETLKW, fileLock(F_UNLCK));
inputFile.close();
// if (inputFile.fail()) {
// cout << "Options: Error while closing " << filename << endl;
// }
}
Options::~Options() {
;
}
// flock* Options::fileLock(const short type) {
// static flock ret ;
// ret.l_type = type ;
// ret.l_start = 0 ;
// ret.l_whence = SEEK_SET ;
// ret.l_len = 0 ;
// ret.l_pid = getpid() ;
// return &ret ;
// }
int Options::saveFile( const string lFilename ) {
// alternative filename given ?
if (lFilename != "") {
filename = lFilename;
}
// delete original options-file
remove(filename.c_str());
ofstream outputFile( filename.c_str(), ios::out );
// TODO FIXME ordentliches Filelocking machen
// fcntl(outputFile.rdbuf()->fd(), F_SETLKW, fileLock(F_WRLCK));
if (outputFile.good()) {
map< string, string >::const_iterator iter;
for(iter = keymap.begin(); iter != keymap.end(); iter++){
outputFile << iter->first << "=" << iter->second << endl;
}
}
else {
cout << "Options::saveFile(): error while opening file " << filename << endl;
return -1;
}
// TODO FIXME ordentliches Filelocking machen
// fcntl(outputFile.rdbuf()->fd(), F_SETLKW, fileLock(F_UNLCK));
outputFile.close();
if (!outputFile.good()) {
cout << "Options::saveFile(): error while closing file " << filename << endl;
return -2;
}
return 0; // everything's fine
}
bool Options::exist(const string keyword) {
return (keymap.find(keyword) != keymap.end());
}
bool Options::getStr(const string keyword, string &val) {
if (exist(keyword)) {
val = keymap[keyword];
return true;
}
return false;
}
bool Options::getInt(const string keyword, int &val) {
if (exist(keyword)) {
val = atoi( keymap[keyword].c_str() );
return true;
}
return false;
}
bool Options::getUInt(const string keyword, unsigned int &val) {
if (exist(keyword)) {
val = atoll( keymap[keyword].c_str() );
return true;
}
return false;
}
bool Options::setStr(const string newValue, const string keyword) {
keymap[keyword] = newValue;
return true;
}
bool Options::setInt(const int newValue, const string keyword) {
keymap[keyword] = asString( newValue );
return true;
}
bool Options::setUInt(const unsigned int newValue, const string keyword) {
keymap[keyword] = asString( newValue );
return true;
}
void Options::printall() {
map< string, string >::const_iterator iter;
for(iter = keymap.begin(); iter != keymap.end(); iter++){
cout << iter->first << "=" << iter->second << endl;
}
}