2
0

OsFile2.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 <android/asset_manager_jni.h>
  24. #include "Assert.h"
  25. #include "OS.h"
  26. #include "OsFile.h"
  27. #include "AndroidOS.h"
  28. namespace crown
  29. {
  30. static AAssetManager* g_android_asset_manager = NULL;
  31. //-----------------------------------------------------------------------------
  32. OsFile::OsFile(const char* path, FileOpenMode mode)
  33. {
  34. // Android assets are always read-only
  35. (void) mode;
  36. m_mode = FOM_READ;
  37. m_asset = AAssetManager_open(get_android_asset_manager(), path, AASSET_MODE_RANDOM);
  38. CE_ASSERT(m_asset != NULL, "Unable to open file: %s", path);
  39. }
  40. //-----------------------------------------------------------------------------
  41. OsFile::~OsFile()
  42. {
  43. close();
  44. }
  45. //-----------------------------------------------------------------------------
  46. void OsFile::close()
  47. {
  48. if (m_asset != NULL)
  49. {
  50. AAsset_close(m_asset);
  51. m_asset = NULL;
  52. }
  53. }
  54. //-----------------------------------------------------------------------------
  55. bool OsFile::is_open() const
  56. {
  57. return m_asset != NULL;
  58. }
  59. //-----------------------------------------------------------------------------
  60. FileOpenMode OsFile::mode() const
  61. {
  62. return m_mode;
  63. }
  64. //-----------------------------------------------------------------------------
  65. size_t OsFile::size() const
  66. {
  67. return AAsset_getLength(m_asset);
  68. }
  69. //-----------------------------------------------------------------------------
  70. size_t OsFile::read(void* data, size_t size)
  71. {
  72. CE_ASSERT(data != NULL, "Data must be != NULL");
  73. return (size_t)AAsset_read(m_asset, data, size);
  74. }
  75. //-----------------------------------------------------------------------------
  76. size_t OsFile::write(const void* data, size_t size)
  77. {
  78. CE_ASSERT(data != NULL, "Data must be != NULL");
  79. os::printf("Android asset directory is read-only!");
  80. return 0;
  81. }
  82. //-----------------------------------------------------------------------------
  83. void OsFile::seek(size_t position)
  84. {
  85. off_t seek_result = AAsset_seek(m_asset, (off_t)position, SEEK_SET);
  86. CE_ASSERT(seek_result != (off_t) -1, "Failed to seek");
  87. }
  88. //-----------------------------------------------------------------------------
  89. void OsFile::seek_to_end()
  90. {
  91. off_t seek_result = AAsset_seek(m_asset, 0, SEEK_END);
  92. CE_ASSERT(seek_result != (off_t) -1, "Failed to seek");
  93. }
  94. //-----------------------------------------------------------------------------
  95. void OsFile::skip(size_t bytes)
  96. {
  97. off_t seek_result = AAsset_seek(m_asset, (off_t) bytes, SEEK_CUR);
  98. CE_ASSERT(seek_result != (off_t) -1, "Failed to seek");
  99. }
  100. //-----------------------------------------------------------------------------
  101. size_t OsFile::position() const
  102. {
  103. return (size_t) (AAsset_getLength(m_asset) - AAsset_getRemainingLength(m_asset));
  104. }
  105. //-----------------------------------------------------------------------------
  106. bool OsFile::eof() const
  107. {
  108. return AAsset_getRemainingLength(m_asset) == 0;
  109. }
  110. //-----------------------------------------------------------------------------
  111. AAssetManager* get_android_asset_manager()
  112. {
  113. return g_android_asset_manager;
  114. }
  115. //-----------------------------------------------------------------------------
  116. extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_initAssetManager(JNIEnv* env, jobject obj, jobject assetManager)
  117. {
  118. g_android_asset_manager = AAssetManager_fromJava(env, assetManager);
  119. }
  120. } // namespace crown