apk_file.cpp 2.2 KB

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