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