HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FS_Info.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_Info.h ( FS Library, C++)
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __FS_Info__
13 #define __FS_Info__
14 
15 #include "FS_API.h"
16 #include <UT/UT_Assert.h>
17 #include <UT/UT_ValArray.h>
18 #include <UT/UT_Array.h>
19 #include <UT/UT_String.h>
20 #include <UT/UT_StringArray.h>
21 #include <UT/UT_SysClone.h>
22 #include <SYS/SYS_Types.h>
23 
24 
25 class FS_Info;
26 class FS_InfoHelper;
27 class FS_IndexFile;
28 
29 
30 /// @brief Make a directory and all the parent directories needed.
31 ///
32 /// @note Currently, this function only accepts native file paths.
33 ///
34 /// @param dir_info @n The directory path to create
35 /// @param mode @n The unix file mode
36 /// @param ignore_umask @n By default, the users umask will be used in
37 /// conjunction with the mode. This parameter will @b force
38 /// the mode to be the given value.
39 ///
40 /// @return Returns false only if a directory creation attempt failed.
41 FS_API bool FSmakeDirs(
42  FS_Info &dir_info,
43  mode_t mode = 0777,
44  bool ignore_umask = false);
45 
46 
47 /// Read, write and execute permissions for files.
49 {
50  FS_READ = 0x01,
51  FS_WRITE = 0x02,
52  FS_EXECUTE = 0x04
53 };
54 
55 /// Helper class for encapsulating file information
57 {
58 public:
59  FS_Stat(time_t f_mtime = 0, int64 fsize = 0, bool is_dir = false, bool is_shortcut = false)
60  : myModTime(f_mtime), mySize(fsize), myDirectory(is_dir), myShortcut(is_shortcut)
61  {
62  }
64  {
65  *this = src;
66  }
67 
68  // This method is used to maintain the stat information when a
69  // sequence of files is being collapsed into one entry.
70  void updateSequenceStat(const FS_Stat &entry)
71  {
72  // The modification time for a sequence is the most current
73  // of the modification times of its entries.
74  if (myModTime < entry.myModTime)
75  myModTime = entry.myModTime;
76 
77  // Size for a sequence should be the sum of all files
78  mySize += entry.mySize;
79  }
80 
82  {
83  myModTime = s.myModTime;
84  mySize = s.mySize;
85  myDirectory = s.myDirectory;
86  myShortcut = s.myShortcut;
87  return *this;
88  }
89 
90  bool operator==(const FS_Stat &s)
91  {
92  return (myModTime == s.myModTime
93  && mySize == s.mySize
94  && myDirectory == s.myDirectory
95  && myShortcut == s.myShortcut);
96  }
97 
98  time_t modTime() const { return myModTime; }
99  int64 size() const { return mySize; }
100  bool isDirectory() const { return myDirectory; }
101  bool isShortcut() const { return myShortcut; }
102 
103  time_t myModTime;
105  bool myDirectory:1,
106  myShortcut:1; // only on Windows
107 };
108 
109 
110 /// Class for retrieving file information
112 {
113 public:
114  /// Use this constructor to create a new info object. The
115  /// specified file is not actually opened or accessed until one of
116  /// the information gathering methods is called.
117  FS_Info(const char *source);
118 
119  virtual ~FS_Info();
120 
121  /// Return the path parsed from the source
122  const UT_StringHolder &path() const { return myPath; }
123 
124  /// Return the section parsed from the source
125  const UT_StringHolder &section() const { return mySection; }
126 
127  /// This function returns whether or not the current
128  /// user has rights to access the file in the given
129  /// mode. The mode parameter can be any of FS_READ,
130  /// FS_WRITE, or FS_EXECUTE. You can also or these
131  /// values together to check multiple access types
132  /// simultaneously. If the mode value is 0, the
133  /// function returns @c true if the file exists.
134  bool hasAccess(int mode = 0) const;
135 
136  /// Returns if the path is a directory
137  bool getIsDirectory() const;
138 
139  /// Returns the modification time of the file. The
140  /// returned value can be cast directly to a time_t
141  /// value, as defined in the standard C library.
142  int getModTime() const;
143 
144  /// Returns the latest modification time of the
145  /// directory and all of its contents recursively.
146  /// On a file it works like getModTime().
147  int getRecursiveMaxModTime() const;
148 
149  /// Returns the file size
150  int64 getFileDataSize() const;
151 
152  /// Returns the file extension
153  UT_String getExtension() const;
154 
155  /// Does bulk load of files and directories from the
156  /// source. If the source specified in the constructor
157  /// is a directory rather than an individual file,
158  /// this method will return a listing of all files and
159  /// directories contained in that directory.
160  bool getContents(UT_StringArray &contents,
161  UT_StringArray *dirs = 0,
162  UT_Array<FS_Stat> *stats = 0,
163  UT_Array<FS_Stat> *dir_stats = 0
164  );
165 
166  /// Treats the source as a path glob pattern and
167  /// expands it to the available files. Note that
168  /// backslash escaping with [] is not supported.
169  bool glob(UT_StringArray &result, bool only_readable=false);
170 
171  bool exists() const;
172  bool fileExists() const;
173  bool canReadFile() const;
174  bool canWriteFile() const;
175  bool canWriteDirectory() const;
176  bool canDeleteFile() const;
177  static char getNextSepChar(const char *source);
178  static char getPrevSepChar(const char *source);
179  static bool customNavigatePath(UT_String &path, const char *file);
180  static bool getPathOnDisk(UT_String &path, const char *file);
181 
182  static bool isNativePath(const char *path);
183 
184  static bool getContentsFromIndexFile(FS_IndexFile &index,
185  UT_StringArray &contents,
186  UT_StringArray *dirs);
187  static bool getContentsFromIndexFile(
189  UT_StringArray &contents,
190  UT_StringArray *dirs,
191  UT_Array<FS_Stat> *stats,
192  UT_Array<FS_Stat> *dir_stats);
193  static bool getContentsFromDiskPath(const char *path,
194  UT_StringArray &contents,
195  UT_StringArray *dirs,
196  UT_Array<FS_Stat> *stats = NULL,
197  UT_Array<FS_Stat> *dir_stats = NULL);
198 
199  static bool statFile(const char *source,
200  const char *filename,
201  FS_Stat *fstat);
202 
203  // Functions for adding and removing helpers.
204  static void addInfoHelper(FS_InfoHelper *helper);
205  static void removeInfoHelper(FS_InfoHelper *helper);
206 
207 private:
208  static UT_ValArray<FS_InfoHelper *> &getHelpers();
209 
210  UT_StringHolder myPath;
211  UT_StringHolder mySection;
212 };
213 
214 
215 /// This class provides a plug-in method for adding a custom "file system".
216 /// @see FS_ReaderHelper, FS_WriterHelper
218 {
219 public:
221  { FS_Info::addInfoHelper(this); }
222  virtual ~FS_InfoHelper()
223  { FS_Info::removeInfoHelper(this); }
224 
225  /// Determine whether this helper can process the filename.
226  virtual bool canHandle(const char *source) = 0;
227 
228  /// @param source Filename for access check
229  /// @param mode The read/write/execute status @see @ref FS_FileAccessMode
230  virtual bool hasAccess(const char *source, int mode) = 0;
231 
232  /// Return whether the filename is a directory
233  virtual bool getIsDirectory(const char *source) = 0;
234 
235  /// Get the modification timestamp (returns time_t)
236  virtual int getModTime(const char *source) = 0;
237 
238  /// Get the file size in bytes
239  virtual int64 getSize (const char *source) = 0;
240 
241  /// Read the contents of a directory
242  /// @param source - The directory to read
243  /// @param contents - The list of files in the directory
244  /// @param dirs - The list of directories in the directory
245  /// If @c dirs is NULL, then all directories should be included in the @c
246  /// contents.
247  virtual bool getContents(const char *source,
248  UT_StringArray &contents,
249  UT_StringArray *dirs) = 0;
250 
251  virtual UT_String getExtension(const char *source)
252  {
254  ext = UT_String(source).fileExtension();
255  return ext;
256  }
257 
258  /// Next separator character
259  virtual char getNextSepChar(const char * /*source*/)
260  { return '/'; }
261  /// Previous separator character
262  virtual char getPrevSepChar(const char * /*source*/)
263  { return '/'; }
264  /// Constructs a new path and returns true, if navigating a file path
265  /// requires something else than appending getNextSepChar() and
266  /// concatenating file. But if concatenating sep char and file is
267  /// sufficient, does nothing and returns false.
268  virtual bool customNavigatePath(UT_String &path, const char *file)
269  { return false; }
270 
271  virtual bool getPathOnDisk(UT_String &path, const char *file)
272  { return false; }
273 };
274 
275 #endif
276 
time_t modTime() const
Definition: FS_Info.h:98
const UT_StringHolder & path() const
Return the path parsed from the source.
Definition: FS_Info.h:122
virtual char getPrevSepChar(const char *)
Previous separator character.
Definition: FS_Info.h:262
GLsizei const GLchar *const * path
Definition: glcorearb.h:3340
virtual char getNextSepChar(const char *)
Next separator character.
Definition: FS_Info.h:259
virtual bool customNavigatePath(UT_String &path, const char *file)
Definition: FS_Info.h:268
const char * fileExtension() const
Return the extension of a file path string.
Definition: UT_String.h:635
virtual ~FS_InfoHelper()
Definition: FS_Info.h:222
FS_Stat(const FS_Stat &src)
Definition: FS_Info.h:63
FS_Stat(time_t f_mtime=0, int64 fsize=0, bool is_dir=false, bool is_shortcut=false)
Definition: FS_Info.h:59
FS_API bool FSmakeDirs(FS_Info &dir_info, mode_t mode=0777, bool ignore_umask=false)
Make a directory and all the parent directories needed.
int64 mySize
Definition: FS_Info.h:104
bool myShortcut
Definition: FS_Info.h:105
long long int64
Definition: SYS_Types.h:100
static void removeInfoHelper(FS_InfoHelper *helper)
time_t myModTime
Definition: FS_Info.h:103
void updateSequenceStat(const FS_Stat &entry)
Definition: FS_Info.h:70
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:802
FS_Stat & operator=(const FS_Stat &s)
Definition: FS_Info.h:81
bool isShortcut() const
Definition: FS_Info.h:101
GLenum mode
Definition: glcorearb.h:98
bool myDirectory
Definition: FS_Info.h:105
Helper class for encapsulating file information.
Definition: FS_Info.h:56
virtual bool getPathOnDisk(UT_String &path, const char *file)
Definition: FS_Info.h:271
int64 size() const
Definition: FS_Info.h:99
bool isDirectory() const
Definition: FS_Info.h:100
virtual UT_String getExtension(const char *source)
Definition: FS_Info.h:251
FS_FileAccessMode
Read, write and execute permissions for files.
Definition: FS_Info.h:48
GLuint index
Definition: glcorearb.h:785
const UT_StringHolder & section() const
Return the section parsed from the source.
Definition: FS_Info.h:125
bool operator==(const FS_Stat &s)
Definition: FS_Info.h:90
static void addInfoHelper(FS_InfoHelper *helper)
Class for retrieving file information.
Definition: FS_Info.h:111
#define FS_API
Definition: FS_API.h:10
GLenum src
Definition: glcorearb.h:1792