File.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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. /// File open mode
  28. enum FileMode
  29. {
  30. FILE_READ = 0,
  31. FILE_WRITE,
  32. FILE_READWRITE
  33. };
  34. class PackageFile;
  35. /// File opened either through the filesystem or from within a package file
  36. class File : public Object, public Deserializer, public Serializer
  37. {
  38. OBJECT(File);
  39. public:
  40. /// Construct
  41. File(Context* context);
  42. /// Construct and open
  43. File(Context* context, const std::string& fileName, FileMode mode = FILE_READ);
  44. /// Construct and open from a package file
  45. File(Context* context, PackageFile* package, const std::string& fileName);
  46. /// Destruct. Close the file if open
  47. virtual ~File();
  48. /// Read bytes from the file
  49. virtual unsigned Read(void* dest, unsigned size);
  50. /// Set position from the beginning of the file
  51. virtual unsigned Seek(unsigned position);
  52. /// Write bytes to the file
  53. virtual unsigned Write(const void* data, unsigned size);
  54. /// Return the file name
  55. virtual const std::string& GetName() const { return fileName_; }
  56. /// Return a checksum of the file contents, using the SDBM hash algorithm
  57. virtual unsigned GetChecksum();
  58. /// Open a filesystem file. Return true if successful
  59. bool Open(const std::string& fileName, FileMode mode = FILE_READ);
  60. /// Open from within a package file. Return true if successful
  61. bool Open(PackageFile* package, const std::string& fileName);
  62. /// Close the file
  63. void Close();
  64. /// Change the file name. Used by the resource system
  65. void SetName(const std::string& name);
  66. /// Return the open mode
  67. FileMode GetMode() const { return mode_; }
  68. /// Return whether is open
  69. bool IsOpen() const { return handle_ != 0; }
  70. /// Return the file handle
  71. void* GetHandle() const { return handle_; }
  72. private:
  73. /// File name
  74. std::string fileName_;
  75. /// Open mode
  76. FileMode mode_;
  77. /// File handle
  78. void* handle_;
  79. /// Start position within a package file, 0 for regular files
  80. unsigned offset_;
  81. /// Content checksum
  82. unsigned checksum_;
  83. };