HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pathUtils.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_BASE_TF_PATH_UTILS_H
8 #define PXR_BASE_TF_PATH_UTILS_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/base/tf/api.h"
13 #include <string>
14 #include <vector>
15 
17 
18 /// \file tf/pathUtils.h
19 /// \ingroup group_tf_Path
20 /// Definitions of basic path utilities in tf.
21 ///
22 /// These are utilities that operate on paths (represented by strings as
23 /// something like: "/chars/Buzz/Torso".
24 
25 /// Returns the canonical path of the specified filename, eliminating any
26 /// symbolic links encountered in the path.
27 ///
28 /// This is a wrapper to realpath(3), which caters for situations where the
29 /// real realpath() would return a NULL string, such as the case where the
30 /// path is really just a program name. The memory allocated by realpath is
31 /// managed internally.
32 ///
33 /// If \a allowInaccessibleSuffix is true, then this function will only invoke
34 /// realpath on the longest accessible prefix of \a path, and then append the
35 /// inaccessible suffix.
36 ///
37 /// If \a error is provided, it is set to the error reason should an error
38 /// occur while computing the real path. If no error occurs, the string is
39 /// cleared.
40 TF_API
41 std::string TfRealPath(std::string const& path,
42  bool allowInaccessibleSuffix = false,
43  std::string* error = 0);
44 
45 /// Normalizes the specified path, eliminating double slashes, etc.
46 ///
47 /// This canonicalizes paths, removing any double slashes, and eliminiating
48 /// '.', and '..' components of the path. This emulates the behavior of
49 /// os.path.normpath in Python. A trailing '/' character will be stripped from
50 /// the result if the input contained one.
51 ///
52 /// On Windows, all backslashes are converted to forward slashes and drive
53 /// specifiers (e.g., "C:") are lower-cased. If \p stripDriveSpecifier
54 /// is \c true, these drive specifiers are removed from the path.
55 TF_API
56 std::string TfNormPath(std::string const& path,
57  bool stripDriveSpecifier = false);
58 
59 /// Return the index delimiting the longest accessible prefix of \a path.
60 ///
61 /// The returned value is safe to use to split the string via it's generalized
62 /// copy constructor. If the entire path is accessible, return the length of
63 /// the input string. If none of the path is accessible, return 0. Otherwise
64 /// the index points to the path separator that delimits the existing prefix
65 /// from the non-existing suffix.
66 ///
67 /// Examples: suppose the paths /, /usr, and /usr/anim exist, but no other
68 /// paths exist.
69 ///
70 /// TfFindLongestAccessiblePrefix('/usr/anim') -> 9
71 /// TfFindLongestAccessiblePrefix('/usr/anim/foo') -> 9
72 /// TfFindLongestAccessiblePrefix('/foo/bar') -> 0
73 ///
74 /// If an error occurs, and the \a error string is not null, it is set to the
75 /// reason for the error. If the error string is set, the returned index is
76 /// the path separator before the element at which the error occurred.
77 TF_API
78 std::string::size_type
79 TfFindLongestAccessiblePrefix(std::string const &path, std::string* error = 0);
80 
81 /// Returns the canonical absolute path of the specified filename.
82 ///
83 /// This makes the specified path absolute, by prepending the current working
84 /// directory. If the path is already absolute, it is returned unmodified.
85 /// This function differs from TfRealPath in that the path may point to a
86 /// symlink, or not exist at all, and still result in an absolute path, rather
87 /// than an empty string.
88 TF_API
89 std::string TfAbsPath(std::string const& path);
90 
91 /// Returns the extension for a file path
92 ///
93 /// If \p path is a directory path, an empty path, or a dotfile path, return
94 /// the empty string. Otherwise return \p path 's dot-separated extension as
95 /// a string(dot not included).
96 ///
97 /// Examples:
98 ///
99 /// TfGetExtension('/foo/bar') -> ''
100 /// TfGetExtension('/foo/bar/foo.baz') -> 'baz'
101 /// TfGetExtension('/foo.bar/foo.baz') -> 'baz'
102 /// TfGetExtension('/foo/bar/foo.101.baz') -> 'baz'
103 /// TfGetExtension('/foo/bar/.foo.baz') -> 'baz'
104 /// TfGetExtension('/foo/bar/.foo') -> ''
105 TF_API
106 std::string TfGetExtension(std::string const& path);
107 
108 /// Returns the value of a symbolic link. Returns the empty string on
109 /// error or if path is not a symbolic link.
110 TF_API
111 std::string TfReadLink(std::string const& path);
112 
113 /// Return true if and only if a path is relative (not absolute).
114 TF_API
115 bool TfIsRelativePath(std::string const& path);
116 
117 /// Expands one or more shell glob patterns.
118 ///
119 /// This is a wrapper to glob(3), which manages the C structures necessary to
120 /// glob a pattern, returning a std::vector of results. If no flags are
121 /// specified, the GLOB_MARK and GLOB_NOCHECK flags are set by default.
122 /// GLOB_MARK marks directories which match the glob pattern with a trailing
123 /// slash. GLOB_NOCHECK returns any unexpanded patterns in the result.
124 TF_API
125 std::vector<std::string> TfGlob(std::vector<std::string> const& paths,
126  unsigned int flags=ARCH_GLOB_DEFAULT);
127 
128 /// Expands a shell glob pattern.
129 ///
130 /// This form of Glob calls TfGlob. For efficiency reasons, if expanding more
131 /// than one pattern, use the vector form. As with the vector form of TfGlob,
132 /// if flags is not set, the default glob flags are GLOB_MARK and
133 /// GLOB_NOCHECK.
134 TF_API
135 std::vector<std::string> TfGlob(std::string const& path,
136  unsigned int flags=ARCH_GLOB_DEFAULT);
137 
139 
140 #endif /* PXR_BASE_TF_PATH_UTILS_H */
GLbitfield flags
Definition: glcorearb.h:1596
#define TF_API
Definition: api.h:23
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
TF_API std::string TfNormPath(std::string const &path, bool stripDriveSpecifier=false)
TF_API std::string TfRealPath(std::string const &path, bool allowInaccessibleSuffix=false, std::string *error=0)
< returns > If no error
Definition: snippets.dox:2
#define ARCH_GLOB_DEFAULT
Definition: fileSystem.h:71
TF_API std::string TfReadLink(std::string const &path)
TF_API bool TfIsRelativePath(std::string const &path)
Return true if and only if a path is relative (not absolute).
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
TF_API std::string TfAbsPath(std::string const &path)
TF_API std::string::size_type TfFindLongestAccessiblePrefix(std::string const &path, std::string *error=0)
TF_API std::vector< std::string > TfGlob(std::vector< std::string > const &paths, unsigned int flags=ARCH_GLOB_DEFAULT)
TF_API std::string TfGetExtension(std::string const &path)