]> git.tdb.fi Git - libs/core.git/blob - source/io/android/asset.cpp
Use size_t to represent sizes
[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 size_t Asset::do_read(char *buf, size_t size)
38 {
39         // The function actually returns an int, despite taking size_t
40         int ret = AAsset_read(priv->asset, buf, size);
41         if(ret<0)
42                 throw runtime_error("Asset::do_read");
43         else if(ret==0)
44                 set_eof();
45
46         return ret;
47 }
48
49 SeekOffset Asset::seek(SeekOffset off, SeekType type)
50 {
51         signal_flush_required.emit();
52         off = AAsset_seek(priv->asset, off, type);
53         if(off<0)
54                 throw runtime_error("Asset::seek");
55         return off;
56 }
57
58 SeekOffset Asset::tell() const
59 {
60         SeekOffset off = AAsset_seek(priv->asset, 0, S_CUR);
61         if(off<0)
62                 throw runtime_error("Asset::tell");
63         return off;
64 }
65
66 } // namespace IO
67 } // namespace Msp