-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigLoader.h
111 lines (95 loc) · 2.98 KB
/
ConfigLoader.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
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
/*
* Copyright (c) 2024 Quantag IT Solutions GmbH
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
#define RENDER_CIRCUIT_KEY "render.circuit"
#define DEMO_FILE_KEY "demo.file"
#define SOURCE_FOLDER_KEY "source.folder"
#define LOG_LEVEL_KEY "log.level"
#define DEMO_FILE "/home/qbit/qasm/file1.qasm"
#define SOURCE_FOLDER "/var/dap/"
class ConfigLoader {
public:
// Load the properties file and parse it
bool load(const std::string& filename) {
std::ifstream file(filename);
if (!file.is_open()) {
// LOGI( "Failed to open config file: %s", filename);
return false;
}
std::string line;
while (std::getline(file, line)) {
// Ignore empty lines and comments
if (line.empty() || line[0] == '#') {
continue;
}
std::istringstream lineStream(line);
std::string key, value;
// Parse key-value pair
if (std::getline(lineStream, key, '=') && std::getline(lineStream, value)) {
// Trim whitespace from key and value
key = trim(key);
value = trim(value);
properties_[key] = value;
}
}
file.close();
return true;
}
bool isRenderCircuit() {
std::string val = getValue(RENDER_CIRCUIT_KEY);
if (!val.empty()) {
Utils::trim(val);
LOGI("[%s]", val.c_str());
if (!strcmp(val.c_str(), "0"))
return false;
}
return true;
}
int getLogLevel() {
std::string val = getValue(LOG_LEVEL_KEY);
if (!val.empty()) {
return atoi(val.c_str());
}
return 2;
}
std::string getSourceFolder() {
std::string val = getValue(SOURCE_FOLDER_KEY);
if (!val.empty()) {
return val;
}
return SOURCE_FOLDER;
}
std::string getDemoFile() {
std::string val = getValue(DEMO_FILE);
if (!val.empty()) {
return val;
}
return DEMO_FILE;
}
// Get the value associated with a given key
std::string getValue(const std::string& key) const {
auto it = properties_.find(key);
if (it != properties_.end()) {
return it->second;
}
return ""; // Return an empty string if the key is not found
}
private:
std::map<std::string, std::string> properties_;
// Helper function to trim whitespace from start and end of a string
std::string trim(const std::string& str) const {
size_t first = str.find_first_not_of(" \t");
if (first == std::string::npos) return "";
size_t last = str.find_last_not_of(" \t");
return str.substr(first, last - first + 1);
}
};