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 
123 };
124 
126 {
127 public:
128  UT_PathSearch(const char *defpath,
129  int var_id, // From UT_EnvControl
130  const char *carat_expand = "",
131  int at_expand = UT_HOUDINI_PATH);
132  UT_PathSearch(const UT_PathSearch &copy) = default;
133 
134  static bool pathMap(UT_String &path);
135  static bool pathMap(UT_StringHolder &result, const char *path);
136  static void pathMapSave(UT_WorkBuffer &wbuf, bool pretty);
137  static void pathMapLoad(const char *dict);
138  static void pathMapAdd(const char *src, const char *dest,
139  bool case_sensitive);
140  static bool pathMapRemove(const char *src);
141 
142  /// Given a variable name (e.g. "HOUDINI_PATH") return the known path enum
143  /// associated with it. If the name doesn't match a known path, the
144  /// function will return @c UT_MAX_KNOWN_PATHS.
145  static UT_KnownPath lookupPath(const char *name);
146 
147  /// Given a known path, return the variable name associated with the path
148  /// Returns @c nullptr on error.
149  static const char *lookupPath(UT_KnownPath path);
150 
151  static const UT_PathSearch *getInstance(UT_KnownPath path_type);
152 
153  // Make your own copy of a UT_PathSearch class. You are responsible for
154  // deleting this. However, using this, you can add to the path dynamically
155  // (i.e. CPP)
156  static UT_PathSearch *getHardPath(UT_KnownPath path_type);
157 
158  // Cause all known paths to be re-built.
159  static void rehashAll();
160  // Rehashes a specific known path - note that this will not rehash
161  // copies of known paths.
162  static void rehashSpecific(UT_KnownPath pathid);
163  void rehash();
164 
165  // A useful little convenience function which will return the expansion of
166  // $HOME/houdini$HOUDINI_VER
167  static void getHomeHoudini(UT_String &str);
168 
169  // extending the path allows multiple paths to be created in a single
170  // search path. For example, when scanning for Surface shaders, the
171  // carat_expand can be set to "Surface" and then extended with "include"
172  void extendPath(const char *defpath = 0,
173  int var_id = -1,
174  const char *carat_expand = 0,
175  int at_expand = -1);
176 
177  // Add a single path to the search path
178  void appendPath(const char *path);
179 
180  // Prepend single path to the search path
181  void prependPath(const char *path);
182 
183  // Find the first file that occurs in the path
184  int findFile(UT_String &result, const char *search) const;
185  int findFile(UT_WorkBuffer &result, const char *search) const;
186  int findDirectory(UT_String &result, const char *search) const;
187 
188  // Find the first of the given list of files in the path. If they exist
189  // in the same directory, then the order is determined from the search_list
190  bool findFileFromList(UT_String &result,
191  const UT_StringArray &search_list) const;
192  bool findDirectoryFromList(UT_String &result,
193  const UT_StringArray &search_list) const;
194 
195  // Find all files in the path.
196  // The contents of the list is APPENDED to,
197  // it isn't automatically cleared.
198  int findAllFiles(const char *search,
199  UT_StringArray &list) const;
200 
201  int findAllDirectories(const char *search,
202  UT_StringArray &list) const;
203 
204  // This function will match all files with the given extension in the
205  // search path and fill the given list with the filenames only. Returns the
206  // # of entries in the filled list. (sample extension = ".vex")
207  int matchAllFiles(const char *extension,
208  bool returnfullpaths,
209  UT_StringArray &list,
210  bool recurse=false,
211  bool skip_dup_paths=false) const;
212 
213  // This function will match all files with one of the given extensions
214  // in the search path and fill the given list with the filenames only.
215  // Returns the # of entries in the filled list. The extensions should begin
216  // with a period.
217  int matchAllFiles(const UT_StringList &exts,
218  bool returnfullpaths,
219  UT_StringArray &list,
220  bool recurse=false,
221  bool skip_dup_paths=false) const;
222 
223  int matchAllFilesInSubdirectory(const char *extension,
224  const char *subdirectory,
225  bool returnfullpaths,
226  UT_StringArray &list,
227  bool recurse=false,
228  bool skip_dup_paths=false) const;
229  int matchAllFilesInSubdirectory(const UT_StringList &exts,
230  const char *subdirectory,
231  bool returnfullpaths,
232  UT_StringArray &list,
233  bool recurse=false,
234  bool skip_dup_paths=false) const;
235 
236  // Old style find-file. If a result buffer is passed in, the search
237  // result will be stored in it. This is a little dangerous since it's a
238  // fixed length buffer.
239  int houdiniFindFile(const char *find, UT_String &result) const;
240  int houdiniFindFile(const char *find, UT_WorkBuffer &result) const;
241 
242  int houdiniFindDirectory(const char *find, UT_String &result) const;
243  int houdiniFindDirectory(const char *find, UT_WorkBuffer &r) const;
244 
245  int getEntries() const { return myPaths.entries(); }
246  const char *getPathComponent(int i) const { return myPaths(i); }
247 
248  // Base paths are all paths tested, whether they are valid directories or
249  // not.
250  int getBaseEntries() const { return myBasePaths.entries(); }
251  const char *getBaseComponent(int i) const { return myBasePaths(i); }
252  const char *getCondensedComponent(int i) const
253  { return myCondensedPaths(i); }
254 
255  const char *getDefaultPath() const { return myDefaultPath; }
256  const char *getCaratExpand() const { return myCaratExpand; }
257 
258  int getVariableId() const { return myVariableId; }
259  int getAtExpand() const { return myAtExpand; }
260 
261  // Take a fully qualified path and collapse just the $HIP part of the path
262  static int condenseHip(UT_String &path);
263  static int condenseHip(UT_WorkBuffer &path);
264 
265  // Take a fully qualified path and collapse just the $JOB part of the path
266  static int condenseJob(UT_String &path);
267 
268  // Helper struct which constructs with myPaths/myCondensedPaths suitable
269  // for calling into condensePath().
271  {
272  CommonPaths();
273 
274  enum { NUM_PATHS = 7 };
275 
276  int size() const { return NUM_PATHS; }
277  const char *hip() const { return myPaths[2]; }
278 
279  const char *myPaths[NUM_PATHS];
280  const char *myCondensedPaths[NUM_PATHS];
281  };
282 
283  // Take a fully qualified path and collapse it to a "common" variable path.
284  // For example, $HFS/... instead of /usr/hfs5.3.391/...
285  // @{
286  // @note This is slow if you use it in a loop. Instead, use the alternate
287  // form of condenseCommon() with a CommonPaths object constructed outside
288  // the loop.
289  static inline bool
291  {
292  CommonPaths common;
293  return condenseCommon(path, path, common);
294  }
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.buffer(), 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
310  condenseCommon(UT_String &condensed, const char *path)
311  {
312  CommonPaths common;
313  return condenseCommon(condensed, path, 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_WorkBuffer &condensed, const char *path)
321  {
322  CommonPaths common;
323  return condenseCommon(condensed, path, common);
324  }
325 
326  static inline bool
327  condenseCommon(UT_String &path, const CommonPaths &common)
328  {
329  return condenseCommon(path, path, common);
330  }
331 
332  static inline bool
334  {
335  return condenseCommon(path, path.buffer(), common);
336  }
337 
338  static inline bool
339  condenseCommon(UT_String &path, const char *source_path,
340  const CommonPaths &common)
341  {
342  return condensePath(path, source_path, common.myPaths,
343  common.myCondensedPaths, common.size());
344  }
345 
346  static inline bool
347  condenseCommon(UT_WorkBuffer &path, const char *source_path,
348  const CommonPaths &common)
349  {
350  return condensePath(path, source_path, common.myPaths,
351  common.myCondensedPaths, common.size());
352  }
353  // @}
354 
355  // The path variables passed in should ALWAYS be UT_StrControl types.
356  // The values of these variables are tested against the leading portion of
357  // the full path specified and the "longest" match is used for replacement
358  // of the lead. For example the result might be $HOME/relpath.
359  static bool condensePath(UT_WorkBuffer &result,
360  const char *source_path,
361  const char *const paths[],
362  const char *const condensed_paths[],
363  int npaths);
364 
365  static inline bool
367  const char *source_path,
368  const char *const paths[],
369  const char *const condensed_paths[],
370  int npaths)
371  {
372  UT_WorkBuffer condensed;
373  bool ok = condensePath(condensed, source_path,
374  paths, condensed_paths, npaths);
375  if (ok)
376  condensed.copyIntoString(path);
377  else if (path.buffer() != source_path)
378  path.harden(source_path);
379  return ok;
380  }
381 
382  static inline bool
384  const char *const paths[],
385  const char *const condensed_paths[],
386  int npaths)
387  {
388  return condensePath(path, path, paths, condensed_paths, npaths);
389  }
390 
391  // This method will take a fully qualified path and strip off the lead
392  // characters of the pathname -- resulting in path which can still be found
393  // by the path searching code, but which is simpler in nature...
394  // For example, the path $HOME/houdini/fonts/Type1/Courier might be
395  // stripped to simply "Type1/Courier"
396  static int stripLeadPath(UT_String &path, UT_KnownPath pathvar);
397 
398  // Similar to stripLeadPath except that it will always return the shortest
399  // path, irrespective of the order of the paths.
400  static int stripMaxLeadPath(UT_String &path, UT_KnownPath pathvar);
401 
402  // This method, given a filename that is possibly fully specified, will
403  // try to return the fully specified path to the file. Returns true
404  // if absolute_path is valid, false otherwise.
405  static bool resolveAbsolutePath(const char *filename,
406  UT_String &absolute_path);
407 
408  // return the expansion of $HOME/houdini$HOUDINI_VER as though it was
409  // the @ in the search pattern. If there is no search pattern then just
410  // return $HOME/houdini$HOUDINI_VER
411  void getHomeHoudiniSearch(UT_StringArray &strs) const;
412 
413  // Utility for splitting an environment variable path. The function expects
414  // the path to be separated with these characters:
415  // Windows: ";"
416  // Linux or Mac: ":" or ";"
417  //
418  // path: path content. May contain "&" as insertion point of an existing path.
419  // list: return list of splitted elements
420  // insert_path: insert_path is prepended to path at the insertion point (&).
421  static void splitPath(const char *path, UT_StringArray &list,
422  const char *insert_path);
423 
424 private:
425  UT_PathSearch & operator=( const UT_PathSearch &copy ); // not implemented
426 
427 private:
428  void initialize(const char *def,
429  int var_id,
430  const char *carat,
431  int at_expand);
432  void clearPath();
433 
434  /// Helper function for matchAllFiles() and matchAllFilesInSubdirectory().
435  void matchAllFilesInPath(const UT_StringList &exts,
436  const char *path,
437  bool returnfullpaths,
438  bool recurse,
439  UT_StringArray &list,
440  UT_StringSet *seen)const;
441 
442  int myVariableId;
443  UT_String myDefaultPath;
444  UT_String myCaratExpand;
445  int myAtExpand;
446  int myMaxPath; // Max path length
447 
448  UT_StringArray myPaths;
449  UT_StringArray myBasePaths;
450  UT_StringArray myCondensedPaths;
451 };
452 
453 inline int
455 {
457  houdiniFindFile(find, result))
458  result = 0;
459  return result.isstring();
460 }
461 
462 inline int
464 {
466  houdiniFindFile(find, result))
467  result.clear();
468  return result.length() != 0;
469 }
470 
471 inline int
473 {
474  result.clear();
476  findAllFiles(find, result);
477  return result.entries();
478 }
479 
480 inline int
482 {
484  houdiniFindDirectory(find, result))
485  result = 0;
486  return result.isstring();
487 }
488 
489 inline int
491 {
493  houdiniFindDirectory(find, result))
494  result.clear();
495  return result.length() != 0;
496 }
497 
498 inline int
500 {
501  result.clear();
503  findAllDirectories(find, result);
504  return result.entries();
505 }
506 
507 #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
GLenum src
Definition: glew.h:2410
SYS_FORCE_INLINE exint length() const
GLuint const GLchar * name
Definition: glew.h:1814
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
const char * myPaths[NUM_PATHS]
SYS_FORCE_INLINE const char * buffer() const
#define UT_API
Definition: UT_API.h:13
void copyIntoString(UT_String &str) const
static bool condensePath(UT_String &path, const char *source_path, const char *const paths[], const char *const condensed_paths[], int npaths)
int HoudiniFindDir(const char *find, UT_String &result)
const char * buffer() const
Definition: UT_String.h:506
OIIO_API std::string extension(const std::string &filepath, bool include_dot=true)
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: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:213
UT_KnownPath
Definition: UT_PathSearch.h:52
OPENVDB_API void initialize()
Global registration of basic types.
Definition: logging.h:291
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:460
GLsizei const GLchar *const * path
Definition: glew.h:6461
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)
FMT_CONSTEXPR bool find(Ptr first, Ptr last, T value, Ptr &out)
Definition: format.h:2104
GLdouble GLdouble GLdouble r
Definition: glew.h:1406
OIIO_API bool copy(string_view from, string_view to, std::string &err)
static bool condenseCommon(UT_String &condensed, const char *path)
GLuint64EXT * result
Definition: glew.h:14007
bool isstring() const
Definition: UT_String.h:711
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:528
int getEntries() const
int getAtExpand() const
static bool condenseCommon(UT_String &path)
int getVariableId() const
GLenum const void * paths
Definition: glew.h:13589