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