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
29 /** @brief Parse a filename
30 * @param path Filename to parse
31 * @param Where to put directory name, or NULL
32 * @param Where to put basename, or NULL
34 static void parse_filename(const char *path
,
37 const char *s
, *e
= path
+ strlen(path
);
39 /* Strip trailing slashes. We never take these into account. */
40 while(e
> path
&& e
[-1] == '/')
43 /* The path is empty or contains only slashes */
46 *dirnamep
= xstrdup("/");
48 *basenamep
= xstrdup("/");
51 *dirnamep
= xstrdup("");
53 *basenamep
= xstrdup("");
56 /* The path isn't empty and has more than just slashes. e therefore now
57 * points at the end of the basename. */
59 while(s
> path
&& s
[-1] != '/')
61 /* Now s points at the start of the basename */
63 *basenamep
= xstrndup(s
, e
- s
);
66 /* s must now be pointing at a '/' before the basename */
68 while(s
> path
&& s
[-1] == '/')
70 /* Now s must be pointing at the last '/' after the dirname */
73 /* If we reached the start we must be at the root */
75 *dirnamep
= xstrdup("/");
77 /* There's more than just the root here */
79 *dirnamep
= xstrndup(path
, s
- path
);
82 /* There wasn't a slash */
84 *dirnamep
= xstrdup(".");
89 /** @brief Return the directory part of @p path
90 * @param path Path to parse
91 * @return Directory part of @p path
93 * Extracts the directory part of @p path. This is a simple lexical
94 * transformation and no canonicalization is performed. The result will only
95 * ever end "/" if it is the root directory. The result will be "." if there
96 * is no directory part.
98 char *d_dirname(const char *path
) {
101 parse_filename(path
, &d
, 0);
106 /** @brief Return the basename part of @p path
107 * @param Path to parse
108 * @return Base part of @p path
110 * Extracts the base part of @p path. This is a simple lexical transformation
111 * and no canonicalization is performed. The result is always newly allocated
112 * even if compares equal to @p path.
114 char *d_basename(const char *path
) {
117 parse_filename(path
, 0, &b
);
122 /** @brief Find the extension part of @p path
123 * @param path Path to parse
124 * @return Start of extension in @p path, or NULL
126 * The return value points into @p path and points at the "." at the start of
127 * the path. If the basename has no extension the result is NULL. Extensions
128 * are assumed to only contain the ASCII digits and letters.
130 * See also extension().
132 static const char *find_extension(const char *path
) {
133 const char *q
= path
+ strlen(path
);
135 while(q
> path
&& strchr("abcdefghijklmnopqrstuvwxyz"
136 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
139 return *q
== '.' ? q
: 0;
142 /** @brief Strip the extension from @p path
143 * @param path Path to parse
144 * @return @p path with extension removed, or @p path
146 * The extension is defined exactly as for find_extension(). The result might
147 * or might not point into @p path.
149 const char *strip_extension(const char *path
) {
150 const char *q
= find_extension(path
);
152 return q ?
xstrndup(path
, q
- path
) : path
;
155 /** @brief Find the extension part of @p path
156 * @param path Path to parse
157 * @return Start of extension in @p path, or ""
159 * The return value may points into @p path and if so points at the "." at the
160 * start of the path. If the basename has no extension the result is "".
161 * Extensions are assumed to only contain the ASCII digits and letters.
163 * See also find_extension().
165 const char *extension(const char *path
) {
166 const char *q
= find_extension(path
);