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