Line | Branch | Exec | Source |
---|---|---|---|
1 | #include "ConfigFileReader.h" | ||
2 | #include <filesystem> | ||
3 | #include <cstdlib> | ||
4 | #include <sstream> | ||
5 | |||
6 | namespace iguana { | ||
7 | |||
8 | 44 | ConfigFileReader::ConfigFileReader(std::string_view name) | |
9 | 44 | : 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 (7→8) taken 44 times.
✗ Branch 1 (7→65) not taken.
✓ Branch 2 (8→9) taken 44 times.
✗ Branch 3 (8→43) not taken.
|
44 | 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 | 44 | auto user_paths_env_var = std::getenv("IGUANA_CONFIG_PATH"); | |
21 |
1/2✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→42) taken 44 times.
|
44 | 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 | 44 | } | |
33 | |||
34 | 44 | std::string ConfigFileReader::GetConfigInstallationPrefix() | |
35 | { | ||
36 | 44 | return IGUANA_ETCDIR; | |
37 | } | ||
38 | |||
39 | 88 | void ConfigFileReader::AddDirectory(std::string const& dir) | |
40 | { | ||
41 |
2/2✓ Branch 0 (2→3) taken 53 times.
✓ Branch 1 (2→13) taken 35 times.
|
88 | if(dir == "") |
42 | return; // handle unset directory name | ||
43 |
2/6✓ Branch 0 (6→7) taken 53 times.
✗ Branch 1 (6→14) not taken.
✗ Branch 2 (7→8) not taken.
✓ Branch 3 (7→10) taken 53 times.
✗ Branch 4 (14→15) not taken.
✗ Branch 5 (14→17) not taken.
|
53 | m_log->Trace("Add directory {}", dir); |
44 | 53 | m_directories.push_front(dir); | |
45 | } | ||
46 | |||
47 | 85 | void ConfigFileReader::AddFile(std::string const& name) | |
48 | { | ||
49 |
2/2✓ Branch 0 (2→3) taken 35 times.
✓ Branch 1 (2→4) taken 50 times.
|
85 | if(name == "") |
50 | 35 | return; // handle unset file name | |
51 | 50 | auto full_name = FindFile(name); | |
52 |
2/8✓ Branch 0 (9→10) taken 47 times.
✗ Branch 1 (9→23) not taken.
✗ Branch 2 (10→11) not taken.
✓ Branch 3 (10→13) taken 47 times.
✗ Branch 4 (23→24) not taken.
✗ Branch 5 (23→26) not taken.
✗ Branch 6 (29→30) not taken.
✗ Branch 7 (29→32) not taken.
|
47 | m_log->Trace(" ===> Add file {}", full_name); |
53 |
1/2✓ Branch 0 (15→16) taken 47 times.
✗ Branch 1 (15→29) not taken.
|
47 | m_files.push_front(full_name); |
54 | } | ||
55 | |||
56 | 6 | void ConfigFileReader::PrintDirectories(Logger::Level const level) | |
57 | { | ||
58 |
1/2✓ Branch 0 (3→4) taken 6 times.
✗ Branch 1 (3→22) 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 (19→7) taken 12 times.
✓ Branch 1 (19→20) taken 6 times.
|
18 | for(auto const& dir : m_directories) |
62 |
4/8✓ Branch 0 (10→11) taken 12 times.
✗ Branch 1 (10→23) not taken.
✗ Branch 2 (11→12) not taken.
✓ Branch 3 (11→14) taken 12 times.
✓ Branch 4 (16→17) taken 6 times.
✓ Branch 5 (16→18) taken 6 times.
✗ Branch 6 (23→24) not taken.
✗ Branch 7 (23→26) not taken.
|
24 | m_log->Print(level, " - {}", dir); |
63 | 6 | m_log->Print(level, "{:=^60}", ""); | |
64 | } | ||
65 | 6 | } | |
66 | |||
67 | 50 | std::string ConfigFileReader::FindFile(std::string const& name) | |
68 | { | ||
69 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 50 times.
|
50 | if(name == "") |
70 | ✗ | return ""; // handle unset file name | |
71 |
3/6✓ Branch 0 (8→9) taken 50 times.
✗ Branch 1 (8→88) not taken.
✓ Branch 2 (9→10) taken 3 times.
✓ Branch 3 (9→12) taken 47 times.
✗ Branch 4 (88→89) not taken.
✗ Branch 5 (88→91) not taken.
|
50 | 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 (20→21) taken 49 times.
✓ Branch 1 (20→22) taken 1 times.
|
100 | auto found_local = std::filesystem::exists(name); |
74 |
2/2✓ Branch 0 (20→21) taken 49 times.
✓ Branch 1 (20→22) taken 1 times.
|
50 | m_log->Trace(" - ./{}", found_local ? " - FOUND" : ""); |
75 |
2/2✓ Branch 0 (23→24) taken 1 times.
✓ Branch 1 (23→27) taken 49 times.
|
50 | if(found_local) |
76 | return name; | ||
77 | // then search each entry of `m_directories` | ||
78 |
2/2✓ Branch 0 (72→28) taken 57 times.
✓ Branch 1 (72→73) taken 3 times.
|
60 | for(auto const& dir : m_directories) { |
79 | 57 | std::string filename = dir + "/" + name; | |
80 |
3/4✓ Branch 0 (38→39) taken 57 times.
✗ Branch 1 (38→110) not taken.
✓ Branch 2 (44→45) taken 11 times.
✓ Branch 3 (44→46) taken 46 times.
|
114 | auto found = std::filesystem::exists(filename); |
81 |
4/10✓ Branch 0 (44→45) taken 11 times.
✓ Branch 1 (44→46) taken 46 times.
✓ Branch 2 (50→51) taken 57 times.
✗ Branch 3 (50→104) not taken.
✗ Branch 4 (51→52) not taken.
✓ Branch 5 (51→54) taken 57 times.
✗ Branch 6 (104→105) not taken.
✗ Branch 7 (104→107) not taken.
✗ Branch 8 (110→111) not taken.
✗ Branch 9 (110→113) not taken.
|
114 | m_log->Trace(" - {}{}", dir, found ? " - FOUND" : ""); |
82 |
2/2✓ Branch 0 (56→57) taken 46 times.
✓ Branch 1 (56→64) taken 11 times.
|
57 | if(found) |
83 | 46 | return filename; | |
84 | } | ||
85 | // throw exception if not found anywhere | ||
86 |
2/6✓ Branch 0 (76→77) taken 3 times.
✗ Branch 1 (76→116) not taken.
✗ Branch 2 (77→78) not taken.
✓ Branch 3 (77→80) taken 3 times.
✗ Branch 4 (116→117) not taken.
✗ Branch 5 (116→119) not taken.
|
3 | m_log->Error("Cannot find configuration file named '{}'", name); |
87 | 3 | PrintDirectories(Logger::error); | |
88 |
1/2✓ Branch 0 (84→85) taken 3 times.
✗ Branch 1 (84→122) not taken.
|
3 | throw std::runtime_error("configuration file not found"); |
89 | } | ||
90 | |||
91 | 86 | std::string ConfigFileReader::ConvertAlgoNameToConfigName(std::string_view algo_name, std::string_view ext) | |
92 | { | ||
93 | 86 | std::string result = std::string(algo_name); | |
94 | std::string::size_type it = 0; | ||
95 |
2/2✓ Branch 0 (5→4) taken 63 times.
✓ Branch 1 (5→6) taken 86 times.
|
149 | while((it = result.find("::", it)) != std::string::npos) |
96 |
1/2✓ Branch 0 (4→5) taken 63 times.
✗ Branch 1 (4→51) not taken.
|
63 | result.replace(it, 2, "/"); |
97 |
5/12✓ Branch 0 (8→9) taken 86 times.
✗ Branch 1 (8→45) not taken.
✓ Branch 2 (11→12) taken 86 times.
✗ Branch 3 (11→33) not taken.
✓ Branch 4 (22→23) taken 86 times.
✗ Branch 5 (22→25) not taken.
✓ Branch 6 (27→28) taken 23 times.
✓ Branch 7 (27→30) taken 63 times.
✗ Branch 8 (45→46) not taken.
✗ Branch 9 (45→48) not taken.
✗ Branch 10 (51→52) not taken.
✗ Branch 11 (51→54) not taken.
|
430 | return "algorithms/" + result + "/Config." + std::string(ext); |
98 | } | ||
99 | |||
100 | } | ||
101 |