-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cpp
executable file
·284 lines (222 loc) · 6.79 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "Config.h"
#include <dirent.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
//Initialize the config class with the path to a config file
Config::Config(std::string pathToConfigFile)
{
//Use the standard namespace
using namespace std;
path = pathToConfigFile;
isOpen = false;
//Open the files
inputConfigFile.open(path.c_str(), ios::in);
//We don't open the input file because we'll be instead overwriting the entire file so we can just truncate it
//Read the file into our vector
if (inputConfigFile.is_open())
{
isOpen = true;
//Line to hold the line in the file
string line;
//While we aren't at the end of the file
while (inputConfigFile.good())
{
//get the line
getline(inputConfigFile, line);
//and add it to the vector
file.push_back(line);
}
//Close it because we don't need it anymore.
closeFiles();
}
else
{
//Invalid file so tell the user
cerr << "Unable to open file " << pathToConfigFile << endl;
}
}
//Dealloc method
Config::~Config()
{
//Close the files
closeFiles();
isOpen = false;
}
bool Config::open(std::string pathToConfigFile)
{
//Use the standard namespace
using namespace std;
isOpen = false;
path = pathToConfigFile;
//Make sure the input file is closed already
closeFiles();
//Clear our vector
file.clear();
//Open the files
inputConfigFile.open(path.c_str(), ios::in);
//Read the file into our vector
if (inputConfigFile.is_open())
{
isOpen = true;
//Line to hold the line in the file
string line;
//While we aren't at the end of the file
while (inputConfigFile.good())
{
//get the line
getline(inputConfigFile, line);
//and add it to the vector
file.push_back(line);
}
//Close it because we don't need it anymore.
closeFiles();
}
return isOpen;
}
bool Config::open()
{
return isOpen;
}
//Get an element from the config file (the part after the equals) returns "" if element doesn't exist
std::string Config::getElement(std::string element, int start_line, int* element_line)
{
if (!isOpen)
{
std::cerr << "Tried to perform operation on non-open config file!" << std::endl;
return "";
}
//Use the standard namespace
using namespace std;
//Loop through the vector of strings searching for a match
for (int i = start_line; i < file.size();i ++)
{
//Get the line to check from our vector
string line = file[i];
//Check to see if we have found it
unsigned long positionInLine = line.find(element);
//Make sure it exists in the string and is at the 1st (.at(0) ) position
if (positionInLine != string::npos && positionInLine == 0)
{
//We have it
unsigned long positionOfEquals = line.find("=");
//Check to see if it is properly formatted
if (positionOfEquals != string::npos)
{
string fileElement = line.substr(0,positionOfEquals);
if (fileElement == element)
{
//It is properly formatted
//Get the contents after the equals
string contents = line.substr(positionOfEquals + 1, line.size() - positionOfEquals);
if (element_line != NULL)
*element_line = i;
//Return it
return contents;
}
}
}
}
return "";
}
//Set an element in the configuration file (will overwrite any existing element with the same name
void Config::setElement(std::string element, std::string contents)
{
if (!isOpen)
{
std::cerr << "Tried to perform operation on non-open config file!" << std::endl;
return;
}
//Use the standard namespace
using namespace std;
//Combine the two strings to make it formatted to be read
string finalFormat = element + "=" + contents;
long positionToInsertAt = file.size();
//Loop through the vector to see if the element exists
for (int i = 0;i < file.size();i++)
{
//Get the line
string line = file[i];
//Try to find the element
long positionOfElement = line.find(element);
//Check to see if we've found it and it's at the first position
if (positionOfElement != string::npos && positionOfElement == 0)
{
//We have it
//Set our position to insert the element at
positionToInsertAt = i;
//Remove the line in the file
file.erase(file.begin() + positionToInsertAt);
//Add the line
file.insert(file.begin() + positionToInsertAt, finalFormat);
}
}
//Check to see if we have already inserted the line
if (positionToInsertAt == file.size())
{
//We haven't so lets add it to the end
file.push_back(finalFormat);
}
//Now lets write the new file
outputConfigFile.open(path.c_str(), ios::in | ios::trunc /* this last flag is set so we overwrite the previous contents (delete the previous contents and then we add our own */);
//Loop through the vector and replace the contents with the new contents
for (int i = 0;i < file.size();i++)
{
//Write the line to the file
outputConfigFile << file[i];
}
}
//Close the files
void Config::closeFiles()
{
//If the input file hasn't been closed
if (inputConfigFile.is_open())
{
//Close it
inputConfigFile.close();
}
//If the output file hasn't been closed
if (outputConfigFile.is_open())
{
//Close it
outputConfigFile.close();
}
}
bool has_extension(std::string to_check, std::string ext)
{
using namespace std;
if (to_check.size() < ext.size())
return false;
size_t location = to_check.find(ext);
if (location == string::npos)
return false;
if (location != to_check.size()-ext.size())
return false;
return true;
}
int getdir(std::string dir, std::vector<std::string> &files)
{
using namespace std;
DIR *dp;
struct dirent *dirp;
if((dp = opendir(dir.c_str())) == NULL)
{
cout << "Error(" << errno << " : " << strerror(errno) << ") opening " << dir << endl;
return errno;
}
while ((dirp = readdir(dp)) != NULL) {
struct stat st_buf;
string file_name = dir + "/" + string(dirp->d_name);
int status = stat(file_name.c_str(), &st_buf);
if (status != 0)
{
cerr << "Error stat'ing file (" + file_name + "): " << strerror(errno) << endl;
continue;
}
if (S_ISDIR(st_buf.st_mode))
continue;
files.push_back(file_name);
}
closedir(dp);
return 0;
}