-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathsettings.cpp
More file actions
106 lines (88 loc) · 2.75 KB
/
Copy pathsettings.cpp
File metadata and controls
106 lines (88 loc) · 2.75 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
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
/*
* This file is part of AdaptiveCpp, an implementation of SYCL and C++ standard
* parallelism for CPUs and GPUs.
*
* Copyright The AdaptiveCpp Contributors
*
* AdaptiveCpp is released under the BSD 2-Clause "Simplified" License.
* See file LICENSE in the project root for full license details.
*/
// SPDX-License-Identifier: BSD-2-Clause
#include "hipSYCL/common/filesystem.hpp"
#include "hipSYCL/common/settings.hpp"
#include <string>
#include <fstream>
namespace hipsycl {
namespace common::settings {
namespace {
std::unordered_map<std::string, std::string> parse_config_file(const std::string& filename){
std::ifstream file{filename};
std::unordered_map<std::string, std::string> result;
if(file.is_open()) {
std::string line;
while (std::getline(file, line)) {
auto equal_pos = line.find("=");
if(equal_pos != std::string::npos) {
std::string key = line.substr(0, equal_pos);
if(equal_pos+1 < line.length()) {
result[key] = line.substr(equal_pos+1);
}
}
}
}
return result;
}
void trim(std::string& str) {
str.erase(0, str.find_first_not_of("\t\n\v\f\r "));
str.erase(str.find_last_not_of("\t\n\v\f\r ") + 1);
}
bool default_allocation_tracking = true;
}
bool get_default_enable_allocation_tracking() {
return __atomic_load_n(&default_allocation_tracking, __ATOMIC_RELAXED);
}
void force_default_enable_allocation_tracking(bool v) {
__atomic_store_n(&default_allocation_tracking, v, __ATOMIC_RELAXED);
}
void settings_config_file::load_file(const std::string& filename) {
auto kv_map = parse_config_file(filename);
for(const auto& entry : kv_map) {
std::string key = entry.first;
std::string value = entry.second;
trim(key);
trim(value);
_values[key] = value;
}
}
settings_config_file::settings_config_file() {
std::string app_filename, app_directory;
common::filesystem::get_this_executable_path(&app_filename, &app_directory);
if(!app_directory.empty()){
std::string acpp_config_file =
common::filesystem::join_path(app_directory, "acpp-config.cfg");
if(common::filesystem::exists(acpp_config_file)) {
load_file(acpp_config_file);
}
if(!app_filename.empty()) {
std::string app_config_file = common::filesystem::join_path(
app_directory, "acpp-config-" + app_filename + ".cfg");
if(common::filesystem::exists(app_config_file)) {
load_file(app_config_file);
}
}
}
}
bool settings_config_file::retrieve_setting(const std::string& key, std::string& value) const {
auto it = _values.find(key);
if(it != _values.end()) {
value = it->second;
return true;
}
return false;
}
settings_config_file& settings_config_file::get() {
static settings_config_file cfg;
return cfg;
}
}
}