HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_PathSearch.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_PathSearch.h ( FS Library, C++)
7  *
8  * COMMENTS:
9  * This class defines Houdini path searching. The class assumes that you
10  * will be searching for sub-paths within a given path. Each path class
11  * will cache the list of directories to scan for. This is done in the
12  * creation of the path searcher.
13  * Each path search is given a default path (which defaults to the
14  * houdini path), a path environment variable.
15  *
16  * In the path expansion, the following rules apply:
17  * @ = Expands to the HOUDINI_PATH
18  * & = Expands to the default path
19  * ^ = Expands to the default sub-path
20  * Paths may be colon or semi-colon separated.
21  *
22  * For example:
23  * UT_PathSearch theHotkeyPath(def = "@/config/Hotkeys",
24  * env = "HOUDINI_HOTKEY_PATH");
25  * UT_PathSearch theVexPath(
26  * def = "@/vex/^",
27  * env = "HOUDINI_VEX_PATH");
28  *
29  * When searching for a file, can use one of:
30  * int findFile(const char *search, UT_String &path);
31  * int findAllFiles(const char *search,
32  * UT_StringArray &paths);
33  *
34  * There are a number of well known paths. Typically, you should simply
35  * call the static access method to get the path that you're interested
36  * in.
37  */
38 
39 #ifndef __UT_PathSearch__
40 #define __UT_PathSearch__
41 
42 #include "UT_API.h"
43 #include "UT_KnownPath.h"
44 #include "UT_StringArray.h"
45 #include "UT_UniquePtr.h"
46 #include "UT_WorkBuffer.h"
47 #include <SYS/SYS_Compiler.h>
48 
49 
50 template <typename T> class UT_ValArray;
52 class UT_StringSet;
53 
54 
56 {
57 public:
58  UT_PathSearch() = delete;
59  UT_PathSearch(const char *defpath,
60  int var_id, // From UT_EnvControl
61  const char *carat_expand = "",
62  int at_expand = UT_HOUDINI_PATH);
63  UT_PathSearch(const UT_PathSearch &copy) = default;
64 
65  static bool pathMap(UT_String &path);
66  static bool pathMap(UT_StringHolder &result, const char *path);
67  static void pathMapSave(UT_WorkBuffer &wbuf, bool pretty);
68  static void pathMapLoad(const char *dict);
69  static void pathMapAdd(const char *src, const char *dest,
70  bool case_sensitive);
71  static bool pathMapRemove(const char *src);
72 
73  /// Given a variable name (e.g. "HOUDINI_PATH") return the known path enum
74  /// associated with it. If the name doesn't match a known path, the
75  /// function will return @c UT_MAX_KNOWN_PATHS.
76  static UT_KnownPath lookupPath(const char *name);
77 
78  /// Given a known path, return the variable name associated with the path
79  /// Returns @c nullptr on error.
80  static const char *lookupPath(UT_KnownPath path);
81 
82  static const UT_PathSearch *getInstance(UT_KnownPath path_type);
83 
84  // Make your own copy of a UT_PathSearch class.
85  // Using this, you can add to the path dynamically (eg. CPP)
86  static UT_UniquePtr<UT_PathSearch> getHardPath(UT_KnownPath path_type);
87 
88  // Cause all known paths to be re-built.
89  static void rehashAll();
90  // Rehashes a specific known path - note that this will not rehash
91  // copies of known paths.
92  static void rehashSpecific(UT_KnownPath pathid);
93  void rehash();
94 
95  static void clearStartupCache();
96 
97  // A useful little convenience function which will return the expansion of
98  // $HOME/houdini$HOUDINI_VER
99  /// @{
100  static void getHomeHoudini(UT_String &str);
102  static UT_StringHolder getHomeHoudini();
103  /// @}
104 
105  // extending the path allows multiple paths to be created in a single
106  // search path. For example, when scanning for Surface shaders, the
107  // carat_expand can be set to "Surface" and then extended with "include"
108  void extendPath(const char *defpath = 0,
109  int var_id = -1,
110  const char *carat_expand = 0,
111  int at_expand = -1);
112 
113  // Add a single path to the search path. The path must be unique
114  // otherwise the function returns false. The new path condensed
115  // format is added if condensed=true.
116  // Return true if a new path is added or false otherwise.
117  bool appendPath(const char *path, bool condensed = false);
118 
119  // Removes a path from the search path.
120  // Returns true if the path ws removed, false otherwise.
121  bool removePath(const char *path);
122 
123  // Prepend single path to the search path. The path must be unique
124  // otherwise the function returns false. The new path condensed
125  // format is prepended if condensed=true.
126  // Return true if a new path is prepended or false otherwise.
127  bool prependPath(const char *path, bool condensed = false);
128 
129  // Find the first file that occurs in the path
130  int findFile(UT_String &result, const char *search) const;
131  int findFile(UT_WorkBuffer &result, const char *search) const;
132  int findDirectory(UT_String &result, const char *search) const;
133 
134  // Find the first of the given list of files in the path. If they exist
135  // in the same directory, then the order is determined from the search_list
136  bool findFileFromList(UT_String &result,
137  const UT_StringArray &search_list) const;
138  bool findDirectoryFromList(UT_String &result,
139  const UT_StringArray &search_list) const;
140 
141  // Find all files in the path.
142  // The contents of the list is APPENDED to,
143  // it isn't automatically cleared.
144  int findAllFiles(const char *search,
145  UT_StringArray &list) const;
146 
147  int findAllDirectories(const char *search,
148  UT_StringArray &list) const;
149 
150  // This function will match all files with the given extension in the
151  // search path and fill the given list with the filenames only. Returns the
152  // # of entries in the filled list. (sample extension = ".vex")
153  int matchAllFiles(const char *extension,
154  bool returnfullpaths,
155  UT_StringArray &list,
156  bool recurse=false,
157  bool skip_dup_paths=false) const;
158 
159  // This function will match all files with one of the given extensions
160  // in the search path and fill the given list with the filenames only.
161  // Returns the # of entries in the filled list. The extensions should begin
162  // with a period.
163  int matchAllFiles(const UT_StringList &exts,
164  bool returnfullpaths,
165  UT_StringArray &list,
166  bool recurse=false,
167  bool skip_dup_paths=false) const;
168 
169  int matchAllFilesInSubdirectory(const char *extension,
170  const char *subdirectory,
171  bool returnfullpaths,
172  UT_StringArray &list,
173  bool recurse=false,
174  bool skip_dup_paths=false) const;
175  int matchAllFilesInSubdirectory(const UT_StringList &exts,
176  const char *subdirectory,
177  bool returnfullpaths,
178  UT_StringArray &list,
179  bool recurse=false,
180  bool skip_dup_paths=false) const;
181 
182  // Old style find-file. If a result buffer is passed in, the search
183  // result will be stored in it. This is a little dangerous since it's a
184  // fixed length buffer.
185  int houdiniFindFile(const char *find, UT_String &result) const;
186  int houdiniFindFile(const char *find, UT_WorkBuffer &result) const;
187 
188  int houdiniFindDirectory(const char *find, UT_String &result) const;
189  int houdiniFindDirectory(const char *find, UT_WorkBuffer &r) const;
190 
191  int getEntries() const { return myPaths.entries(); }
192  const char *getPathComponent(int i) const { return myPaths(i); }
193 
194  // Base paths are all paths tested, whether they are valid directories or
195  // not.
196  int getBaseEntries() const { return myBasePaths.entries(); }
197  const char *getBaseComponent(int i) const { return myBasePaths(i); }
198  const char *getCondensedComponent(int i) const
199  { return myCondensedPaths(i); }
200 
201  const char *getDefaultPath() const { return myDefaultPath; }
202  const char *getCaratExpand() const { return myCaratExpand; }
203 
204  int getVariableId() const { return myVariableId; }
205  int getAtExpand() const { return myAtExpand; }
206 
207  // Take a fully qualified path and collapse just the $HIP part of the path
208  static int condenseHip(UT_String &path);
209  static int condenseHip(UT_WorkBuffer &path);
210 
211  // Take a fully qualified path and collapse just the $JOB part of the path
212  static int condenseJob(UT_String &path);
213 
214  // Helper struct which constructs with myPaths/myCondensedPaths suitable
215  // for calling into condensePath().
217  {
218  CommonPaths();
219 
220  enum { NUM_PATHS = 8 };
221 
222  int size() const { return NUM_PATHS; }
223  const char *hip() const { return myPaths[2]; }
224 
225  const char *myPaths[NUM_PATHS];
226  const char *myCondensedPaths[NUM_PATHS];
227  };
228 
229  // Take a fully qualified path and collapse it to a "common" variable path.
230  // For example, $HFS/... instead of /usr/hfs5.3.391/...
231  // @{
232  // @note This is slow if you use it in a loop. Instead, use the alternate
233  // form of condenseCommon() with a CommonPaths object constructed outside
234  // the loop.
235  static inline bool
237  {
238  CommonPaths common;
239  return condenseCommon(path, path, common);
240  }
241 
242  // @note This is slow if you use it in a loop. Instead, use the alternate
243  // form of condenseCommon() with a CommonPaths object constructed outside
244  // the loop.
245  static inline bool
247  {
248  CommonPaths common;
249  return condenseCommon(path, path.buffer(), common);
250  }
251 
252  // @note This is slow if you use it in a loop. Instead, use the alternate
253  // form of condenseCommon() with a CommonPaths object constructed outside
254  // the loop.
255  static inline bool
256  condenseCommon(UT_String &condensed, const char *path)
257  {
258  CommonPaths common;
259  return condenseCommon(condensed, path, common);
260  }
261 
262  // @note This is slow if you use it in a loop. Instead, use the alternate
263  // form of condenseCommon() with a CommonPaths object constructed outside
264  // the loop.
265  static inline bool
266  condenseCommon(UT_WorkBuffer &condensed, const char *path)
267  {
268  CommonPaths common;
269  return condenseCommon(condensed, path, common);
270  }
271 
272  static inline bool
273  condenseCommon(UT_String &path, const CommonPaths &common)
274  {
275  return condenseCommon(path, path, common);
276  }
277 
278  static inline bool
280  {
281  return condenseCommon(path, path.buffer(), common);
282  }
283 
284  static inline bool
285  condenseCommon(UT_String &path, const char *source_path,
286  const CommonPaths &common)
287  {
288  return condensePath(path, source_path, common.myPaths,
289  common.myCondensedPaths, common.size());
290  }
291 
292  static inline bool
293  condenseCommon(UT_WorkBuffer &path, const char *source_path,
294  const CommonPaths &common)
295  {
296  return condensePath(path, source_path, common.myPaths,
297  common.myCondensedPaths, common.size());
298  }
299  // @}
300 
301  // The path variables passed in should ALWAYS be UT_StrControl types.
302  // The values of these variables are tested against the leading portion of
303  // the full path specified and the "longest" match is used for replacement
304  // of the lead. For example the result might be $HOME/relpath.
305  static bool condensePath(UT_WorkBuffer &result,
306  const char *source_path,
307  const char *const paths[],
308  const char *const condensed_paths[],
309  int npaths);
310 
311  static inline bool
313  const char *source_path,
314  const char *const paths[],
315  const char *const condensed_paths[],
316  int npaths)
317  {
318  UT_WorkBuffer condensed;
319  bool ok = condensePath(condensed, source_path,
320  paths, condensed_paths, npaths);
321  if (ok)
322  condensed.copyIntoString(path);
323  else if (path.buffer() != source_path)
324  path.harden(source_path);
325  return ok;
326  }
327 
328  static inline bool
330  const char *const paths[],
331  const char *const condensed_paths[],
332  int npaths)
333  {
334  return condensePath(path, path, paths, condensed_paths, npaths);
335  }
336 
337  // This method will take a fully qualified path and strip off the lead
338  // characters of the pathname -- resulting in path which can still be found
339  // by the path searching code, but which is simpler in nature...
340  // For example, the path $HOME/houdini/fonts/Type1/Courier might be
341  // stripped to simply "Type1/Courier"
342  static int stripLeadPath(UT_String &path, UT_KnownPath pathvar);
343 
344  // Similar to stripLeadPath except that it will always return the shortest
345  // path, irrespective of the order of the paths.
346  static int stripMaxLeadPath(UT_String &path, UT_KnownPath pathvar);
347 
348  // This method, given a filename that is possibly fully specified, will
349  // try to return the fully specified path to the file. Returns true
350  // if absolute_path is valid, false otherwise.
351  static bool resolveAbsolutePath(const char *filename,
352  UT_String &absolute_path);
353 
354  // return the expansion of $HOME/houdini$HOUDINI_VER as though it was
355  // the @ in the search pattern. If there is no search pattern then just
356  // return $HOME/houdini$HOUDINI_VER
357  void getHomeHoudiniSearch(UT_StringArray &strs) const;
358 
359  // Utility for splitting an environment variable path. The function expects
360  // the path to be separated with these characters:
361  // Windows: ";"
362  // Linux or Mac: ":" or ";"
363  //
364  // path: path content. May contain "&" as insertion point of an existing path.
365  // list: return list of splitted elements
366  // insert_path: insert_path is prepended to path at the insertion point (&).
367  static void splitPath(const char *path, UT_StringArray &list,
368  const char *insert_path);
369 
370 private:
371  UT_PathSearch & operator=( const UT_PathSearch &copy ); // not implemented
372 
373 private:
374  void initialize(const char *def,
375  int var_id,
376  const char *carat,
377  int at_expand);
378  void clearPath();
379 
380  /// Helper function for matchAllFiles() and matchAllFilesInSubdirectory().
381  void matchAllFilesInPath(const UT_StringList &exts,
382  const char *path,
383  bool returnfullpaths,
384  bool recurse,
385  UT_StringArray &list,
386  UT_StringSet *seen)const;
387 
388  int myVariableId;
389  UT_String myDefaultPath;
390  UT_String myCaratExpand;
391  int myAtExpand;
392  int myMaxPath; // Max path length
393 
394  UT_StringArray myPaths;
395  UT_StringArray myBasePaths;
396  UT_StringArray myCondensedPaths;
397 };
398 
399 inline int
401 {
403  houdiniFindFile(find, result))
404  result = 0;
405  return result.isstring();
406 }
407 
408 inline int
410 {
412  houdiniFindFile(find, result))
413  result.clear();
414  return result.length() != 0;
415 }
416 
417 inline int
419 {
420  result.clear();
422  findAllFiles(find, result);
423  return result.entries();
424 }
425 
426 inline int
428 {
430  houdiniFindDirectory(find, result))
431  result = 0;
432  return result.isstring();
433 }
434 
435 inline int
437 {
439  houdiniFindDirectory(find, result))
440  result.clear();
441  return result.length() != 0;
442 }
443 
444 inline int
446 {
447  result.clear();
449  findAllDirectories(find, result);
450  return result.entries();
451 }
452 
453 #endif
static const UT_PathSearch * getInstance(UT_KnownPath path_type)
const char * getCondensedComponent(int i) const
path_type
CLI enumeration of different file types.
Definition: CLI11.h:2963
int HoudiniFindFile(const char *find, UT_String &result)
GT_API const UT_StringHolder filename
SYS_FORCE_INLINE exint length() const
UT_KnownPath
Definition: UT_KnownPath.h:14
int getBaseEntries() const
static bool condensePath(UT_String &path, const char *const paths[], const char *const condensed_paths[], int npaths)
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
const char * getBaseComponent(int i) const
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
const char * myPaths[NUM_PATHS]
SYS_FORCE_INLINE const char * buffer() const
#define UT_API
Definition: UT_API.h:14
void copyIntoString(UT_String &str) const
**But if you need a result
Definition: thread.h:622
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2138
static bool condensePath(UT_String &path, const char *source_path, const char *const paths[], const char *const condensed_paths[], int npaths)
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
int HoudiniFindDir(const char *find, UT_String &result)
const char * buffer() const
Definition: UT_String.h:516
const char * myCondensedPaths[NUM_PATHS]
static bool condenseCommon(UT_WorkBuffer &path, const char *source_path, const CommonPaths &common)
static bool condenseCommon(UT_WorkBuffer &path, const CommonPaths &common)
UT_ValArray< const char * > UT_StringList
Definition: UT_PathSearch.h:50
static bool condenseCommon(UT_String &path, const char *source_path, const CommonPaths &common)
void harden()
Take shallow copy and make it deep.
Definition: UT_String.h:222
#define SYS_NO_DISCARD_RESULT
Definition: SYS_Compiler.h:93
GLuint const GLchar * name
Definition: glcorearb.h:786
OPENVDB_API void initialize()
Global registration of native Grid, Transform, Metadata and Point attribute types. Also initializes blosc (if enabled).
Definition: logging.h:294
static bool condenseCommon(UT_WorkBuffer &condensed, const char *path)
auto search(const T &set, const V &val) -> std::pair< bool, decltype(std::begin(detail::smart_deref(set)))>
A search function.
Definition: CLI11.h:3170
const char * getDefaultPath() const
exint entries() const
Alias of size(). size() is preferred.
Definition: UT_Array.h:655
const char * hip() const
const char * getPathComponent(int i) const
static bool condenseCommon(UT_WorkBuffer &path)
int HoudiniFindMulti(const char *find, UT_StringArray &result)
const char * getCaratExpand() const
LeafData & operator=(const LeafData &)=delete
static bool condenseCommon(UT_String &path, const CommonPaths &common)
static bool condenseCommon(UT_String &condensed, const char *path)
bool isstring() const
Definition: UT_String.h:698
SYS_FORCE_INLINE void clear()
int HoudiniFindDirMulti(const char *find, UT_StringArray &result)
GLboolean r
Definition: glcorearb.h:1222
void clear()
Resets list to an empty list.
Definition: UT_Array.h:736
int getEntries() const
int getAtExpand() const
static bool condenseCommon(UT_String &path)
OIIO_UTIL_API std::string extension(string_view filepath, bool include_dot=true) noexcept
int getVariableId() const
GLenum src
Definition: glcorearb.h:1793