GCC Code Coverage Report


Directory: ./
File: src/iguana/services/ConfigFileReader.cc
Date: 2025-11-25 17:57:04
Coverage Exec Excl Total
Lines: 86.8% 46 0 53
Functions: 100.0% 7 0 7
Branches: 46.4% 51 0 110

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