file_access_memory.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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-2015 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(const String& p_path, int p_mode_flags) {
  61. ERR_FAIL_COND_V(!files, ERR_FILE_NOT_FOUND);
  62. String name = fix_path(p_path);
  63. name = DirAccess::normalize_path(name);
  64. Map<String, Vector<uint8_t> >::Element* E = files->find(name);
  65. ERR_FAIL_COND_V(!E, ERR_FILE_NOT_FOUND);
  66. data = &(E->get()[0]);
  67. length = E->get().size();
  68. pos = 0;
  69. return OK;
  70. }
  71. void FileAccessMemory::close() {
  72. data = NULL;
  73. }
  74. bool FileAccessMemory::is_open() const {
  75. return data != NULL;
  76. }
  77. void FileAccessMemory::seek(size_t p_position) {
  78. ERR_FAIL_COND(!data);
  79. pos = p_position;
  80. }
  81. void FileAccessMemory::seek_end(int64_t p_position) {
  82. ERR_FAIL_COND(!data);
  83. pos = length + p_position;
  84. }
  85. size_t FileAccessMemory::get_pos() const {
  86. ERR_FAIL_COND_V(!data, 0);
  87. return pos;
  88. }
  89. size_t FileAccessMemory::get_len() const {
  90. ERR_FAIL_COND_V(!data, 0);
  91. return length;
  92. }
  93. bool FileAccessMemory::eof_reached() const {
  94. return pos >= length;
  95. }
  96. uint8_t FileAccessMemory::get_8() const {
  97. uint8_t ret = 0;
  98. if (pos < length) {
  99. ret = data[pos];
  100. }
  101. ++pos;
  102. return ret;
  103. }
  104. int FileAccessMemory::get_buffer(uint8_t *p_dst,int p_length) const {
  105. ERR_FAIL_COND_V(!data, -1);
  106. int left = length - pos;
  107. int read = MIN(p_length, left);
  108. if (read < p_length) {
  109. WARN_PRINT("Reading less data than requested");
  110. };
  111. copymem(p_dst, &data[pos], read);
  112. pos += p_length;
  113. return read;
  114. }
  115. Error FileAccessMemory::get_error() const {
  116. return pos >= length ? ERR_FILE_EOF : OK;
  117. }
  118. void FileAccessMemory::store_8(uint8_t p_byte) {
  119. ERR_FAIL_COND(!data);
  120. ERR_FAIL_COND(pos >= length);
  121. data[pos++] = p_byte;
  122. }
  123. void FileAccessMemory::store_buffer(const uint8_t *p_src,int p_length) {
  124. int left = length - pos;
  125. int write = MIN(p_length, left);
  126. if (write < p_length) {
  127. WARN_PRINT("Writing less data than requested");
  128. }
  129. copymem(&data[pos], p_src, write);
  130. pos += p_length;
  131. }
  132. FileAccessMemory::FileAccessMemory() {
  133. data = NULL;
  134. }