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