chiark / gitweb /
ALSA mixer support.
[disorder] / lib / filepart.c
1 /*
2  * This file is part of DisOrder
3  * Copyright (C) 2005, 2007 Richard Kettlewell
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  */
20 /** @file lib/filepart.c
21  * @brief Filename parsing
22  */
23
24 #include <config.h>
25 #include "types.h"
26
27 #include <string.h>
28
29 #include "filepart.h"
30 #include "mem.h"
31
32 /** @brief Return the directory part of @p path
33  * @param path Path to parse
34  * @return Directory part of @p path
35  *
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.
39  */
40 char *d_dirname(const char *path) {
41   const char *s;
42
43   if((s = strrchr(path, '/'))) {
44     while(s > path && s[-1] == '/')
45       --s;
46     if(s == path)
47       return xstrdup("/");
48     else
49       return xstrndup(path, s - path);
50   } else
51     return xstrdup(".");
52 }
53
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
57  *
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.
61  *
62  * See also extension().
63  */
64 static const char *find_extension(const char *path) {
65   const char *q = path + strlen(path);
66   
67   while(q > path && strchr("abcdefghijklmnopqrstuvwxyz"
68                         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
69                         "0123456789", *q))
70     --q;
71   return *q == '.' ? q : 0;
72 }
73
74 /** @brief Strip the extension from @p path
75  * @param path Path to parse
76  * @return @p path with extension removed, or @p path
77  *
78  * The extension is defined exactly as for find_extension().  The result might
79  * or might not point into @p path.
80  */
81 const char *strip_extension(const char *path) {
82   const char *q = find_extension(path);
83
84   return q ? xstrndup(path, q - path) : path;
85 }
86
87 /** @brief Find the extension part of @p path
88  * @param path Path to parse
89  * @return Start of extension in @p path, or ""
90  *
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.
94  *
95  * See also find_extension().
96  */
97 const char *extension(const char *path) {
98   const char *q = find_extension(path);
99
100   return q ? q : "";
101 }
102
103 /*
104 Local Variables:
105 c-basic-offset:2
106 comment-column:40
107 fill-column:79
108 indent-tabs-mode:nil
109 End:
110 */