2 * paths.c - Path manipulation routines
6 * Copyright (C) 2012, 2013 Eric Biggers
8 * This file is part of wimlib, a library for working with WIM files.
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)
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
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/.
28 #include "wimlib/paths.h"
29 #include "wimlib/util.h"
33 /* Like the basename() function, but does not modify @path; it just returns a
36 path_basename(const tchar *path)
38 return path_basename_with_len(path, tstrlen(path));
41 /* Like path_basename(), but take an explicit string length. */
43 path_basename_with_len(const tchar *path, size_t len)
45 const tchar *p = &path[len] - 1;
47 /* Trailing slashes. */
56 while ((p != path - 1) && *p != T('/'))
64 * Returns a pointer to the part of @path following the first colon in the last
65 * path component, or NULL if the last path component does not contain a colon.
68 path_stream_name(const tchar *path)
70 const tchar *base = path_basename(path);
71 const tchar *stream_name = tstrchr(base, T(':'));
75 return stream_name + 1;
79 /* Translate backslashes to forward slashes in-place. */
81 zap_backslashes(tchar *s)
84 while (*s != T('\0')) {
92 /* Duplicate a path, with backslashes translated into forward slashes; return
93 * empty string for NULL input. */
95 canonicalize_fs_path(const tchar *fs_path)
97 tchar *canonical_path;
101 canonical_path = TSTRDUP(fs_path);
102 zap_backslashes(canonical_path);
103 return canonical_path;
106 /* Duplicate a path, with backslashes translated into forward slashes; return
107 * empty string for NULL input; also strip leading and trailing slashes. */
109 canonicalize_wim_path(const tchar *wim_path)
112 tchar *canonical_path;
114 if (wim_path == NULL) {
117 while (*wim_path == T('/') || *wim_path == T('\\'))
120 canonical_path = TSTRDUP(wim_path);
121 if (canonical_path) {
122 zap_backslashes(canonical_path);
123 for (p = tstrchr(canonical_path, T('\0')) - 1;
124 p >= canonical_path && *p == T('/');
130 return canonical_path;