file_access_memory.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*************************************************************************/
  2. /* file_access_memory.cpp */
  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. #include "file_access_memory.h"
  30. #include "os/dir_access.h"
  31. #include "os/copymem.h"
  32. #include "globals.h"
  33. #include "map.h"
  34. static Map<String, Vector<uint8_t> >* files = NULL;
  35. void FileAccessMemory::register_file(String p_name, Vector<uint8_t> p_data) {
  36. if (!files) {
  37. files = memnew((Map<String, Vector<uint8_t> >));
  38. }
  39. String name;
  40. if (Globals::get_singleton())
  41. name = Globals::get_singleton()->globalize_path(p_name);
  42. else
  43. name = p_name;
  44. name = DirAccess::normalize_path(name);
  45. (*files)[name] = p_data;
  46. }
  47. void FileAccessMemory::cleanup() {
  48. if (!files)
  49. return;
  50. memdelete(files);
  51. }
  52. FileAccess* FileAccessMemory::create() {
  53. return memnew(FileAccessMemory);
  54. }
  55. bool FileAccessMemory::file_exists(const String& p_name) {
  56. String name = fix_path(p_name);
  57. name = DirAccess::normalize_path(name);
  58. return files && (files->find(name) != NULL);
  59. }
  60. Error FileAccessMemory::open_custom(const uint8_t* p_data, int p_len) {
  61. data=(uint8_t*)p_data;
  62. length=p_len;
  63. pos=0;
  64. return OK;
  65. }
  66. Error FileAccessMemory::_open(const String& p_path, int p_mode_flags) {
  67. ERR_FAIL_COND_V(!files, ERR_FILE_NOT_FOUND);
  68. String name = fix_path(p_path);
  69. name = DirAccess::normalize_path(name);
  70. Map<String, Vector<uint8_t> >::Element* E = files->find(name);
  71. ERR_FAIL_COND_V(!E, ERR_FILE_NOT_FOUND);
  72. data = &(E->get()[0]);
  73. length = E->get().size();
  74. pos = 0;
  75. return OK;
  76. }
  77. void FileAccessMemory::close() {
  78. data = NULL;
  79. }
  80. bool FileAccessMemory::is_open() const {
  81. return data != NULL;
  82. }
  83. void FileAccessMemory::seek(size_t p_position) {
  84. ERR_FAIL_COND(!data);
  85. pos = p_position;
  86. }
  87. void FileAccessMemory::seek_end(int64_t p_position) {
  88. ERR_FAIL_COND(!data);
  89. pos = length + p_position;
  90. }
  91. size_t FileAccessMemory::get_pos() const {
  92. ERR_FAIL_COND_V(!data, 0);
  93. return pos;
  94. }
  95. size_t FileAccessMemory::get_len() const {
  96. ERR_FAIL_COND_V(!data, 0);
  97. return length;
  98. }
  99. bool FileAccessMemory::eof_reached() const {
  100. return pos > length;
  101. }
  102. uint8_t FileAccessMemory::get_8() const {
  103. uint8_t ret = 0;
  104. if (pos < length) {
  105. ret = data[pos];
  106. }
  107. ++pos;
  108. return ret;
  109. }
  110. int FileAccessMemory::get_buffer(uint8_t *p_dst,int p_length) const {
  111. ERR_FAIL_COND_V(!data, -1);
  112. int left = length - pos;
  113. int read = MIN(p_length, left);
  114. if (read < p_length) {
  115. WARN_PRINT("Reading less data than requested");
  116. };
  117. copymem(p_dst, &data[pos], read);
  118. pos += p_length;
  119. return read;
  120. }
  121. Error FileAccessMemory::get_error() const {
  122. return pos >= length ? ERR_FILE_EOF : OK;
  123. }
  124. void FileAccessMemory::store_8(uint8_t p_byte) {
  125. ERR_FAIL_COND(!data);
  126. ERR_FAIL_COND(pos >= length);
  127. data[pos++] = p_byte;
  128. }
  129. void FileAccessMemory::store_buffer(const uint8_t *p_src,int p_length) {
  130. int left = length - pos;
  131. int write = MIN(p_length, left);
  132. if (write < p_length) {
  133. WARN_PRINT("Writing less data than requested");
  134. }
  135. copymem(&data[pos], p_src, write);
  136. pos += p_length;
  137. }
  138. FileAccessMemory::FileAccessMemory() {
  139. data = NULL;
  140. }