file_access.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*************************************************************************/
  2. /* file_access.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_H
  30. #define FILE_ACCESS_H
  31. #include "typedefs.h"
  32. #include "ustring.h"
  33. #include "os/memory.h"
  34. #include "math_defs.h"
  35. /**
  36. * Multi-Platform abstraction for accessing to files.
  37. */
  38. class FileAccess {
  39. public:
  40. enum AccessType {
  41. ACCESS_RESOURCES,
  42. ACCESS_USERDATA,
  43. ACCESS_FILESYSTEM,
  44. ACCESS_MAX
  45. };
  46. typedef FileAccess*(*CreateFunc)();
  47. bool endian_swap;
  48. bool real_is_double;
  49. protected:
  50. String fix_path(const String& p_path) const;
  51. virtual Error _open(const String& p_path, int p_mode_flags)=0; ///< open a file
  52. virtual uint64_t _get_modified_time(const String& p_file)=0;
  53. private:
  54. static bool backup_save;
  55. AccessType _access_type;
  56. static CreateFunc create_func[ACCESS_MAX]; /** default file access creation function for a platform */
  57. template<class T>
  58. static FileAccess* _create_builtin() {
  59. return memnew( T );
  60. }
  61. public:
  62. virtual void _set_access_type(AccessType p_access);
  63. enum ModeFlags {
  64. READ=1,
  65. WRITE=2,
  66. READ_WRITE=3,
  67. };
  68. virtual void close()=0; ///< close a file
  69. virtual bool is_open() const=0; ///< true when file is open
  70. virtual void seek(size_t p_position)=0; ///< seek to a given position
  71. virtual void seek_end(int64_t p_position=0)=0; ///< seek from the end of file
  72. virtual size_t get_pos() const=0; ///< get position in the file
  73. virtual size_t get_len() const=0; ///< get size of the file
  74. virtual bool eof_reached() const=0; ///< reading passed EOF
  75. virtual uint8_t get_8() const=0; ///< get a byte
  76. virtual uint16_t get_16() const; ///< get 16 bits uint
  77. virtual uint32_t get_32() const; ///< get 32 bits uint
  78. virtual uint64_t get_64() const; ///< get 64 bits uint
  79. virtual float get_float() const;
  80. virtual double get_double() const;
  81. virtual real_t get_real() const;
  82. virtual int get_buffer(uint8_t *p_dst,int p_length) const; ///< get an array of bytes
  83. virtual String get_line() const;
  84. virtual Vector<String> get_csv_line() const;
  85. /**< use this for files WRITTEN in _big_ endian machines (ie, amiga/mac)
  86. * It's not about the current CPU type but file formats.
  87. * this flags get reset to false (little endian) on each open
  88. */
  89. virtual void set_endian_swap(bool p_swap) { endian_swap=p_swap; }
  90. inline bool get_endian_swap() const { return endian_swap; }
  91. virtual Error get_error() const=0; ///< get last error
  92. virtual void store_8(uint8_t p_dest)=0; ///< store a byte
  93. virtual void store_16(uint16_t p_dest); ///< store 16 bits uint
  94. virtual void store_32(uint32_t p_dest); ///< store 32 bits uint
  95. virtual void store_64(uint64_t p_dest); ///< store 64 bits uint
  96. virtual void store_float(float p_dest);
  97. virtual void store_double(double p_dest);
  98. virtual void store_real(real_t p_real);
  99. virtual void store_string(const String& p_string);
  100. virtual void store_line(const String& p_string);
  101. virtual void store_pascal_string(const String& p_string);
  102. virtual String get_pascal_string();
  103. virtual void store_buffer(const uint8_t *p_src,int p_length); ///< store an array of bytes
  104. virtual bool file_exists(const String& p_name)=0; ///< return true if a file exists
  105. virtual Error reopen(const String& p_path, int p_mode_flags); ///< does not change the AccessType
  106. static FileAccess *create(AccessType p_access); /// Create a file access (for the current platform) this is the only portable way of accessing files.
  107. static FileAccess *create_for_path(const String& p_path);
  108. static FileAccess *open(const String& p_path, int p_mode_flags, Error *r_error=NULL); /// Create a file access (for the current platform) this is the only portable way of accessing files.
  109. static CreateFunc get_create_func(AccessType p_access);
  110. static bool exists(const String& p_name); ///< return true if a file exists
  111. static uint64_t get_modified_time(const String& p_file);
  112. static void set_backup_save(bool p_enable) { backup_save=p_enable; };
  113. static bool is_backup_save_enabled() { return backup_save; };
  114. static String get_md5(const String& p_file);
  115. static Vector<uint8_t> get_file_as_array(const String& p_path);
  116. template<class T>
  117. static void make_default(AccessType p_access) {
  118. create_func[p_access]=_create_builtin<T>;
  119. }
  120. FileAccess();
  121. virtual ~FileAccess(){}
  122. };
  123. struct FileAccessRef {
  124. _FORCE_INLINE_ FileAccess* operator->() {
  125. return f;
  126. }
  127. operator bool() const { return f!=NULL; }
  128. FileAccess *f;
  129. FileAccessRef(FileAccess *fa) { f = fa; }
  130. ~FileAccessRef() { if (f) memdelete(f); }
  131. };
  132. #endif