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.
50 ///
51 /// On Windows, all backslashes are converted to forward slashes and drive
52 /// specifiers (e.g., "C:") are lower-cased. If \p stripDriveSpecifier
53 /// is \c true, these drive specifiers are removed from the path.
54 TF_API
55 std::string TfNormPath(std::string const& path,
56  bool stripDriveSpecifier = false);
57 
58 /// Return the index delimiting the longest accessible prefix of \a path.
59 ///
60 /// The returned value is safe to use to split the string via it's generalized
61 /// copy constructor. If the entire path is accessible, return the length of
62 /// the input string. If none of the path is accessible, return 0. Otherwise
63 /// the index points to the path separator that delimits the existing prefix
64 /// from the non-existing suffix.
65 ///
66 /// Examples: suppose the paths /, /usr, and /usr/anim exist, but no other
67 /// paths exist.
68 ///
69 /// TfFindLongestAccessiblePrefix('/usr/anim') -> 9
70 /// TfFindLongestAccessiblePrefix('/usr/anim/foo') -> 9
71 /// TfFindLongestAccessiblePrefix('/foo/bar') -> 0
72 ///
73 /// If an error occurs, and the \a error string is not null, it is set to the
74 /// reason for the error. If the error string is set, the returned index is
75 /// the path separator before the element at which the error occurred.
76 TF_API
77 std::string::size_type
78 TfFindLongestAccessiblePrefix(std::string const &path, std::string* error = 0);
79 
80 /// Returns the canonical absolute path of the specified filename.
81 ///
82 /// This makes the specified path absolute, by prepending the current working
83 /// directory. If the path is already absolute, it is returned unmodified.
84 /// This function differs from TfRealPath in that the path may point to a
85 /// symlink, or not exist at all, and still result in an absolute path, rather
86 /// than an empty string.
87 TF_API
88 std::string TfAbsPath(std::string const& path);
89 
90 /// Returns the extension for a file path
91 ///
92 /// If \p path is a directory path, an empty path, or a dotfile path, return
93 /// the empty string. Otherwise return \p path 's dot-separated extension as
94 /// a string(dot not included).
95 ///
96 /// Examples:
97 ///
98 /// TfGetExtension('/foo/bar') -> ''
99 /// TfGetExtension('/foo/bar/foo.baz') -> 'baz'
100 /// TfGetExtension('/foo.bar/foo.baz') -> 'baz'
101 /// TfGetExtension('/foo/bar/foo.101.baz') -> 'baz'
102 /// TfGetExtension('/foo/bar/.foo.baz') -> 'baz'
103 /// TfGetExtension('/foo/bar/.foo') -> ''
104 TF_API
105 std::string TfGetExtension(std::string const& path);
106 
107 /// Returns the value of a symbolic link. Returns the empty string on
108 /// error or if path is not a symbolic link.
109 TF_API
110 std::string TfReadLink(std::string const& path);
111 
112 /// Return true if and only if a path is relative (not absolute).
113 TF_API
114 bool TfIsRelativePath(std::string const& path);
115 
116 /// Expands one or more shell glob patterns.
117 ///
118 /// This is a wrapper to glob(3), which manages the C structures necessary to
119 /// glob a pattern, returning a std::vector of results. If no flags are
120 /// specified, the GLOB_MARK and GLOB_NOCHECK flags are set by default.
121 /// GLOB_MARK marks directories which match the glob pattern with a trailing
122 /// slash. GLOB_NOCHECK returns any unexpanded patterns in the result.
123 TF_API
124 std::vector<std::string> TfGlob(std::vector<std::string> const& paths,
125  unsigned int flags=ARCH_GLOB_DEFAULT);
126 
127 /// Expands a shell glob pattern.
128 ///
129 /// This form of Glob calls TfGlob. For efficiency reasons, if expanding more
130 /// than one pattern, use the vector form. As with the vector form of TfGlob,
131 /// if flags is not set, the default glob flags are GLOB_MARK and
132 /// GLOB_NOCHECK.
133 TF_API
134 std::vector<std::string> TfGlob(std::string const& path,
135  unsigned int flags=ARCH_GLOB_DEFAULT);
136 
138 
139 #endif /* PXR_BASE_TF_PATH_UTILS_H */
GLbitfield flags
Definition: glcorearb.h:1596
#define TF_API
Definition: api.h:23
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:72
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).
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#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)