summaryrefslogtreecommitdiffstats
path: root/core/io/file_access_buffered_fa.h
blob: bb3d3f33f621f880a78e4077d16e1b0749044570 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*************************************************************************/
/*  file_access_buffered_fa.h                                            */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md)    */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/
#ifndef FILE_ACCESS_BUFFERED_FA_H
#define FILE_ACCESS_BUFFERED_FA_H

#include "core/io/file_access_buffered.h"

template <class T>
class FileAccessBufferedFA : public FileAccessBuffered {

	T f;

	int read_data_block(int p_offset, int p_size, uint8_t *p_dest = 0) const {

		ERR_FAIL_COND_V(!f.is_open(), -1);

		((T *)&f)->seek(p_offset);

		if (p_dest) {

			f.get_buffer(p_dest, p_size);
			return p_size;

		} else {

			cache.offset = p_offset;
			cache.buffer.resize(p_size);

			// on dvector
			//PoolVector<uint8_t>::Write write = cache.buffer.write();
			//f.get_buffer(write.ptrw(), p_size);

			// on vector
			f.get_buffer(cache.buffer.ptrw(), p_size);

			return p_size;
		};
	};

	static FileAccess *create() {

		return memnew(FileAccessBufferedFA<T>());
	};

protected:
	virtual void _set_access_type(AccessType p_access) {
		f._set_access_type(p_access);
		FileAccessBuffered::_set_access_type(p_access);
	};

public:
	void flush() {

		f.flush();
	};

	void store_8(uint8_t p_dest) {

		f.store_8(p_dest);
	};

	void store_buffer(const uint8_t *p_src, int p_length) {

		f.store_buffer(p_src, p_length);
	};

	bool file_exists(const String &p_name) {

		return f.file_exists(p_name);
	};

	Error _open(const String &p_path, int p_mode_flags) {

		close();

		Error ret = f._open(p_path, p_mode_flags);
		if (ret != OK)
			return ret;
		//ERR_FAIL_COND_V( ret != OK, ret );

		file.size = f.get_len();
		file.offset = 0;
		file.open = true;
		file.name = p_path;
		file.access_flags = p_mode_flags;

		cache.buffer.resize(0);
		cache.offset = 0;

		return set_error(OK);
	};

	void close() {

		f.close();

		file.offset = 0;
		file.size = 0;
		file.open = false;
		file.name = "";

		cache.buffer.resize(0);
		cache.offset = 0;
		set_error(OK);
	};

	/*
	static void make_default() {
		FileAccess::create_func = FileAccessBufferedFA<T>::create;
	};
	*/

	virtual uint64_t _get_modified_time(const String &p_file) {

		return f._get_modified_time(p_file);
	}

	FileAccessBufferedFA(){

	};
};

#endif // FILE_ACCESS_BUFFERED_FA_H