apk_file.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "config.h"
  24. #if CROWN_PLATFORM_ANDROID
  25. #include "apk_file.h"
  26. #include "assert.h"
  27. #include "macros.h"
  28. #include <android/asset_manager.h>
  29. namespace crown
  30. {
  31. ApkFile::ApkFile(AAssetManager* asset_manager, const char* path)
  32. : File(FOM_READ)
  33. , _asset(NULL)
  34. {
  35. _asset = AAssetManager_open(asset_manager, path, AASSET_MODE_RANDOM);
  36. CE_ASSERT(_asset != NULL, "AAssetManager_open: failed to open %s", path);
  37. }
  38. ApkFile::~ApkFile()
  39. {
  40. if (_asset != NULL)
  41. {
  42. AAsset_close(_asset);
  43. _asset = NULL;
  44. }
  45. }
  46. void ApkFile::seek(size_t position)
  47. {
  48. off_t seek_result = AAsset_seek(_asset, (off_t)position, SEEK_SET);
  49. CE_ASSERT(seek_result != (off_t) -1, "AAsset_seek: error");
  50. CE_UNUSED(seek_result);
  51. }
  52. void ApkFile::seek_to_end()
  53. {
  54. off_t seek_result = AAsset_seek(_asset, 0, SEEK_END);
  55. CE_ASSERT(seek_result != (off_t) -1, "AAsset_seek: error");
  56. CE_UNUSED(seek_result);
  57. }
  58. void ApkFile::skip(size_t bytes)
  59. {
  60. off_t seek_result = AAsset_seek(_asset, (off_t) bytes, SEEK_CUR);
  61. CE_ASSERT(seek_result != (off_t) -1, "AAsset_seek: error");
  62. CE_UNUSED(seek_result);
  63. }
  64. void ApkFile::read(void* buffer, size_t size)
  65. {
  66. CE_ASSERT_NOT_NULL(buffer);
  67. size_t bytes_read = (size_t) AAsset_read(_asset, buffer, size);
  68. CE_ASSERT(bytes_read == size, "AAsset_read: requested: %lu, read: %lu", size, bytes_read);
  69. CE_UNUSED(bytes_read);
  70. }
  71. void ApkFile::write(const void* /*buffer*/, size_t /*size*/)
  72. {
  73. CE_ASSERT(false, "Apk files are read only!");
  74. }
  75. bool ApkFile::copy_to(File& /*file*/, size_t /*size = 0*/)
  76. {
  77. CE_ASSERT(false, "Not implemented");
  78. return false;
  79. }
  80. void ApkFile::flush()
  81. {
  82. // Not needed
  83. }
  84. bool ApkFile::is_valid()
  85. {
  86. return _asset != NULL;
  87. }
  88. bool ApkFile::end_of_file()
  89. {
  90. return AAsset_getRemainingLength(_asset) == 0;
  91. }
  92. size_t ApkFile::size()
  93. {
  94. return AAsset_getLength(_asset);
  95. }
  96. size_t ApkFile::position()
  97. {
  98. return (size_t) (AAsset_getLength(_asset) - AAsset_getRemainingLength(_asset));
  99. }
  100. bool ApkFile::can_read() const
  101. {
  102. return true;
  103. }
  104. bool ApkFile::can_write() const
  105. {
  106. return false;
  107. }
  108. bool ApkFile::can_seek() const
  109. {
  110. return true;
  111. }
  112. } // namespace crown
  113. #endif // CROWN_PLATFORM_ANDROID