AndroidFile.cpp 3.7 KB

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