]> git.tdb.fi Git - libs/core.git/blob - source/io/android/asset.cpp
Have get_user_data_dir return a proper location on Android
[libs/core.git] / source / io / android / asset.cpp
1 #include <stdexcept>
2 #include <msp/core/application.h>
3 #include <msp/core/mainthread.h>
4 #include <android/asset_manager.h>
5 #include "asset.h"
6 #include "file.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace IO {
12
13 struct Asset::Private
14 {
15         AAsset *asset;
16 };
17
18 Asset::Asset(const string &name)
19 {
20         AAssetManager *asset_manager = reinterpret_cast<Android::MainThread *>(Application::get_data())->get_asset_manager();
21         AAsset *asset = AAssetManager_open(asset_manager, name.c_str(), AASSET_MODE_RANDOM);
22         if(!asset)
23                 throw file_not_found(name);
24
25         priv = new Private;
26         priv->asset = asset;
27
28         mode = M_READ;
29 }
30
31 Asset::~Asset()
32 {
33         AAsset_close(priv->asset);
34         delete priv;
35 }
36
37 unsigned Asset::do_read(char *buf, unsigned size)
38 {
39         int ret = AAsset_read(priv->asset, buf, size);
40         if(ret<0)
41                 throw runtime_error("Asset::do_read");
42         else if(ret==0)
43                 set_eof();
44
45         return ret;
46 }
47
48 SeekOffset Asset::seek(SeekOffset off, SeekType type)
49 {
50         signal_flush_required.emit();
51         off = AAsset_seek(priv->asset, off, type);
52         if(off<0)
53                 throw runtime_error("Asset::seek");
54         return off;
55 }
56
57 SeekOffset Asset::tell() const
58 {
59         SeekOffset off = AAsset_seek(priv->asset, 0, S_CUR);
60         if(off<0)
61                 throw runtime_error("Asset::tell");
62         return off;
63 }
64
65 } // namespace IO
66 } // namespace Msp