File.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // Copyright (c) 2008-2014 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 "../IO/Deserializer.h"
  25. #include "../IO/Serializer.h"
  26. #include "../Core/Object.h"
  27. #ifdef ANDROID
  28. #include <SDL/include/SDL_rwops.h>
  29. #endif
  30. namespace Atomic
  31. {
  32. /// File open mode.
  33. enum FileMode
  34. {
  35. FILE_READ = 0,
  36. FILE_WRITE,
  37. FILE_READWRITE
  38. };
  39. class PackageFile;
  40. /// %File opened either through the filesystem or from within a package file.
  41. class ATOMIC_API File : public Object, public Deserializer, public Serializer
  42. {
  43. OBJECT(File);
  44. public:
  45. /// Construct.
  46. File(Context* context);
  47. /// Construct and open a filesystem file.
  48. File(Context* context, const String& fileName, FileMode mode = FILE_READ);
  49. /// Construct and open from a package file.
  50. File(Context* context, PackageFile* package, const String& fileName);
  51. /// Destruct. Close the file if open.
  52. virtual ~File();
  53. /// Read bytes from the file. Return number of bytes actually read.
  54. virtual unsigned Read(void* dest, unsigned size);
  55. /// Reads a text file, ensuring data from file is 0 terminated
  56. virtual void ReadText(String& text);
  57. /// Set position from the beginning of the file.
  58. virtual unsigned Seek(unsigned position);
  59. /// Write bytes to the file. Return number of bytes actually written.
  60. virtual unsigned Write(const void* data, unsigned size);
  61. /// Return the file name.
  62. virtual const String& GetName() const { return fileName_; }
  63. /// Return a checksum of the file contents using the SDBM hash algorithm.
  64. virtual unsigned GetChecksum();
  65. /// Open a filesystem file. Return true if successful.
  66. bool Open(const String& fileName, FileMode mode = FILE_READ);
  67. /// Open from within a package file. Return true if successful.
  68. bool Open(PackageFile* package, const String& fileName);
  69. /// Close the file.
  70. void Close();
  71. /// Flush any buffered output to the file.
  72. void Flush();
  73. /// Change the file name. Used by the resource system.
  74. void SetName(const String& name);
  75. /// Set the fullpath to the file
  76. void SetFullPath(const String& path) { fullPath_ = path; }
  77. /// Return the open mode.
  78. FileMode GetMode() const { return mode_; }
  79. /// Return whether is open.
  80. bool IsOpen() const;
  81. /// Return the file handle.
  82. void* GetHandle() const { return handle_; }
  83. /// Return whether the file originates from a package.
  84. bool IsPackaged() const { return offset_ != 0; }
  85. /// Return the fullpath to the file
  86. const String& GetFullPath() const { return fullPath_; }
  87. private:
  88. /// File name.
  89. String fileName_;
  90. /// Full path to file
  91. String fullPath_;
  92. /// Open mode.
  93. FileMode mode_;
  94. /// File handle.
  95. void* handle_;
  96. #ifdef ANDROID
  97. /// SDL RWops context for Android asset loading.
  98. SDL_RWops* assetHandle_;
  99. #endif
  100. /// Read buffer for Android asset or compressed file loading.
  101. SharedArrayPtr<unsigned char> readBuffer_;
  102. /// Decompression input buffer for compressed file loading.
  103. SharedArrayPtr<unsigned char> inputBuffer_;
  104. /// Read buffer position.
  105. unsigned readBufferOffset_;
  106. /// Bytes in the current read buffer.
  107. unsigned readBufferSize_;
  108. /// Start position within a package file, 0 for regular files.
  109. unsigned offset_;
  110. /// Content checksum.
  111. unsigned checksum_;
  112. /// Compression flag.
  113. bool compressed_;
  114. /// Synchronization needed before read -flag.
  115. bool readSyncNeeded_;
  116. /// Synchronization needed before write -flag.
  117. bool writeSyncNeeded_;
  118. };
  119. }