Line | Branch | Exec | Source |
---|---|---|---|
1 | #include "ConfigFileReader.h" | ||
2 | #include <filesystem> | ||
3 | #include <cstdlib> | ||
4 | #include <sstream> | ||
5 | |||
6 | namespace iguana { | ||
7 | |||
8 | 42 | ConfigFileReader::ConfigFileReader(std::string_view name) | |
9 | 42 | : Object(name) | |
10 | { | ||
11 | // the algorithms need to know where the config files are; | ||
12 | // first, add config files from installation prefix; since this | ||
13 | // is added first, it's the lowest priority in the configuration | ||
14 | // search path | ||
15 |
2/4✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
|
42 | AddDirectory(GetConfigInstallationPrefix()); |
16 | // next, add `IGUANA_CONFIG_PATH` paths, which provides: | ||
17 | // - user override of the configuration path(s) | ||
18 | // - a fallback, if `GetConfigInstallationPrefix` is wrong, which happens | ||
19 | // if the Iguana installation is relocated | ||
20 | 42 | auto user_paths_env_var = std::getenv("IGUANA_CONFIG_PATH"); | |
21 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
|
42 | if(user_paths_env_var != nullptr) { |
22 | ✗ | std::istringstream user_paths_stream(user_paths_env_var); | |
23 | std::string user_path_token; | ||
24 | decltype(m_directories) user_paths; | ||
25 | ✗ | while(getline(user_paths_stream, user_path_token, ':')) // tokenize `IGUANA_CONFIG_PATH` | |
26 | ✗ | user_paths.push_front(user_path_token); | |
27 | ✗ | for(auto const& user_path : user_paths) // add the paths in the correct order | |
28 | ✗ | AddDirectory(user_path); | |
29 | // after these default paths have been added, the user | ||
30 | // may still override by calling `AddDirectory` etc. themselves | ||
31 | ✗ | } | |
32 | 42 | } | |
33 | |||
34 | 42 | std::string ConfigFileReader::GetConfigInstallationPrefix() | |
35 | { | ||
36 | 42 | return IGUANA_ETCDIR; | |
37 | } | ||
38 | |||
39 | 84 | void ConfigFileReader::AddDirectory(std::string const& dir) | |
40 | { | ||
41 |
2/2✓ Branch 0 taken 51 times.
✓ Branch 1 taken 33 times.
|
84 | if(dir == "") |
42 | return; // handle unset directory name | ||
43 |
1/2✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
|
51 | m_log->Trace("Add directory {}", dir); |
44 | 51 | m_directories.push_front(dir); | |
45 | } | ||
46 | |||
47 | 81 | void ConfigFileReader::AddFile(std::string const& name) | |
48 | { | ||
49 |
2/2✓ Branch 0 taken 33 times.
✓ Branch 1 taken 48 times.
|
81 | if(name == "") |
50 | 33 | return; // handle unset file name | |
51 | 48 | auto full_name = FindFile(name); | |
52 |
1/2✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
|
45 | m_log->Trace(" ===> Add file {}", full_name); |
53 |
1/2✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
|
45 | m_files.push_front(full_name); |
54 | } | ||
55 | |||
56 | 6 | void ConfigFileReader::PrintDirectories(Logger::Level const level) | |
57 | { | ||
58 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if(m_log->GetLevel() <= level) { |
59 | 6 | m_log->Print(level, "{:=^60}", " Configuration file search path order: "); | |
60 | 6 | m_log->Print(level, " - ./"); | |
61 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
|
18 | for(auto const& dir : m_directories) |
62 |
3/4✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 6 times.
|
12 | m_log->Print(level, " - {}", dir); |
63 | 6 | m_log->Print(level, "{:=^60}", ""); | |
64 | } | ||
65 | 6 | } | |
66 | |||
67 | 48 | std::string ConfigFileReader::FindFile(std::string const& name) | |
68 | { | ||
69 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if(name == "") |
70 | ✗ | return ""; // handle unset file name | |
71 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | m_log->Trace("Searching for file '{}' in:", name); |
72 | // first try `./` or assume `name` is a relative or absolute path + filename | ||
73 |
2/2✓ Branch 0 taken 47 times.
✓ Branch 1 taken 1 times.
|
96 | auto found_local = std::filesystem::exists(name); |
74 |
2/2✓ Branch 0 taken 47 times.
✓ Branch 1 taken 1 times.
|
48 | m_log->Trace(" - ./{}", found_local ? " - FOUND" : ""); |
75 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 47 times.
|
48 | if(found_local) |
76 | return name; | ||
77 | // then search each entry of `m_directories` | ||
78 |
2/2✓ Branch 0 taken 55 times.
✓ Branch 1 taken 3 times.
|
58 | for(auto const& dir : m_directories) { |
79 | 55 | std::string filename = dir + "/" + name; | |
80 |
3/4✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 44 times.
|
110 | auto found = std::filesystem::exists(filename); |
81 |
3/4✓ Branch 0 taken 11 times.
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
|
110 | m_log->Trace(" - {}{}", dir, found ? " - FOUND" : ""); |
82 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 11 times.
|
55 | if(found) |
83 | 44 | return filename; | |
84 | } | ||
85 | // throw exception if not found anywhere | ||
86 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | m_log->Error("Cannot find configuration file named '{}'", name); |
87 | 3 | PrintDirectories(Logger::error); | |
88 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | throw std::runtime_error("configuration file not found"); |
89 | } | ||
90 | |||
91 | 72 | std::string ConfigFileReader::ConvertAlgoNameToConfigName(std::string_view algo_name, std::string_view ext) | |
92 | { | ||
93 | 72 | std::string result = std::string(algo_name); | |
94 | std::string::size_type it = 0; | ||
95 |
2/2✓ Branch 0 taken 52 times.
✓ Branch 1 taken 72 times.
|
124 | while((it = result.find("::", it)) != std::string::npos) |
96 |
1/2✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
|
52 | result.replace(it, 2, "/"); |
97 |
2/4✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
✗ Branch 3 not taken.
|
216 | return "algorithms/" + result + "/Config." + std::string(ext); |
98 | } | ||
99 | |||
100 | } | ||
101 |