chiark / gitweb /
Move player/decoder PIDs back into the main queue_entry structure, now
[disorder] / lib / filepart.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
011e0b89 3 * Copyright (C) 2005, 2007 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
e7eb3a27
RK
9 *
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.
14 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
011e0b89
RK
18/** @file lib/filepart.c
19 * @brief Filename parsing
20 */
460b9539 21
05b75f8d 22#include "common.h"
460b9539 23
24#include "filepart.h"
25#include "mem.h"
26
2257512d
RK
27/** @brief Parse a filename
28 * @param path Filename to parse
59cf25c4
RK
29 * @param dirnamep Where to put directory name, or NULL
30 * @param basenamep Where to put basename, or NULL
2257512d
RK
31 */
32static void parse_filename(const char *path,
33 char **dirnamep,
34 char **basenamep) {
35 const char *s, *e = path + strlen(path);
36
37 /* Strip trailing slashes. We never take these into account. */
38 while(e > path && e[-1] == '/')
39 --e;
40 if(e == path) {
41 /* The path is empty or contains only slashes */
42 if(*path) {
43 if(dirnamep)
44 *dirnamep = xstrdup("/");
45 if(basenamep)
46 *basenamep = xstrdup("/");
47 } else {
48 if(dirnamep)
49 *dirnamep = xstrdup("");
50 if(basenamep)
51 *basenamep = xstrdup("");
52 }
53 } else {
54 /* The path isn't empty and has more than just slashes. e therefore now
55 * points at the end of the basename. */
56 s = e;
57 while(s > path && s[-1] != '/')
58 --s;
59 /* Now s points at the start of the basename */
60 if(basenamep)
61 *basenamep = xstrndup(s, e - s);
62 if(s > path) {
63 --s;
64 /* s must now be pointing at a '/' before the basename */
65 assert(*s == '/');
66 while(s > path && s[-1] == '/')
67 --s;
68 /* Now s must be pointing at the last '/' after the dirname */
69 assert(*s == '/');
70 if(s == path) {
71 /* If we reached the start we must be at the root */
72 if(dirnamep)
73 *dirnamep = xstrdup("/");
74 } else {
75 /* There's more than just the root here */
76 if(dirnamep)
77 *dirnamep = xstrndup(path, s - path);
78 }
79 } else {
80 /* There wasn't a slash */
81 if(dirnamep)
82 *dirnamep = xstrdup(".");
83 }
84 }
85}
86
011e0b89
RK
87/** @brief Return the directory part of @p path
88 * @param path Path to parse
89 * @return Directory part of @p path
90 *
91 * Extracts the directory part of @p path. This is a simple lexical
92 * transformation and no canonicalization is performed. The result will only
2257512d
RK
93 * ever end "/" if it is the root directory. The result will be "." if there
94 * is no directory part.
011e0b89 95 */
460b9539 96char *d_dirname(const char *path) {
2257512d 97 char *d = 0;
460b9539 98
2257512d
RK
99 parse_filename(path, &d, 0);
100 assert(d != 0);
101 return d;
102}
103
104/** @brief Return the basename part of @p path
59cf25c4 105 * @param path Path to parse
2257512d
RK
106 * @return Base part of @p path
107 *
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.
111 */
112char *d_basename(const char *path) {
113 char *b = 0;
114
115 parse_filename(path, 0, &b);
116 assert(b != 0);
117 return b;
460b9539 118}
119
011e0b89
RK
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
123 *
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.
127 *
128 * See also extension().
129 */
460b9539 130static const char *find_extension(const char *path) {
131 const char *q = path + strlen(path);
132
133 while(q > path && strchr("abcdefghijklmnopqrstuvwxyz"
134 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
135 "0123456789", *q))
136 --q;
137 return *q == '.' ? q : 0;
138}
139
011e0b89
RK
140/** @brief Strip the extension from @p path
141 * @param path Path to parse
142 * @return @p path with extension removed, or @p path
143 *
144 * The extension is defined exactly as for find_extension(). The result might
145 * or might not point into @p path.
146 */
460b9539 147const char *strip_extension(const char *path) {
148 const char *q = find_extension(path);
149
150 return q ? xstrndup(path, q - path) : path;
151}
152
011e0b89
RK
153/** @brief Find the extension part of @p path
154 * @param path Path to parse
155 * @return Start of extension in @p path, or ""
156 *
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.
160 *
161 * See also find_extension().
162 */
460b9539 163const char *extension(const char *path) {
164 const char *q = find_extension(path);
165
166 return q ? q : "";
167}
168
169/*
170Local Variables:
171c-basic-offset:2
172comment-column:40
173fill-column:79
174indent-tabs-mode:nil
175End:
176*/