HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_DirUtil.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_DirUtil.C (UT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef _UT_DIRUTIL_H_
12 #define _UT_DIRUTIL_H_
13 
14 #include "UT_API.h"
15 #include "UT_FileStat.h"
16 #include <SYS/SYS_Deprecated.h>
17 
18 #include <time.h>
19 
20 class UT_FileStat;
21 class UT_String;
22 class UT_StringArray;
23 class UT_StringHolder;
24 
25 // Absolute path prefixes can be specified as one of 3 type:
26 // Default: the prefix can be followed by an absolute path, beginning in '/'
27 // ie no drive letter support
28 // AllowAbsolutePathSuffix: the prefix can be followed by an absolute path,
29 // beginning in either '/' or a drive letter (ie file:/ file:C:/)
30 // AlwaysAbsolute: the prefix always specifies an absolute path, regardless
31 // if suffix is followed by '/', a drive letter, or something else.
33 {
37 };
38 
39 UT_API int UTunixFullPathSpecified(const char *name);
40 UT_API int UTfullPathSpecified(const char *name);
41 // This, unlike, UTfullPathSpecified, returns false for ./filename
42 UT_API bool UTisAbsolutePath(const char *filename);
43 UT_API bool UTisRootPath(const char *filename);
44 
45 /// UTgetRootPrefixLength() returns the length of the root part of the file
46 /// path.
47 ///
48 /// The remainder of the file path after the root part length will have
49 /// one of the following forms:
50 /// '/path/to/file' => for absolute paths
51 /// 'relative/path/to/file' => for relative paths
52 /// '' => for root paths
53 ///
54 /// Some examples of input file paths and output root lengths:
55 /// '/path/to/file' => 0
56 /// 'relative/path/to/file' => 0
57 /// 'C:/path/to/file' => 2 (Windows only)
58 /// 'C:relative/path/to/file' => 2 (Windows only)
59 /// '//path/to/file' => 1 (Windows only)
60 /// '//path/to/file' => 0 (Unix only)
61 /// 'file:///path/to/file' => 5
62 /// 'file:/path/to/file' => 5
63 /// 'file:relative/path/to/file'=> 5
64 UT_API int UTgetRootPrefixLength(const char *filename);
65 
66 /// Functions to manage the set of prefixes which Houdini treats as being
67 /// valid ways to start an absolute path.
69 /// The "allow_absolute_path_suffix" parameter can be set to true to indicate
70 /// that this root prefix can be followed by an absolute path to a file on
71 /// disk. This matters because on Windows, an absolute path can start with
72 /// "C:", which doesn't look like an absolute path by the normal metric of
73 /// looking for a leading slash.
74 UT_API void UTaddAbsolutePathPrefix(const char *prefix,
76 
77 /// Functions to convert absolute paths into relative paths and vice versa,
78 /// given a path and a "base" path which is used as the root location for
79 /// relative paths. When basepath is not given, getUnixCwd() will be used.
81  const char *basepath = nullptr);
83  const char *basepath = nullptr);
85  const char *basepath = nullptr,
86  bool allow_relative_path_from_root = true);
88  const char *basepath = nullptr,
89  bool allow_relative_path_from_root = true);
90 
91 /// Resolves symlinks, symbolic links on a path.
92 /// Calls ::realpath() or fs::canonical() depdending of the platform.
95 
96 /// Same as UTrealPath but with support for missing files.
97 UT_API void UTrealPath(UT_String &path, bool support_missing_files);
98 UT_API void UTrealPath(UT_StringHolder &path, bool support_missing_files);
99 
100 /// Normalizes a file path. This means ensuring all slashes are forward
101 /// slashes, removing redundant slashes, and collapsing '..' and '.'
102 /// components as much as possible (whether they occur at the start or
103 /// in the middle of the path).
106 
107 /// If the path starts with "file:", strip off however much prefix is required
108 /// to get the to "path" path. Note that there may be 0, 1, 2, or 3 slashes
109 /// after "file:", and behavior may need to change based on OS (e.g. on
110 /// Windows file:///C:/foo -> C:/foo, otherwise file:///foo/bar -> /foo/bar.
113 
114 /// Checks if the file name starts with '.'. basepath (parent directory)
115 /// is ignored in non Windows envirenment, whereas in Windows full path
116 /// is needed to see if the file has the hidden attribute.
117 UT_API bool UTisHiddenFile(const char *basepath, const char *filename);
118 
119 /// Returns true if the given path is a readable, _regular_ file, ie. not a
120 /// directory.
121 UT_API bool UTisValidRegularFile(const char *path);
122 
123 /// Returns true if the given path is a readable _directory_.
124 UT_API bool UTisValidDirectory(const char *path);
125 
126 /// Returns true if path is a _directory_, without permission checks
127 UT_API bool UTisDirectory(const char *path);
128 
129 /// Returns true if path is a _regular_ file, without permission checks
130 UT_API bool UTisRegularFile(const char *path);
131 
132 /// Returns true if file path exists, without permission checks.
133 /// This is optimized for known platforms.
134 UT_API bool UTfileExists(const char *path);
135 
136 #ifdef WIN32
137  #include <sys/types.h>
138  #include <direct.h>
139  #include <errno.h>
140 
141  // Define dummy value to compare with errno in assertions related
142  // to Linux libraries and large file systems. This define now exists on
143  // newer versions of Visual Studio. The current value is defined to be the
144  // same as the one found in VC10.
145  #ifndef EOVERFLOW
146  #define EOVERFLOW 132
147  #endif
148 
149  namespace { struct ut_DirData; }
150 
151  typedef void* DIR;
152  struct dirent
153  {
154  public:
155  char *d_name;
156 
157  private:
158  void *myFindData;
159 
160  friend UT_API int statLastRead(dirent *, const char *, UT_FileStat &);
161  friend struct ut_DirData;
162  };
163  UT_API DIR *opendir( const char *name );
164  UT_API struct dirent *readdir( DIR *dirp );
165  UT_API void closedir( DIR *dirp );
166 
167  /// Get the current working directory
168  UT_API char *getUnixCwd( char *buffer, int maxlen );
169 #else
170  #include <sys/types.h>
171  #include <sys/stat.h>
172  #include <string.h>
173  #include <dirent.h>
174  #include <unistd.h>
175  #include <stdlib.h>
176 
177  /// Get the current working directory
178  UT_API char *getUnixCwd( char *buffer, int maxlen );
179 #endif
180 
181 /// Do the equivalent of a stat on the last file from readdir. This will perform
182 /// a stat call on Linux.
183 UT_API int statLastRead(dirent *dp, const char *path, UT_FileStat &file_stat);
184 
185 /// Specialization that only returns the file type of the directory entry. This
186 /// should be called instead of statLastRead if you only need the type of the
187 /// entry.
188 ///
189 /// Note: On Linux, if this is called and the entry is a symlink or the
190 /// underlying filesystem doesn't support obtaining file types when accessing a
191 /// directory, this will incur a stat call.
192 ///
193 /// Note: On Linux, if check_executable is true, this will incur a stat call.
194 UT_API int statLastRead(dirent *dp, const char *path,
195  UT_FileStat::FileType &file_type, const bool check_executable);
196 
197 #endif
198 
GT_API const UT_StringHolder filename
UT_API int UTunixFullPathSpecified(const char *name)
UT_API bool UTisRootPath(const char *filename)
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
UT_API const UT_StringArray & UTgetAbsolutePathPrefixes()
UT_API void UTstripFileUrlPrefix(UT_String &path)
#define UT_API
Definition: UT_API.h:14
UT_API bool UTisValidDirectory(const char *path)
Returns true if the given path is a readable directory.
UT_API int UTgetRootPrefixLength(const char *filename)
UT_API void UTmakeRelativeFilePath(UT_String &path, const char *basepath=nullptr, bool allow_relative_path_from_root=true)
GLuint buffer
Definition: glcorearb.h:660
UT_API bool UTisHiddenFile(const char *basepath, const char *filename)
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
UT_API void UTaddAbsolutePathPrefix(const char *prefix, UT_AbsolutePrefixType type=Default)
UT_API void UTmakeAbsoluteFilePath(UT_String &path, const char *basepath=nullptr)
UT_API int statLastRead(dirent *dp, const char *path, UT_FileStat &file_stat)
UT_API int UTfullPathSpecified(const char *name)
GLuint const GLchar * name
Definition: glcorearb.h:786
UT_API bool UTfileExists(const char *path)
UT_API bool UTisRegularFile(const char *path)
Returns true if path is a regular file, without permission checks.
UT_API char * getUnixCwd(char *buffer, int maxlen)
Get the current working directory.
UT_API void UTnormalizeFilePath(UT_String &path)
UT_API bool UTisDirectory(const char *path)
Returns true if path is a directory, without permission checks.
UT_API bool UTisAbsolutePath(const char *filename)
UT_API void UTrealPath(UT_String &path)
UT_AbsolutePrefixType
Definition: UT_DirUtil.h:32
UT_API bool UTisValidRegularFile(const char *path)