headers/bsd: Add sys/queue.h.
[haiku.git] / src / kits / storage / PathFinder.cpp
blob89b9de1d627f033ebdd44f1f1ee281eea6366b32
1 /*
2 * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include <PathFinder.h>
9 #include <AutoDeleter.h>
10 #include <FindDirectory.h>
11 #include <Path.h>
12 #include <StringList.h>
15 // NOTE: The package kit specific part of BPathFinder (BResolvableExpression
16 // constructor and SetTo()) is implemented in the package kit.
19 BPathFinder::BPathFinder(const void* codePointer, const char* dependency)
21 _SetTo(codePointer, NULL, dependency);
25 BPathFinder::BPathFinder(const char* path, const char* dependency)
27 _SetTo(NULL, path, dependency);
31 BPathFinder::BPathFinder(const entry_ref& ref, const char* dependency)
33 SetTo(ref, dependency);
37 status_t
38 BPathFinder::SetTo(const void* codePointer, const char* dependency)
40 return _SetTo(codePointer, NULL, dependency);
44 status_t
45 BPathFinder::SetTo(const char* path, const char* dependency)
47 return _SetTo(NULL, path, dependency);
51 status_t
52 BPathFinder::SetTo(const entry_ref& ref, const char* dependency)
54 BPath path;
55 fInitStatus = path.SetTo(&ref);
56 if (fInitStatus != B_OK)
57 return fInitStatus;
59 return _SetTo(NULL, path.Path(), dependency);
63 status_t
64 BPathFinder::FindPath(const char* architecture,
65 path_base_directory baseDirectory, const char* subPath, uint32 flags,
66 BPath& _path)
68 _path.Unset();
70 if (fInitStatus != B_OK)
71 return fInitStatus;
73 const char* dependency = fDependency.IsEmpty()
74 ? NULL : fDependency.String();
76 char pathBuffer[B_PATH_NAME_LENGTH];
77 status_t error;
79 if (!fPath.IsEmpty()) {
80 error = find_path_for_path_etc(fPath, dependency, architecture,
81 baseDirectory, subPath, flags, pathBuffer, sizeof(pathBuffer));
82 } else {
83 error = find_path_etc(fCodePointer, dependency, architecture,
84 baseDirectory, subPath, flags, pathBuffer, sizeof(pathBuffer));
87 if (error != B_OK)
88 return error;
90 return _path.SetTo(pathBuffer);
94 status_t
95 BPathFinder::FindPath(path_base_directory baseDirectory, const char* subPath,
96 uint32 flags, BPath& _path)
98 return FindPath(NULL, baseDirectory, subPath, flags, _path);
102 status_t
103 BPathFinder::FindPath(path_base_directory baseDirectory, const char* subPath,
104 BPath& _path)
106 return FindPath(NULL, baseDirectory, subPath, 0, _path);
110 status_t
111 BPathFinder::FindPath(path_base_directory baseDirectory, BPath& _path)
113 return FindPath(NULL, baseDirectory, NULL, 0, _path);
117 /*static*/ status_t
118 BPathFinder::FindPaths(const char* architecture,
119 path_base_directory baseDirectory, const char* subPath, uint32 flags,
120 BStringList& _paths)
122 _paths.MakeEmpty();
124 // get the paths
125 char** pathArray;
126 size_t pathCount;
127 status_t error = find_paths_etc(architecture, baseDirectory, subPath, flags,
128 &pathArray, &pathCount);
129 if (error != B_OK)
130 return error;
132 MemoryDeleter pathArrayDeleter(pathArray);
134 // add them to BStringList
135 for (size_t i = 0; i < pathCount; i++) {
136 BString path(pathArray[i]);
137 if (path.IsEmpty() || !_paths.Add(path)) {
138 _paths.MakeEmpty();
139 return B_NO_MEMORY;
143 return B_OK;
147 /*static*/ status_t
148 BPathFinder::FindPaths(path_base_directory baseDirectory, const char* subPath,
149 uint32 flags, BStringList& _paths)
151 return FindPaths(NULL, baseDirectory, subPath, flags, _paths);
155 /*static*/ status_t
156 BPathFinder::FindPaths(path_base_directory baseDirectory, const char* subPath,
157 BStringList& _paths)
159 return FindPaths(NULL, baseDirectory, subPath, 0, _paths);
163 /*static*/ status_t
164 BPathFinder::FindPaths(path_base_directory baseDirectory, BStringList& _paths)
166 return FindPaths(NULL, baseDirectory, NULL, 0, _paths);
170 status_t
171 BPathFinder::_SetTo(const void* codePointer, const char* path,
172 const char* dependency)
174 fCodePointer = codePointer;
175 fPath = path;
176 fDependency = dependency;
178 if ((path != NULL && path[0] != '\0' && fPath.IsEmpty())
179 || (dependency != NULL && dependency[0] != '\0'
180 && fDependency.IsEmpty())) {
181 fInitStatus = B_NO_MEMORY;
182 } else
183 fInitStatus = B_OK;
185 return fInitStatus;