AssetFile.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "AssetFile.h"
  24. namespace crown
  25. {
  26. //-----------------------------------------------------------------------------
  27. AssetFile::AssetFile(const char* path) :
  28. File(FOM_READ),
  29. m_file(path, FOM_READ),
  30. m_last_was_read(true)
  31. {
  32. }
  33. //-----------------------------------------------------------------------------
  34. void AssetFile::seek(size_t position)
  35. {
  36. check_valid();
  37. m_file.seek(position);
  38. }
  39. //-----------------------------------------------------------------------------
  40. void AssetFile::seek_to_end()
  41. {
  42. check_valid();
  43. m_file.seek_to_end();
  44. }
  45. //-----------------------------------------------------------------------------
  46. void AssetFile::skip(size_t bytes)
  47. {
  48. check_valid();
  49. m_file.skip(bytes);
  50. }
  51. //-----------------------------------------------------------------------------
  52. void AssetFile::read(void* buffer, size_t size)
  53. {
  54. check_valid();
  55. if (!m_last_was_read)
  56. {
  57. m_last_was_read = true;
  58. m_file.seek(0);
  59. }
  60. size_t bytes_read = m_file.read(buffer, size);
  61. CE_ASSERT(bytes_read == size, "Failed to read from file");
  62. }
  63. //-----------------------------------------------------------------------------
  64. void AssetFile::write(const void* buffer, size_t size)
  65. {
  66. // Not needed
  67. }
  68. //-----------------------------------------------------------------------------
  69. bool AssetFile::copy_to(File& file, size_t size = 0)
  70. {
  71. // Not needed
  72. }
  73. //-----------------------------------------------------------------------------
  74. void AssetFile::flush()
  75. {
  76. // Not needed
  77. }
  78. //-----------------------------------------------------------------------------
  79. bool AssetFile::is_valid() const
  80. {
  81. return m_file.is_open();
  82. }
  83. //-----------------------------------------------------------------------------
  84. bool AssetFile::end_of_file() const
  85. {
  86. return position() == size();
  87. }
  88. //-----------------------------------------------------------------------------
  89. size_t AssetFile::size() const
  90. {
  91. check_valid();
  92. return m_file.size();
  93. }
  94. //-----------------------------------------------------------------------------
  95. size_t AssetFile::position() const
  96. {
  97. check_valid();
  98. return m_file.position();
  99. }
  100. //-----------------------------------------------------------------------------
  101. bool AssetFile::can_read() const
  102. {
  103. check_valid();
  104. return true;
  105. }
  106. //-----------------------------------------------------------------------------
  107. bool AssetFile::can_write() const
  108. {
  109. check_valid();
  110. return true;
  111. }
  112. //-----------------------------------------------------------------------------
  113. bool AssetFile::can_seek() const
  114. {
  115. check_valid();
  116. return true;
  117. }
  118. } // namespace crown