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. The path must be unique
121  // otherwise the function returns false. The new path condensed
122  // format is prepended if condensed=true.
123  // Return true if a new path is prepended or false otherwise.
124  bool prependPath(const char *path, bool condensed = false);
125 
126  // Find the first file that occurs in the path
127  int findFile(UT_String &result, const char *search) const;
128  int findFile(UT_WorkBuffer &result, const char *search) const;
129  int findDirectory(UT_String &result, const char *search) const;
130 
131  // Find the first of the given list of files in the path. If they exist
132  // in the same directory, then the order is determined from the search_list
133  bool findFileFromList(UT_String &result,
134  const UT_StringArray &search_list) const;
135  bool findDirectoryFromList(UT_String &result,
136  const UT_StringArray &search_list) const;
137 
138  // Find all files in the path.
139  // The contents of the list is APPENDED to,
140  // it isn't automatically cleared.
141  int findAllFiles(const char *search,
142  UT_StringArray &list) const;
143 
144  int findAllDirectories(const char *search,
145  UT_StringArray &list) const;
146 
147  // This function will match all files with the given extension in the
148  // search path and fill the given list with the filenames only. Returns the
149  // # of entries in the filled list. (sample extension = ".vex")
150  int matchAllFiles(const char *extension,
151  bool returnfullpaths,
152  UT_StringArray &list,
153  bool recurse=false,
154  bool skip_dup_paths=false) const;
155 
156  // This function will match all files with one of the given extensions
157  // in the search path and fill the given list with the filenames only.
158  // Returns the # of entries in the filled list. The extensions should begin
159  // with a period.
160  int matchAllFiles(const UT_StringList &exts,
161  bool returnfullpaths,
162  UT_StringArray &list,
163  bool recurse=false,
164  bool skip_dup_paths=false) const;
165 
166  int matchAllFilesInSubdirectory(const char *extension,
167  const char *subdirectory,
168  bool returnfullpaths,
169  UT_StringArray &list,
170  bool recurse=false,
171  bool skip_dup_paths=false) const;
172  int matchAllFilesInSubdirectory(const UT_StringList &exts,
173  const char *subdirectory,
174  bool returnfullpaths,
175  UT_StringArray &list,
176  bool recurse=false,
177  bool skip_dup_paths=false) const;
178 
179  // Old style find-file. If a result buffer is passed in, the search
180  // result will be stored in it. This is a little dangerous since it's a
181  // fixed length buffer.
182  int houdiniFindFile(const char *find, UT_String &result) const;
183  int houdiniFindFile(const char *find, UT_WorkBuffer &result) const;
184 
185  int houdiniFindDirectory(const char *find, UT_String &result) const;
186  int houdiniFindDirectory(const char *find, UT_WorkBuffer &r) const;
187 
188  int getEntries() const { return myPaths.entries(); }
189  const char *getPathComponent(int i) const { return myPaths(i); }
190 
191  // Base paths are all paths tested, whether they are valid directories or
192  // not.
193  int getBaseEntries() const { return myBasePaths.entries(); }
194  const char *getBaseComponent(int i) const { return myBasePaths(i); }
195  const char *getCondensedComponent(int i) const
196  { return myCondensedPaths(i); }
197 
198  const char *getDefaultPath() const { return myDefaultPath; }
199  const char *getCaratExpand() const { return myCaratExpand; }
200 
201  int getVariableId() const { return myVariableId; }
202  int getAtExpand() const { return myAtExpand; }
203 
204  // Take a fully qualified path and collapse just the $HIP part of the path
205  static int condenseHip(UT_String &path);
206  static int condenseHip(UT_WorkBuffer &path);
207 
208  // Take a fully qualified path and collapse just the $JOB part of the path
209  static int condenseJob(UT_String &path);
210 
211  // Helper struct which constructs with myPaths/myCondensedPaths suitable
212  // for calling into condensePath().
214  {
215  CommonPaths();
216 
217  enum { NUM_PATHS = 7 };
218 
219  int size() const { return NUM_PATHS; }
220  const char *hip() const { return myPaths[2]; }
221 
222  const char *myPaths[NUM_PATHS];
223  const char *myCondensedPaths[NUM_PATHS];
224  };
225 
226  // Take a fully qualified path and collapse it to a "common" variable path.
227  // For example, $HFS/... instead of /usr/hfs5.3.391/...
228  // @{
229  // @note This is slow if you use it in a loop. Instead, use the alternate
230  // form of condenseCommon() with a CommonPaths object constructed outside
231  // the loop.
232  static inline bool
234  {
235  CommonPaths common;
236  return condenseCommon(path, path, common);
237  }
238 
239  // @note This is slow if you use it in a loop. Instead, use the alternate
240  // form of condenseCommon() with a CommonPaths object constructed outside
241  // the loop.
242  static inline bool
244  {
245  CommonPaths common;
246  return condenseCommon(path, path.buffer(), common);
247  }
248 
249  // @note This is slow if you use it in a loop. Instead, use the alternate
250  // form of condenseCommon() with a CommonPaths object constructed outside
251  // the loop.
252  static inline bool
253  condenseCommon(UT_String &condensed, const char *path)
254  {
255  CommonPaths common;
256  return condenseCommon(condensed, path, common);
257  }
258 
259  // @note This is slow if you use it in a loop. Instead, use the alternate
260  // form of condenseCommon() with a CommonPaths object constructed outside
261  // the loop.
262  static inline bool
263  condenseCommon(UT_WorkBuffer &condensed, const char *path)
264  {
265  CommonPaths common;
266  return condenseCommon(condensed, path, common);
267  }
268 
269  static inline bool
270  condenseCommon(UT_String &path, const CommonPaths &common)
271  {
272  return condenseCommon(path, path, common);
273  }
274 
275  static inline bool
277  {
278  return condenseCommon(path, path.buffer(), common);
279  }
280 
281  static inline bool
282  condenseCommon(UT_String &path, const char *source_path,
283  const CommonPaths &common)
284  {
285  return condensePath(path, source_path, common.myPaths,
286  common.myCondensedPaths, common.size());
287  }
288 
289  static inline bool
290  condenseCommon(UT_WorkBuffer &path, const char *source_path,
291  const CommonPaths &common)
292  {
293  return condensePath(path, source_path, common.myPaths,
294  common.myCondensedPaths, common.size());
295  }
296  // @}
297 
298  // The path variables passed in should ALWAYS be UT_StrControl types.
299  // The values of these variables are tested against the leading portion of
300  // the full path specified and the "longest" match is used for replacement
301  // of the lead. For example the result might be $HOME/relpath.
302  static bool condensePath(UT_WorkBuffer &result,
303  const char *source_path,
304  const char *const paths[],
305  const char *const condensed_paths[],
306  int npaths);
307 
308  static inline bool
310  const char *source_path,
311  const char *const paths[],
312  const char *const condensed_paths[],
313  int npaths)
314  {
315  UT_WorkBuffer condensed;
316  bool ok = condensePath(condensed, source_path,
317  paths, condensed_paths, npaths);
318  if (ok)
319  condensed.copyIntoString(path);
320  else if (path.buffer() != source_path)
321  path.harden(source_path);
322  return ok;
323  }
324 
325  static inline bool
327  const char *const paths[],
328  const char *const condensed_paths[],
329  int npaths)
330  {
331  return condensePath(path, path, paths, condensed_paths, npaths);
332  }
333 
334  // This method will take a fully qualified path and strip off the lead
335  // characters of the pathname -- resulting in path which can still be found
336  // by the path searching code, but which is simpler in nature...
337  // For example, the path $HOME/houdini/fonts/Type1/Courier might be
338  // stripped to simply "Type1/Courier"
339  static int stripLeadPath(UT_String &path, UT_KnownPath pathvar);
340 
341  // Similar to stripLeadPath except that it will always return the shortest
342  // path, irrespective of the order of the paths.
343  static int stripMaxLeadPath(UT_String &path, UT_KnownPath pathvar);
344 
345  // This method, given a filename that is possibly fully specified, will
346  // try to return the fully specified path to the file. Returns true
347  // if absolute_path is valid, false otherwise.
348  static bool resolveAbsolutePath(const char *filename,
349  UT_String &absolute_path);
350 
351  // return the expansion of $HOME/houdini$HOUDINI_VER as though it was
352  // the @ in the search pattern. If there is no search pattern then just
353  // return $HOME/houdini$HOUDINI_VER
354  void getHomeHoudiniSearch(UT_StringArray &strs) const;
355 
356  // Utility for splitting an environment variable path. The function expects
357  // the path to be separated with these characters:
358  // Windows: ";"
359  // Linux or Mac: ":" or ";"
360  //
361  // path: path content. May contain "&" as insertion point of an existing path.
362  // list: return list of splitted elements
363  // insert_path: insert_path is prepended to path at the insertion point (&).
364  static void splitPath(const char *path, UT_StringArray &list,
365  const char *insert_path);
366 
367 private:
368  UT_PathSearch & operator=( const UT_PathSearch &copy ); // not implemented
369 
370 private:
371  void initialize(const char *def,
372  int var_id,
373  const char *carat,
374  int at_expand);
375  void clearPath();
376 
377  /// Helper function for matchAllFiles() and matchAllFilesInSubdirectory().
378  void matchAllFilesInPath(const UT_StringList &exts,
379  const char *path,
380  bool returnfullpaths,
381  bool recurse,
382  UT_StringArray &list,
383  UT_StringSet *seen)const;
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
LeafData & operator=(const LeafData &)=delete
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:729
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