disk_file.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "disk_file.h"
  6. #include "types.h"
  7. #include "log.h"
  8. #include "math_utils.h"
  9. #include "memory.h"
  10. namespace crown
  11. {
  12. DiskFile::DiskFile(FileOpenMode mode, const char* filename)
  13. : File(mode)
  14. , _file(filename, mode)
  15. , _last_was_read(true)
  16. {
  17. }
  18. DiskFile::~DiskFile()
  19. {
  20. _file.close();
  21. }
  22. void DiskFile::seek(size_t position)
  23. {
  24. check_valid();
  25. _file.seek(position);
  26. }
  27. void DiskFile::seek_to_end()
  28. {
  29. check_valid();
  30. _file.seek_to_end();
  31. }
  32. void DiskFile::skip(size_t bytes)
  33. {
  34. check_valid();
  35. _file.skip(bytes);
  36. }
  37. void DiskFile::read(void* buffer, size_t size)
  38. {
  39. check_valid();
  40. if (!_last_was_read)
  41. {
  42. _last_was_read = true;
  43. _file.seek(0);
  44. }
  45. /*size_t bytes_read =*/ _file.read(buffer, size);
  46. //CE_ASSERT(bytes_read == size, "Failed to read from file: requested: %llu, read: %llu", size, bytes_read);
  47. }
  48. void DiskFile::write(const void* buffer, size_t size)
  49. {
  50. check_valid();
  51. if (_last_was_read)
  52. {
  53. _last_was_read = false;
  54. _file.seek(0);
  55. }
  56. /*size_t bytes_written =*/ _file.write(buffer, size);
  57. //CE_ASSERT(bytes_written == size, "Failed to write to file: requested: %llu, written: %llu", size, bytes_written);
  58. }
  59. bool DiskFile::copy_to(File& file, size_t size)
  60. {
  61. check_valid();
  62. const size_t chunksize = 1024*1024;
  63. char* buff = (char*) default_allocator().allocate(chunksize * sizeof(char));
  64. size_t tot_read_bytes = 0;
  65. while (tot_read_bytes < size)
  66. {
  67. size_t read_bytes;
  68. size_t expected_read_bytes = min(size - tot_read_bytes, chunksize);
  69. read_bytes = _file.read(buff, expected_read_bytes);
  70. if (read_bytes < expected_read_bytes)
  71. {
  72. if (_file.eof())
  73. {
  74. if (read_bytes != 0)
  75. {
  76. file.write(buff, read_bytes);
  77. }
  78. }
  79. default_allocator().deallocate(buff);
  80. //Either the file gave an error, or ended before size bytes could be copied
  81. return false;
  82. }
  83. file.write(buff, read_bytes);
  84. tot_read_bytes += read_bytes;
  85. }
  86. default_allocator().deallocate(buff);
  87. return true;
  88. }
  89. bool DiskFile::end_of_file()
  90. {
  91. return position() == size();
  92. }
  93. bool DiskFile::is_valid()
  94. {
  95. return _file.is_open();
  96. }
  97. void DiskFile::flush()
  98. {
  99. check_valid();
  100. // FIXME implement flush in File
  101. }
  102. size_t DiskFile::position()
  103. {
  104. check_valid();
  105. return _file.position();
  106. }
  107. size_t DiskFile::size()
  108. {
  109. check_valid();
  110. return _file.size();
  111. }
  112. bool DiskFile::can_read() const
  113. {
  114. check_valid();
  115. return true;
  116. }
  117. bool DiskFile::can_write() const
  118. {
  119. check_valid();
  120. return true;
  121. }
  122. bool DiskFile::can_seek() const
  123. {
  124. return true;
  125. }
  126. } // namespace crown