dir_access.h
1 /*************************************************************************/
2 /* dir_access.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef DIR_ACCESS_H
30 #define DIR_ACCESS_H
31 
32 
33 #include "typedefs.h"
34 #include "ustring.h"
35 
40 //@ TOOD, excellent candidate for THREAD_SAFE MACRO, should go through all these and add THREAD_SAFE where it applies
41 class DirAccess {
42 public:
43 
44  enum AccessType {
45  ACCESS_RESOURCES,
46  ACCESS_USERDATA,
47  ACCESS_FILESYSTEM,
48  ACCESS_MAX
49  };
50 
51 
52 
53  typedef DirAccess*(*CreateFunc)();
54 
55 private:
56 
57 
58  AccessType _access_type;
59  static CreateFunc create_func[ACCESS_MAX];
60 protected:
61  String _get_root_path() const;
62  String _get_root_string() const;
63 
64  String fix_path(String p_path) const;
65  bool next_is_dir;
66 
67  template<class T>
68  static DirAccess* _create_builtin() {
69 
70  return memnew( T );
71  }
72 
73 public:
74 
75  static String normalize_path(const String& p_path);
76 
77  virtual bool list_dir_begin()=0;
78  virtual String get_next(bool* p_is_dir); // compatibility
79  virtual String get_next()=0;
80  virtual bool current_is_dir() const=0;
81  virtual bool current_is_hidden() const=0;
82 
83  virtual void list_dir_end()=0;
84 
85  virtual int get_drive_count()=0;
86  virtual String get_drive(int p_drive)=0;
87  virtual int get_current_drive();
88 
89  virtual Error change_dir(String p_dir)=0;
90  virtual String get_current_dir()=0;
91  virtual Error make_dir(String p_dir)=0;
92  virtual Error make_dir_recursive(String p_dir);
93  virtual Error erase_contents_recursive(); //super dangerous, use with care!
94 
95  virtual bool file_exists(String p_file)=0;
96  virtual bool dir_exists(String p_dir)=0;
97  static bool exists(String p_dir);
98  virtual size_t get_space_left()=0;
99 
100  virtual Error copy(String p_from,String p_to);
101  virtual Error rename(String p_from, String p_to)=0;
102  virtual Error remove(String p_name)=0;
103 
104  static String get_full_path(const String& p_path,AccessType p_access);
105  static DirAccess *create_for_path(const String& p_path);
106 
107 /*
108  enum DirType {
109 
110  FILE_TYPE_INVALID,
111  FILE_TYPE_FILE,
112  FILE_TYPE_DIR,
113  };
114 
115  //virtual DirType get_file_type() const=0;
116 */
117  static DirAccess *create(AccessType p_access);
118 
119  template<class T>
120  static void make_default(AccessType p_access) {
121 
122  create_func[p_access]=_create_builtin<T>;
123  }
124 
125  static DirAccess *open(const String& p_path,Error *r_error=NULL);
126 
127  DirAccess();
128  virtual ~DirAccess();
129 
130 };
131 
132 struct DirAccessRef {
133 
134  _FORCE_INLINE_ DirAccess* operator->() {
135 
136  return f;
137  }
138 
139  operator bool() const { return f!=NULL; }
140  DirAccess *f;
141  DirAccessRef(DirAccess *fa) { f = fa; }
142  ~DirAccessRef() { if (f) memdelete(f); }
143 };
144 
145 #endif
virtual String get_current_dir()=0
return current dir location
virtual Error change_dir(String p_dir)=0
can be relative or absolute, return false on success
virtual bool list_dir_begin()=0
This starts dir listing.
Definition: ustring.h:64
Definition: dir_access.h:132
Definition: dir_access.h:41