blob: beb65492ec5db03ef66d1ce7e9af538c7fc03931 [file] [log] [blame]
Mathias Agopiane1c61d32012-03-23 14:19:36 -07001/*
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
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070017/**
18 * @addtogroup Asset
19 * @{
20 */
21
22/**
23 * @file asset_manager.h
24 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070025
26#ifndef ANDROID_ASSET_MANAGER_H
27#define ANDROID_ASSET_MANAGER_H
28
Dan Albert494ed552016-09-23 15:57:45 -070029#include <sys/cdefs.h>
30
Mathias Agopiane1c61d32012-03-23 14:19:36 -070031#ifdef __cplusplus
32extern "C" {
33#endif
34
35struct AAssetManager;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070036/**
37 * {@link AAssetManager} provides access to an application's raw assets by
38 * creating {@link AAsset} objects.
39 *
40 * AAssetManager is a wrapper to the low-level native implementation
41 * of the java {@link AAssetManager}, a pointer can be obtained using
42 * AAssetManager_fromJava().
43 *
44 * The asset hierarchy may be examined like a filesystem, using
45 * {@link AAssetDir} objects to peruse a single directory.
46 *
47 * A native {@link AAssetManager} pointer may be shared across multiple threads.
48 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070049typedef struct AAssetManager AAssetManager;
50
51struct AAssetDir;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070052/**
53 * {@link AAssetDir} provides access to a chunk of the asset hierarchy as if
54 * it were a single directory. The contents are populated by the
55 * {@link AAssetManager}.
56 *
57 * The list of files will be sorted in ascending order by ASCII value.
58 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070059typedef struct AAssetDir AAssetDir;
60
61struct AAsset;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070062/**
63 * {@link AAsset} provides access to a read-only asset.
64 *
65 * {@link AAsset} objects are NOT thread-safe, and should not be shared across
66 * threads.
67 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070068typedef struct AAsset AAsset;
69
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070070/** Available access modes for opening assets with {@link AAssetManager_open} */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070071enum {
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070072 /** No specific information about how data will be accessed. **/
Mathias Agopiane1c61d32012-03-23 14:19:36 -070073 AASSET_MODE_UNKNOWN = 0,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070074 /** Read chunks, and seek forward and backward. */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070075 AASSET_MODE_RANDOM = 1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070076 /** Read sequentially, with an occasional forward seek. */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070077 AASSET_MODE_STREAMING = 2,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070078 /** Caller plans to ask for a read-only buffer with all data. */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070079 AASSET_MODE_BUFFER = 3
80};
81
82
83/**
84 * Open the named directory within the asset hierarchy. The directory can then
85 * be inspected with the AAssetDir functions. To open the top-level directory,
86 * pass in "" as the dirName.
87 *
88 * The object returned here should be freed by calling AAssetDir_close().
89 */
90AAssetDir* AAssetManager_openDir(AAssetManager* mgr, const char* dirName);
91
92/**
93 * Open an asset.
94 *
95 * The object returned here should be freed by calling AAsset_close().
96 */
97AAsset* AAssetManager_open(AAssetManager* mgr, const char* filename, int mode);
98
99/**
100 * Iterate over the files in an asset directory. A NULL string is returned
101 * when all the file names have been returned.
102 *
103 * The returned file name is suitable for passing to AAssetManager_open().
104 *
105 * The string returned here is owned by the AssetDir implementation and is not
106 * guaranteed to remain valid if any other calls are made on this AAssetDir
107 * instance.
108 */
109const char* AAssetDir_getNextFileName(AAssetDir* assetDir);
110
111/**
112 * Reset the iteration state of AAssetDir_getNextFileName() to the beginning.
113 */
114void AAssetDir_rewind(AAssetDir* assetDir);
115
116/**
117 * Close an opened AAssetDir, freeing any related resources.
118 */
119void AAssetDir_close(AAssetDir* assetDir);
120
121/**
122 * Attempt to read 'count' bytes of data from the current offset.
123 *
124 * Returns the number of bytes read, zero on EOF, or < 0 on error.
125 */
126int AAsset_read(AAsset* asset, void* buf, size_t count);
127
128/**
129 * Seek to the specified offset within the asset data. 'whence' uses the
130 * same constants as lseek()/fseek().
131 *
132 * Returns the new position on success, or (off_t) -1 on error.
133 */
134off_t AAsset_seek(AAsset* asset, off_t offset, int whence);
135
Dan Albert494ed552016-09-23 15:57:45 -0700136#if __ANDROID_API__ >= 13
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700137/**
138 * Seek to the specified offset within the asset data. 'whence' uses the
139 * same constants as lseek()/fseek().
140 *
141 * Uses 64-bit data type for large files as opposed to the 32-bit type used
142 * by AAsset_seek.
143 *
144 * Returns the new position on success, or (off64_t) -1 on error.
145 */
146off64_t AAsset_seek64(AAsset* asset, off64_t offset, int whence);
Dan Albert494ed552016-09-23 15:57:45 -0700147#endif
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700148
149/**
150 * Close the asset, freeing all associated resources.
151 */
152void AAsset_close(AAsset* asset);
153
154/**
155 * Get a pointer to a buffer holding the entire contents of the assset.
156 *
157 * Returns NULL on failure.
158 */
159const void* AAsset_getBuffer(AAsset* asset);
160
161/**
162 * Report the total size of the asset data.
163 */
164off_t AAsset_getLength(AAsset* asset);
165
Dan Albert494ed552016-09-23 15:57:45 -0700166#if __ANDROID_API__ >= 13
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700167/**
168 * Report the total size of the asset data. Reports the size using a 64-bit
169 * number insted of 32-bit as AAsset_getLength.
170 */
171off64_t AAsset_getLength64(AAsset* asset);
Dan Albert494ed552016-09-23 15:57:45 -0700172#endif
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700173
174/**
175 * Report the total amount of asset data that can be read from the current position.
176 */
177off_t AAsset_getRemainingLength(AAsset* asset);
178
Dan Albert494ed552016-09-23 15:57:45 -0700179#if __ANDROID_API__ >= 13
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700180/**
181 * Report the total amount of asset data that can be read from the current position.
182 *
183 * Uses a 64-bit number instead of a 32-bit number as AAsset_getRemainingLength does.
184 */
185off64_t AAsset_getRemainingLength64(AAsset* asset);
Dan Albert494ed552016-09-23 15:57:45 -0700186#endif
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700187
188/**
189 * Open a new file descriptor that can be used to read the asset data. If the
190 * start or length cannot be represented by a 32-bit number, it will be
191 * truncated. If the file is large, use AAsset_openFileDescriptor64 instead.
192 *
193 * Returns < 0 if direct fd access is not possible (for example, if the asset is
194 * compressed).
195 */
196int AAsset_openFileDescriptor(AAsset* asset, off_t* outStart, off_t* outLength);
197
Dan Albert494ed552016-09-23 15:57:45 -0700198#if __ANDROID_API__ >= 13
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700199/**
200 * Open a new file descriptor that can be used to read the asset data.
201 *
202 * Uses a 64-bit number for the offset and length instead of 32-bit instead of
203 * as AAsset_openFileDescriptor does.
204 *
205 * Returns < 0 if direct fd access is not possible (for example, if the asset is
206 * compressed).
207 */
208int AAsset_openFileDescriptor64(AAsset* asset, off64_t* outStart, off64_t* outLength);
Dan Albert494ed552016-09-23 15:57:45 -0700209#endif
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700210
211/**
212 * Returns whether this asset's internal buffer is allocated in ordinary RAM (i.e. not
213 * mmapped).
214 */
215int AAsset_isAllocated(AAsset* asset);
216
217
218
219#ifdef __cplusplus
220};
221#endif
222
223#endif // ANDROID_ASSET_MANAGER_H
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700224
225/** @} */