File.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // Copyright (c) 2008-2017 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/AbstractFile.h"
  26. #ifdef __ANDROID__
  27. struct SDL_RWops;
  28. #endif
  29. namespace Atomic
  30. {
  31. #ifdef __ANDROID__
  32. extern const char* APK;
  33. // Macro for checking if a given pathname is inside APK's assets directory
  34. #define ATOMIC_IS_ASSET(p) p.StartsWith(APK)
  35. // 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)
  36. #ifdef ASSET_DIR_INDICATOR
  37. #define ATOMIC_ASSET(p) p.Substring(5).Replaced("/", ASSET_DIR_INDICATOR "/").CString()
  38. #else
  39. #define ATOMIC_ASSET(p) p.Substring(5).CString()
  40. #endif
  41. #endif
  42. /// File open mode.
  43. enum FileMode
  44. {
  45. FILE_READ = 0,
  46. FILE_WRITE,
  47. FILE_READWRITE,
  48. // ATOMIC BEGIN
  49. FILE_APPEND
  50. // ATOMIC END
  51. };
  52. class PackageFile;
  53. /// %File opened either through the filesystem or from within a package file.
  54. class ATOMIC_API File : public Object, public AbstractFile
  55. {
  56. ATOMIC_OBJECT(File, Object);
  57. public:
  58. /// Construct.
  59. File(Context* context);
  60. /// Construct and open a filesystem file.
  61. File(Context* context, const String& fileName, FileMode mode = FILE_READ);
  62. /// Construct and open from a package file.
  63. File(Context* context, PackageFile* package, const String& fileName);
  64. /// Destruct. Close the file if open.
  65. virtual ~File();
  66. /// Read bytes from the file. Return number of bytes actually read.
  67. virtual unsigned Read(void* dest, unsigned size);
  68. /// Set position from the beginning of the file.
  69. virtual unsigned Seek(unsigned position);
  70. /// Write bytes to the file. Return number of bytes actually written.
  71. virtual unsigned Write(const void* data, unsigned size);
  72. /// Return the file name.
  73. virtual const String& GetName() const { return fileName_; }
  74. /// Return a checksum of the file contents using the SDBM hash algorithm.
  75. virtual unsigned GetChecksum();
  76. /// Open a filesystem file. Return true if successful.
  77. bool Open(const String& fileName, FileMode mode = FILE_READ);
  78. /// Open from within a package file. Return true if successful.
  79. bool Open(PackageFile* package, const String& fileName);
  80. /// Close the file.
  81. void Close();
  82. /// Flush any buffered output to the file.
  83. void Flush();
  84. /// Change the file name. Used by the resource system.
  85. void SetName(const String& name);
  86. /// Return the open mode.
  87. FileMode GetMode() const { return mode_; }
  88. /// Return whether is open.
  89. bool IsOpen() const;
  90. /// Return the file handle.
  91. void* GetHandle() const { return handle_; }
  92. /// Return whether the file originates from a package.
  93. bool IsPackaged() const { return offset_ != 0; }
  94. // ATOMIC BEGIN
  95. /// Reads a text file, ensuring data from file is 0 terminated
  96. virtual void ReadText(String& text);
  97. /// Reads a text file, ensuring data from file is 0 terminated
  98. virtual String ReadText() { String retValue; ReadText(retValue); return retValue; }
  99. /// Return the fullpath to the file
  100. const String& GetFullPath() const { return fullPath_; }
  101. /// Copy a file from a source file, must be opened and FILE_WRITE
  102. /// Unlike FileSystem.Copy this copy works when the source file is in a package file
  103. bool Copy(File* srcFile);
  104. // ATOMIC END
  105. private:
  106. /// Open file internally using either C standard IO functions or SDL RWops for Android asset files. Return true if successful.
  107. bool OpenInternal(const String& fileName, FileMode mode, bool fromPackage = false);
  108. /// Perform the file read internally using either C standard IO functions or SDL RWops for Android asset files. Return true if successful. This does not handle compressed package file reading.
  109. bool ReadInternal(void* dest, unsigned size);
  110. /// Seek in file internally using either C standard IO functions or SDL RWops for Android asset files.
  111. void SeekInternal(unsigned newPosition);
  112. /// File name.
  113. String fileName_;
  114. /// Open mode.
  115. FileMode mode_;
  116. /// File handle.
  117. void* handle_;
  118. #ifdef __ANDROID__
  119. /// SDL RWops context for Android asset loading.
  120. SDL_RWops* assetHandle_;
  121. #endif
  122. /// Read buffer for Android asset or compressed file loading.
  123. SharedArrayPtr<unsigned char> readBuffer_;
  124. /// Decompression input buffer for compressed file loading.
  125. SharedArrayPtr<unsigned char> inputBuffer_;
  126. /// Read buffer position.
  127. unsigned readBufferOffset_;
  128. /// Bytes in the current read buffer.
  129. unsigned readBufferSize_;
  130. /// Start position within a package file, 0 for regular files.
  131. unsigned offset_;
  132. /// Content checksum.
  133. unsigned checksum_;
  134. /// Compression flag.
  135. bool compressed_;
  136. /// Synchronization needed before read -flag.
  137. bool readSyncNeeded_;
  138. /// Synchronization needed before write -flag.
  139. bool writeSyncNeeded_;
  140. // ATOMIC BEGIN
  141. /// Full path to file
  142. String fullPath_;
  143. // ATOMIC END
  144. };
  145. }