1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /**
18  * @addtogroup Asset
19  * @{
20  */
21 
22 /**
23  * @file asset_manager.h
24  */
25 
26 #ifndef ANDROID_ASSET_MANAGER_H
27 #define ANDROID_ASSET_MANAGER_H
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 struct AAssetManager;
34 /**
35  * {@link AAssetManager} provides access to an application's raw assets by
36  * creating {@link AAsset} objects.
37  *
38  * AAssetManager is a wrapper to the low-level native implementation
39  * of the java {@link AAssetManager}, a pointer can be obtained using
40  * AAssetManager_fromJava().
41  *
42  * The asset hierarchy may be examined like a filesystem, using
43  * {@link AAssetDir} objects to peruse a single directory.
44  *
45  * A native {@link AAssetManager} pointer may be shared across multiple threads.
46  */
47 typedef struct AAssetManager AAssetManager;
48 
49 struct AAssetDir;
50 /**
51  * {@link AAssetDir} provides access to a chunk of the asset hierarchy as if
52  * it were a single directory. The contents are populated by the
53  * {@link AAssetManager}.
54  *
55  * The list of files will be sorted in ascending order by ASCII value.
56  */
57 typedef struct AAssetDir AAssetDir;
58 
59 struct AAsset;
60 /**
61  * {@link AAsset} provides access to a read-only asset.
62  *
63  * {@link AAsset} objects are NOT thread-safe, and should not be shared across
64  * threads.
65  */
66 typedef struct AAsset AAsset;
67 
68 /** Available access modes for opening assets with {@link AAssetManager_open} */
69 enum {
70     /** No specific information about how data will be accessed. **/
71     AASSET_MODE_UNKNOWN      = 0,
72     /** Read chunks, and seek forward and backward. */
73     AASSET_MODE_RANDOM       = 1,
74     /** Read sequentially, with an occasional forward seek. */
75     AASSET_MODE_STREAMING    = 2,
76     /** Caller plans to ask for a read-only buffer with all data. */
77     AASSET_MODE_BUFFER       = 3
78 };
79 
80 
81 /**
82  * Open the named directory within the asset hierarchy.  The directory can then
83  * be inspected with the AAssetDir functions.  To open the top-level directory,
84  * pass in "" as the dirName.
85  *
86  * The object returned here should be freed by calling AAssetDir_close().
87  */
88 AAssetDir* AAssetManager_openDir(AAssetManager* mgr, const char* dirName);
89 
90 /**
91  * Open an asset.
92  *
93  * The object returned here should be freed by calling AAsset_close().
94  */
95 AAsset* AAssetManager_open(AAssetManager* mgr, const char* filename, int mode);
96 
97 /**
98  * Iterate over the files in an asset directory.  A NULL string is returned
99  * when all the file names have been returned.
100  *
101  * The returned file name is suitable for passing to AAssetManager_open().
102  *
103  * The string returned here is owned by the AssetDir implementation and is not
104  * guaranteed to remain valid if any other calls are made on this AAssetDir
105  * instance.
106  */
107 const char* AAssetDir_getNextFileName(AAssetDir* assetDir);
108 
109 /**
110  * Reset the iteration state of AAssetDir_getNextFileName() to the beginning.
111  */
112 void AAssetDir_rewind(AAssetDir* assetDir);
113 
114 /**
115  * Close an opened AAssetDir, freeing any related resources.
116  */
117 void AAssetDir_close(AAssetDir* assetDir);
118 
119 /**
120  * Attempt to read 'count' bytes of data from the current offset.
121  *
122  * Returns the number of bytes read, zero on EOF, or < 0 on error.
123  */
124 int AAsset_read(AAsset* asset, void* buf, size_t count);
125 
126 /**
127  * Seek to the specified offset within the asset data.  'whence' uses the
128  * same constants as lseek()/fseek().
129  *
130  * Returns the new position on success, or (off_t) -1 on error.
131  */
132 off_t AAsset_seek(AAsset* asset, off_t offset, int whence);
133 
134 /**
135  * Seek to the specified offset within the asset data.  'whence' uses the
136  * same constants as lseek()/fseek().
137  *
138  * Uses 64-bit data type for large files as opposed to the 32-bit type used
139  * by AAsset_seek.
140  *
141  * Returns the new position on success, or (off64_t) -1 on error.
142  */
143 off64_t AAsset_seek64(AAsset* asset, off64_t offset, int whence);
144 
145 /**
146  * Close the asset, freeing all associated resources.
147  */
148 void AAsset_close(AAsset* asset);
149 
150 /**
151  * Get a pointer to a buffer holding the entire contents of the assset.
152  *
153  * Returns NULL on failure.
154  */
155 const void* AAsset_getBuffer(AAsset* asset);
156 
157 /**
158  * Report the total size of the asset data.
159  */
160 off_t AAsset_getLength(AAsset* asset);
161 
162 /**
163  * Report the total size of the asset data. Reports the size using a 64-bit
164  * number insted of 32-bit as AAsset_getLength.
165  */
166 off64_t AAsset_getLength64(AAsset* asset);
167 
168 /**
169  * Report the total amount of asset data that can be read from the current position.
170  */
171 off_t AAsset_getRemainingLength(AAsset* asset);
172 
173 /**
174  * Report the total amount of asset data that can be read from the current position.
175  *
176  * Uses a 64-bit number instead of a 32-bit number as AAsset_getRemainingLength does.
177  */
178 off64_t AAsset_getRemainingLength64(AAsset* asset);
179 
180 /**
181  * Open a new file descriptor that can be used to read the asset data. If the
182  * start or length cannot be represented by a 32-bit number, it will be
183  * truncated. If the file is large, use AAsset_openFileDescriptor64 instead.
184  *
185  * Returns < 0 if direct fd access is not possible (for example, if the asset is
186  * compressed).
187  */
188 int AAsset_openFileDescriptor(AAsset* asset, off_t* outStart, off_t* outLength);
189 
190 /**
191  * Open a new file descriptor that can be used to read the asset data.
192  *
193  * Uses a 64-bit number for the offset and length instead of 32-bit instead of
194  * as AAsset_openFileDescriptor does.
195  *
196  * Returns < 0 if direct fd access is not possible (for example, if the asset is
197  * compressed).
198  */
199 int AAsset_openFileDescriptor64(AAsset* asset, off64_t* outStart, off64_t* outLength);
200 
201 /**
202  * Returns whether this asset's internal buffer is allocated in ordinary RAM (i.e. not
203  * mmapped).
204  */
205 int AAsset_isAllocated(AAsset* asset);
206 
207 
208 
209 #ifdef __cplusplus
210 };
211 #endif
212 
213 #endif      // ANDROID_ASSET_MANAGER_H
214 
215 /** @} */
216