APKFile.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #pragma once
  24. #include <sys/types.h>
  25. #include <android/asset_manager.h>
  26. #include "Types.h"
  27. #include "File.h"
  28. namespace crown
  29. {
  30. AAssetManager* get_android_asset_manager();
  31. /// Android assets wrapper
  32. class APKFile
  33. {
  34. public:
  35. /// Opens the file located at @a path with the given @a mode.
  36. APKFile(const char* path, FileOpenMode mode);
  37. ~APKFile();
  38. /// Closes the file.
  39. void close();
  40. bool is_open() const;
  41. /// Return the size of the file in bytes.
  42. size_t size() const;
  43. /// Returs the mode used to open the file.
  44. FileOpenMode mode() const;
  45. /// Reads @a size bytes from the file and stores it into @a data.
  46. /// Returns the number of bytes read.
  47. size_t read(void* data, size_t size);
  48. /// Writes @a size bytes of data stored in @a data and returns the
  49. /// number of bytes written.
  50. size_t write(const void* data, size_t size);
  51. /// Moves the file pointer to the given @a position.
  52. void seek(size_t position);
  53. /// Moves the file pointer to the end of the file.
  54. void seek_to_end();
  55. /// Moves the file pointer @a bytes bytes ahead the current
  56. /// file pointer position.
  57. void skip(size_t bytes);
  58. /// Returns the position of the file pointer from the
  59. /// start of the file in bytes.
  60. size_t position() const;
  61. /// Returns whether the file pointer is at the end of the file.
  62. bool eof() const;
  63. private:
  64. AAsset* m_asset;
  65. FileOpenMode m_mode;
  66. };
  67. } // namespace crown