file_access.h
1 /*************************************************************************/
2 /* file_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 FILE_ACCESS_H
30 #define FILE_ACCESS_H
31 
32 #include "typedefs.h"
33 #include "ustring.h"
34 #include "os/memory.h"
35 #include "math_defs.h"
40 class FileAccess {
41 
42 public:
43  enum AccessType {
44  ACCESS_RESOURCES,
45  ACCESS_USERDATA,
46  ACCESS_FILESYSTEM,
47  ACCESS_MAX
48  };
49 
50  typedef FileAccess*(*CreateFunc)();
51  bool endian_swap;
52  bool real_is_double;
53 protected:
54 
55  String fix_path(const String& p_path) const;
56  virtual Error _open(const String& p_path, int p_mode_flags)=0;
57  virtual uint64_t _get_modified_time(const String& p_file)=0;
58 
59 
60 private:
61 
62  static bool backup_save;
63 
64  AccessType _access_type;
65  static CreateFunc create_func[ACCESS_MAX];
66  template<class T>
67  static FileAccess* _create_builtin() {
68 
69  return memnew( T );
70  }
71 
72 public:
73 
74  virtual void _set_access_type(AccessType p_access);
75 
76  enum ModeFlags {
77 
78  READ=1,
79  WRITE=2,
80  READ_WRITE=3,
81  WRITE_READ=7,
82  };
83 
84  virtual void close()=0;
85  virtual bool is_open() const=0;
86 
87  virtual void seek(size_t p_position)=0;
88  virtual void seek_end(int64_t p_position=0)=0;
89  virtual size_t get_pos() const=0;
90  virtual size_t get_len() const=0;
91 
92  virtual bool eof_reached() const=0;
93 
94  virtual uint8_t get_8() const=0;
95  virtual uint16_t get_16() const;
96  virtual uint32_t get_32() const;
97  virtual uint64_t get_64() const;
98 
99  virtual float get_float() const;
100  virtual double get_double() const;
101  virtual real_t get_real() const;
102 
103  virtual int get_buffer(uint8_t *p_dst,int p_length) const;
104  virtual String get_line() const;
105  virtual Vector<String> get_csv_line(String delim=",") const;
106 
112  virtual void set_endian_swap(bool p_swap) { endian_swap=p_swap; }
113  inline bool get_endian_swap() const { return endian_swap; }
114 
115  virtual Error get_error() const=0;
116 
117  virtual void store_8(uint8_t p_dest)=0;
118  virtual void store_16(uint16_t p_dest);
119  virtual void store_32(uint32_t p_dest);
120  virtual void store_64(uint64_t p_dest);
121 
122  virtual void store_float(float p_dest);
123  virtual void store_double(double p_dest);
124  virtual void store_real(real_t p_real);
125 
126  virtual void store_string(const String& p_string);
127  virtual void store_line(const String& p_string);
128 
129  virtual void store_pascal_string(const String& p_string);
130  virtual String get_pascal_string();
131 
132  virtual void store_buffer(const uint8_t *p_src,int p_length);
133 
134  virtual bool file_exists(const String& p_name)=0;
135 
136  virtual Error reopen(const String& p_path, int p_mode_flags);
137 
138  static FileAccess *create(AccessType p_access);
139  static FileAccess *create_for_path(const String& p_path);
140  static FileAccess *open(const String& p_path, int p_mode_flags, Error *r_error=NULL);
141  static CreateFunc get_create_func(AccessType p_access);
142  static bool exists(const String& p_name);
143  static uint64_t get_modified_time(const String& p_file);
144 
145 
146  static void set_backup_save(bool p_enable) { backup_save=p_enable; };
147  static bool is_backup_save_enabled() { return backup_save; };
148 
149  static String get_md5(const String& p_file);
150 
151  static Vector<uint8_t> get_file_as_array(const String& p_path);
152 
153 
154  template<class T>
155  static void make_default(AccessType p_access) {
156 
157  create_func[p_access]=_create_builtin<T>;
158  }
159 
160  FileAccess();
161  virtual ~FileAccess(){}
162 
163 };
164 
165 
167 
168  _FORCE_INLINE_ FileAccess* operator->() {
169 
170  return f;
171  }
172 
173  operator bool() const { return f!=NULL; }
174  FileAccess *f;
175  FileAccessRef(FileAccess *fa) { f = fa; }
176  ~FileAccessRef() { if (f) memdelete(f); }
177 };
178 
179 #endif
static bool exists(const String &p_name)
return true if a file exists
Definition: file_access.cpp:51
virtual uint8_t get_8() const =0
get a byte
Definition: file_access.h:166
virtual bool eof_reached() const =0
reading passed EOF
virtual size_t get_len() const =0
get size of the file
virtual uint64_t get_64() const
get 64 bits uint
Definition: file_access.cpp:216
static FileAccess * create_for_path(const String &p_path)
Create a file access (for the current platform) this is the only portable way of accessing files...
Definition: file_access.cpp:70
virtual size_t get_pos() const =0
get position in the file
virtual void store_buffer(const uint8_t *p_src, int p_length)
store an array of bytes
Definition: file_access.cpp:465
virtual uint32_t get_32() const
get 32 bits uint
Definition: file_access.cpp:197
virtual uint16_t get_16() const
get 16 bits uint
Definition: file_access.cpp:178
static CreateFunc get_create_func(AccessType p_access)
Create a file access (for the current platform) this is the only portable way of accessing files...
Definition: file_access.cpp:125
virtual Error reopen(const String &p_path, int p_mode_flags)
does not change the AccessType
Definition: file_access.cpp:90
virtual bool is_open() const =0
true when file is open
virtual void store_16(uint16_t p_dest)
store 16 bits uint
Definition: file_access.cpp:344
virtual void seek(size_t p_position)=0
seek to a given position
virtual bool file_exists(const String &p_name)=0
return true if a file exists
virtual void store_8(uint8_t p_dest)=0
store a byte
virtual void store_32(uint32_t p_dest)
store 32 bits uint
Definition: file_access.cpp:360
virtual void seek_end(int64_t p_position=0)=0
seek from the end of file
virtual Error _open(const String &p_path, int p_mode_flags)=0
open a file
Definition: ustring.h:64
virtual void store_64(uint64_t p_dest)
store 64 bits uint
Definition: file_access.cpp:377
Definition: file_access.h:40
virtual int get_buffer(uint8_t *p_dst, int p_length) const
get an array of bytes
Definition: file_access.cpp:335
virtual void close()=0
close a file
virtual Vector< String > get_csv_line(String delim=",") const
Definition: file_access.cpp:280
virtual Error get_error() const =0
get last error