-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.cpp
More file actions
40 lines (34 loc) · 1.24 KB
/
Copy pathconfig.cpp
File metadata and controls
40 lines (34 loc) · 1.24 KB
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
#include "config.h"
#include <fstream>
#include <sstream>
#include <iostream>
Config::Config(const std::string& filename) : maxAttempts(3), timeLimit(30) {
std::ifstream file(filename);
if (!file.is_open()) {
std::ofstream out(filename);
out << "MAX_ATTEMPTS=3\nTIME_LIMIT=30\n";
out.close();
return;
}
std::string line;
while (std::getline(file, line)) {
line.erase(0, line.find_first_not_of(" \t"));
line.erase(line.find_last_not_of(" \t") + 1);
if (line.empty() || line[0] == '#') continue;
size_t pos = line.find('=');
if (pos == std::string::npos) continue;
std::string key = line.substr(0, pos);
std::string value = line.substr(pos + 1);
value.erase(0, value.find_first_not_of(" \t"));
value.erase(value.find_last_not_of(" \t") + 1);
try {
if (key.find("MAX_ATTEMPTS") != std::string::npos) {
maxAttempts = std::stoi(value);
} else if (key.find("TIME_LIMIT") != std::string::npos) {
timeLimit = std::stoi(value);
}
} catch (...) {
// Use defaults
}
}
}