File.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. /// 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 a filesystem file.
  43. File(Context* context, const String& fileName, FileMode mode = FILE_READ);
  44. /// Construct and open from a package file.
  45. File(Context* context, PackageFile* package, const String& fileName);
  46. /// Destruct. Close the file if open.
  47. virtual ~File();
  48. /// Read bytes from the file. Return number of bytes actually read.
  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. Return number of bytes actually written.
  53. virtual unsigned Write(const void* data, unsigned size);
  54. /// Return the file name.
  55. virtual const 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 String& fileName, FileMode mode = FILE_READ);
  60. /// Open from within a package file. Return true if successful.
  61. bool Open(PackageFile* package, const String& fileName);
  62. /// Close the file.
  63. void Close();
  64. /// Flush any buffered output to the file.
  65. void Flush();
  66. /// Change the file name. Used by the resource system.
  67. void SetName(const String& name);
  68. /// Return the open mode.
  69. FileMode GetMode() const { return mode_; }
  70. /// Return whether is open.
  71. bool IsOpen() const { return handle_ != 0; }
  72. /// Return the file handle.
  73. void* GetHandle() const { return handle_; }
  74. private:
  75. /// File name.
  76. String fileName_;
  77. /// Open mode.
  78. FileMode mode_;
  79. /// File handle.
  80. void* handle_;
  81. /// Start position within a package file, 0 for regular files.
  82. unsigned offset_;
  83. /// Content checksum.
  84. unsigned checksum_;
  85. };