]> wimlib.net Git - wimlib/blob - src/paths.c
A few comment fixes
[wimlib] / src / paths.c
1 /*
2  * paths.c - Path manipulation routines
3  */
4
5 /*
6  * Copyright (C) 2012, 2013 Eric Biggers
7  *
8  * This file is part of wimlib, a library for working with WIM files.
9  *
10  * wimlib is free software; you can redistribute it and/or modify it under the
11  * terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 3 of the License, or (at your option)
13  * any later version.
14  *
15  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17  * A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with wimlib; if not, see http://www.gnu.org/licenses/.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "wimlib.h"
29 #include "wimlib/paths.h"
30 #include "wimlib/util.h"
31
32 #include <string.h>
33
34 /* Like the basename() function, but does not modify @path; it just returns a
35  * pointer to it.  This assumes the path separator is the
36  * OS_PREFERRED_PATH_SEPARATOR.  */
37 const tchar *
38 path_basename(const tchar *path)
39 {
40         return path_basename_with_len(path, tstrlen(path));
41 }
42
43 /* Like path_basename(), but take an explicit string length.  */
44 const tchar *
45 path_basename_with_len(const tchar *path, size_t len)
46 {
47         const tchar *p = &path[len];
48
49         do {
50                 if (p == path)
51                         return &path[len];
52         } while (*--p == OS_PREFERRED_PATH_SEPARATOR);
53
54         do {
55                 if (p == path)
56                         return &path[0];
57         } while (*--p != OS_PREFERRED_PATH_SEPARATOR);
58
59         return ++p;
60 }
61
62
63 /* Returns a pointer to the part of @path following the first colon in the last
64  * path component, or NULL if the last path component does not contain a colon
65  * or has no characters following the first colon.  */
66 const tchar *
67 path_stream_name(const tchar *path)
68 {
69         const tchar *base = path_basename(path);
70         const tchar *stream_name = tstrchr(base, T(':'));
71         if (stream_name == NULL || *(stream_name + 1) == T('\0'))
72                 return NULL;
73         else
74                 return stream_name + 1;
75 }
76
77 /* Collapse and translate path separators, and strip trailing slashes.  Doesn't
78  * add or delete a leading slash.
79  *
80  * @in may alias @out.
81  */
82 void
83 do_canonicalize_path(const tchar *in, tchar *out)
84 {
85         tchar *orig_out = out;
86
87         while (*in) {
88                 if (is_any_path_separator(*in)) {
89                         /* Collapse multiple path separators into one  */
90                         *out++ = WIM_PATH_SEPARATOR;
91                         do {
92                                 in++;
93                         } while (is_any_path_separator(*in));
94                 } else {
95                         /* Copy non-path-separator character  */
96                         *out++ = *in++;
97                 }
98         }
99
100         /* Remove trailing slash if existent  */
101         if (out - orig_out > 1 && *(out - 1) == WIM_PATH_SEPARATOR)
102                 --out;
103
104         *out = T('\0');
105 }
106
107 /*
108  * canonicalize_wim_path() - Given a user-provided path to a file within a WIM
109  * image, translate it into a "canonical" path.
110  *
111  * - Translate both types of slash into a consistent type (WIM_PATH_SEPARATOR).
112  * - Collapse path separators.
113  * - Add leading slash if missing.
114  * - Strip trailing slashes.
115  *
116  * Examples (with WIM_PATH_SEPARATOR == '/'):
117  *
118  *              => /            [ either NULL or empty string ]
119  * /            => /
120  * \            => /
121  * hello        => /hello
122  * \hello       => /hello
123  * \hello       => /hello
124  * /hello/      => /hello
125  * \hello/      => /hello
126  * /hello//1    => /hello/1
127  * \\hello\\1\\ => /hello/1
128  */
129 tchar *
130 canonicalize_wim_path(const tchar *wim_path)
131 {
132         const tchar *in;
133         tchar *out;
134         tchar *result;
135
136         in = wim_path;
137         if (!in)
138                 in = T("");
139
140         result = MALLOC((1 + tstrlen(in) + 1) * sizeof(result[0]));
141         if (!result)
142                 return NULL;
143
144         out = result;
145
146         /* Add leading slash if missing  */
147         if (!is_any_path_separator(*in))
148                 *out++ = WIM_PATH_SEPARATOR;
149
150         do_canonicalize_path(in, out);
151
152         return result;
153 }