summaryrefslogtreecommitdiff
path: root/src/search.cc
blob: e33fc041881ae82481746ad907a3a3502de603d7 (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
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);
}