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_WorkBuffer.h"
45 #include "UT_StringArray.h"
46 #include <SYS/SYS_Compiler.h>
47 
48 
49 template <typename T> class UT_ValArray;
51 class UT_StringSet;
52 
53 
55 {
56 public:
57  UT_PathSearch(const char *defpath,
58  int var_id, // From UT_EnvControl
59  const char *carat_expand = "",
60  int at_expand = UT_HOUDINI_PATH);
61  UT_PathSearch(const UT_PathSearch &copy) = default;
62 
63  static bool pathMap(UT_String &path);
64  static bool pathMap(UT_StringHolder &result, const char *path);
65  static void pathMapSave(UT_WorkBuffer &wbuf, bool pretty);
66  static void pathMapLoad(const char *dict);
67  static void pathMapAdd(const char *src, const char *dest,
68  bool case_sensitive);
69  static bool pathMapRemove(const char *src);
70 
71  /// Given a variable name (e.g. "HOUDINI_PATH") return the known path enum
72  /// associated with it. If the name doesn't match a known path, the
73  /// function will return @c UT_MAX_KNOWN_PATHS.
74  static UT_KnownPath lookupPath(const char *name);
75 
76  /// Given a known path, return the variable name associated with the path
77  /// Returns @c nullptr on error.
78  static const char *lookupPath(UT_KnownPath path);
79 
80  static const UT_PathSearch *getInstance(UT_KnownPath path_type);
81 
82  // Make your own copy of a UT_PathSearch class. You are responsible for
83  // deleting this. However, using this, you can add to the path dynamically
84  // (i.e. CPP)
85  static UT_PathSearch *getHardPath(UT_KnownPath path_type);
86 
87  // Cause all known paths to be re-built.
88  static void rehashAll();
89  // Rehashes a specific known path - note that this will not rehash
90  // copies of known paths.
91  static void rehashSpecific(UT_KnownPath pathid);
92  void rehash();
93 
94  // A useful little convenience function which will return the expansion of
95  // $HOME/houdini$HOUDINI_VER
96  /// @{
97  static void getHomeHoudini(UT_String &str);
99  static UT_StringHolder getHomeHoudini();
100  /// @}
101 
102  // extending the path allows multiple paths to be created in a single
103  // search path. For example, when scanning for Surface shaders, the
104  // carat_expand can be set to "Surface" and then extended with "include"
105  void extendPath(const char *defpath = 0,
106  int var_id = -1,
107  const char *carat_expand = 0,
108  int at_expand = -1);
109 
110  // Add a single path to the search path. The path must be unique
111  // otherwise the function returns false. The new path condensed
112  // format is added if condensed=true.
113  // Return true if a new path is added or false otherwise.
114  bool appendPath(const char *path, bool condensed = false);
115 
116  // Removes a path from the search path.
117  // Returns true if the path ws removed, false otherwise.
118  bool removePath(const char *path);
119 
120  // Prepend single path to the search path
121  void prependPath(const char *path);
122 
123 
124  // Find the first file that occurs in the path
125  int findFile(UT_String &result, const char *search) const;
126  int findFile(UT_WorkBuffer &result, const char *search) const;
127  int findDirectory(UT_String &result, const char *search) const;
128 
129  // Find the first of the given list of files in the path. If they exist
130  // in the same directory, then the order is determined from the search_list
131  bool findFileFromList(UT_String &result,
132  const UT_StringArray &search_list) const;
133  bool findDirectoryFromList(UT_String &result,
134  const UT_StringArray &search_list) const;
135 
136  // Find all files in the path.
137  // The contents of the list is APPENDED to,
138  // it isn't automatically cleared.
139  int findAllFiles(const char *search,
140  UT_StringArray &list) const;
141 
142  int findAllDirectories(const char *search,
143  UT_StringArray &list) const;
144 
145  // This function will match all files with the given extension in the
146  // search path and fill the given list with the filenames only. Returns the
147  // # of entries in the filled list. (sample extension = ".vex")
148  int matchAllFiles(const char *extension,
149  bool returnfullpaths,
150  UT_StringArray &list,
151  bool recurse=false,
152  bool skip_dup_paths=false) const;
153 
154  // This function will match all files with one of the given extensions
155  // in the search path and fill the given list with the filenames only.
156  // Returns the # of entries in the filled list. The extensions should begin
157  // with a period.
158  int matchAllFiles(const UT_StringList &exts,
159  bool returnfullpaths,
160  UT_StringArray &list,
161  bool recurse=false,
162  bool skip_dup_paths=false) const;
163 
164  int matchAllFilesInSubdirectory(const char *extension,
165  const char *subdirectory,
166  bool returnfullpaths,
167  UT_StringArray &list,
168  bool recurse=false,
169  bool skip_dup_paths=false) const;
170  int matchAllFilesInSubdirectory(const UT_StringList &exts,
171  const char *subdirectory,
172  bool returnfullpaths,
173  UT_StringArray &list,
174  bool recurse=false,
175  bool skip_dup_paths=false) const;
176 
177  // Old style find-file. If a result buffer is passed in, the search
178  // result will be stored in it. This is a little dangerous since it's a
179  // fixed length buffer.
180  int houdiniFindFile(const char *find, UT_String &result) const;
181  int houdiniFindFile(const char *find, UT_WorkBuffer &result) const;
182 
183  int houdiniFindDirectory(const char *find, UT_String &result) const;
184  int houdiniFindDirectory(const char *find, UT_WorkBuffer &r) const;
185 
186  int getEntries() const { return myPaths.entries(); }
187  const char *getPathComponent(int i) const { return myPaths(i); }
188 
189  // Base paths are all paths tested, whether they are valid directories or
190  // not.
191  int getBaseEntries() const { return myBasePaths.entries(); }
192  const char *getBaseComponent(int i) const { return myBasePaths(i); }
193  const char *getCondensedComponent(int i) const
194  { return myCondensedPaths(i); }
195 
196  const char *getDefaultPath() const { return myDefaultPath; }
197  const char *getCaratExpand() const { return myCaratExpand; }
198 
199  int getVariableId() const { return myVariableId; }
200  int getAtExpand() const { return myAtExpand; }
201 
202  // Take a fully qualified path and collapse just the $HIP part of the path
203  static int condenseHip(UT_String &path);
204  static int condenseHip(UT_WorkBuffer &path);
205 
206  // Take a fully qualified path and collapse just the $JOB part of the path
207  static int condenseJob(UT_String &path);
208 
209  // Helper struct which constructs with myPaths/myCondensedPaths suitable
210  // for calling into condensePath().
212  {
213  CommonPaths();
214 
215  enum { NUM_PATHS = 7 };
216 
217  int size() const { return NUM_PATHS; }
218  const char *hip() const { return myPaths[2]; }
219 
220  const char *myPaths[NUM_PATHS];
221  const char *myCondensedPaths[NUM_PATHS];
222  };
223 
224  // Take a fully qualified path and collapse it to a "common" variable path.
225  // For example, $HFS/... instead of /usr/hfs5.3.391/...
226  // @{
227  // @note This is slow if you use it in a loop. Instead, use the alternate
228  // form of condenseCommon() with a CommonPaths object constructed outside
229  // the loop.
230  static inline bool
232  {
233  CommonPaths common;
234  return condenseCommon(path, path, common);
235  }
236 
237  // @note This is slow if you use it in a loop. Instead, use the alternate
238  // form of condenseCommon() with a CommonPaths object constructed outside
239  // the loop.
240  static inline bool
242  {
243  CommonPaths common;
244  return condenseCommon(path, path.buffer(), common);
245  }
246 
247  // @note This is slow if you use it in a loop. Instead, use the alternate
248  // form of condenseCommon() with a CommonPaths object constructed outside
249  // the loop.
250  static inline bool
251  condenseCommon(UT_String &condensed, const char *path)
252  {
253  CommonPaths common;
254  return condenseCommon(condensed, path, common);
255  }
256 
257  // @note This is slow if you use it in a loop. Instead, use the alternate
258  // form of condenseCommon() with a CommonPaths object constructed outside
259  // the loop.
260  static inline bool
261  condenseCommon(UT_WorkBuffer &condensed, const char *path)
262  {
263  CommonPaths common;
264  return condenseCommon(condensed, path, common);
265  }
266 
267  static inline bool
268  condenseCommon(UT_String &path, const CommonPaths &common)
269  {
270  return condenseCommon(path, path, common);
271  }
272 
273  static inline bool
275  {
276  return condenseCommon(path, path.buffer(), common);
277  }
278 
279  static inline bool
280  condenseCommon(UT_String &path, const char *source_path,
281  const CommonPaths &common)
282  {
283  return condensePath(path, source_path, common.myPaths,
284  common.myCondensedPaths, common.size());
285  }
286 
287  static inline bool
288  condenseCommon(UT_WorkBuffer &path, const char *source_path,
289  const CommonPaths &common)
290  {
291  return condensePath(path, source_path, common.myPaths,
292  common.myCondensedPaths, common.size());
293  }
294  // @}
295 
296  // The path variables passed in should ALWAYS be UT_StrControl types.
297  // The values of these variables are tested against the leading portion of
298  // the full path specified and the "longest" match is used for replacement
299  // of the lead. For example the result might be $HOME/relpath.
300  static bool condensePath(UT_WorkBuffer &result,
301  const char *source_path,
302  const char *const paths[],
303  const char *const condensed_paths[],
304  int npaths);
305 
306  static inline bool
308  const char *source_path,
309  const char *const paths[],
310  const char *const condensed_paths[],
311  int npaths)
312  {
313  UT_WorkBuffer condensed;
314  bool ok = condensePath(condensed, source_path,
315  paths, condensed_paths, npaths);
316  if (ok)
317  condensed.copyIntoString(path);
318  else if (path.buffer() != source_path)
319  path.harden(source_path);
320  return ok;
321  }
322 
323  static inline bool
325  const char *const paths[],
326  const char *const condensed_paths[],
327  int npaths)
328  {
329  return condensePath(path, path, paths, condensed_paths, npaths);
330  }
331 
332  // This method will take a fully qualified path and strip off the lead
333  // characters of the pathname -- resulting in path which can still be found
334  // by the path searching code, but which is simpler in nature...
335  // For example, the path $HOME/houdini/fonts/Type1/Courier might be
336  // stripped to simply "Type1/Courier"
337  static int stripLeadPath(UT_String &path, UT_KnownPath pathvar);
338 
339  // Similar to stripLeadPath except that it will always return the shortest
340  // path, irrespective of the order of the paths.
341  static int stripMaxLeadPath(UT_String &path, UT_KnownPath pathvar);
342 
343  // This method, given a filename that is possibly fully specified, will
344  // try to return the fully specified path to the file. Returns true
345  // if absolute_path is valid, false otherwise.
346  static bool resolveAbsolutePath(const char *filename,
347  UT_String &absolute_path);
348 
349  // return the expansion of $HOME/houdini$HOUDINI_VER as though it was
350  // the @ in the search pattern. If there is no search pattern then just
351  // return $HOME/houdini$HOUDINI_VER
352  void getHomeHoudiniSearch(UT_StringArray &strs) const;
353 
354  // Utility for splitting an environment variable path. The function expects
355  // the path to be separated with these characters:
356  // Windows: ";"
357  // Linux or Mac: ":" or ";"
358  //
359  // path: path content. May contain "&" as insertion point of an existing path.
360  // list: return list of splitted elements
361  // insert_path: insert_path is prepended to path at the insertion point (&).
362  static void splitPath(const char *path, UT_StringArray &list,
363  const char *insert_path);
364 
365 private:
366  UT_PathSearch & operator=( const UT_PathSearch &copy ); // not implemented
367 
368 private:
369  void initialize(const char *def,
370  int var_id,
371  const char *carat,
372  int at_expand);
373  void clearPath();
374 
375  /// Helper function for matchAllFiles() and matchAllFilesInSubdirectory().
376  void matchAllFilesInPath(const UT_StringList &exts,
377  const char *path,
378  bool returnfullpaths,
379  bool recurse,
380  UT_StringArray &list,
381  UT_StringSet *seen)const;
382 
383  void appendPathPrivate(const char *path);
384 
385  int myVariableId;
386  UT_String myDefaultPath;
387  UT_String myCaratExpand;
388  int myAtExpand;
389  int myMaxPath; // Max path length
390 
391  UT_StringArray myPaths;
392  UT_StringArray myBasePaths;
393  UT_StringArray myCondensedPaths;
394 };
395 
396 inline int
398 {
400  houdiniFindFile(find, result))
401  result = 0;
402  return result.isstring();
403 }
404 
405 inline int
407 {
409  houdiniFindFile(find, result))
410  result.clear();
411  return result.length() != 0;
412 }
413 
414 inline int
416 {
417  result.clear();
419  findAllFiles(find, result);
420  return result.entries();
421 }
422 
423 inline int
425 {
427  houdiniFindDirectory(find, result))
428  result = 0;
429  return result.isstring();
430 }
431 
432 inline int
434 {
436  houdiniFindDirectory(find, result))
437  result.clear();
438  return result.length() != 0;
439 }
440 
441 inline int
443 {
444  result.clear();
446  findAllDirectories(find, result);
447  return result.entries();
448 }
449 
450 #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:613
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:509
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:49
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:215
#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:648
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:691
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:716
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
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2089
GLenum src
Definition: glcorearb.h:1793