2 * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
7 #include <PathFinder.h>
9 #include <AutoDeleter.h>
10 #include <FindDirectory.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
);
38 BPathFinder::SetTo(const void* codePointer
, const char* dependency
)
40 return _SetTo(codePointer
, NULL
, dependency
);
45 BPathFinder::SetTo(const char* path
, const char* dependency
)
47 return _SetTo(NULL
, path
, dependency
);
52 BPathFinder::SetTo(const entry_ref
& ref
, const char* dependency
)
55 fInitStatus
= path
.SetTo(&ref
);
56 if (fInitStatus
!= B_OK
)
59 return _SetTo(NULL
, path
.Path(), dependency
);
64 BPathFinder::FindPath(const char* architecture
,
65 path_base_directory baseDirectory
, const char* subPath
, uint32 flags
,
70 if (fInitStatus
!= B_OK
)
73 const char* dependency
= fDependency
.IsEmpty()
74 ? NULL
: fDependency
.String();
76 char pathBuffer
[B_PATH_NAME_LENGTH
];
79 if (!fPath
.IsEmpty()) {
80 error
= find_path_for_path_etc(fPath
, dependency
, architecture
,
81 baseDirectory
, subPath
, flags
, pathBuffer
, sizeof(pathBuffer
));
83 error
= find_path_etc(fCodePointer
, dependency
, architecture
,
84 baseDirectory
, subPath
, flags
, pathBuffer
, sizeof(pathBuffer
));
90 return _path
.SetTo(pathBuffer
);
95 BPathFinder::FindPath(path_base_directory baseDirectory
, const char* subPath
,
96 uint32 flags
, BPath
& _path
)
98 return FindPath(NULL
, baseDirectory
, subPath
, flags
, _path
);
103 BPathFinder::FindPath(path_base_directory baseDirectory
, const char* subPath
,
106 return FindPath(NULL
, baseDirectory
, subPath
, 0, _path
);
111 BPathFinder::FindPath(path_base_directory baseDirectory
, BPath
& _path
)
113 return FindPath(NULL
, baseDirectory
, NULL
, 0, _path
);
118 BPathFinder::FindPaths(const char* architecture
,
119 path_base_directory baseDirectory
, const char* subPath
, uint32 flags
,
127 status_t error
= find_paths_etc(architecture
, baseDirectory
, subPath
, flags
,
128 &pathArray
, &pathCount
);
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
)) {
148 BPathFinder::FindPaths(path_base_directory baseDirectory
, const char* subPath
,
149 uint32 flags
, BStringList
& _paths
)
151 return FindPaths(NULL
, baseDirectory
, subPath
, flags
, _paths
);
156 BPathFinder::FindPaths(path_base_directory baseDirectory
, const char* subPath
,
159 return FindPaths(NULL
, baseDirectory
, subPath
, 0, _paths
);
164 BPathFinder::FindPaths(path_base_directory baseDirectory
, BStringList
& _paths
)
166 return FindPaths(NULL
, baseDirectory
, NULL
, 0, _paths
);
171 BPathFinder::_SetTo(const void* codePointer
, const char* path
,
172 const char* dependency
)
174 fCodePointer
= codePointer
;
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
;