File.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Container/ArrayPtr.h"
  24. #include "../Core/Object.h"
  25. #include "../IO/Deserializer.h"
  26. #include "../IO/Serializer.h"
  27. #ifdef ANDROID
  28. #include <SDL/include/SDL_rwops.h>
  29. #endif
  30. namespace Atomic
  31. {
  32. #ifdef ANDROID
  33. extern const char* APK;
  34. #define ASSET_DIR_INDICATOR "_"
  35. // Macro for checking if a given pathname is inside APK's assets directory
  36. #define IS_ASSET(p) p.StartsWith(APK)
  37. // Macro for truncating the APK prefix string from the asset pathname and at the same time patching the directory name components (see custom_rules.xml)
  38. #define ASSET(p) p.Substring(5).Replaced("/", ASSET_DIR_INDICATOR "/").CString()
  39. #endif
  40. /// File open mode.
  41. enum FileMode
  42. {
  43. FILE_READ = 0,
  44. FILE_WRITE,
  45. FILE_READWRITE
  46. };
  47. class PackageFile;
  48. /// %File opened either through the filesystem or from within a package file.
  49. class ATOMIC_API File : public Object, public Deserializer, public Serializer
  50. {
  51. OBJECT(File);
  52. public:
  53. /// Construct.
  54. File(Context* context);
  55. /// Construct and open a filesystem file.
  56. File(Context* context, const String& fileName, FileMode mode = FILE_READ);
  57. /// Construct and open from a package file.
  58. File(Context* context, PackageFile* package, const String& fileName);
  59. /// Destruct. Close the file if open.
  60. virtual ~File();
  61. /// Read bytes from the file. Return number of bytes actually read.
  62. virtual unsigned Read(void* dest, unsigned size);
  63. /// Reads a text file, ensuring data from file is 0 terminated
  64. virtual void ReadText(String& text);
  65. /// Set position from the beginning of the file.
  66. virtual unsigned Seek(unsigned position);
  67. /// Write bytes to the file. Return number of bytes actually written.
  68. virtual unsigned Write(const void* data, unsigned size);
  69. /// Return the file name.
  70. virtual const String& GetName() const { return fileName_; }
  71. /// Return a checksum of the file contents using the SDBM hash algorithm.
  72. virtual unsigned GetChecksum();
  73. /// Open a filesystem file. Return true if successful.
  74. bool Open(const String& fileName, FileMode mode = FILE_READ);
  75. /// Open from within a package file. Return true if successful.
  76. bool Open(PackageFile* package, const String& fileName);
  77. /// Close the file.
  78. void Close();
  79. /// Flush any buffered output to the file.
  80. void Flush();
  81. /// Change the file name. Used by the resource system.
  82. void SetName(const String& name);
  83. /// Set the fullpath to the file
  84. void SetFullPath(const String& path) { fullPath_ = path; }
  85. /// Return the open mode.
  86. FileMode GetMode() const { return mode_; }
  87. /// Return whether is open.
  88. bool IsOpen() const;
  89. /// Return the file handle.
  90. void* GetHandle() const { return handle_; }
  91. /// Return whether the file originates from a package.
  92. bool IsPackaged() const { return offset_ != 0; }
  93. /// Return the fullpath to the file
  94. const String& GetFullPath() const { return fullPath_; }
  95. // Atomic Begin
  96. /// Copy a file from a source file, must be opened and FILE_WRITE
  97. /// Unlike FileSystem.Copy this copy works when the source file is in a package file
  98. bool Copy(File* srcFile);
  99. // Atomic End
  100. private:
  101. /// File name.
  102. String fileName_;
  103. /// Full path to file
  104. String fullPath_;
  105. /// Open mode.
  106. FileMode mode_;
  107. /// File handle.
  108. void* handle_;
  109. #ifdef ANDROID
  110. /// SDL RWops context for Android asset loading.
  111. SDL_RWops* assetHandle_;
  112. #endif
  113. /// Read buffer for Android asset or compressed file loading.
  114. SharedArrayPtr<unsigned char> readBuffer_;
  115. /// Decompression input buffer for compressed file loading.
  116. SharedArrayPtr<unsigned char> inputBuffer_;
  117. /// Read buffer position.
  118. unsigned readBufferOffset_;
  119. /// Bytes in the current read buffer.
  120. unsigned readBufferSize_;
  121. /// Start position within a package file, 0 for regular files.
  122. unsigned offset_;
  123. /// Content checksum.
  124. unsigned checksum_;
  125. /// Compression flag.
  126. bool compressed_;
  127. /// Synchronization needed before read -flag.
  128. bool readSyncNeeded_;
  129. /// Synchronization needed before write -flag.
  130. bool writeSyncNeeded_;
  131. };
  132. }