disk_file.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #include "disk_file.h"
  24. #include "types.h"
  25. #include "log.h"
  26. #include "math_utils.h"
  27. #include "memory.h"
  28. namespace crown
  29. {
  30. DiskFile::DiskFile(FileOpenMode mode, const char* filename) :
  31. File(mode),
  32. m_file(filename, mode),
  33. m_last_was_read(true)
  34. {
  35. }
  36. DiskFile::~DiskFile()
  37. {
  38. m_file.close();
  39. }
  40. void DiskFile::seek(size_t position)
  41. {
  42. check_valid();
  43. m_file.seek(position);
  44. }
  45. void DiskFile::seek_to_end()
  46. {
  47. check_valid();
  48. m_file.seek_to_end();
  49. }
  50. void DiskFile::skip(size_t bytes)
  51. {
  52. check_valid();
  53. m_file.skip(bytes);
  54. }
  55. void DiskFile::read(void* buffer, size_t size)
  56. {
  57. check_valid();
  58. if (!m_last_was_read)
  59. {
  60. m_last_was_read = true;
  61. m_file.seek(0);
  62. }
  63. /*size_t bytes_read =*/ m_file.read(buffer, size);
  64. //CE_ASSERT(bytes_read == size, "Failed to read from file: requested: %llu, read: %llu", size, bytes_read);
  65. }
  66. void DiskFile::write(const void* buffer, size_t size)
  67. {
  68. check_valid();
  69. if (m_last_was_read)
  70. {
  71. m_last_was_read = false;
  72. m_file.seek(0);
  73. }
  74. /*size_t bytes_written =*/ m_file.write(buffer, size);
  75. //CE_ASSERT(bytes_written == size, "Failed to write to file: requested: %llu, written: %llu", size, bytes_written);
  76. }
  77. bool DiskFile::copy_to(File& file, size_t size)
  78. {
  79. check_valid();
  80. const size_t chunksize = 1024*1024;
  81. char* buff = (char*) default_allocator().allocate(chunksize * sizeof(char));
  82. size_t tot_read_bytes = 0;
  83. while (tot_read_bytes < size)
  84. {
  85. size_t read_bytes;
  86. size_t expected_read_bytes = math::min(size - tot_read_bytes, chunksize);
  87. read_bytes = m_file.read(buff, expected_read_bytes);
  88. if (read_bytes < expected_read_bytes)
  89. {
  90. if (m_file.eof())
  91. {
  92. if (read_bytes != 0)
  93. {
  94. file.write(buff, read_bytes);
  95. }
  96. }
  97. default_allocator().deallocate(buff);
  98. //Either the file gave an error, or ended before size bytes could be copied
  99. return false;
  100. }
  101. file.write(buff, read_bytes);
  102. tot_read_bytes += read_bytes;
  103. }
  104. default_allocator().deallocate(buff);
  105. return true;
  106. }
  107. bool DiskFile::end_of_file()
  108. {
  109. return position() == size();
  110. }
  111. bool DiskFile::is_valid()
  112. {
  113. return m_file.is_open();
  114. }
  115. void DiskFile::flush()
  116. {
  117. check_valid();
  118. // FIXME implement flush in File
  119. }
  120. size_t DiskFile::position()
  121. {
  122. check_valid();
  123. return m_file.position();
  124. }
  125. size_t DiskFile::size()
  126. {
  127. check_valid();
  128. return m_file.size();
  129. }
  130. bool DiskFile::can_read() const
  131. {
  132. check_valid();
  133. return true;
  134. }
  135. bool DiskFile::can_write() const
  136. {
  137. check_valid();
  138. return true;
  139. }
  140. bool DiskFile::can_seek() const
  141. {
  142. return true;
  143. }
  144. } // namespace crown