File: DataDirsAccess.cpp

package info (click to toggle)
spring 104.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 47,512 kB
  • sloc: cpp: 391,093; ansic: 79,943; python: 12,356; java: 12,201; awk: 5,889; sh: 1,826; xml: 655; makefile: 486; perl: 405; php: 211; objc: 194; sed: 2
file content (202 lines) | stat: -rw-r--r-- 5,581 bytes parent folder | download | duplicates (2)
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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "DataDirsAccess.h"
#include "DataDirLocater.h"
#include "FileHandler.h"
#include "FileQueryFlags.h"
#include "FileSystem.h"

#include <cassert>
#include <string>
#include <vector>


DataDirsAccess dataDirsAccess;


std::vector<std::string> DataDirsAccess::FindFiles(std::string dir, const std::string& pattern, int flags) const
{
	if (!FileSystem::CheckFile(dir)) {
		return std::vector<std::string>();
	}

	FileSystem::FixSlashes(dir);
	FileSystem::EnsurePathSepAtEnd(dir);

	if (flags & FileQueryFlags::ONLY_DIRS) {
		flags |= FileQueryFlags::INCLUDE_DIRS;
	}

	return FindFilesInternal(dir, pattern, flags);
}

std::vector<std::string> DataDirsAccess::FindFilesInternal(const std::string& dir, const std::string& pattern, int flags) const
{
	std::vector<std::string> matches;

	// if it is an absolute path, do not look for it in the data directories
	if (FileSystem::IsAbsolutePath(dir)) {
		// pass the directory as second directory argument,
		// so the path gets included in the matches.
		FindFilesSingleDir(matches, "", dir, pattern, flags);
		return matches;
	}

	std::string dir2 = FileSystem::RemoveLocalPathPrefix(dir);

	const std::vector<DataDir>& datadirs = dataDirLocater.GetDataDirs();
	for (auto d = datadirs.crbegin(); d != datadirs.crend(); ++d) {
		FindFilesSingleDir(matches, d->path, dir2, pattern, flags);
	}
	return matches;
}

std::string DataDirsAccess::LocateFileInternal(const std::string& file) const
{
	// if it's an absolute path, don't look for it in the data directories
	if (FileSystem::IsAbsolutePath(file)) {
		return file;
	}

	const std::vector<DataDir>& datadirs = dataDirLocater.GetDataDirs();
	for (const DataDir& d: datadirs) {
		const std::string fn(d.path + file);
		// does the file exist, and is it readable?
		if (FileSystem::IsReadableFile(fn)) {
			return fn;
		}
	}

	return file;
}


void DataDirsAccess::FindFilesSingleDir(std::vector<std::string>& matches, const std::string& datadir, const std::string& dir, const std::string& pattern, int flags) const
{
	assert(datadir.empty() || datadir[datadir.length() - 1] == FileSystem::GetNativePathSeparator());

	const std::string regexPattern = FileSystem::ConvertGlobToRegex(pattern);

	FileSystem::FindFiles(matches, datadir, dir, regexPattern, flags);
}



std::string DataDirsAccess::LocateFile(std::string file, int flags) const
{
	if (!FileSystem::CheckFile(file)) {
		return "";
	}

	// if it is an absolute path, do not look for it in the data directories
	if (FileSystem::IsAbsolutePath(file)) {
		return file;
	}

	FileSystem::FixSlashes(file);

	if (flags & FileQueryFlags::WRITE) {
		std::string writeableFile = dataDirLocater.GetWriteDirPath() + file;
		FileSystem::FixSlashes(writeableFile);
		if (flags & FileQueryFlags::CREATE_DIRS) {
			FileSystem::CreateDirectory(FileSystem::GetDirectory(writeableFile));
		}
		return writeableFile;
	}

	return LocateFileInternal(file);
}

std::string DataDirsAccess::LocateDir(std::string dir, int flags) const
{
	if (!FileSystem::CheckFile(dir)) {
		return "";
	}

	if (FileSystem::IsAbsolutePath(dir)) {
		return dir;
	}

	FileSystem::FixSlashes(dir);

	if (flags & FileQueryFlags::WRITE) {
		std::string writeableDir = dataDirLocater.GetWriteDirPath() + dir;
		FileSystem::FixSlashes(writeableDir);
		if (flags & FileQueryFlags::CREATE_DIRS) {
			FileSystem::CreateDirectory(writeableDir);
		}
		return writeableDir;
	} else {
		const std::vector<std::string>& datadirs = dataDirLocater.GetDataDirPaths();
		for (const std::string& dd: datadirs) {
			std::string dirPath(dd + dir);
			if (FileSystem::DirExists(dirPath)) {
				return dirPath;
			}
		}
		return dir;
	}
}
std::vector<std::string> DataDirsAccess::LocateDirs(std::string dir) const
{
	std::vector<std::string> found;

	if (!FileSystem::CheckFile(dir) || FileSystem::IsAbsolutePath(dir)) {
		return found;
	}

	FileSystem::FixSlashes(dir);

	const std::vector<std::string>& datadirs = dataDirLocater.GetDataDirPaths();
	for (const std::string& dd: datadirs) {
		std::string dirPath(dd + dir);
		if (FileSystem::DirExists(dirPath)) {
			found.push_back(dirPath);
		}
	}

	return found;
}

std::vector<std::string> DataDirsAccess::FindDirsInDirectSubDirs(
		const std::string& relPath) const {

	std::vector<std::string> found;

	static const std::string pattern = "*";

	// list of all occurences of the relative path in the data directories
	const std::vector<std::string>& rootDirs = LocateDirs(relPath);

	// list of subdirs in all occurences of the relative path in the data directories
	std::vector<std::string> mainDirs;

	// find all subdirectories in the rootDirs
	for (const std::string& dir: rootDirs) {
		const std::vector<std::string>& localMainDirs = CFileHandler::SubDirs(dir, pattern, SPRING_VFS_RAW);
		mainDirs.insert(mainDirs.end(), localMainDirs.begin(), localMainDirs.end());
	}
	//found.insert(found.end(), mainDirs.begin(), mainDirs.end());

	// and add all subdriectories of these
	for (const std::string& dir: mainDirs) {
		const std::vector<std::string>& subDirs = CFileHandler::SubDirs(dir, pattern, SPRING_VFS_RAW);
		found.insert(found.end(), subDirs.begin(), subDirs.end());
	}

	return found;
}


bool DataDirsAccess::InReadDir(const std::string& path)
{
	std::string locatedFile = LocateFile(path);
	return (locatedFile != "") && (locatedFile != path);
}


bool DataDirsAccess::InWriteDir(const std::string& path)
{
	std::string locatedFile = LocateFile(path, FileQueryFlags::WRITE);
	return (locatedFile != "") && (locatedFile != path);
}