FileStream.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. #include "FileStream.h"
  23. #include "Types.h"
  24. #include "Log.h"
  25. #include "MathUtils.h"
  26. namespace crown
  27. {
  28. //-----------------------------------------------------------------------------
  29. FileStream::FileStream(StreamOpenMode mode, const char* filename) :
  30. Stream(mode),
  31. m_file(filename, mode),
  32. m_last_was_read(true)
  33. {
  34. }
  35. //-----------------------------------------------------------------------------
  36. FileStream::~FileStream()
  37. {
  38. //m_file.close();
  39. }
  40. //-----------------------------------------------------------------------------
  41. void FileStream::seek(size_t position)
  42. {
  43. check_valid();
  44. m_file.seek(position);
  45. }
  46. //-----------------------------------------------------------------------------
  47. void FileStream::seek_to_end()
  48. {
  49. check_valid();
  50. m_file.seek_to_end();
  51. }
  52. //-----------------------------------------------------------------------------
  53. void FileStream::skip(size_t bytes)
  54. {
  55. check_valid();
  56. m_file.skip(bytes);
  57. }
  58. //-----------------------------------------------------------------------------
  59. uint8_t FileStream::read_byte()
  60. {
  61. check_valid();
  62. if (!m_last_was_read)
  63. {
  64. m_last_was_read = true;
  65. m_file.seek(0);
  66. }
  67. uint8_t buffer;
  68. ce_assert(m_file.read(&buffer, 1) == 1, "Failed to read from file");
  69. return buffer;
  70. }
  71. //-----------------------------------------------------------------------------
  72. void FileStream::read(void* buffer, size_t size)
  73. {
  74. check_valid();
  75. if (!m_last_was_read)
  76. {
  77. m_last_was_read = true;
  78. m_file.seek(0);
  79. }
  80. ce_assert(m_file.read(buffer, size) == size, "Failed to read from file");
  81. }
  82. //-----------------------------------------------------------------------------
  83. bool FileStream::copy_to(Stream& stream, size_t size)
  84. {
  85. check_valid();
  86. const size_t chunksize = 1024*1024;
  87. char* buff = new char[chunksize];
  88. size_t tot_read_bytes = 0;
  89. while (tot_read_bytes < size)
  90. {
  91. size_t read_bytes;
  92. size_t expected_read_bytes = math::min(size - tot_read_bytes, chunksize);
  93. read_bytes = m_file.read(buff, expected_read_bytes);
  94. if (read_bytes < expected_read_bytes)
  95. {
  96. if (m_file.eof())
  97. {
  98. if (read_bytes != 0)
  99. {
  100. stream.write(buff, read_bytes);
  101. }
  102. }
  103. delete[] buff;
  104. //Either the file gave an error, or ended before size bytes could be copied
  105. return false;
  106. }
  107. stream.write(buff, read_bytes);
  108. tot_read_bytes += read_bytes;
  109. }
  110. delete [] buff;
  111. return true;
  112. }
  113. //-----------------------------------------------------------------------------
  114. bool FileStream::end_of_stream() const
  115. {
  116. return position() == size();
  117. }
  118. //-----------------------------------------------------------------------------
  119. bool FileStream::is_valid() const
  120. {
  121. return m_file.is_open();
  122. }
  123. //-----------------------------------------------------------------------------
  124. void FileStream::write_byte(uint8_t val)
  125. {
  126. check_valid();
  127. if (m_last_was_read)
  128. {
  129. m_last_was_read = false;
  130. m_file.seek(0);
  131. }
  132. ce_assert(m_file.write(&val, 1) == 1, "Failed to write to file");
  133. }
  134. //-----------------------------------------------------------------------------
  135. void FileStream::write(const void* buffer, size_t size)
  136. {
  137. check_valid();
  138. if (m_last_was_read)
  139. {
  140. m_last_was_read = false;
  141. m_file.seek(0);
  142. }
  143. ce_assert(m_file.write(buffer, size) == size, "Failed to write to file");
  144. }
  145. //-----------------------------------------------------------------------------
  146. void FileStream::flush()
  147. {
  148. check_valid();
  149. // FIXME implement flush in File
  150. }
  151. //-----------------------------------------------------------------------------
  152. size_t FileStream::position() const
  153. {
  154. check_valid();
  155. return m_file.position();
  156. }
  157. //-----------------------------------------------------------------------------
  158. size_t FileStream::size() const
  159. {
  160. check_valid();
  161. return m_file.size();
  162. }
  163. //-----------------------------------------------------------------------------
  164. bool FileStream::can_read() const
  165. {
  166. check_valid();
  167. return true;
  168. }
  169. //-----------------------------------------------------------------------------
  170. bool FileStream::can_write() const
  171. {
  172. check_valid();
  173. return true;
  174. }
  175. //-----------------------------------------------------------------------------
  176. bool FileStream::can_seek() const
  177. {
  178. return true;
  179. }
  180. } // namespace crown