file_access_buffered_fa.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*************************************************************************/
  2. /* file_access_buffered_fa.h */
  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. #ifndef FILE_ACCESS_BUFFERED_FA_H
  30. #define FILE_ACCESS_BUFFERED_FA_H
  31. #include "core/io/file_access_buffered.h"
  32. template<class T>
  33. class FileAccessBufferedFA : public FileAccessBuffered {
  34. T f;
  35. int read_data_block(int p_offset, int p_size, uint8_t *p_dest = 0) const {
  36. ERR_FAIL_COND_V( !f.is_open(), -1 );
  37. ((T*)&f)->seek(p_offset);
  38. if (p_dest) {
  39. f.get_buffer(p_dest, p_size);
  40. return p_size;
  41. } else {
  42. cache.offset = p_offset;
  43. cache.buffer.resize(p_size);
  44. // on dvector
  45. //DVector<uint8_t>::Write write = cache.buffer.write();
  46. //f.get_buffer(write.ptr(), p_size);
  47. // on vector
  48. f.get_buffer(cache.buffer.ptr(), p_size);
  49. return p_size;
  50. };
  51. };
  52. static FileAccess* create() {
  53. return memnew( FileAccessBufferedFA<T>() );
  54. };
  55. protected:
  56. virtual void _set_access_type(AccessType p_access) {
  57. f._set_access_type(p_access);
  58. FileAccessBuffered::_set_access_type(p_access);
  59. };
  60. public:
  61. void store_8(uint8_t p_dest) {
  62. f.store_8(p_dest);
  63. };
  64. void store_buffer(const uint8_t *p_src,int p_length) {
  65. f.store_buffer(p_src, p_length);
  66. };
  67. bool file_exists(const String& p_name) {
  68. return f.file_exists(p_name);
  69. };
  70. Error _open(const String& p_path, int p_mode_flags) {
  71. close();
  72. Error ret = f._open(p_path, p_mode_flags);
  73. if (ret !=OK)
  74. return ret;
  75. //ERR_FAIL_COND_V( ret != OK, ret );
  76. file.size = f.get_len();
  77. file.offset = 0;
  78. file.open = true;
  79. file.name = p_path;
  80. file.access_flags = p_mode_flags;
  81. cache.buffer.resize(0);
  82. cache.offset = 0;
  83. return set_error(OK);
  84. };
  85. void close() {
  86. f.close();
  87. file.offset = 0;
  88. file.size = 0;
  89. file.open = false;
  90. file.name = "";
  91. cache.buffer.resize(0);
  92. cache.offset = 0;
  93. set_error(OK);
  94. };
  95. // static void make_default() {
  96. //FileAccess::create_func = FileAccessBufferedFA<T>::create;
  97. // };
  98. virtual uint64_t _get_modified_time(const String& p_file) {
  99. return f._get_modified_time(p_file);
  100. }
  101. FileAccessBufferedFA() {
  102. };
  103. };
  104. #endif // FILE_ACCESS_BUFFERED_FA_H