summaryrefslogtreecommitdiff
path: root/src/plugins/filesystem/FileLocator.cpp
blob: 994bbb95c89ebfa52dfb6e556e3d305dc30e6e93 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
    Ousía
    Copyright (C) 2014, 2015  Benjamin Paaßen, Andreas Stöckel

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef NDEBUG
//#define FILELOCATOR_DEBUG_PRINT
#endif

#ifdef FILELOCATOR_DEBUG_PRINT
#include <iostream>
#endif

#include <algorithm>
#include <fstream>

#include <boost/filesystem.hpp>

#include <core/common/Utils.hpp>

#include "FileLocator.hpp"
#include "SpecialPaths.hpp"

namespace fs = boost::filesystem;

namespace ousia {

/**
 * Function used internally to ignore backup files when performing auto
 * completion.
 *
 * @param fn is the filename that should be checked for looking like a temporary
 * or backup file.
 * @return true if the file might be a backup file, false otherwise.
 */
static bool isBackupFile(const std::string fn)
{
	return Utils::endsWith(fn, "~") || Utils::endsWith(fn, "backup");
}

void FileLocator::addPath(const std::string &path,
                          std::vector<std::string> &paths)
{
	auto it = std::find(paths.begin(), paths.end(), path);
	if (it == paths.end()) {
		paths.push_back(path);
	}
}

void FileLocator::addSearchPath(const std::string &path,
                                std::set<ResourceType> types)
{
	// Skip empty or non-existant paths
	if (path.empty() || !fs::exists(path) || !fs::is_directory(path)) {
		return;
	}

	// Canonicalize the given path, check whether it exists
	std::string canonicalPath = fs::canonical(path).generic_string();

#ifdef FILELOCATOR_DEBUG_PRINT
	std::cout << "FileLocator: Adding search path " << canonicalPath
	          << std::endl;
#endif

	// Insert the path for all given types.
	for (auto &type : types) {
		auto it = searchPaths.find(type);
		if (it != searchPaths.end()) {
			addPath(canonicalPath, it->second);
		} else {
			searchPaths.insert({type, {canonicalPath}});
		}
	}
}

void FileLocator::addSearchPath(const std::string &path, ResourceType type)
{
	addSearchPath(path, std::set<ResourceType>{type});
}

void FileLocator::addDefaultSearchPaths(const std::string &relativeTo)
{
	// Abort if the base directory is empty
	if (relativeTo.empty() || !fs::exists(relativeTo) ||
	    !fs::is_directory(relativeTo)) {
		return;
	}

	// Add the search paths
	fs::path base{relativeTo};
	addSearchPath(base.generic_string(), ResourceType::UNKNOWN);
	addSearchPath((base / "ontology").generic_string(),
	              ResourceType::ONTOLOGY);
	addSearchPath((base / "typesystem").generic_string(),
	              ResourceType::TYPESYSTEM);
}

void FileLocator::addDefaultSearchPaths()
{
	addDefaultSearchPaths(SpecialPaths::getGlobalDataDir());
	addDefaultSearchPaths(SpecialPaths::getLocalDataDir());
#ifndef NDEBUG
	addDefaultSearchPaths(SpecialPaths::getDebugDataDir());
#endif
}

void FileLocator::addUnittestSearchPath(const std::string &subdir,
                                        ResourceType type)
{
	addSearchPath((fs::path{SpecialPaths::getDebugTestdataDir()} / subdir)
	                  .generic_string(),
	              type);
}

template <typename CallbackType>
static bool iteratePaths(const FileLocator::SearchPaths &searchPaths,
                         const std::string &path, const ResourceType type,
                         const std::string &relativeTo, CallbackType callback)
{
#ifdef FILELOCATOR_DEBUG_PRINT
	std::cout << "FileLocator: Searching for \"" << path << "\"" << std::endl;
#endif

	// Divide the given path into the directory and the filename
	fs::path p{path};
	fs::path dir = p.parent_path();
	std::string filename = p.filename().generic_string();

	// Check whether the given resource has an absolute path -- if yes, call the
	// callback function and do not try any search paths
	if (dir.is_absolute()) {
		return callback(dir, filename, dir);
	}

	// If the path starts with "./" or "../" only perform relative lookups!
	if (path.substr(0, 2) != "./" && path.substr(0, 3) != "../") {
		// Look in the search paths, search backwards, last defined search
		// paths have a higher precedence
		auto it = searchPaths.find(type);
		if (it != searchPaths.end()) {
			const auto &paths = it->second;
			for (auto it = paths.rbegin(); it != paths.rend(); it++) {
#ifdef FILELOCATOR_DEBUG_PRINT
				std::cout << "FileLocator: Entering " << *it << std::endl;
#endif
				// Concatenate the searchpath with the given directory
				fs::path curDir = fs::path(*it) / dir;
				if (callback(curDir, filename, dir)) {
					return true;
				}
			}
		}
	}

	// Perform relative lookups
	if (!relativeTo.empty()) {
		fs::path curDir(relativeTo);
		if (fs::exists(curDir)) {
			// Look if 'relativeTo' is a directory already.
			if (!fs::is_directory(curDir)) {
				// If not we use the parent directory.
				curDir = curDir.parent_path();
			}

			// Append the directory to the base path and try to resolve this
			// pair
			curDir = curDir / dir;

			// If we already found a fitting resource there, use that.
			if (callback(curDir, filename, dir)) {
				return true;
			}
		}
	}
	return false;
}

bool FileLocator::doLocate(Resource &resource, const std::string &path,
                           const ResourceType type,
                           const std::string &relativeTo) const
{
	return iteratePaths(searchPaths, path, type, relativeTo,
	                    [&](const fs::path &dir, const std::string &filename,
	                        const fs::path &) -> bool {
		// Combine directory and filename
		fs::path p = dir / filename;

		// Check whether p exists
		if (fs::exists(p) && fs::is_regular_file(p)) {
			std::string location = fs::canonical(p).generic_string();
#ifdef FILELOCATOR_DEBUG_PRINT
			std::cout << "FileLocator: Found at " << location << std::endl;
#endif
			resource = Resource(true, *this, type, location);
			return true;
		}
		return false;
	});
}

std::vector<std::string> FileLocator::doAutocomplete(
    const std::string &path, const ResourceType type,
    const std::string &relativeTo) const
{
	std::vector<std::string> res;
	iteratePaths(searchPaths, path, type, relativeTo,
	             [&](const fs::path &dir, const std::string &filename,
	                 const fs::path &originalDir) -> bool {
		// Make sure the given directory actually is a directory
		if (!fs::is_directory(dir)) {
			return false;
		}

		// Check whether the file itself exists -- if yes, return this file
		// directly intead of performing any autocomplete
		fs::path p = dir / filename;
		if (fs::exists(p) && fs::is_regular_file(p)) {
			res.push_back((originalDir / filename).generic_string());
			return true;
		}

		// Append a point to the filename -- this allows us to only take files
		// into acount that actually extend the extension
		const std::string fn = filename + ".";

		// Iterate over the directory content
		fs::directory_iterator end;
		for (fs::directory_iterator it(dir); it != end; it++) {
			// Only consider regular files
			fs::path p = it->path();
			if (!fs::is_regular_file(p)) {
				continue;
			}

			// Fetch the filename of the found file, ignore temporary files
			const std::string fn2 = it->path().filename().generic_string();
			if (!fn.empty() && !isBackupFile(fn2) &&
			    Utils::startsWith(fn2, fn)) {
				res.push_back((originalDir / fn2).generic_string());
			}
		}
		return !res.empty();
	});
	return res;
}

std::unique_ptr<std::istream> FileLocator::doStream(
    const std::string &location) const
{
	return std::unique_ptr<std::istream>{new std::ifstream(location)};
}
}