• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2005 The Android Open Source Project
3 //
4 // Provide a wxInputStream subclass based on the Android Asset class.
5 // This is necessary because some wxWidgets functions require either a
6 // filename or a wxInputStream (e.g. wxImage).
7 //
8 #ifndef _SIM_ASSETSTREAM_H
9 #define _SIM_ASSETSTREAM_H
10 
11 #include "wx/stream.h"
12 #include <utils/Asset.h>
13 
14 /*
15  * There is no sample code or concrete documentation about providing
16  * input streams, but it seems straightforward.  The PNG loading code
17  * uses the following:
18  *  OnSysTell()
19  *  OnSysSeek()
20  *  Read()
21  *
22  * The AssetStream takes ownership of the Asset.
23  */
24 class AssetStream : public wxInputStream {
25 public:
AssetStream(android::Asset * pAsset)26     AssetStream(android::Asset* pAsset)
27         : mpAsset(pAsset)
28         {}
~AssetStream(void)29     virtual ~AssetStream(void) {
30         delete mpAsset;
31     }
32 
GetLength()33     virtual wxFileOffset GetLength() const {
34         //printf("## GetLength --> %ld\n", (long) mpAsset->getLength());
35         return mpAsset->getLength();
36     }
GetSize()37     virtual size_t GetSize() const {
38         //printf("## GetSize --> %ld\n", (long) mpAsset->getLength());
39         return mpAsset->getLength();
40     }
IsSeekable()41     virtual bool IsSeekable() const { return true; }
42 
Eof()43     virtual bool Eof() const {
44         //printf("## Eof\n");
45         return (mpAsset->seek(0, SEEK_CUR) == mpAsset->getLength());
46     }
47 
CanRead()48     virtual bool CanRead() const {
49         //printf("## CanRead\n");
50         return !Eof();
51     }
52 
Read(void * buffer,size_t size)53     virtual wxInputStream& Read(void* buffer, size_t size) {
54         OnSysRead(buffer, size);
55 
56         return *this;
57     }
58 
59 protected:
60     /* read data, return number of bytes or 0 if EOF reached */
OnSysRead(void * buffer,size_t size)61     virtual size_t OnSysRead(void* buffer, size_t size) {
62         ssize_t actual = mpAsset->read(buffer, size);
63         if (actual < 0) {
64             // TODO: flag error
65             actual = 0;
66         }
67         //printf("## OnSysRead(%p %u) --> %d\n", buffer, size, actual);
68         return actual;
69     }
70 
71     /* seek, using wxWidgets-defined values for "whence" */
OnSysSeek(wxFileOffset seek,wxSeekMode mode)72     virtual wxFileOffset OnSysSeek(wxFileOffset seek, wxSeekMode mode) {
73         int whence;
74         off_t newPosn;
75 
76         if (mode == wxFromStart)
77             whence = SEEK_SET;
78         else if (mode == wxFromEnd)
79             whence = SEEK_END;
80         else
81             whence = SEEK_CUR;
82         newPosn = mpAsset->seek(seek, whence);
83         //printf("## OnSysSeek(%ld %d) --> %ld\n",
84         //    (long) seek, mode, (long) newPosn);
85         if (newPosn == (off_t) -1)
86             return wxInvalidOffset;
87         else
88             return newPosn;
89     }
90 
OnSysTell()91     virtual wxFileOffset OnSysTell() const {
92         //printf("## OnSysTell() --> %ld\n", (long) mpAsset->seek(0, SEEK_CUR));
93         return mpAsset->seek(0, SEEK_CUR);
94     }
95 
96 private:
97     android::Asset*     mpAsset;
98 
99     DECLARE_NO_COPY_CLASS(AssetStream);     // private copy-ctor and op=
100 };
101 
102 #endif // _SIM_ASSETSTREAM_H
103