FileStream.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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), m_file(NULL),
  31. m_last_was_read(true)
  32. {
  33. //Takes ownership
  34. FileOpenMode file_open_mode = (FileOpenMode)0;
  35. if (math::test_bitmask(mode, SOM_READ))
  36. file_open_mode = (FileOpenMode)(file_open_mode | FOM_READ);
  37. if (math::test_bitmask(mode, SOM_WRITE))
  38. file_open_mode = (FileOpenMode)(file_open_mode | FOM_WRITE);
  39. m_file = File::open(filename, file_open_mode);
  40. }
  41. //-----------------------------------------------------------------------------
  42. FileStream::~FileStream()
  43. {
  44. delete m_file;
  45. m_file = 0;
  46. }
  47. //-----------------------------------------------------------------------------
  48. void FileStream::seek(size_t position)
  49. {
  50. check_valid();
  51. m_file->seek(position, SEEK_SET);
  52. }
  53. //-----------------------------------------------------------------------------
  54. void FileStream::seek_to_end()
  55. {
  56. check_valid();
  57. m_file->seek(0, SEEK_END);
  58. }
  59. //-----------------------------------------------------------------------------
  60. void FileStream::skip(size_t bytes)
  61. {
  62. check_valid();
  63. m_file->seek(bytes, SEEK_CUR);
  64. }
  65. //-----------------------------------------------------------------------------
  66. uint8_t FileStream::read_byte()
  67. {
  68. check_valid();
  69. if (!m_last_was_read)
  70. {
  71. m_last_was_read = true;
  72. m_file->seek(0, SEEK_CUR);
  73. }
  74. uint8_t buffer;
  75. if (m_file->read(&buffer, 1, 1) != 1)
  76. {
  77. Log::e("Could not read from file");
  78. }
  79. return buffer;
  80. }
  81. //-----------------------------------------------------------------------------
  82. void FileStream::read(void* buffer, size_t size)
  83. {
  84. check_valid();
  85. if (!m_last_was_read)
  86. {
  87. m_last_was_read = true;
  88. m_file->seek(0, SEEK_CUR);
  89. }
  90. if (m_file->read(buffer, size, 1) != 1)
  91. {
  92. Log::e("Could not read from file.");
  93. }
  94. }
  95. //-----------------------------------------------------------------------------
  96. bool FileStream::copy_to(Stream* stream, size_t size)
  97. {
  98. check_valid();
  99. if (stream == 0)
  100. return false;
  101. if (size == 0)
  102. return true;
  103. const size_t chunksize = 1024*1024;
  104. char* buff = new char[chunksize];
  105. size_t totReadBytes = 0;
  106. while (totReadBytes < size)
  107. {
  108. int32_t readBytes;
  109. int32_t expectedReadBytes = math::min(size - totReadBytes, chunksize);
  110. readBytes = m_file->read(buff, 1, expectedReadBytes);
  111. if (readBytes < expectedReadBytes)
  112. {
  113. if (m_file->eof())
  114. {
  115. if (readBytes != 0)
  116. {
  117. stream->write(buff, readBytes);
  118. }
  119. }
  120. delete[] buff;
  121. //Either the file gave an error, or ended before size bytes could be copied
  122. return false;
  123. }
  124. stream->write(buff, readBytes);
  125. totReadBytes += readBytes;
  126. }
  127. delete [] buff;
  128. return true;
  129. }
  130. //-----------------------------------------------------------------------------
  131. bool FileStream::end_of_stream() const
  132. {
  133. return position() == size();
  134. }
  135. //-----------------------------------------------------------------------------
  136. bool FileStream::is_valid() const
  137. {
  138. {
  139. if (!m_file)
  140. {
  141. return false;
  142. }
  143. return m_file->is_valid();
  144. }
  145. }
  146. //-----------------------------------------------------------------------------
  147. void FileStream::write_byte(uint8_t val)
  148. {
  149. check_valid();
  150. if (m_last_was_read)
  151. {
  152. m_last_was_read = false;
  153. m_file->seek(0, SEEK_CUR);
  154. }
  155. if (m_file->write(&val, 1, 1) != 1)
  156. {
  157. Log::e("Could not write to file.");
  158. }
  159. }
  160. //-----------------------------------------------------------------------------
  161. void FileStream::write(const void* buffer, size_t size)
  162. {
  163. check_valid();
  164. if (m_last_was_read)
  165. {
  166. m_last_was_read = false;
  167. m_file->seek(0, SEEK_CUR);
  168. }
  169. if (m_file->write(buffer, size, 1) != 1)
  170. {
  171. Log::e("Could not write to file.");
  172. }
  173. }
  174. //-----------------------------------------------------------------------------
  175. void FileStream::flush()
  176. {
  177. check_valid();
  178. // FIXME implement flush in File
  179. }
  180. //-----------------------------------------------------------------------------
  181. size_t FileStream::position() const
  182. {
  183. check_valid();
  184. return m_file->tell();
  185. }
  186. //-----------------------------------------------------------------------------
  187. size_t FileStream::size() const
  188. {
  189. check_valid();
  190. return m_file->size();
  191. }
  192. //-----------------------------------------------------------------------------
  193. bool FileStream::can_read() const
  194. {
  195. check_valid();
  196. return true;
  197. }
  198. //-----------------------------------------------------------------------------
  199. bool FileStream::can_write() const
  200. {
  201. check_valid();
  202. return true;
  203. }
  204. //-----------------------------------------------------------------------------
  205. bool FileStream::can_seek() const
  206. {
  207. return true;
  208. }
  209. } // namespace crown