FileStream.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #pragma once
  23. #include "Assert.h"
  24. #include "Stream.h"
  25. #include "File.h"
  26. namespace crown
  27. {
  28. /// File stream.
  29. /// Provides common facilities to access files on disk.
  30. class FileStream: public Stream
  31. {
  32. public:
  33. /// Opens @filename with specified @mode
  34. FileStream(StreamOpenMode mode, const char* filename);
  35. virtual ~FileStream();
  36. /// @copydoc Stream::seek()
  37. void seek(size_t position);
  38. /// @copydoc Stream::seek_to_end()
  39. void seek_to_end();
  40. /// @copydoc Stream::skip()
  41. void skip(size_t bytes);
  42. /// @copydoc Stream::read()
  43. void read(void* buffer, size_t size);
  44. /// @copydoc Stream::write()
  45. void write(const void* buffer, size_t size);
  46. /// @copydoc Stream::copy_to()
  47. bool copy_to(Stream& stream, size_t size = 0);
  48. /// @copydoc Stream::flush()
  49. void flush();
  50. /// @copydoc Stream::end_of_stream()
  51. bool end_of_stream() const;
  52. /// @copydoc Stream::is_valid()
  53. bool is_valid() const;
  54. /// @copydoc Stream::size()
  55. size_t size() const;
  56. /// @copydoc Stream::position()
  57. size_t position() const;
  58. /// @copydoc Stream::can_read()
  59. bool can_read() const;
  60. /// @copydoc Stream::can_write()
  61. bool can_write() const;
  62. /// @copydoc Stream::can_seek()
  63. bool can_seek() const;
  64. protected:
  65. File m_file;
  66. bool m_last_was_read;
  67. protected:
  68. inline void check_valid() const
  69. {
  70. ce_assert(m_file.is_open(), "File is not open");
  71. }
  72. };
  73. } // namespace crown