Paths related utility functions.
git-svn-id: file:///srv/svn/repos/haiku/trunk/current@11580 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2005, Ingo Weinhold, [email protected].
|
||||||
|
* Distributed under the terms of the MIT License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "compat.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "path_util.h"
|
||||||
|
|
||||||
|
status_t
|
||||||
|
get_last_path_component(const char *path, char *buffer, int bufferLen)
|
||||||
|
{
|
||||||
|
int len = strlen(path);
|
||||||
|
if (len == 0)
|
||||||
|
return FS_EINVAL;
|
||||||
|
|
||||||
|
// eat trailing '/'
|
||||||
|
while (len > 0 && path[len - 1] == '/')
|
||||||
|
len--;
|
||||||
|
|
||||||
|
if (len == 0) {
|
||||||
|
// path is `/'
|
||||||
|
len = 1;
|
||||||
|
} else {
|
||||||
|
// find previous '/'
|
||||||
|
int pos = len - 1;
|
||||||
|
while (pos > 0 && path[pos] != '/')
|
||||||
|
pos--;
|
||||||
|
if (path[pos] == '/')
|
||||||
|
pos++;
|
||||||
|
|
||||||
|
path += pos;
|
||||||
|
len -= pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (len >= bufferLen)
|
||||||
|
return FS_NAME_TOO_LONG;
|
||||||
|
|
||||||
|
memcpy(buffer, path, len);
|
||||||
|
buffer[len] = '\0';
|
||||||
|
return FS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *
|
||||||
|
make_path(const char *dir, const char *entry)
|
||||||
|
{
|
||||||
|
// get the len
|
||||||
|
int dirLen = strlen(dir);
|
||||||
|
int entryLen = strlen(entry);
|
||||||
|
bool insertSeparator = (dir[dirLen - 1] != '/');
|
||||||
|
int pathLen = dirLen + entryLen + (insertSeparator ? 1 : 0) + 1;
|
||||||
|
|
||||||
|
// allocate the path
|
||||||
|
char *path = (char*)malloc(pathLen);
|
||||||
|
if (!path)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
// compose the path
|
||||||
|
strcpy(path, dir);
|
||||||
|
if (insertSeparator)
|
||||||
|
strcat(path + dirLen, "/");
|
||||||
|
strcat(path + dirLen, entry);
|
||||||
|
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2005, Ingo Weinhold, [email protected].
|
||||||
|
* Distributed under the terms of the MIT License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef FS_SHELL_PATH_UTIL_H
|
||||||
|
#define FS_SHELL_PATH_UTIL_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
status_t get_last_path_component(const char *path, char *buffer, int bufferLen);
|
||||||
|
char *make_path(const char *dir, const char *entry);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // FS_SHELL_PATH_UTIL_H
|
||||||
Reference in New Issue
Block a user