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