File.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Types.h"
  25. namespace crown
  26. {
  27. enum FileOpenMode
  28. {
  29. FOM_READ = 1,
  30. FOM_WRITE = 2
  31. };
  32. class Compressor;
  33. /// An abstraction to access data files.
  34. ///
  35. /// It represents a flow of data attached to a 'file' which can be an archived file,
  36. /// a regular file, a location in memory or anything that can be read or wrote.
  37. /// A File is an abstraction to interact with these in an uniform way; every file
  38. /// comes with a convenient set of methods to facilitate reading from it, writing to
  39. /// it and so on.
  40. class File
  41. {
  42. public:
  43. /// Opens the file with the given @a mode
  44. File(FileOpenMode mode) : m_open_mode(mode) {}
  45. virtual ~File() {};
  46. /// Sets the position indicator of the file to position.
  47. virtual void seek(size_t position) = 0;
  48. /// Sets the position indicator to the end of the file
  49. virtual void seek_to_end() = 0;
  50. /// Sets the position indicator to bytes after current position
  51. virtual void skip(size_t bytes) = 0;
  52. /// Reads a block of data from the file.
  53. virtual void read(void* buffer, size_t size) = 0;
  54. /// Writes a block of data to the file.
  55. virtual void write(const void* buffer, size_t size) = 0;
  56. /// Copies a chunk of 'size' bytes of data from this to another file.
  57. virtual bool copy_to(File& file, size_t size = 0) = 0;
  58. /// Zips a chunk of 'size' bytes of data from this to another file using compressor.
  59. virtual bool compress_to(File& file, size_t size, size_t& compressed_size, Compressor& compressor);
  60. /// Unzip a zipped file of data from this to another file using compressor.
  61. virtual bool uncompress_to(File& file, size_t& uncompressed_size, Compressor& compressor);
  62. /// Forces the previouses write operations to complete.
  63. /// Generally, when a File is attached to a file,
  64. /// write operations are not performed instantly, the output data
  65. /// may be stored to a temporary buffer before making its way to
  66. /// the file. This method forces all the pending output operations
  67. /// to be written to the file.
  68. virtual void flush() = 0;
  69. /// Returns whether the file is valid.
  70. /// A file is valid when the buffer where it operates
  71. /// exists. (i.e. a file descriptor is attached to the file,
  72. /// a memory area is attached to the file etc.)
  73. virtual bool is_valid() const = 0;
  74. /// Returns whether the position is at end of file.
  75. virtual bool end_of_file() const = 0;
  76. /// Returns the size of file in bytes.
  77. virtual size_t size() const = 0;
  78. /// Returns the current position in file.
  79. /// Generally, for binary data, it means the number of bytes
  80. /// from the beginning of the file.
  81. virtual size_t position() const = 0;
  82. /// Returns whether the file can be read.
  83. virtual bool can_read() const = 0;
  84. /// Returns whether the file can be wrote.
  85. virtual bool can_write() const = 0;
  86. /// Returns whether the file can be sought.
  87. virtual bool can_seek() const = 0;
  88. protected:
  89. FileOpenMode m_open_mode;
  90. };
  91. } // namespace crown