]> wimlib.net Git - wimlib/blob - src/paths.c
23e8205054e6fa7728b7ae431c65d9dc564889bc
[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] - 1;
48
49         /* Trailing slashes. */
50         while (1) {
51                 if (p == path - 1)
52                         return T("");
53                 if (*p != OS_PREFERRED_PATH_SEPARATOR)
54                         break;
55                 p--;
56         }
57
58         while ((p != path - 1) && *p != OS_PREFERRED_PATH_SEPARATOR)
59                 p--;
60
61         return p + 1;
62 }
63
64
65 /*
66  * Returns a pointer to the part of @path following the first colon in the last
67  * path component, or NULL if the last path component does not contain a colon
68  * or has no characters following the first colon.
69  */
70 const tchar *
71 path_stream_name(const tchar *path)
72 {
73         const tchar *base = path_basename(path);
74         const tchar *stream_name = tstrchr(base, T(':'));
75         if (stream_name == NULL || *(stream_name + 1) == T('\0'))
76                 return NULL;
77         else
78                 return stream_name + 1;
79 }
80
81
82 /* Duplicate a path; return empty string for NULL input. */
83 tchar *
84 canonicalize_fs_path(const tchar *fs_path)
85 {
86         if (!fs_path)
87                 fs_path = T("");
88         return TSTRDUP(fs_path);
89 }
90
91 /*
92  * canonicalize_wim_path - Given a user-provided path to a file within a WIM
93  * image, translate it into a "canonical" path.
94  *
95  * To do this, we translate all supported path separators
96  * (is_any_path_separator()) into the WIM_PATH_SEPARATOR, and strip any leading
97  * and trailing slashes.  The returned string is allocated.  Note that there
98  * still may be consecutive path separators within the string.  Furthermore, the
99  * string may be empty, which indicates the root dentry of the WIM image.
100  */
101 tchar *
102 canonicalize_wim_path(const tchar *wim_path)
103 {
104         tchar *p;
105         tchar *canonical_path;
106
107         if (wim_path == NULL) {
108                 wim_path = T("");
109         } else {
110                 while (is_any_path_separator(*wim_path))
111                         wim_path++;
112         }
113         canonical_path = TSTRDUP(wim_path);
114         if (canonical_path) {
115                 for (p = canonical_path; *p; p++)
116                         if (is_any_path_separator(*p))
117                                 *p = WIM_PATH_SEPARATOR;
118                 for (p = tstrchr(canonical_path, T('\0')) - 1;
119                      p >= canonical_path && *p == WIM_PATH_SEPARATOR;
120                      p--)
121                 {
122                         *p = T('\0');
123                 }
124         }
125         return canonical_path;
126 }