Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder | |
011e0b89 | 3 | * Copyright (C) 2005, 2007 Richard Kettlewell |
460b9539 | 4 | * |
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. | |
9 | * | |
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. | |
14 | * | |
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 | |
18 | * USA | |
19 | */ | |
011e0b89 RK |
20 | /** @file lib/filepart.c |
21 | * @brief Filename parsing | |
22 | */ | |
460b9539 | 23 | |
05b75f8d | 24 | #include "common.h" |
460b9539 | 25 | |
26 | #include "filepart.h" | |
27 | #include "mem.h" | |
28 | ||
2257512d RK |
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 | |
33 | */ | |
34 | static void parse_filename(const char *path, | |
35 | char **dirnamep, | |
36 | char **basenamep) { | |
37 | const char *s, *e = path + strlen(path); | |
38 | ||
39 | /* Strip trailing slashes. We never take these into account. */ | |
40 | while(e > path && e[-1] == '/') | |
41 | --e; | |
42 | if(e == path) { | |
43 | /* The path is empty or contains only slashes */ | |
44 | if(*path) { | |
45 | if(dirnamep) | |
46 | *dirnamep = xstrdup("/"); | |
47 | if(basenamep) | |
48 | *basenamep = xstrdup("/"); | |
49 | } else { | |
50 | if(dirnamep) | |
51 | *dirnamep = xstrdup(""); | |
52 | if(basenamep) | |
53 | *basenamep = xstrdup(""); | |
54 | } | |
55 | } else { | |
56 | /* The path isn't empty and has more than just slashes. e therefore now | |
57 | * points at the end of the basename. */ | |
58 | s = e; | |
59 | while(s > path && s[-1] != '/') | |
60 | --s; | |
61 | /* Now s points at the start of the basename */ | |
62 | if(basenamep) | |
63 | *basenamep = xstrndup(s, e - s); | |
64 | if(s > path) { | |
65 | --s; | |
66 | /* s must now be pointing at a '/' before the basename */ | |
67 | assert(*s == '/'); | |
68 | while(s > path && s[-1] == '/') | |
69 | --s; | |
70 | /* Now s must be pointing at the last '/' after the dirname */ | |
71 | assert(*s == '/'); | |
72 | if(s == path) { | |
73 | /* If we reached the start we must be at the root */ | |
74 | if(dirnamep) | |
75 | *dirnamep = xstrdup("/"); | |
76 | } else { | |
77 | /* There's more than just the root here */ | |
78 | if(dirnamep) | |
79 | *dirnamep = xstrndup(path, s - path); | |
80 | } | |
81 | } else { | |
82 | /* There wasn't a slash */ | |
83 | if(dirnamep) | |
84 | *dirnamep = xstrdup("."); | |
85 | } | |
86 | } | |
87 | } | |
88 | ||
011e0b89 RK |
89 | /** @brief Return the directory part of @p path |
90 | * @param path Path to parse | |
91 | * @return Directory part of @p path | |
92 | * | |
93 | * Extracts the directory part of @p path. This is a simple lexical | |
94 | * transformation and no canonicalization is performed. The result will only | |
2257512d RK |
95 | * ever end "/" if it is the root directory. The result will be "." if there |
96 | * is no directory part. | |
011e0b89 | 97 | */ |
460b9539 | 98 | char *d_dirname(const char *path) { |
2257512d | 99 | char *d = 0; |
460b9539 | 100 | |
2257512d RK |
101 | parse_filename(path, &d, 0); |
102 | assert(d != 0); | |
103 | return d; | |
104 | } | |
105 | ||
106 | /** @brief Return the basename part of @p path | |
107 | * @param Path to parse | |
108 | * @return Base part of @p path | |
109 | * | |
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. | |
113 | */ | |
114 | char *d_basename(const char *path) { | |
115 | char *b = 0; | |
116 | ||
117 | parse_filename(path, 0, &b); | |
118 | assert(b != 0); | |
119 | return b; | |
460b9539 | 120 | } |
121 | ||
011e0b89 RK |
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 | |
125 | * | |
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. | |
129 | * | |
130 | * See also extension(). | |
131 | */ | |
460b9539 | 132 | static const char *find_extension(const char *path) { |
133 | const char *q = path + strlen(path); | |
134 | ||
135 | while(q > path && strchr("abcdefghijklmnopqrstuvwxyz" | |
136 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
137 | "0123456789", *q)) | |
138 | --q; | |
139 | return *q == '.' ? q : 0; | |
140 | } | |
141 | ||
011e0b89 RK |
142 | /** @brief Strip the extension from @p path |
143 | * @param path Path to parse | |
144 | * @return @p path with extension removed, or @p path | |
145 | * | |
146 | * The extension is defined exactly as for find_extension(). The result might | |
147 | * or might not point into @p path. | |
148 | */ | |
460b9539 | 149 | const char *strip_extension(const char *path) { |
150 | const char *q = find_extension(path); | |
151 | ||
152 | return q ? xstrndup(path, q - path) : path; | |
153 | } | |
154 | ||
011e0b89 RK |
155 | /** @brief Find the extension part of @p path |
156 | * @param path Path to parse | |
157 | * @return Start of extension in @p path, or "" | |
158 | * | |
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. | |
162 | * | |
163 | * See also find_extension(). | |
164 | */ | |
460b9539 | 165 | const char *extension(const char *path) { |
166 | const char *q = find_extension(path); | |
167 | ||
168 | return q ? q : ""; | |
169 | } | |
170 | ||
171 | /* | |
172 | Local Variables: | |
173 | c-basic-offset:2 | |
174 | comment-column:40 | |
175 | fill-column:79 | |
176 | indent-tabs-mode:nil | |
177 | End: | |
178 | */ |