-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.h
71 lines (55 loc) · 1.94 KB
/
settings.h
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
#ifndef SETTINGSFILEHANDLER_H_
#define SETTINGSFILEHANDLER_H_
#include <limits>
char* getSetting(char* name, char* path);
void writeSetting(char* name, char* value, char* path);
std::string get_hash_from_line(const std::string& line) {
auto pos = line.find(":");
if (pos != std::string::npos) {
return line.substr(0, pos); // Return only the hash part
}
return ""; // Return empty string if format is invalid
}
std::string search_hex_hash(const std::string& file_path, const std::string& target_hash) {
std::ifstream file(file_path);
if (!file.is_open()) {
throw std::runtime_error("Failed to open file: " + file_path);
}
file.seekg(0, std::ios::end);
std::streampos file_size = file.tellg();
std::streampos start = 0;
std::streampos end = file_size;
std::string line;
while (start <= end) {
std::streampos mid = (start + end) / 2;
file.seekg(mid);
// Move to the beginning of the current line
if (mid > 0) {
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
if (!std::getline(file, line)) {
break; // End of file reached
}
std::string hash = get_hash_from_line(line);
if (hash == target_hash) {
return line.substr(hash.length() + 2); // Return the text part after ": "
} else if (hash < target_hash) {
start = file.tellg(); // Move to the right
} else {
end = mid - 1; // Move to the left
}
// Handle special case where the file has only one line
if (start == 0 && end == 0) {
file.seekg(0);
if (std::getline(file, line)) {
hash = get_hash_from_line(line);
if (hash == target_hash) {
return line.substr(hash.length() + 2);
}
}
break;
}
}
return ""; // Hash not found
}
#endif