diff options
Diffstat (limited to 'src/search.cc')
-rw-r--r-- | src/search.cc | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/src/search.cc b/src/search.cc new file mode 100644 index 0000000..e33fc04 --- /dev/null +++ b/src/search.cc @@ -0,0 +1,120 @@ +#include "search.h" + +#include <iostream> + +#include "libctor.h" + +// https://stackoverflow.com/questions/17939930/finding-out-what-the-gcc-include-path-is + +// /usr/include/ +// /usr/local/include +std::filesystem::path findFile(const Settings& settings, + const std::string& needle, + const std::vector<std::string>& haystack) +{ + if(settings.verbose > 0) + { + std::cout << "Looking for '" << needle << "'\n"; + } + for(const auto& path : haystack) + { + std::filesystem::path candidate = std::filesystem::path(path) / needle; + if(settings.verbose > 1) + { + std::cout << " in '" << candidate.string() << "' ... "; + } + if(std::filesystem::exists(candidate)) + { + if(settings.verbose > 1) + { + std::cout << "yes\n"; + } + return path; + } + else + { + if(settings.verbose > 1) + { + std::cout << "no\n"; + } + } + } + + if(settings.verbose > 0) + { + std::cout << "Not found.\n"; + } + + return ""; // not found +} + +std::filesystem::path findHeader(const Settings& settings, + const std::string& needle, + const std::vector<std::string>& haystack) +{ + auto paths = haystack; + paths.insert(paths.end(), + settings.include_paths.begin(), + settings.include_paths.end()); + return findFile(settings, needle, paths); +} + +std::vector<std::filesystem::path> findBasename(const Settings& settings, + const std::string& needle, + const std::vector<std::string>& haystack) +{ + std::vector<std::filesystem::path> matches; + if(settings.verbose > 0) + { + std::cout << "Looking for basename '" << needle << "'\n"; + } + + for(const auto& path : haystack) + { + if(settings.verbose > 1) + { + std::cout << "Looking in '" << path << "\n"; + } + + for(const auto& dir_entry : std::filesystem::directory_iterator{path}) + { + if(std::filesystem::is_regular_file(dir_entry)) + { + if(dir_entry.path().stem() == needle) + { + if(settings.verbose > 1) + { + std::cout << "Found " << path / dir_entry.path() << "\n"; + } + matches.push_back(path / dir_entry.path()); + } + } + } + } + + if(matches.empty() && settings.verbose > 0) + { + std::cout << "Not found.\n"; + } + + return matches; +} + +std::vector<std::filesystem::path> findLibrary(const Settings& settings, + const std::string& needle, + const std::vector<std::string>& haystack) +{ + using namespace std::string_literals; + + std::string lib = needle; + if(!lib.starts_with("lib")) + { + lib = "lib"s + needle; + } + auto paths = haystack; + paths.insert(paths.end(), + settings.library_paths.begin(), + settings.library_paths.end()); + return findBasename(settings, lib, paths); +} + |