HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FS_Traverse.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: FS_Traverse.h (FS Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __FS_Traverse__
12 #define __FS_Traverse__
13 
14 #include "FS_Info.h"
15 #include <UT/UT_Array.h>
16 #include <UT/UT_StringArray.h>
17 #include <UT/UT_WorkBuffer.h>
18 
19 namespace FS
20 {
21  /// Utility function to process the contents of the traverse() function
22  template <bool DIR, typename T>
23  bool
25  UT_WorkBuffer &fullpath,
26  exint fullpath_len,
27  const UT_StringArray &paths,
28  const UT_Array<FS_Stat> &stats)
29  {
30  for (exint i = 0, n = paths.size(); i < n; ++i)
31  {
32  fullpath.truncate(fullpath_len);
33  fullpath.append(paths[i]);
34  if (DIR)
35  {
36  if (!func.dir(fullpath.buffer(), stats[i]))
37  return false;
38  }
39  else
40  {
41  if (!func.file(fullpath.buffer(), stats[i]))
42  return false;
43  }
44  }
45  return true;
46  }
47 
48  /// FS function to process all files in a directory. The template functor
49  /// should have two methods:
50  /// - bool dir(const char *path, const FS_Stat &stat);
51  /// - bool file(const char *path, const FS_Stat &stat);
52  /// If either of these functions returns false, traversal will be terminated
53  ///
54  /// For example, to list all files in a directory: @code
55  /// struct FileList {
56  /// bool file(const char *dir, const FS_Stat &stat) const {
57  /// if (myRecursive) {
58  /// printf("%s/\n", dir);
59  /// FS::traverse(dir, this);
60  /// }
61  /// }
62  /// bool file(const char *path, const FS_Stat &stat) const {
63  /// printf("%s\n", path);
64  /// }
65  /// bool myRecursive = true;
66  /// };
67  /// @endcode
68  template <typename T>
69  void
71  const char *path,
72  bool sort_contents=false,
73  bool directories_first=true)
74  {
75  FS_Info info(path);
76  if (!info.getIsDirectory())
77  {
78  FS_Stat stat;
79  if (FS_Info::statFile("", path, &stat))
80  func.file(path, stat);
81  return;
82  }
83 
84  UT_WorkBuffer fullpath;
85  if (!UTisstring(path))
86  fullpath.strcpy("./");
87  else
88  {
89  fullpath.strcpy(path);
90  if (fullpath.last() != '/')
91  fullpath.append('/');
92  }
93  exint olen = fullpath.length();
94 
95  UT_StringArray files;
96  UT_StringArray dirs;
97  UT_Array<FS_Stat> stats;
98  UT_Array<FS_Stat> dir_stats;
99  if (info.getContents(files, &dirs, &stats, &dir_stats))
100  {
101  // Traverse directories first
102  if (sort_contents)
103  {
104  dirs.sort();
105  files.sort();
106  }
107  if (directories_first)
108  {
109  if (!process<true, T>(func, fullpath, olen, dirs, dir_stats))
110  return;
111  }
112  if (!process<false, T>(func, fullpath, olen, files, stats))
113  return;
114  if (!directories_first)
115  {
116  if (!process<true, T>(func, fullpath, olen, dirs, dir_stats))
117  return;
118  }
119  }
120  }
121 }
122 
123 #endif
SYS_FORCE_INLINE exint length() const
bool getIsDirectory() const
Returns if the path is a directory.
void sort(bool forward, bool numbered)
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
int64 exint
Definition: SYS_Types.h:125
SYS_FORCE_INLINE const char * buffer() const
SYS_FORCE_INLINE void strcpy(const char *src)
SYS_FORCE_INLINE char last() const
static bool statFile(const char *source, const char *filename, FS_Stat *fstat)
exint size() const
Definition: UT_Array.h:653
GLdouble n
Definition: glcorearb.h:2008
bool getContents(UT_StringArray &contents, UT_StringArray *dirs=0, UT_Array< FS_Stat > *stats=0, UT_Array< FS_Stat > *dir_stats=0)
void traverse(T &func, const char *path, bool sort_contents=false, bool directories_first=true)
Definition: FS_Traverse.h:70
SYS_FORCE_INLINE void truncate(exint new_length)
Helper class for encapsulating file information.
Definition: FS_Info.h:55
GLenum func
Definition: glcorearb.h:783
SYS_FORCE_INLINE bool UTisstring(const char *s)
SYS_FORCE_INLINE void append(char character)
bool process(T &func, UT_WorkBuffer &fullpath, exint fullpath_len, const UT_StringArray &paths, const UT_Array< FS_Stat > &stats)
Utility function to process the contents of the traverse() function.
Definition: FS_Traverse.h:24
Class for retrieving file information.
Definition: FS_Info.h:99