Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 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 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 17 | /** |
| 18 | * @addtogroup Asset |
| 19 | * @{ |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * @file asset_manager.h |
| 24 | */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 25 | |
| 26 | #ifndef ANDROID_ASSET_MANAGER_H |
| 27 | #define ANDROID_ASSET_MANAGER_H |
| 28 | |
Dan Albert | 494ed55 | 2016-09-23 15:57:45 -0700 | [diff] [blame^] | 29 | #include <sys/cdefs.h> |
| 30 | |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 31 | #ifdef __cplusplus |
| 32 | extern "C" { |
| 33 | #endif |
| 34 | |
| 35 | struct AAssetManager; |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 36 | /** |
| 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 Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 49 | typedef struct AAssetManager AAssetManager; |
| 50 | |
| 51 | struct AAssetDir; |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 52 | /** |
| 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 Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 59 | typedef struct AAssetDir AAssetDir; |
| 60 | |
| 61 | struct AAsset; |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 62 | /** |
| 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 Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 68 | typedef struct AAsset AAsset; |
| 69 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 70 | /** Available access modes for opening assets with {@link AAssetManager_open} */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 71 | enum { |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 72 | /** No specific information about how data will be accessed. **/ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 73 | AASSET_MODE_UNKNOWN = 0, |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 74 | /** Read chunks, and seek forward and backward. */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 75 | AASSET_MODE_RANDOM = 1, |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 76 | /** Read sequentially, with an occasional forward seek. */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 77 | AASSET_MODE_STREAMING = 2, |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 78 | /** Caller plans to ask for a read-only buffer with all data. */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 79 | 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 | */ |
| 90 | AAssetDir* 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 | */ |
| 97 | AAsset* 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 | */ |
| 109 | const char* AAssetDir_getNextFileName(AAssetDir* assetDir); |
| 110 | |
| 111 | /** |
| 112 | * Reset the iteration state of AAssetDir_getNextFileName() to the beginning. |
| 113 | */ |
| 114 | void AAssetDir_rewind(AAssetDir* assetDir); |
| 115 | |
| 116 | /** |
| 117 | * Close an opened AAssetDir, freeing any related resources. |
| 118 | */ |
| 119 | void 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 | */ |
| 126 | int 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 | */ |
| 134 | off_t AAsset_seek(AAsset* asset, off_t offset, int whence); |
| 135 | |
Dan Albert | 494ed55 | 2016-09-23 15:57:45 -0700 | [diff] [blame^] | 136 | #if __ANDROID_API__ >= 13 |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 137 | /** |
| 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 | */ |
| 146 | off64_t AAsset_seek64(AAsset* asset, off64_t offset, int whence); |
Dan Albert | 494ed55 | 2016-09-23 15:57:45 -0700 | [diff] [blame^] | 147 | #endif |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * Close the asset, freeing all associated resources. |
| 151 | */ |
| 152 | void 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 | */ |
| 159 | const void* AAsset_getBuffer(AAsset* asset); |
| 160 | |
| 161 | /** |
| 162 | * Report the total size of the asset data. |
| 163 | */ |
| 164 | off_t AAsset_getLength(AAsset* asset); |
| 165 | |
Dan Albert | 494ed55 | 2016-09-23 15:57:45 -0700 | [diff] [blame^] | 166 | #if __ANDROID_API__ >= 13 |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 167 | /** |
| 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 | */ |
| 171 | off64_t AAsset_getLength64(AAsset* asset); |
Dan Albert | 494ed55 | 2016-09-23 15:57:45 -0700 | [diff] [blame^] | 172 | #endif |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 173 | |
| 174 | /** |
| 175 | * Report the total amount of asset data that can be read from the current position. |
| 176 | */ |
| 177 | off_t AAsset_getRemainingLength(AAsset* asset); |
| 178 | |
Dan Albert | 494ed55 | 2016-09-23 15:57:45 -0700 | [diff] [blame^] | 179 | #if __ANDROID_API__ >= 13 |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 180 | /** |
| 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 | */ |
| 185 | off64_t AAsset_getRemainingLength64(AAsset* asset); |
Dan Albert | 494ed55 | 2016-09-23 15:57:45 -0700 | [diff] [blame^] | 186 | #endif |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 187 | |
| 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 | */ |
| 196 | int AAsset_openFileDescriptor(AAsset* asset, off_t* outStart, off_t* outLength); |
| 197 | |
Dan Albert | 494ed55 | 2016-09-23 15:57:45 -0700 | [diff] [blame^] | 198 | #if __ANDROID_API__ >= 13 |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 199 | /** |
| 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 | */ |
| 208 | int AAsset_openFileDescriptor64(AAsset* asset, off64_t* outStart, off64_t* outLength); |
Dan Albert | 494ed55 | 2016-09-23 15:57:45 -0700 | [diff] [blame^] | 209 | #endif |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 210 | |
| 211 | /** |
| 212 | * Returns whether this asset's internal buffer is allocated in ordinary RAM (i.e. not |
| 213 | * mmapped). |
| 214 | */ |
| 215 | int AAsset_isAllocated(AAsset* asset); |
| 216 | |
| 217 | |
| 218 | |
| 219 | #ifdef __cplusplus |
| 220 | }; |
| 221 | #endif |
| 222 | |
| 223 | #endif // ANDROID_ASSET_MANAGER_H |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 224 | |
| 225 | /** @} */ |