apk_file.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "config.h"
  6. #if CROWN_PLATFORM_ANDROID
  7. #include "apk_file.h"
  8. #include "error.h"
  9. #include "macros.h"
  10. #include <stdio.h> // SEEK_SET, ...
  11. #include <android/asset_manager.h>
  12. namespace crown
  13. {
  14. ApkFile::ApkFile(AAssetManager* asset_manager, const char* path)
  15. : File(FOM_READ)
  16. , _asset(NULL)
  17. {
  18. _asset = AAssetManager_open(asset_manager, path, AASSET_MODE_RANDOM);
  19. CE_ASSERT(_asset != NULL, "AAssetManager_open: failed to open %s", path);
  20. }
  21. ApkFile::~ApkFile()
  22. {
  23. if (_asset != NULL)
  24. {
  25. AAsset_close(_asset);
  26. _asset = NULL;
  27. }
  28. }
  29. void ApkFile::seek(size_t position)
  30. {
  31. off_t seek_result = AAsset_seek(_asset, (off_t)position, SEEK_SET);
  32. CE_ASSERT(seek_result != (off_t) -1, "AAsset_seek: error");
  33. CE_UNUSED(seek_result);
  34. }
  35. void ApkFile::seek_to_end()
  36. {
  37. off_t seek_result = AAsset_seek(_asset, 0, SEEK_END);
  38. CE_ASSERT(seek_result != (off_t) -1, "AAsset_seek: error");
  39. CE_UNUSED(seek_result);
  40. }
  41. void ApkFile::skip(size_t bytes)
  42. {
  43. off_t seek_result = AAsset_seek(_asset, (off_t) bytes, SEEK_CUR);
  44. CE_ASSERT(seek_result != (off_t) -1, "AAsset_seek: error");
  45. CE_UNUSED(seek_result);
  46. }
  47. void ApkFile::read(void* buffer, size_t size)
  48. {
  49. CE_ASSERT_NOT_NULL(buffer);
  50. size_t bytes_read = (size_t) AAsset_read(_asset, buffer, size);
  51. CE_ASSERT(bytes_read == size, "AAsset_read: requested: %lu, read: %lu", size, bytes_read);
  52. CE_UNUSED(bytes_read);
  53. }
  54. void ApkFile::write(const void* /*buffer*/, size_t /*size*/)
  55. {
  56. CE_ASSERT(false, "Apk files are read only!");
  57. }
  58. bool ApkFile::copy_to(File& /*file*/, size_t /*size = 0*/)
  59. {
  60. CE_ASSERT(false, "Not implemented");
  61. return false;
  62. }
  63. void ApkFile::flush()
  64. {
  65. // Not needed
  66. }
  67. bool ApkFile::is_valid()
  68. {
  69. return _asset != NULL;
  70. }
  71. bool ApkFile::end_of_file()
  72. {
  73. return AAsset_getRemainingLength(_asset) == 0;
  74. }
  75. size_t ApkFile::size()
  76. {
  77. return AAsset_getLength(_asset);
  78. }
  79. size_t ApkFile::position()
  80. {
  81. return (size_t) (AAsset_getLength(_asset) - AAsset_getRemainingLength(_asset));
  82. }
  83. bool ApkFile::can_read() const
  84. {
  85. return true;
  86. }
  87. bool ApkFile::can_write() const
  88. {
  89. return false;
  90. }
  91. bool ApkFile::can_seek() const
  92. {
  93. return true;
  94. }
  95. } // namespace crown
  96. #endif // CROWN_PLATFORM_ANDROID