file_access_buffered.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*************************************************************************/
  2. /* file_access_buffered.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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. #include "file_access_buffered.h"
  31. #include "core/error_macros.h"
  32. Error FileAccessBuffered::set_error(Error p_error) const {
  33. return (last_error = p_error);
  34. };
  35. void FileAccessBuffered::set_cache_size(int p_size) {
  36. cache_size = p_size;
  37. };
  38. int FileAccessBuffered::get_cache_size() {
  39. return cache_size;
  40. };
  41. int FileAccessBuffered::cache_data_left() const {
  42. if (file.offset >= file.size) {
  43. return 0;
  44. };
  45. if (cache.offset == -1 || file.offset < cache.offset || file.offset >= cache.offset + cache.buffer.size()) {
  46. return read_data_block(file.offset, cache_size);
  47. } else {
  48. return cache.buffer.size() - (file.offset - cache.offset);
  49. };
  50. return 0;
  51. };
  52. void FileAccessBuffered::seek(size_t p_position) {
  53. file.offset = p_position;
  54. };
  55. void FileAccessBuffered::seek_end(int64_t p_position) {
  56. file.offset = file.size + p_position;
  57. };
  58. size_t FileAccessBuffered::get_position() const {
  59. return file.offset;
  60. };
  61. size_t FileAccessBuffered::get_len() const {
  62. return file.size;
  63. };
  64. bool FileAccessBuffered::eof_reached() const {
  65. return file.offset > file.size;
  66. };
  67. uint8_t FileAccessBuffered::get_8() const {
  68. ERR_FAIL_COND_V(!file.open, 0);
  69. uint8_t byte = 0;
  70. if (cache_data_left() >= 1) {
  71. byte = cache.buffer[file.offset - cache.offset];
  72. };
  73. ++file.offset;
  74. return byte;
  75. };
  76. int FileAccessBuffered::get_buffer(uint8_t *p_dest, int p_length) const {
  77. ERR_FAIL_COND_V(!file.open, -1);
  78. if (p_length > cache_size) {
  79. int total_read = 0;
  80. if (!(cache.offset == -1 || file.offset < cache.offset || file.offset >= cache.offset + cache.buffer.size())) {
  81. int size = (cache.buffer.size() - (file.offset - cache.offset));
  82. size = size - (size % 4);
  83. //PoolVector<uint8_t>::Read read = cache.buffer.read();
  84. //memcpy(p_dest, read.ptr() + (file.offset - cache.offset), size);
  85. memcpy(p_dest, cache.buffer.ptr() + (file.offset - cache.offset), size);
  86. p_dest += size;
  87. p_length -= size;
  88. file.offset += size;
  89. total_read += size;
  90. };
  91. int err = read_data_block(file.offset, p_length, p_dest);
  92. if (err >= 0) {
  93. total_read += err;
  94. file.offset += err;
  95. };
  96. return total_read;
  97. };
  98. int to_read = p_length;
  99. int total_read = 0;
  100. while (to_read > 0) {
  101. int left = cache_data_left();
  102. if (left == 0) {
  103. if (to_read > 0) {
  104. file.offset += to_read;
  105. };
  106. return total_read;
  107. };
  108. if (left < 0) {
  109. return left;
  110. };
  111. int r = MIN(left, to_read);
  112. //PoolVector<uint8_t>::Read read = cache.buffer.read();
  113. //memcpy(p_dest+total_read, &read.ptr()[file.offset - cache.offset], r);
  114. memcpy(p_dest + total_read, cache.buffer.ptr() + (file.offset - cache.offset), r);
  115. file.offset += r;
  116. total_read += r;
  117. to_read -= r;
  118. };
  119. return p_length;
  120. };
  121. bool FileAccessBuffered::is_open() const {
  122. return file.open;
  123. };
  124. Error FileAccessBuffered::get_error() const {
  125. return last_error;
  126. };
  127. FileAccessBuffered::FileAccessBuffered() {
  128. cache_size = DEFAULT_CACHE_SIZE;
  129. };
  130. FileAccessBuffered::~FileAccessBuffered() {
  131. }