TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
FileSystem.h
Go to the documentation of this file.
1 
9 #ifndef G3D_FileSystem_h
10 #define G3D_FileSystem_h
11 
12 #include "G3D/platform.h"
13 #include "G3D/Array.h"
14 #include "G3D/Table.h"
15 #include "G3D/Set.h"
16 #include "G3D/GMutex.h"
17 
18 namespace G3D {
19 
46 class FileSystem {
47 public:
48 
49  class ListSettings {
50  public:
52  bool files;
53 
56 
59 
61  bool recursive;
62 
65 
67  files(true),
68  directories(true),
69 # ifdef G3D_WINDOWS
70  caseSensitive(true),
71 # else
72  caseSensitive(false),
73 # endif
74  recursive(false),
75  includeParentPath(true) {}
76  };
77 
78 private:
79 
83 
85 
86  enum Type {
91  };
92 
93  class Entry {
94  public:
96  std::string name;
98  Entry() : type(UNKNOWN) {}
99  Entry(const char* n) : name(n), type(UNKNOWN) {}
100  };
101 
102  class Dir {
103  public:
104 
106  bool exists;
107 
108  bool isZipfile;
109  bool inZipfile;
110 
113 
115  double lastChecked;
116 
117  bool contains(const std::string& child, bool caseSensitive =
118 #ifdef G3D_WINDOWS
119  false
120 #else
121  true
122 #endif
123 ) const;
124 
126  void computeZipListing(const std::string& zipfile, const std::string& pathInsideZipfile);
127 
128  Dir() : exists(false), isZipfile(false), inZipfile(false), lastChecked(0) {}
129  };
130 
134 
137  Dir& getContents(const std::string& path, bool forceUpdate);
138 
140  FileSystem();
141 
142  static FileSystem& instance();
143  static GMutex mutex;
144 
145 
146 # ifdef G3D_WINDOWS
147 
148  const Array<std::string>& _drives();
149 # endif
150 
152  bool _inZipfile(const std::string& path, std::string& zipfile);
153 
155  void _clearCache(const std::string& path);
156 
158  bool _inZipfile(const std::string& path) {
159  std::string ignore;
160  return inZipfile(path, ignore);
161  }
162 
164  void _setCacheLifetime(float t);
165 
166 
168  float _cacheLifetime() const {
169  return m_cacheLifetime;
170  }
171 
173  void _createDirectory(const std::string& path);
174 
176  bool _exists(const std::string& f, bool trustCache = true, bool caseSensitive =
177 #ifdef G3D_WINDOWS
178  false
179 #else
180  true
181 #endif
182  );
183 
185  bool _isDirectory(const std::string& path);
186 
188  bool _isFile(const std::string& path) {
189  return ! isDirectory(path);
190  }
191 
193  void _copyFile(const std::string& srcPath, const std::string& dstPath);
194 
196  std::string _resolve(const std::string& path, const std::string& cwd = currentDirectory());
197 
199  bool _isNewer(const std::string& src, const std::string& dst);
200 
202  std::string _currentDirectory();
203 
205  int64 _size(const std::string& path);
206 
208  void listHelper(const std::string& shortSpec, const std::string& parentPath, Array<std::string>& result, const ListSettings& settings);
209 
211  void _list(const std::string& spec, Array<std::string>& result, const ListSettings& listSettings = ListSettings());
212 
214  bool _isZipfile(const std::string& path);
215 
217  void _getFiles(const std::string& spec, Array<std::string>& result, bool includeParentPath = false) {
218  ListSettings set;
219  set.includeParentPath = includeParentPath;
220  set.directories = false;
221  set.files = true;
222  return list(spec, result, set);
223  }
224 
226  void _getDirectories(const std::string& spec, Array<std::string>& result, bool includeParentPath = false) {
227  ListSettings set;
228  set.includeParentPath = includeParentPath;
229  set.directories = true;
230  set.files = false;
231  return list(spec, result, set);
232  }
233 
235  FILE* _fopen(const char* filename, const char* mode);
236 
238  void _removeFile(const std::string& path);
239 
240 public:
241 
243  static void init();
244 
246  static void cleanup();
247 
248 # ifdef G3D_WINDOWS
249 
250  static const Array<std::string>& drives() {
251  mutex.lock();
252  const Array<std::string>& s = instance()._drives();
253  mutex.unlock();
254  return s;
255  }
256 # endif
257 
264  static bool inZipfile(const std::string& path, std::string& zipfile) {
265  mutex.lock();
266  bool b = instance()._inZipfile(path, zipfile);
267  mutex.unlock();
268  return b;
269  }
270 
273  static void clearCache(const std::string& path = "") {
274  mutex.lock();
275  instance()._clearCache(path);
276  mutex.unlock();
277  }
278 
279 
282  static FILE* fopen(const char* filename, const char* mode) {
283  mutex.lock();
284  FILE* f = instance()._fopen(filename, mode);
285  mutex.unlock();
286  return f;
287  }
288 
289 
290  static void fclose(FILE* f) {
291  mutex.lock();
292  ::fclose(f);
293  mutex.unlock();
294  }
295 
300  static bool inZipfile(const std::string& path) {
301  mutex.lock();
302  bool b = instance()._inZipfile(path);
303  mutex.unlock();
304  return b;
305  }
306 
307 
314  static void removeFile(const std::string& path) {
315  mutex.lock();
316  instance()._removeFile(path);
317  mutex.unlock();
318  }
319 
320 
323  static bool isZipfile(const std::string& path) {
324  mutex.lock();
325  bool b = instance()._isZipfile(path);
326  mutex.unlock();
327  return b;
328  }
329 
330 
333  void setCacheLifetime(float t) {
334  mutex.lock();
336  mutex.unlock();
337  }
338 
341  static float cacheLifetime() {
342  mutex.lock();
343  float f = instance()._cacheLifetime();
344  mutex.unlock();
345  return f;
346  }
347 
348 
356  static void createDirectory(const std::string& path) {
357  mutex.lock();
358  instance()._createDirectory(path);
359  mutex.unlock();
360  }
361 
362 
364  static std::string currentDirectory() {
365  mutex.lock();
366  const std::string& s = instance()._currentDirectory();
367  mutex.unlock();
368  return s;
369  }
370 
371 
378  static void copyFile(const std::string& srcPath, const std::string& dstPath) {
379  mutex.lock();
380  instance()._copyFile(srcPath, dstPath);
381  mutex.unlock();
382  }
383 
384 
385 
401  static bool exists(const std::string& f, bool trustCache = true, bool caseSensitive =
402 #ifdef G3D_WINDOWS
403  false
404 #else
405  true
406 #endif
407  ) {
408  mutex.lock();
409  bool e = instance()._exists(f, trustCache, caseSensitive);
410  mutex.unlock();
411  return e;
412  }
413 
414 
416  static bool isDirectory(const std::string& path) {
417  mutex.lock();
418  bool b = instance()._isDirectory(path);
419  mutex.unlock();
420  return b;
421  }
422 
423 
425  static bool isFile(const std::string& path) {
426  mutex.lock();
427  bool b = instance()._isFile(path);
428  mutex.unlock();
429  return b;
430  }
431 
432 
440  static std::string resolve(const std::string& path, const std::string& cwd = currentDirectory()) {
441  mutex.lock();
442  const std::string& s = instance()._resolve(path, cwd);
443  mutex.unlock();
444  return s;
445  }
446 
447 
453  static bool isNewer(const std::string& src, const std::string& dst) {
454  mutex.lock();
455  bool b = instance()._isNewer(src, dst);
456  mutex.unlock();
457  return b;
458  }
459 
460 
462  static int64 size(const std::string& path) {
463  mutex.lock();
464  int64 i = instance()._size(path);
465  mutex.unlock();
466  return i;
467  }
468 
469 
479  static void list(const std::string& spec, Array<std::string>& result,
480  const ListSettings& listSettings = ListSettings()) {
481  mutex.lock();
482  instance()._list(spec, result, listSettings);
483  mutex.unlock();
484  }
485 
486 
488  static void getFiles(const std::string& spec, Array<std::string>& result, bool includeParentPath = false) {
489  mutex.lock();
490  instance()._getFiles(spec, result, includeParentPath);
491  mutex.unlock();
492  }
493 
494 
496  static void getDirectories(const std::string& spec, Array<std::string>& result, bool includeParentPath = false) {
497  mutex.lock();
498  instance()._getDirectories(spec, result, includeParentPath);
499  mutex.unlock();
500  }
501 
504  static void markFileUsed(const std::string& filename);
505 
508  static const Set<std::string>& usedFiles();
509 };
510 
511 
526 class FilePath {
527 public:
528 
530  static std::string concat(const std::string& a, const std::string& b);
531 
537  static bool isRoot(const std::string& f);
538 
540  static std::string removeTrailingSlash(const std::string& f);
541 
543  static std::string ext(const std::string& path);
544 
546  static std::string baseExt(const std::string& path);
547 
549  static std::string base(const std::string& path);
550 
552  static std::string parent(const std::string& path);
553 
555  static bool containsWildcards(const std::string& p);
556 
558  static std::string canonicalize(std::string x);
559 
563  static std::string expandEnvironmentVariables(const std::string& path);
564 
589  static void parse
590  (const std::string& filename,
591  std::string& drive,
592  Array<std::string>& path,
593  std::string& base,
594  std::string& ext);
595 
599  static bool matches(const std::string& path, const std::string& pattern, bool caseSensitive = true);
600 
602  static std::string makeLegalFilename(const std::string& f, size_t maxLength = 100000);
603 };
604 
605 } // namespace G3D
606 #endif
607 
void _removeFile(const std::string &path)
Delete this file. No effect if path does not exist.
Definition: FileSystem.cpp:492
bool _isFile(const std::string &path)
Definition: FileSystem.h:188
std::string name
Definition: FileSystem.h:96
float _cacheLifetime() const
Definition: FileSystem.h:168
bool _isDirectory(const std::string &path)
Definition: FileSystem.cpp:483
void _setCacheLifetime(float t)
Definition: FileSystem.cpp:364
bool includeParentPath
Definition: FileSystem.h:64
Entry()
Definition: FileSystem.h:98
Definition: FileSystem.h:46
bool _isNewer(const std::string &src, const std::string &dst)
Definition: FileSystem.cpp:565
static std::string expandEnvironmentVariables(const std::string &path)
Replaces $VAR and patterns with the corresponding environment variable. Throws std::string if the en...
Definition: FileSystem.cpp:949
bool isZipfile
Definition: FileSystem.h:108
static bool isFile(const std::string &path)
Definition: FileSystem.h:425
static bool isNewer(const std::string &src, const std::string &dst)
Definition: FileSystem.h:453
static void cleanup()
Definition: FileSystem.cpp:62
void lock()
Definition: GThread.cpp:248
Array< std::string > m_winDrive
Definition: FileSystem.h:82
static bool isZipfile(const std::string &path)
Definition: FileSystem.h:323
static std::string baseExt(const std::string &path)
Definition: FileSystem.cpp:765
float m_cacheLifetime
Definition: FileSystem.h:84
Mutual exclusion lock used for synchronization.
Definition: GMutex.h:76
static void parse(const std::string &filename, std::string &drive, Array< std::string > &path, std::string &base, std::string &ext)
Definition: FileSystem.cpp:838
Type type
Definition: FileSystem.h:97
static void init()
Definition: FileSystem.cpp:55
void computeZipListing(const std::string &zipfile, const std::string &pathInsideZipfile)
Definition: FileSystem.cpp:88
Definition: FileSystem.h:90
static void clearCache(const std::string &path="")
Definition: FileSystem.h:273
Array< Entry > nodeArray
Definition: FileSystem.h:112
static bool containsWildcards(const std::string &p)
Definition: FileSystem.cpp:813
Definition: FileSystem.h:102
Definition: AABox.h:25
static void fclose(FILE *f)
Definition: FileSystem.h:290
static std::string concat(const std::string &a, const std::string &b)
Definition: FileSystem.cpp:743
#define false
Definition: CascPort.h:18
bool recursive
Definition: FileSystem.h:61
bool exists
Definition: FileSystem.h:106
Entry(const char *n)
Definition: FileSystem.h:99
void _clearCache(const std::string &path)
Definition: FileSystem.cpp:337
ListSettings()
Definition: FileSystem.h:66
bool inZipfile
Definition: FileSystem.h:109
Table< std::string, Dir > m_cache
Definition: FileSystem.h:133
static const Set< std::string > & usedFiles()
Definition: FileSystem.cpp:560
static GMutex mutex
Definition: FileSystem.h:143
Dir()
Definition: FileSystem.h:128
#define true
Definition: CascPort.h:17
void unlock()
Definition: GThread.cpp:256
bool contains(const std::string &child, bool caseSensitive=true) const
Definition: FileSystem.cpp:73
static bool exists(const std::string &f, bool trustCache=true, bool caseSensitive=true)
Definition: FileSystem.h:401
bool directories
Definition: FileSystem.h:55
Definition: FileSystem.h:93
void _createDirectory(const std::string &path)
Definition: FileSystem.cpp:369
bool caseSensitive
Definition: FileSystem.h:58
static bool isRoot(const std::string &f)
Definition: FileSystem.cpp:698
static std::string parent(const std::string &path)
Definition: FileSystem.cpp:795
static std::string currentDirectory()
Definition: FileSystem.h:364
static void removeFile(const std::string &path)
Delete this file. No effect if path does not exist.
Definition: FileSystem.h:314
static float cacheLifetime()
Definition: FileSystem.h:341
Parsing of file system paths.
Definition: FileSystem.h:526
int64 _size(const std::string &path)
Definition: FileSystem.cpp:580
static std::string removeTrailingSlash(const std::string &f)
Definition: FileSystem.cpp:736
static std::string ext(const std::string &path)
Definition: FileSystem.cpp:755
static int64 size(const std::string &path)
Definition: FileSystem.h:462
Definition: Table.h:101
static bool matches(const std::string &path, const std::string &pattern, bool caseSensitive=true)
Definition: FileSystem.cpp:818
void setCacheLifetime(float t)
Definition: FileSystem.h:333
void _list(const std::string &spec, Array< std::string > &result, const ListSettings &listSettings=ListSettings())
Definition: FileSystem.cpp:665
static bool isDirectory(const std::string &path)
Definition: FileSystem.h:416
static void copyFile(const std::string &srcPath, const std::string &dstPath)
Definition: FileSystem.h:378
std::string _resolve(const std::string &path, const std::string &cwd=currentDirectory())
Definition: FileSystem.cpp:508
void _copyFile(const std::string &srcPath, const std::string &dstPath)
Definition: FileSystem.cpp:424
int64_t int64
Definition: g3dmath.h:169
static std::string canonicalize(std::string x)
Definition: FileSystem.cpp:832
double lastChecked
Definition: FileSystem.h:115
FileSystem()
Definition: FileSystem.cpp:69
bool _inZipfile(const std::string &path)
Definition: FileSystem.h:158
static std::string resolve(const std::string &path, const std::string &cwd=currentDirectory())
Definition: FileSystem.h:440
static std::string base(const std::string &path)
Definition: FileSystem.cpp:783
static bool inZipfile(const std::string &path)
Definition: FileSystem.h:300
bool _exists(const std::string &f, bool trustCache=true, bool caseSensitive=true)
Definition: FileSystem.cpp:441
static FILE * fopen(const char *filename, const char *mode)
Definition: FileSystem.h:282
Definition: FileSystem.h:49
Definition: FileSystem.h:89
bool _isZipfile(const std::string &path)
Definition: FileSystem.cpp:296
static FileSystem & instance()
Definition: FileSystem.cpp:49
static bool inZipfile(const std::string &path, std::string &zipfile)
Definition: FileSystem.h:264
Definition: FileSystem.h:88
void listHelper(const std::string &shortSpec, const std::string &parentPath, Array< std::string > &result, const ListSettings &settings)
Definition: FileSystem.cpp:617
Type
Definition: FileSystem.h:86
static void getFiles(const std::string &spec, Array< std::string > &result, bool includeParentPath=false)
Definition: FileSystem.h:488
static std::string makeLegalFilename(const std::string &f, size_t maxLength=100000)
Definition: FileSystem.cpp:1015
G3D::int16 x
Definition: Vector2int16.h:37
bool files
Definition: FileSystem.h:52
static void markFileUsed(const std::string &filename)
Definition: FileSystem.cpp:553
static void list(const std::string &spec, Array< std::string > &result, const ListSettings &listSettings=ListSettings())
Definition: FileSystem.h:479
static void getDirectories(const std::string &spec, Array< std::string > &result, bool includeParentPath=false)
Definition: FileSystem.h:496
void _getDirectories(const std::string &spec, Array< std::string > &result, bool includeParentPath=false)
Definition: FileSystem.h:226
Dir & getContents(const std::string &path, bool forceUpdate)
Definition: FileSystem.cpp:147
static void createDirectory(const std::string &path)
Definition: FileSystem.h:356
std::string _currentDirectory()
Definition: FileSystem.cpp:542
FILE * _fopen(const char *filename, const char *mode)
Definition: FileSystem.cpp:321
void _getFiles(const std::string &spec, Array< std::string > &result, bool includeParentPath=false)
Definition: FileSystem.h:217
bool _inZipfile(const std::string &path, std::string &zipfile)
Definition: FileSystem.cpp:261