X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/blobdiff_plain/460b9539a7c15580e41a71bbc0f47ae776238915..15091f766f1e8c380d2a7f752c387a2cecaf5bb1:/lib/filepart.c diff --git a/lib/filepart.c b/lib/filepart.c index 575cbc5..c080b4d 100644 --- a/lib/filepart.c +++ b/lib/filepart.c @@ -1,6 +1,6 @@ /* * This file is part of DisOrder - * Copyright (C) 2005 Richard Kettlewell + * Copyright (C) 2005, 2007 Richard Kettlewell * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,6 +17,9 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA */ +/** @file lib/filepart.c + * @brief Filename parsing + */ #include #include "types.h" @@ -26,10 +29,20 @@ #include "filepart.h" #include "mem.h" +/** @brief Return the directory part of @p path + * @param path Path to parse + * @return Directory part of @p path + * + * Extracts the directory part of @p path. This is a simple lexical + * transformation and no canonicalization is performed. The result will only + * ever end "/" if it is the root directory. + */ char *d_dirname(const char *path) { const char *s; if((s = strrchr(path, '/'))) { + while(s > path && s[-1] == '/') + --s; if(s == path) return xstrdup("/"); else @@ -38,6 +51,16 @@ char *d_dirname(const char *path) { return xstrdup("."); } +/** @brief Find the extension part of @p path + * @param path Path to parse + * @return Start of extension in @p path, or NULL + * + * The return value points into @p path and points at the "." at the start of + * the path. If the basename has no extension the result is NULL. Extensions + * are assumed to only contain the ASCII digits and letters. + * + * See also extension(). + */ static const char *find_extension(const char *path) { const char *q = path + strlen(path); @@ -48,12 +71,29 @@ static const char *find_extension(const char *path) { return *q == '.' ? q : 0; } +/** @brief Strip the extension from @p path + * @param path Path to parse + * @return @p path with extension removed, or @p path + * + * The extension is defined exactly as for find_extension(). The result might + * or might not point into @p path. + */ const char *strip_extension(const char *path) { const char *q = find_extension(path); return q ? xstrndup(path, q - path) : path; } +/** @brief Find the extension part of @p path + * @param path Path to parse + * @return Start of extension in @p path, or "" + * + * The return value may points into @p path and if so points at the "." at the + * start of the path. If the basename has no extension the result is "". + * Extensions are assumed to only contain the ASCII digits and letters. + * + * See also find_extension(). + */ const char *extension(const char *path) { const char *q = find_extension(path); @@ -68,4 +108,3 @@ fill-column:79 indent-tabs-mode:nil End: */ -/* arch-tag:4WKGtvPyLNQ6h3I0n2cMIg */