2 * This file is part of DisOrder
3 * Copyright (C) 2005, 2007 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 /** @file lib/filepart.c
21 * @brief Filename parsing
32 /** @brief Return the directory part of @p path
33 * @param path Path to parse
34 * @return Directory part of @p path
36 * Extracts the directory part of @p path. This is a simple lexical
37 * transformation and no canonicalization is performed. The result will only
38 * ever end "/" if it is the root directory.
40 char *d_dirname(const char *path) {
43 if((s = strrchr(path, '/'))) {
44 while(s > path && s[-1] == '/')
49 return xstrndup(path, s - path);
54 /** @brief Find the extension part of @p path
55 * @param path Path to parse
56 * @return Start of extension in @p path, or NULL
58 * The return value points into @p path and points at the "." at the start of
59 * the path. If the basename has no extension the result is NULL. Extensions
60 * are assumed to only contain the ASCII digits and letters.
62 * See also extension().
64 static const char *find_extension(const char *path) {
65 const char *q = path + strlen(path);
67 while(q > path && strchr("abcdefghijklmnopqrstuvwxyz"
68 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
71 return *q == '.' ? q : 0;
74 /** @brief Strip the extension from @p path
75 * @param path Path to parse
76 * @return @p path with extension removed, or @p path
78 * The extension is defined exactly as for find_extension(). The result might
79 * or might not point into @p path.
81 const char *strip_extension(const char *path) {
82 const char *q = find_extension(path);
84 return q ? xstrndup(path, q - path) : path;
87 /** @brief Find the extension part of @p path
88 * @param path Path to parse
89 * @return Start of extension in @p path, or ""
91 * The return value may points into @p path and if so points at the "." at the
92 * start of the path. If the basename has no extension the result is "".
93 * Extensions are assumed to only contain the ASCII digits and letters.
95 * See also find_extension().
97 const char *extension(const char *path) {
98 const char *q = find_extension(path);