File.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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. namespace Urho3D
  32. {
  33. /// File open mode.
  34. enum FileMode
  35. {
  36. FILE_READ = 0,
  37. FILE_WRITE,
  38. FILE_READWRITE
  39. };
  40. class PackageFile;
  41. /// %File opened either through the filesystem or from within a package file.
  42. class File : public Object, public Deserializer, public Serializer
  43. {
  44. OBJECT(File);
  45. public:
  46. /// Construct.
  47. File(Context* context);
  48. /// Construct and open a filesystem file.
  49. File(Context* context, const String& fileName, FileMode mode = FILE_READ);
  50. /// Construct and open from a package file.
  51. File(Context* context, PackageFile* package, const String& fileName);
  52. /// Destruct. Close the file if open.
  53. virtual ~File();
  54. /// Read bytes from the file. Return number of bytes actually read.
  55. virtual unsigned Read(void* dest, unsigned size);
  56. /// Set position from the beginning of the file.
  57. virtual unsigned Seek(unsigned position);
  58. /// Write bytes to the file. Return number of bytes actually written.
  59. virtual unsigned Write(const void* data, unsigned size);
  60. /// Return the file name.
  61. virtual const String& GetName() const { return fileName_; }
  62. /// Return a checksum of the file contents using the SDBM hash algorithm.
  63. virtual unsigned GetChecksum();
  64. /// Open a filesystem file. Return true if successful.
  65. bool Open(const String& fileName, FileMode mode = FILE_READ);
  66. /// Open from within a package file. Return true if successful.
  67. bool Open(PackageFile* package, const String& fileName);
  68. /// Close the file.
  69. void Close();
  70. /// Flush any buffered output to the file.
  71. void Flush();
  72. /// Change the file name. Used by the resource system.
  73. void SetName(const String& name);
  74. /// Return the open mode.
  75. FileMode GetMode() const { return mode_; }
  76. /// Return whether is open.
  77. bool IsOpen() const { return handle_ != 0; }
  78. /// Return the file handle.
  79. void* GetHandle() const { return handle_; }
  80. /// Return whether the file originates from a package.
  81. bool IsPackaged() const { return offset_ != 0; }
  82. private:
  83. /// File name.
  84. String fileName_;
  85. /// Open mode.
  86. FileMode mode_;
  87. /// File handle.
  88. void* handle_;
  89. #ifdef ANDROID
  90. /// SDL RWops context for Android asset loading.
  91. SDL_RWops* assetHandle_;
  92. /// Read buffer for Android asset loading.
  93. SharedArrayPtr<unsigned char> readBuffer_;
  94. /// Read buffer position.
  95. unsigned readBufferOffset_;
  96. /// Bytes in the current read buffer.
  97. unsigned readBufferSize_;
  98. #endif
  99. /// Start position within a package file, 0 for regular files.
  100. unsigned offset_;
  101. /// Content checksum.
  102. unsigned checksum_;
  103. };
  104. }