Add class BPathFinder

It's a more convenient C++ wrapper for the new find_path*() C functions.
This commit is contained in:
Ingo Weinhold
2013-11-05 21:40:43 +01:00
parent 986e4abce4
commit 8afbcbeb6f
3 changed files with 225 additions and 0 deletions
+63
View File
@@ -0,0 +1,63 @@
/*
* Copyright 2013, Haiku Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _PATH_FINDER_H
#define _PATH_FINDER_H
#include <FindDirectory.h>
#include <String.h>
class BPath;
class BStringList;
struct entry_ref;
class BPathFinder {
public:
BPathFinder(const void* codePointer = NULL,
const char* dependency = NULL);
BPathFinder(const char* path,
const char* dependency = NULL);
BPathFinder(const entry_ref& ref,
const char* dependency = NULL);
status_t SetTo(const void* codePointer = NULL,
const char* dependency = NULL);
status_t SetTo(const char* path,
const char* dependency = NULL);
status_t SetTo(const entry_ref& ref,
const char* dependency = NULL);
status_t FindPath(path_base_directory baseDirectory,
const char* subPath, uint32 flags,
BPath& path);
status_t FindPath(path_base_directory baseDirectory,
const char* subPath, BPath& path);
status_t FindPath(path_base_directory baseDirectory,
BPath& path);
static status_t FindPaths(path_base_directory baseDirectory,
const char* subPath, uint32 flags,
BStringList& paths);
static status_t FindPaths(path_base_directory baseDirectory,
const char* subPath, BStringList& paths);
static status_t FindPaths(path_base_directory baseDirectory,
BStringList& paths);
private:
status_t _SetTo(const void* codePointer,
const char* path, const char* dependency);
private:
const void* fCodePointer;
BString fPath;
BString fDependency;
status_t fInitStatus;
uint32 fReserved[4];
};
#endif // _PATH_FINDER_H
+1
View File
@@ -39,6 +39,7 @@ for architectureObject in [ MultiArchSubDirSetup ] {
NodeMonitor.cpp
OffsetFile.cpp
Path.cpp
PathFinder.cpp
PathMonitor.cpp
Query.cpp
QueryPredicate.cpp
+161
View File
@@ -0,0 +1,161 @@
/*
* Copyright 2013, Ingo Weinhold, [email protected].
* Distributed under the terms of the MIT License.
*/
#include <PathFinder.h>
#include <FindDirectory.h>
#include <Path.h>
#include <StringList.h>
BPathFinder::BPathFinder(const void* codePointer, const char* dependency)
{
_SetTo(codePointer, NULL, dependency);
}
BPathFinder::BPathFinder(const char* path, const char* dependency)
{
_SetTo(NULL, path, dependency);
}
BPathFinder::BPathFinder(const entry_ref& ref, const char* dependency)
{
SetTo(ref, dependency);
}
status_t
BPathFinder::SetTo(const void* codePointer, const char* dependency)
{
return _SetTo(codePointer, NULL, dependency);
}
status_t
BPathFinder::SetTo(const char* path, const char* dependency)
{
return _SetTo(NULL, path, dependency);
}
status_t
BPathFinder::SetTo(const entry_ref& ref, const char* dependency)
{
BPath path;
fInitStatus = path.SetTo(&ref);
if (fInitStatus != B_OK)
return fInitStatus;
return _SetTo(NULL, path.Path(), dependency);
}
status_t
BPathFinder::FindPath(path_base_directory baseDirectory, const char* subPath,
uint32 flags, BPath& path)
{
path.Unset();
if (fInitStatus != B_OK)
return fInitStatus;
const char* dependency = fDependency.IsEmpty()
? NULL : fDependency.String();
char pathBuffer[B_PATH_NAME_LENGTH];
status_t error;
if (!fPath.IsEmpty()) {
error = find_path_for_path(fPath, dependency, baseDirectory, subPath,
flags, pathBuffer, sizeof(pathBuffer));
} else {
error = find_path(fCodePointer, dependency, baseDirectory, subPath,
flags, pathBuffer, sizeof(pathBuffer));
}
if (error != B_OK)
return error;
return path.SetTo(pathBuffer);
}
status_t
BPathFinder::FindPath(path_base_directory baseDirectory, const char* subPath,
BPath& path)
{
return FindPath(baseDirectory, subPath, 0, path);
}
status_t
BPathFinder::FindPath(path_base_directory baseDirectory, BPath& path)
{
return FindPath(baseDirectory, NULL, 0, path);
}
/*static*/ status_t
BPathFinder::FindPaths(path_base_directory baseDirectory, const char* subPath,
uint32 flags, BStringList& paths)
{
paths.MakeEmpty();
// get the paths
char** pathArray;
size_t pathCount;
status_t error = find_paths(baseDirectory, subPath, flags, &pathArray,
&pathCount);
if (error != B_OK)
return error;
// add them to BStringList
for (size_t i = 0; i < pathCount; i++) {
BString path(pathArray[i]);
if (path.IsEmpty() || !paths.Add(path)) {
paths.MakeEmpty();
return B_NO_MEMORY;
}
}
return B_OK;
}
/*static*/ status_t
BPathFinder::FindPaths(path_base_directory baseDirectory, const char* subPath,
BStringList& paths)
{
return FindPaths(baseDirectory, subPath, 0, paths);
}
/*static*/ status_t
BPathFinder::FindPaths(path_base_directory baseDirectory, BStringList& paths)
{
return FindPaths(baseDirectory, NULL, 0, paths);
}
status_t
BPathFinder::_SetTo(const void* codePointer, const char* path,
const char* dependency)
{
fCodePointer = codePointer;
fPath = path;
fDependency = dependency;
if ((path != NULL && path[0] != '\0' && fPath.IsEmpty())
|| (dependency != NULL && dependency[0] != '\0'
&& fDependency.IsEmpty())) {
fInitStatus = B_NO_MEMORY;
} else
fInitStatus = B_OK;
return fInitStatus;
}