file_access_buffered_fa.h
1 /*************************************************************************/
2 /* file_access_buffered_fa.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_BUFFERED_FA_H
30 #define FILE_ACCESS_BUFFERED_FA_H
31 
32 #include "core/io/file_access_buffered.h"
33 
34 template<class T>
36 
37  T f;
38 
39  int read_data_block(int p_offset, int p_size, uint8_t *p_dest = 0) const {
40 
41  ERR_FAIL_COND_V( !f.is_open(), -1 );
42 
43  ((T*)&f)->seek(p_offset);
44 
45  if (p_dest) {
46 
47  f.get_buffer(p_dest, p_size);
48  return p_size;
49 
50  } else {
51 
52  cache.offset = p_offset;
53  cache.buffer.resize(p_size);
54 
55  // on dvector
56  //DVector<uint8_t>::Write write = cache.buffer.write();
57  //f.get_buffer(write.ptr(), p_size);
58 
59  // on vector
60  f.get_buffer(cache.buffer.ptr(), p_size);
61 
62  return p_size;
63  };
64  };
65 
66  static FileAccess* create() {
67 
68  return memnew( FileAccessBufferedFA<T>() );
69  };
70 
71 protected:
72  virtual void _set_access_type(AccessType p_access) {
73  f._set_access_type(p_access);
74  FileAccessBuffered::_set_access_type(p_access);
75  };
76 
77 public:
78 
79 
80  void store_8(uint8_t p_dest) {
81 
82  f.store_8(p_dest);
83  };
84 
85  void store_buffer(const uint8_t *p_src,int p_length) {
86 
87  f.store_buffer(p_src, p_length);
88  };
89 
90  bool file_exists(const String& p_name) {
91 
92  return f.file_exists(p_name);
93  };
94 
95  Error _open(const String& p_path, int p_mode_flags) {
96 
97  close();
98 
99  Error ret = f._open(p_path, p_mode_flags);
100  if (ret !=OK)
101  return ret;
102  //ERR_FAIL_COND_V( ret != OK, ret );
103 
104  file.size = f.get_len();
105  file.offset = 0;
106  file.open = true;
107  file.name = p_path;
108  file.access_flags = p_mode_flags;
109 
110  cache.buffer.resize(0);
111  cache.offset = 0;
112 
113  return set_error(OK);
114  };
115 
116  void close() {
117 
118  f.close();
119 
120  file.offset = 0;
121  file.size = 0;
122  file.open = false;
123  file.name = "";
124 
125  cache.buffer.resize(0);
126  cache.offset = 0;
127  set_error(OK);
128  };
129 
130 // static void make_default() {
131 
132  //FileAccess::create_func = FileAccessBufferedFA<T>::create;
133 // };
134 
135  virtual uint64_t _get_modified_time(const String& p_file) {
136 
137  return f._get_modified_time(p_file);
138  }
139 
141 
142 
143  };
144 };
145 
146 
147 #endif // FILE_ACCESS_BUFFERED_FA_H
Error _open(const String &p_path, int p_mode_flags)
open a file
Definition: file_access_buffered_fa.h:95
void close()
close a file
Definition: file_access_buffered_fa.h:116
Definition: file_access_buffered_fa.h:35
Definition: file_access_buffered.h:37
void store_8(uint8_t p_dest)
store a byte
Definition: file_access_buffered_fa.h:80
bool file_exists(const String &p_name)
return true if a file exists
Definition: file_access_buffered_fa.h:90
Definition: ustring.h:64
Definition: file_access.h:40
void store_buffer(const uint8_t *p_src, int p_length)
store an array of bytes
Definition: file_access_buffered_fa.h:85