file_access_buffered.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/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. }
  48. return cache.buffer.size() - (file.offset - cache.offset);
  49. }
  50. void FileAccessBuffered::seek(size_t p_position) {
  51. file.offset = p_position;
  52. }
  53. void FileAccessBuffered::seek_end(int64_t p_position) {
  54. file.offset = file.size + p_position;
  55. }
  56. size_t FileAccessBuffered::get_position() const {
  57. return file.offset;
  58. }
  59. size_t FileAccessBuffered::get_len() const {
  60. return file.size;
  61. }
  62. bool FileAccessBuffered::eof_reached() const {
  63. return file.offset > file.size;
  64. }
  65. uint8_t FileAccessBuffered::get_8() const {
  66. ERR_FAIL_COND_V_MSG(!file.open, 0, "Can't get data, when file is not opened.");
  67. uint8_t byte = 0;
  68. if (cache_data_left() >= 1) {
  69. byte = cache.buffer[file.offset - cache.offset];
  70. }
  71. ++file.offset;
  72. return byte;
  73. }
  74. int FileAccessBuffered::get_buffer(uint8_t *p_dest, int p_length) const {
  75. ERR_FAIL_COND_V_MSG(!file.open, -1, "Can't get buffer, when file is not opened.");
  76. if (p_length > cache_size) {
  77. int total_read = 0;
  78. if (!(cache.offset == -1 || file.offset < cache.offset || file.offset >= cache.offset + cache.buffer.size())) {
  79. int size = (cache.buffer.size() - (file.offset - cache.offset));
  80. size = size - (size % 4);
  81. //const uint8_t* read = cache.buffer.ptr();
  82. //memcpy(p_dest, read.ptr() + (file.offset - cache.offset), size);
  83. memcpy(p_dest, cache.buffer.ptr() + (file.offset - cache.offset), size);
  84. p_dest += size;
  85. p_length -= size;
  86. file.offset += size;
  87. total_read += size;
  88. }
  89. int err = read_data_block(file.offset, p_length, p_dest);
  90. if (err >= 0) {
  91. total_read += err;
  92. file.offset += err;
  93. }
  94. return total_read;
  95. }
  96. int to_read = p_length;
  97. int total_read = 0;
  98. while (to_read > 0) {
  99. int left = cache_data_left();
  100. if (left == 0) {
  101. file.offset += to_read;
  102. return total_read;
  103. }
  104. if (left < 0) {
  105. return left;
  106. }
  107. int r = MIN(left, to_read);
  108. //const uint8_t* read = cache.buffer.ptr();
  109. //memcpy(p_dest+total_read, &read.ptr()[file.offset - cache.offset], r);
  110. memcpy(p_dest + total_read, cache.buffer.ptr() + (file.offset - cache.offset), r);
  111. file.offset += r;
  112. total_read += r;
  113. to_read -= r;
  114. }
  115. return p_length;
  116. }
  117. bool FileAccessBuffered::is_open() const {
  118. return file.open;
  119. }
  120. Error FileAccessBuffered::get_error() const {
  121. return last_error;
  122. }