diff options
author | Vicent Marti <tanoku@gmail.com> | 2011-07-04 13:43:34 +0400 |
---|---|---|
committer | Vicent Marti <tanoku@gmail.com> | 2011-07-05 04:04:03 +0400 |
commit | f79026b4912bcd2336667f4c1663c06e233f0b32 (patch) | |
tree | 645b776032e924b587fad986aa3f3dc08c98d4c5 /src/path.h | |
parent | 678e9e045becdc5d75f2ce2259ed01c3531ee181 (diff) |
fileops: Cleanup
Cleaned up the structure of the whole OS-abstraction layer.
fileops.c now contains a set of utility methods for file management used
by the library. These are abstractions on top of the original POSIX
calls.
There's a new file called `posix.c` that contains
emulations/reimplementations of all the POSIX calls the library uses.
These are prefixed with `p_`. There's a specific posix file for each
platform (win32 and unix).
All the path-related methods have been moved from `utils.c` to `path.c`
and have their own prefix.
Diffstat (limited to 'src/path.h')
-rw-r--r-- | src/path.h | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/path.h b/src/path.h new file mode 100644 index 000000000..1bae9dfbe --- /dev/null +++ b/src/path.h @@ -0,0 +1,76 @@ +/* + * posix.h - Path management methods + */ +#ifndef INCLUDE_path_h__ +#define INCLUDE_path_h__ + +#include "common.h" + +/* + * The dirname() function shall take a pointer to a character string + * that contains a pathname, and return a pointer to a string that is a + * pathname of the parent directory of that file. Trailing '/' characters + * in the path are not counted as part of the path. + * + * If path does not contain a '/', then dirname() shall return a pointer to + * the string ".". If path is a null pointer or points to an empty string, + * dirname() shall return a pointer to the string "." . + * + * The `git_path_dirname` implementation is thread safe. The returned + * string must be manually free'd. + * + * The `git_path_dirname_r` implementation expects a string allocated + * by the user with big enough size. + */ +extern char *git_path_dirname(const char *path); +extern int git_path_dirname_r(char *buffer, size_t bufflen, const char *path); + +/* + * This function returns the basename of the file, which is the last + * part of its full name given by fname, with the drive letter and + * leading directories stripped off. For example, the basename of + * c:/foo/bar/file.ext is file.ext, and the basename of a:foo is foo. + * + * Trailing slashes and backslashes are significant: the basename of + * c:/foo/bar/ is an empty string after the rightmost slash. + * + * The `git_path_basename` implementation is thread safe. The returned + * string must be manually free'd. + * + * The `git_path_basename_r` implementation expects a string allocated + * by the user with big enough size. + */ +extern char *git_path_basename(const char *path); +extern int git_path_basename_r(char *buffer, size_t bufflen, const char *path); + +extern const char *git_path_topdir(const char *path); + +/** + * Join two paths together. Takes care of properly fixing the + * middle slashes and everything + * + * The paths are joined together into buffer_out; this is expected + * to be an user allocated buffer of `GIT_PATH_MAX` size + */ +extern void git_path_join_n(char *buffer_out, int npath, ...); + +GIT_INLINE(void) git_path_join(char *buffer_out, const char *path_a, const char *path_b) +{ + git_path_join_n(buffer_out, 2, path_a, path_b); +} + +#ifdef GIT_WIN32 +GIT_INLINE(void) git_path_mkposix(char *path) +{ + while (*path) { + if (*path == '\\') + *path = '/'; + + path++; + } +} +#else +# define git_path_mkposix(p) /* blank */ +#endif + +#endif |