File.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Deserializer.h"
  25. #include "Serializer.h"
  26. #include "Object.h"
  27. #ifdef ANDROID
  28. #include "ArrayPtr.h"
  29. #include <SDL_rwops.h>
  30. #endif
  31. /// File open mode.
  32. enum FileMode
  33. {
  34. FILE_READ = 0,
  35. FILE_WRITE,
  36. FILE_READWRITE
  37. };
  38. class PackageFile;
  39. /// %File opened either through the filesystem or from within a package file.
  40. class File : public Object, public Deserializer, public Serializer
  41. {
  42. OBJECT(File);
  43. public:
  44. /// Construct.
  45. File(Context* context);
  46. /// Construct and open a filesystem file.
  47. File(Context* context, const String& fileName, FileMode mode = FILE_READ);
  48. /// Construct and open from a package file.
  49. File(Context* context, PackageFile* package, const String& fileName);
  50. /// Destruct. Close the file if open.
  51. virtual ~File();
  52. /// Read bytes from the file. Return number of bytes actually read.
  53. virtual unsigned Read(void* dest, unsigned size);
  54. /// %Set position from the beginning of the file.
  55. virtual unsigned Seek(unsigned position);
  56. /// Write bytes to the file. Return number of bytes actually written.
  57. virtual unsigned Write(const void* data, unsigned size);
  58. /// Return the file name.
  59. virtual const String& GetName() const { return fileName_; }
  60. /// Return a checksum of the file contents using the SDBM hash algorithm.
  61. virtual unsigned GetChecksum();
  62. /// Open a filesystem file. Return true if successful.
  63. bool Open(const String& fileName, FileMode mode = FILE_READ);
  64. /// Open from within a package file. Return true if successful.
  65. bool Open(PackageFile* package, const String& fileName);
  66. /// Close the file.
  67. void Close();
  68. /// Flush any buffered output to the file.
  69. void Flush();
  70. /// Change the file name. Used by the resource system.
  71. void SetName(const String& name);
  72. /// Return the open mode.
  73. FileMode GetMode() const { return mode_; }
  74. /// Return whether is open.
  75. bool IsOpen() const { return handle_ != 0; }
  76. /// Return the file handle.
  77. void* GetHandle() const { return handle_; }
  78. /// Return whether the file originates from a package.
  79. bool IsPackaged() const { return offset_ != 0; }
  80. private:
  81. /// File name.
  82. String fileName_;
  83. /// Open mode.
  84. FileMode mode_;
  85. /// File handle.
  86. void* handle_;
  87. #ifdef ANDROID
  88. /// SDL RWops context for Android asset loading.
  89. SDL_RWops* assetHandle_;
  90. /// Read buffer for Android asset loading.
  91. SharedArrayPtr<unsigned char> readBuffer_;
  92. /// Read buffer position.
  93. unsigned readBufferOffset_;
  94. /// Bytes in the current read buffer.
  95. unsigned readBufferSize_;
  96. #endif
  97. /// Start position within a package file, 0 for regular files.
  98. unsigned offset_;
  99. /// Content checksum.
  100. unsigned checksum_;
  101. };