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 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU 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, see <http://www.gnu.org/licenses/>.
18 /** @file lib/filepart.c
19 * @brief Filename parsing
27 /** @brief Parse a filename
28 * @param path Filename to parse
29 * @param dirnamep Where to put directory name, or NULL
30 * @param basenamep Where to put basename, or NULL
32 static void parse_filename(const char *path,
35 const char *s, *e = path + strlen(path);
37 /* Strip trailing slashes. We never take these into account. */
38 while(e > path && e[-1] == '/')
41 /* The path is empty or contains only slashes */
44 *dirnamep = xstrdup("/");
46 *basenamep = xstrdup("/");
49 *dirnamep = xstrdup("");
51 *basenamep = xstrdup("");
54 /* The path isn't empty and has more than just slashes. e therefore now
55 * points at the end of the basename. */
57 while(s > path && s[-1] != '/')
59 /* Now s points at the start of the basename */
61 *basenamep = xstrndup(s, e - s);
64 /* s must now be pointing at a '/' before the basename */
66 while(s > path && s[-1] == '/')
68 /* Now s must be pointing at the last '/' after the dirname */
71 /* If we reached the start we must be at the root */
73 *dirnamep = xstrdup("/");
75 /* There's more than just the root here */
77 *dirnamep = xstrndup(path, s - path);
80 /* There wasn't a slash */
82 *dirnamep = xstrdup(".");
87 /** @brief Return the directory part of @p path
88 * @param path Path to parse
89 * @return Directory part of @p path
91 * Extracts the directory part of @p path. This is a simple lexical
92 * transformation and no canonicalization is performed. The result will only
93 * ever end "/" if it is the root directory. The result will be "." if there
94 * is no directory part.
96 char *d_dirname(const char *path) {
99 parse_filename(path, &d, 0);
104 /** @brief Return the basename part of @p path
105 * @param path Path to parse
106 * @return Base part of @p path
108 * Extracts the base part of @p path. This is a simple lexical transformation
109 * and no canonicalization is performed. The result is always newly allocated
110 * even if compares equal to @p path.
112 char *d_basename(const char *path) {
115 parse_filename(path, 0, &b);
120 /** @brief Find the extension part of @p path
121 * @param path Path to parse
122 * @return Start of extension in @p path, or NULL
124 * The return value points into @p path and points at the "." at the start of
125 * the path. If the basename has no extension the result is NULL. Extensions
126 * are assumed to only contain the ASCII digits and letters.
128 * See also extension().
130 static const char *find_extension(const char *path) {
131 const char *q = path + strlen(path);
133 while(q > path && strchr("abcdefghijklmnopqrstuvwxyz"
134 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
137 return *q == '.' ? q : 0;
140 /** @brief Strip the extension from @p path
141 * @param path Path to parse
142 * @return @p path with extension removed, or @p path
144 * The extension is defined exactly as for find_extension(). The result might
145 * or might not point into @p path.
147 const char *strip_extension(const char *path) {
148 const char *q = find_extension(path);
150 return q ? xstrndup(path, q - path) : path;
153 /** @brief Find the extension part of @p path
154 * @param path Path to parse
155 * @return Start of extension in @p path, or ""
157 * The return value may points into @p path and if so points at the "." at the
158 * start of the path. If the basename has no extension the result is "".
159 * Extensions are assumed to only contain the ASCII digits and letters.
161 * See also find_extension().
163 const char *extension(const char *path) {
164 const char *q = find_extension(path);