Debian Codegraph @

core/io/file_access_memory.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* file_access_memory.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "file_access_memory.h"
32
33 #include "core/config/project_settings.h"
34
35 static HashMap<String, Vector<uint8_t>> *files = nullptr;
36
37 void FileAccessMemory::register_file(const String &p_name, const Vector<uint8_t> &p_data) {
38 if (!files) {
39 files = memnew((HashMap<String, Vector<uint8_t>>));
40 }
41
42 String name;
43 if (ProjectSettings::get_singleton()) {
44 name = ProjectSettings::get_singleton()->globalize_path(p_name);
45 } else {
46 name = p_name;
47 }
48 //name = DirAccess::normalize_path(name);
49
50 (*files)[name] = p_data;
51 }
52
53 void FileAccessMemory::cleanup() {
54 if (!files) {
55 return;
56 }
57
58 memdelete(files);
59 }
60
61 Ref<FileAccess> FileAccessMemory::create() {
62 return memnew(FileAccessMemory);
63 }
64
65 bool FileAccessMemory::file_exists(const String &p_name) {
66 String name = fix_path(p_name);
67 //name = DirAccess::normalize_path(name);
68
69 return files && (files->find(name) != nullptr);
70 }
71
72 Error FileAccessMemory::open_custom(const uint8_t *p_data, uint64_t p_len) {
73 data = (uint8_t *)p_data;
74 length = p_len;
75 pos = 0;
76 return OK;
77 }
78
79 Error FileAccessMemory::open_internal(const String &p_path, int p_mode_flags) {
80 ERR_FAIL_NULL_V(files, ERR_FILE_NOT_FOUND);
81
82 String name = fix_path(p_path);
83 //name = DirAccess::normalize_path(name);
84
85 HashMap<String, Vector<uint8_t>>::Iterator E = files->find(name);
86 ERR_FAIL_COND_V_MSG(!E, ERR_FILE_NOT_FOUND, vformat("Can't find file '%s'.", p_path));
87
88 data = E->value.ptrw();
89 length = E->value.size();
90 pos = 0;
91
92 return OK;
93 }
94
95 bool FileAccessMemory::is_open() const {
96 return data != nullptr;
97 }
98
99 void FileAccessMemory::seek(uint64_t p_position) {
100 ERR_FAIL_NULL(data);
101 ERR_FAIL_COND(p_position > length);
102 pos = p_position;
103 }
104
105 void FileAccessMemory::seek_end(int64_t p_position) {
106 ERR_FAIL_NULL(data);
107 ERR_FAIL_COND((int64_t)length + p_position < 0);
108 seek(length + p_position);
109 }
110
111 uint64_t FileAccessMemory::get_position() const {
112 ERR_FAIL_NULL_V(data, 0);
113 return pos;
114 }
115
116 uint64_t FileAccessMemory::get_length() const {
117 ERR_FAIL_NULL_V(data, 0);
118 return length;
119 }
120
121 bool FileAccessMemory::eof_reached() const {
122 return pos >= length;
123 }
124
125 uint64_t FileAccessMemory::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
126 if (!p_length) {
127 return 0;
128 }
129
130 ERR_FAIL_NULL_V(p_dst, -1);
131 ERR_FAIL_NULL_V(data, -1);
132
133 uint64_t left = length - pos;
134 uint64_t read = MIN(p_length, left);
135
136 if (read < p_length) {
137 WARN_PRINT("Reading less data than requested");
138 }
139
140 memcpy(p_dst, &data[pos], read);
141 pos += read;
142
143 return read;
144 }
145
146 Error FileAccessMemory::get_error() const {
147 return pos >= length ? ERR_FILE_EOF : OK;
148 }
149
150 void FileAccessMemory::flush() {
151 ERR_FAIL_NULL(data);
152 }
153
154 bool FileAccessMemory::store_buffer(const uint8_t *p_src, uint64_t p_length) {
155 if (!p_length) {
156 return true;
157 }
158
159 ERR_FAIL_NULL_V(p_src, false);
160
161 uint64_t left = length - pos;
162 uint64_t write = MIN(p_length, left);
163
164 memcpy(&data[pos], p_src, write);
165 pos += write;
166
167 ERR_FAIL_COND_V_MSG(write < p_length, false, "Writing less data than requested.");
168
169 return true;
170 }