-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.cpp
More file actions
68 lines (63 loc) · 1.68 KB
/
Config.cpp
File metadata and controls
68 lines (63 loc) · 1.68 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
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
//
// Created by remi on 10/01/25.
//
#include "Config.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
// Taken from https://stackoverflow.com/a/46711735
constexpr uint32_t hash(const std::string_view data) noexcept {
uint32_t hash = 5385;
for (const auto& e: data) {
hash = (hash << 5) + hash + e;
}
return hash;
}
constexpr uint32_t operator ""_(const char* p, size_t) { return hash(p); }
void readConfig(const std::string& file, Config& config) {
std::ifstream stream(file);
std::string line;
while (std::getline(stream, line)) {
std::erase_if(line, isspace);
std::istringstream lineStream(line);
std::string key;
std::string value;
if (!std::getline(lineStream, key, '=') || !std::getline(lineStream, value)) { continue; }
switch (hash(key)) {
case "height"_:
config.height = std::stoi(value);
break;
case "width"_:
config.width = std::stoi(value);
break;
case "alpha"_:
config.alpha = std::stod(value);
break;
case "raysPerPixel"_:
config.raysPerPixel = std::stoi(value);
break;
case "maxBounce"_:
config.maxBounce = std::stoi(value);
break;
case "focusDistance"_:
config.focusDistance = std::stod(value);
break;
default:
std::cerr << "Warning: Unknown config key '" << key << "'\n";
}
}
std::vector<std::pair<std::string, int>> fields {
{"height", config.height},
{"width", config.width},
{"alpha", config.alpha},
{"raysPerPixel", config.raysPerPixel},
{"maxBounce", config.maxBounce},
{"focusDistance", config.focusDistance}
};
for (const auto& [name, field]: fields) {
if (field == -1) {
std::cerr << "Warning: " << name << " has not been set\n";
}
}
}