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_WorkBuffer.h"
44 #include "UT_StringArray.h"
45 
46 
47 template <typename T> class UT_ValArray;
49 class UT_StringSet;
50 
51 
53 {
55 
66 
76 
80 
81 
87 
93 
95 
103 
104  // Now, we have the paths required for the UT_PathFile searching
105  UT_PATHFILE_DESK, // Desk macro
106  UT_PATHFILE_ASSET_STORE, // Asset store path
107  UT_PATHFILE_TOOLBAR, // Toolbar path
108  UT_PATHFILE_RADIALMENU, // Radial menu path
109  UT_PATHFILE_RAMP, // Ramp preset path
110  UT_PATHFILE_CUSTOM, // CPUI path
111  UT_PATHFILE_PYTHON_PANEL, // Python panel path
112  UT_PATHFILE_MENU, // XML menus path
113 
114  UT_OUTLINEFONT_PATH, // Outline fonts
115 
116  UT_OCL_PATH, // OpenCL kernels
117  UT_VOP_TYPES_PATH, // VOP connector and language types definitions
118 
119  UT_HOUDINI_VIEWERSTATE_PATH, // Viewer state path (viewer_states)
120  UT_HOUDINI_VIEWERHANDLE_PATH, // Viewer handle path (viewer_handles)
121  UT_HOUDINI_HUSDPLUGINS_PATH, // Python plugins for various
122  UT_HOUDINI_LAYOUTPLUGINS_PATH, // Python plugins for layout tools.
123 
125 };
126 
128 {
129 public:
130  UT_PathSearch(const char *defpath,
131  int var_id, // From UT_EnvControl
132  const char *carat_expand = "",
133  int at_expand = UT_HOUDINI_PATH);
134  UT_PathSearch(const UT_PathSearch &copy) = default;
135 
136  static bool pathMap(UT_String &path);
137  static bool pathMap(UT_StringHolder &result, const char *path);
138  static void pathMapSave(UT_WorkBuffer &wbuf, bool pretty);
139  static void pathMapLoad(const char *dict);
140  static void pathMapAdd(const char *src, const char *dest,
141  bool case_sensitive);
142  static bool pathMapRemove(const char *src);
143 
144  /// Given a variable name (e.g. "HOUDINI_PATH") return the known path enum
145  /// associated with it. If the name doesn't match a known path, the
146  /// function will return @c UT_MAX_KNOWN_PATHS.
147  static UT_KnownPath lookupPath(const char *name);
148 
149  /// Given a known path, return the variable name associated with the path
150  /// Returns @c nullptr on error.
151  static const char *lookupPath(UT_KnownPath path);
152 
153  static const UT_PathSearch *getInstance(UT_KnownPath path_type);
154 
155  // Make your own copy of a UT_PathSearch class. You are responsible for
156  // deleting this. However, using this, you can add to the path dynamically
157  // (i.e. CPP)
158  static UT_PathSearch *getHardPath(UT_KnownPath path_type);
159 
160  // Cause all known paths to be re-built.
161  static void rehashAll();
162  // Rehashes a specific known path - note that this will not rehash
163  // copies of known paths.
164  static void rehashSpecific(UT_KnownPath pathid);
165  void rehash();
166 
167  // A useful little convenience function which will return the expansion of
168  // $HOME/houdini$HOUDINI_VER
169  static void getHomeHoudini(UT_String &str);
170 
171  // extending the path allows multiple paths to be created in a single
172  // search path. For example, when scanning for Surface shaders, the
173  // carat_expand can be set to "Surface" and then extended with "include"
174  void extendPath(const char *defpath = 0,
175  int var_id = -1,
176  const char *carat_expand = 0,
177  int at_expand = -1);
178 
179  // Add a single path to the search path. The path must be unique
180  // otherwise the function returns false. The new path condensed
181  // format is added if condensed=true.
182  // Return true if a new path is added or false otherwise.
183  bool appendPath(const char *path, bool condensed = false);
184 
185  // Removes a path from the search path.
186  // Returns true if the path ws removed, false otherwise.
187  bool removePath(const char *path);
188 
189  // Prepend single path to the search path
190  void prependPath(const char *path);
191 
192 
193  // Find the first file that occurs in the path
194  int findFile(UT_String &result, const char *search) const;
195  int findFile(UT_WorkBuffer &result, const char *search) const;
196  int findDirectory(UT_String &result, const char *search) const;
197 
198  // Find the first of the given list of files in the path. If they exist
199  // in the same directory, then the order is determined from the search_list
200  bool findFileFromList(UT_String &result,
201  const UT_StringArray &search_list) const;
202  bool findDirectoryFromList(UT_String &result,
203  const UT_StringArray &search_list) const;
204 
205  // Find all files in the path.
206  // The contents of the list is APPENDED to,
207  // it isn't automatically cleared.
208  int findAllFiles(const char *search,
209  UT_StringArray &list) const;
210 
211  int findAllDirectories(const char *search,
212  UT_StringArray &list) const;
213 
214  // This function will match all files with the given extension in the
215  // search path and fill the given list with the filenames only. Returns the
216  // # of entries in the filled list. (sample extension = ".vex")
217  int matchAllFiles(const char *extension,
218  bool returnfullpaths,
219  UT_StringArray &list,
220  bool recurse=false,
221  bool skip_dup_paths=false) const;
222 
223  // This function will match all files with one of the given extensions
224  // in the search path and fill the given list with the filenames only.
225  // Returns the # of entries in the filled list. The extensions should begin
226  // with a period.
227  int matchAllFiles(const UT_StringList &exts,
228  bool returnfullpaths,
229  UT_StringArray &list,
230  bool recurse=false,
231  bool skip_dup_paths=false) const;
232 
233  int matchAllFilesInSubdirectory(const char *extension,
234  const char *subdirectory,
235  bool returnfullpaths,
236  UT_StringArray &list,
237  bool recurse=false,
238  bool skip_dup_paths=false) const;
239  int matchAllFilesInSubdirectory(const UT_StringList &exts,
240  const char *subdirectory,
241  bool returnfullpaths,
242  UT_StringArray &list,
243  bool recurse=false,
244  bool skip_dup_paths=false) const;
245 
246  // Old style find-file. If a result buffer is passed in, the search
247  // result will be stored in it. This is a little dangerous since it's a
248  // fixed length buffer.
249  int houdiniFindFile(const char *find, UT_String &result) const;
250  int houdiniFindFile(const char *find, UT_WorkBuffer &result) const;
251 
252  int houdiniFindDirectory(const char *find, UT_String &result) const;
253  int houdiniFindDirectory(const char *find, UT_WorkBuffer &r) const;
254 
255  int getEntries() const { return myPaths.entries(); }
256  const char *getPathComponent(int i) const { return myPaths(i); }
257 
258  // Base paths are all paths tested, whether they are valid directories or
259  // not.
260  int getBaseEntries() const { return myBasePaths.entries(); }
261  const char *getBaseComponent(int i) const { return myBasePaths(i); }
262  const char *getCondensedComponent(int i) const
263  { return myCondensedPaths(i); }
264 
265  const char *getDefaultPath() const { return myDefaultPath; }
266  const char *getCaratExpand() const { return myCaratExpand; }
267 
268  int getVariableId() const { return myVariableId; }
269  int getAtExpand() const { return myAtExpand; }
270 
271  // Take a fully qualified path and collapse just the $HIP part of the path
272  static int condenseHip(UT_String &path);
273  static int condenseHip(UT_WorkBuffer &path);
274 
275  // Take a fully qualified path and collapse just the $JOB part of the path
276  static int condenseJob(UT_String &path);
277 
278  // Helper struct which constructs with myPaths/myCondensedPaths suitable
279  // for calling into condensePath().
281  {
282  CommonPaths();
283 
284  enum { NUM_PATHS = 7 };
285 
286  int size() const { return NUM_PATHS; }
287  const char *hip() const { return myPaths[2]; }
288 
289  const char *myPaths[NUM_PATHS];
290  const char *myCondensedPaths[NUM_PATHS];
291  };
292 
293  // Take a fully qualified path and collapse it to a "common" variable path.
294  // For example, $HFS/... instead of /usr/hfs5.3.391/...
295  // @{
296  // @note This is slow if you use it in a loop. Instead, use the alternate
297  // form of condenseCommon() with a CommonPaths object constructed outside
298  // the loop.
299  static inline bool
301  {
302  CommonPaths common;
303  return condenseCommon(path, path, common);
304  }
305 
306  // @note This is slow if you use it in a loop. Instead, use the alternate
307  // form of condenseCommon() with a CommonPaths object constructed outside
308  // the loop.
309  static inline bool
311  {
312  CommonPaths common;
313  return condenseCommon(path, path.buffer(), common);
314  }
315 
316  // @note This is slow if you use it in a loop. Instead, use the alternate
317  // form of condenseCommon() with a CommonPaths object constructed outside
318  // the loop.
319  static inline bool
320  condenseCommon(UT_String &condensed, const char *path)
321  {
322  CommonPaths common;
323  return condenseCommon(condensed, path, common);
324  }
325 
326  // @note This is slow if you use it in a loop. Instead, use the alternate
327  // form of condenseCommon() with a CommonPaths object constructed outside
328  // the loop.
329  static inline bool
330  condenseCommon(UT_WorkBuffer &condensed, const char *path)
331  {
332  CommonPaths common;
333  return condenseCommon(condensed, path, common);
334  }
335 
336  static inline bool
337  condenseCommon(UT_String &path, const CommonPaths &common)
338  {
339  return condenseCommon(path, path, common);
340  }
341 
342  static inline bool
344  {
345  return condenseCommon(path, path.buffer(), common);
346  }
347 
348  static inline bool
349  condenseCommon(UT_String &path, const char *source_path,
350  const CommonPaths &common)
351  {
352  return condensePath(path, source_path, common.myPaths,
353  common.myCondensedPaths, common.size());
354  }
355 
356  static inline bool
357  condenseCommon(UT_WorkBuffer &path, const char *source_path,
358  const CommonPaths &common)
359  {
360  return condensePath(path, source_path, common.myPaths,
361  common.myCondensedPaths, common.size());
362  }
363  // @}
364 
365  // The path variables passed in should ALWAYS be UT_StrControl types.
366  // The values of these variables are tested against the leading portion of
367  // the full path specified and the "longest" match is used for replacement
368  // of the lead. For example the result might be $HOME/relpath.
369  static bool condensePath(UT_WorkBuffer &result,
370  const char *source_path,
371  const char *const paths[],
372  const char *const condensed_paths[],
373  int npaths);
374 
375  static inline bool
377  const char *source_path,
378  const char *const paths[],
379  const char *const condensed_paths[],
380  int npaths)
381  {
382  UT_WorkBuffer condensed;
383  bool ok = condensePath(condensed, source_path,
384  paths, condensed_paths, npaths);
385  if (ok)
386  condensed.copyIntoString(path);
387  else if (path.buffer() != source_path)
388  path.harden(source_path);
389  return ok;
390  }
391 
392  static inline bool
394  const char *const paths[],
395  const char *const condensed_paths[],
396  int npaths)
397  {
398  return condensePath(path, path, paths, condensed_paths, npaths);
399  }
400 
401  // This method will take a fully qualified path and strip off the lead
402  // characters of the pathname -- resulting in path which can still be found
403  // by the path searching code, but which is simpler in nature...
404  // For example, the path $HOME/houdini/fonts/Type1/Courier might be
405  // stripped to simply "Type1/Courier"
406  static int stripLeadPath(UT_String &path, UT_KnownPath pathvar);
407 
408  // Similar to stripLeadPath except that it will always return the shortest
409  // path, irrespective of the order of the paths.
410  static int stripMaxLeadPath(UT_String &path, UT_KnownPath pathvar);
411 
412  // This method, given a filename that is possibly fully specified, will
413  // try to return the fully specified path to the file. Returns true
414  // if absolute_path is valid, false otherwise.
415  static bool resolveAbsolutePath(const char *filename,
416  UT_String &absolute_path);
417 
418  // return the expansion of $HOME/houdini$HOUDINI_VER as though it was
419  // the @ in the search pattern. If there is no search pattern then just
420  // return $HOME/houdini$HOUDINI_VER
421  void getHomeHoudiniSearch(UT_StringArray &strs) const;
422 
423  // Utility for splitting an environment variable path. The function expects
424  // the path to be separated with these characters:
425  // Windows: ";"
426  // Linux or Mac: ":" or ";"
427  //
428  // path: path content. May contain "&" as insertion point of an existing path.
429  // list: return list of splitted elements
430  // insert_path: insert_path is prepended to path at the insertion point (&).
431  static void splitPath(const char *path, UT_StringArray &list,
432  const char *insert_path);
433 
434 private:
435  UT_PathSearch & operator=( const UT_PathSearch &copy ); // not implemented
436 
437 private:
438  void initialize(const char *def,
439  int var_id,
440  const char *carat,
441  int at_expand);
442  void clearPath();
443 
444  /// Helper function for matchAllFiles() and matchAllFilesInSubdirectory().
445  void matchAllFilesInPath(const UT_StringList &exts,
446  const char *path,
447  bool returnfullpaths,
448  bool recurse,
449  UT_StringArray &list,
450  UT_StringSet *seen)const;
451 
452  void appendPathPrivate(const char *path);
453 
454  int myVariableId;
455  UT_String myDefaultPath;
456  UT_String myCaratExpand;
457  int myAtExpand;
458  int myMaxPath; // Max path length
459 
460  UT_StringArray myPaths;
461  UT_StringArray myBasePaths;
462  UT_StringArray myCondensedPaths;
463 };
464 
465 inline int
467 {
469  houdiniFindFile(find, result))
470  result = 0;
471  return result.isstring();
472 }
473 
474 inline int
476 {
478  houdiniFindFile(find, result))
479  result.clear();
480  return result.length() != 0;
481 }
482 
483 inline int
485 {
486  result.clear();
488  findAllFiles(find, result);
489  return result.entries();
490 }
491 
492 inline int
494 {
496  houdiniFindDirectory(find, result))
497  result = 0;
498  return result.isstring();
499 }
500 
501 inline int
503 {
505  houdiniFindDirectory(find, result))
506  result.clear();
507  return result.length() != 0;
508 }
509 
510 inline int
512 {
513  result.clear();
515  findAllDirectories(find, result);
516  return result.entries();
517 }
518 
519 #endif
static const UT_PathSearch * getInstance(UT_KnownPath path_type)
const char * getCondensedComponent(int i) const
int HoudiniFindFile(const char *find, UT_String &result)
GT_API const UT_StringHolder filename
SYS_FORCE_INLINE exint length() const
int getBaseEntries() const
static bool condensePath(UT_String &path, const char *const paths[], const char *const condensed_paths[], int npaths)
const char * getBaseComponent(int i) const
GLsizei const GLchar *const * path
Definition: glcorearb.h:3340
const char * myPaths[NUM_PATHS]
OIIO_API std::string extension(const std::string &filepath, bool include_dot=true) noexcept
SYS_FORCE_INLINE const char * buffer() const
#define UT_API
Definition: UT_API.h:14
void copyIntoString(UT_String &str) const
GLuint const GLchar * name
Definition: glcorearb.h:785
static bool condensePath(UT_String &path, const char *source_path, const char *const paths[], const char *const condensed_paths[], int npaths)
GLenum src
Definition: glcorearb.h:1792
int HoudiniFindDir(const char *find, UT_String &result)
const char * buffer() const
Definition: UT_String.h:499
GLuint64EXT * result
Definition: glew.h:14311
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)
FMT_CONSTEXPR bool find(Ptr first, Ptr last, T value, Ptr &out)
Definition: format.h:2929
UT_ValArray< const char * > UT_StringList
Definition: UT_PathSearch.h:47
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:206
UT_KnownPath
Definition: UT_PathSearch.h:52
OPENVDB_API void initialize()
Global registration of basic types.
Definition: logging.h:294
static bool condenseCommon(UT_WorkBuffer &condensed, const char *path)
const char * getDefaultPath() const
exint entries() const
Alias of size(). size() is preferred.
Definition: UT_Array.h:481
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
static bool condenseCommon(UT_String &path, const CommonPaths &common)
OIIO_API bool copy(string_view from, string_view to, std::string &err)
static bool condenseCommon(UT_String &condensed, const char *path)
bool isstring() const
Definition: UT_String.h:681
SYS_FORCE_INLINE void clear()
int HoudiniFindDirMulti(const char *find, UT_StringArray &result)
void clear()
Resets list to an empty list.
Definition: UT_Array.h:549
int getEntries() const
GLboolean r
Definition: glcorearb.h:1221
int getAtExpand() const
static bool condenseCommon(UT_String &path)
int getVariableId() const
GLenum const void * paths
Definition: glew.h:13872