FileStream.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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_last_was_read(true)
  32. {
  33. m_file.open(filename, mode);
  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. assert(m_file.read(&buffer, 1));
  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. assert(m_file.read(buffer, size));
  81. }
  82. //-----------------------------------------------------------------------------
  83. bool FileStream::copy_to(Stream* stream, size_t size)
  84. {
  85. check_valid();
  86. if (stream == 0)
  87. return false;
  88. if (size == 0)
  89. return true;
  90. const size_t chunksize = 1024*1024;
  91. char* buff = new char[chunksize];
  92. size_t tot_read_bytes = 0;
  93. while (tot_read_bytes < size)
  94. {
  95. size_t read_bytes;
  96. size_t expected_read_bytes = math::min(size - tot_read_bytes, chunksize);
  97. read_bytes = m_file.read(buff, expected_read_bytes);
  98. if (read_bytes < expected_read_bytes)
  99. {
  100. if (m_file.eof())
  101. {
  102. if (read_bytes != 0)
  103. {
  104. stream->write(buff, read_bytes);
  105. }
  106. }
  107. delete[] buff;
  108. //Either the file gave an error, or ended before size bytes could be copied
  109. return false;
  110. }
  111. stream->write(buff, read_bytes);
  112. tot_read_bytes += read_bytes;
  113. }
  114. delete [] buff;
  115. return true;
  116. }
  117. //-----------------------------------------------------------------------------
  118. bool FileStream::end_of_stream() const
  119. {
  120. return position() == size();
  121. }
  122. //-----------------------------------------------------------------------------
  123. bool FileStream::is_valid() const
  124. {
  125. return m_file.is_open();
  126. }
  127. //-----------------------------------------------------------------------------
  128. void FileStream::write_byte(uint8_t val)
  129. {
  130. check_valid();
  131. if (m_last_was_read)
  132. {
  133. m_last_was_read = false;
  134. m_file.seek(0);
  135. }
  136. assert(m_file.write(&val, 1) == 1);
  137. }
  138. //-----------------------------------------------------------------------------
  139. void FileStream::write(const void* buffer, size_t size)
  140. {
  141. check_valid();
  142. if (m_last_was_read)
  143. {
  144. m_last_was_read = false;
  145. m_file.seek(0);
  146. }
  147. assert(m_file.write(buffer, size) == size);
  148. }
  149. //-----------------------------------------------------------------------------
  150. void FileStream::flush()
  151. {
  152. check_valid();
  153. // FIXME implement flush in File
  154. }
  155. //-----------------------------------------------------------------------------
  156. size_t FileStream::position() const
  157. {
  158. check_valid();
  159. return m_file.position();
  160. }
  161. //-----------------------------------------------------------------------------
  162. size_t FileStream::size() const
  163. {
  164. check_valid();
  165. return m_file.size();
  166. }
  167. //-----------------------------------------------------------------------------
  168. bool FileStream::can_read() const
  169. {
  170. check_valid();
  171. return true;
  172. }
  173. //-----------------------------------------------------------------------------
  174. bool FileStream::can_write() const
  175. {
  176. check_valid();
  177. return true;
  178. }
  179. //-----------------------------------------------------------------------------
  180. bool FileStream::can_seek() const
  181. {
  182. return true;
  183. }
  184. } // namespace crown