xref: /haiku/src/kits/storage/PathFinder.cpp (revision 04a0e9c7b68cbe3a43d38e2bca8e860fd80936fb)
1 /*
2  * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <PathFinder.h>
8 
9 #include <FindDirectory.h>
10 #include <Path.h>
11 #include <StringList.h>
12 
13 
14 // NOTE: The package kit specific part of BPathFinder (BResolvableExpression
15 // constructor and SetTo()) is implemented in the package kit.
16 
17 
18 BPathFinder::BPathFinder(const void* codePointer, const char* dependency)
19 {
20 	_SetTo(codePointer, NULL, dependency);
21 }
22 
23 
24 BPathFinder::BPathFinder(const char* path, const char* dependency)
25 {
26 	_SetTo(NULL, path, dependency);
27 }
28 
29 
30 BPathFinder::BPathFinder(const entry_ref& ref, const char* dependency)
31 {
32 	SetTo(ref, dependency);
33 }
34 
35 
36 status_t
37 BPathFinder::SetTo(const void* codePointer, const char* dependency)
38 {
39 	return _SetTo(codePointer, NULL, dependency);
40 }
41 
42 
43 status_t
44 BPathFinder::SetTo(const char* path, const char* dependency)
45 {
46 	return _SetTo(NULL, path, dependency);
47 }
48 
49 
50 status_t
51 BPathFinder::SetTo(const entry_ref& ref, const char* dependency)
52 {
53 	BPath path;
54 	fInitStatus = path.SetTo(&ref);
55 	if (fInitStatus != B_OK)
56 		return fInitStatus;
57 
58 	return _SetTo(NULL, path.Path(), dependency);
59 }
60 
61 
62 status_t
63 BPathFinder::FindPath(const char* architecture,
64 	path_base_directory baseDirectory, const char* subPath, uint32 flags,
65 	BPath& _path)
66 {
67 	_path.Unset();
68 
69 	if (fInitStatus != B_OK)
70 		return fInitStatus;
71 
72 	const char* dependency = fDependency.IsEmpty()
73 		? NULL : fDependency.String();
74 
75 	char pathBuffer[B_PATH_NAME_LENGTH];
76 	status_t error;
77 
78 	if (!fPath.IsEmpty()) {
79 		error = find_path_for_path_etc(fPath, dependency, architecture,
80 			baseDirectory, subPath, flags, pathBuffer, sizeof(pathBuffer));
81 	} else {
82 		error = find_path_etc(fCodePointer, dependency, architecture,
83 			baseDirectory, subPath, flags, pathBuffer, sizeof(pathBuffer));
84 	}
85 
86 	if (error != B_OK)
87 		return error;
88 
89 	return _path.SetTo(pathBuffer);
90 }
91 
92 
93 status_t
94 BPathFinder::FindPath(path_base_directory baseDirectory, const char* subPath,
95 	uint32 flags, BPath& _path)
96 {
97 	return FindPath(NULL, baseDirectory, subPath, flags, _path);
98 }
99 
100 
101 status_t
102 BPathFinder::FindPath(path_base_directory baseDirectory, const char* subPath,
103 	BPath& _path)
104 {
105 	return FindPath(NULL, baseDirectory, subPath, 0, _path);
106 }
107 
108 
109 status_t
110 BPathFinder::FindPath(path_base_directory baseDirectory, BPath& _path)
111 {
112 	return FindPath(NULL, baseDirectory, NULL, 0, _path);
113 }
114 
115 
116 /*static*/ status_t
117 BPathFinder::FindPaths(const char* architecture,
118 	path_base_directory baseDirectory, const char* subPath, uint32 flags,
119 	BStringList& _paths)
120 {
121 	_paths.MakeEmpty();
122 
123 	// get the paths
124 	char** pathArray;
125 	size_t pathCount;
126 	status_t error = find_paths_etc(architecture, baseDirectory, subPath, flags,
127 		&pathArray, &pathCount);
128 	if (error != B_OK)
129 		return error;
130 
131 	// add them to BStringList
132 	for (size_t i = 0; i < pathCount; i++) {
133 		BString path(pathArray[i]);
134 		if (path.IsEmpty() || !_paths.Add(path)) {
135 			_paths.MakeEmpty();
136 			return B_NO_MEMORY;
137 		}
138 	}
139 
140 	return B_OK;
141 }
142 
143 
144 /*static*/ status_t
145 BPathFinder::FindPaths(path_base_directory baseDirectory, const char* subPath,
146 	uint32 flags, BStringList& _paths)
147 {
148 	return FindPaths(NULL, baseDirectory, subPath, 0, _paths);
149 }
150 
151 
152 /*static*/ status_t
153 BPathFinder::FindPaths(path_base_directory baseDirectory, const char* subPath,
154 	BStringList& _paths)
155 {
156 	return FindPaths(NULL, baseDirectory, subPath, 0, _paths);
157 }
158 
159 
160 /*static*/ status_t
161 BPathFinder::FindPaths(path_base_directory baseDirectory, BStringList& _paths)
162 {
163 	return FindPaths(NULL, baseDirectory, NULL, 0, _paths);
164 }
165 
166 
167 status_t
168 BPathFinder::_SetTo(const void* codePointer, const char* path,
169 	const char* dependency)
170 {
171 	fCodePointer = codePointer;
172 	fPath = path;
173 	fDependency = dependency;
174 
175 	if ((path != NULL && path[0] != '\0' && fPath.IsEmpty())
176 		|| (dependency != NULL && dependency[0] != '\0'
177 			&& fDependency.IsEmpty())) {
178 		fInitStatus = B_NO_MEMORY;
179 	} else
180 		fInitStatus = B_OK;
181 
182 	return fInitStatus;
183 }
184