Files
android_translation_layer/src/libandroid/asset_manager.c

128 lines
2.9 KiB
C
Raw Normal View History

2024-03-05 17:07:21 +01:00
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <androidfw/androidfw_c_api.h>
2022-12-27 17:22:49 +01:00
#include "../api-impl-jni/defines.h"
#include "../api-impl-jni/util.h"
2024-03-05 17:07:21 +01:00
struct AAssetDir {
struct AssetDir *asset_dir;
size_t curr_index;
2024-03-05 17:07:21 +01:00
};
#define ASSET_DIR "assets/"
int AAsset_openFileDescriptor(struct Asset *asset, off_t *out_start, off_t *out_length)
{
return Asset_openFileDescriptor(asset, out_start, out_length);
}
struct Asset* AAssetManager_open(struct AssetManager *asset_manager, const char *file_name, int mode)
{
char *path = malloc(strlen(ASSET_DIR) + strlen(file_name) + 1);
sprintf(path, "%s%s", ASSET_DIR, file_name);
printf("AAssetManager_open called for %s\n", file_name);
struct Asset *asset = AssetManager_openNonAsset(asset_manager, path, mode);
free(path);
return asset;
}
const void * AAsset_getBuffer(struct Asset *asset)
2024-03-05 17:07:21 +01:00
{
return Asset_getBuffer(asset, false);
2024-03-05 17:07:21 +01:00
}
off64_t AAsset_getLength64(struct Asset *asset)
2024-03-05 17:07:21 +01:00
{
return Asset_getLength(asset);
}
2024-03-05 17:07:21 +01:00
off_t AAsset_getLength(struct Asset *asset)
{
return Asset_getLength(asset);
}
2024-03-05 17:07:21 +01:00
int AAsset_read(struct Asset *asset, void *buf, size_t count) {
return Asset_read(asset, buf, count);
}
2024-03-05 17:07:21 +01:00
off_t AAsset_seek(struct Asset *asset, off_t offset, int whence) {
return Asset_seek(asset, offset, whence);
2024-03-05 17:07:21 +01:00
}
off64_t AAsset_seek64(struct Asset *asset, off64_t offset, int whence) {
return Asset_seek(asset, offset, whence);
}
2024-03-05 17:07:21 +01:00
off_t AAsset_getRemainingLength(struct Asset *asset)
2024-03-05 17:07:21 +01:00
{
return Asset_getRemainingLength(asset);
2024-03-05 17:07:21 +01:00
}
off64_t AAsset_getRemainingLength64(struct Asset *asset)
{
return Asset_getRemainingLength(asset);
}
void AAsset_close(struct Asset *asset)
{
Asset_delete(asset);
}
struct AAssetDir * AAssetManager_openDir(struct AssetManager *asset_manager, const char *dirname)
{
char* dirpath = malloc(strlen(ASSET_DIR) + strlen(dirname) + 1);
sprintf(dirpath, "%s%s", ASSET_DIR, dirname);
struct AssetDir *asset_dir = AssetManager_openDir(asset_manager, dirname);
struct AAssetDir* dir = malloc(sizeof(struct AAssetDir));
dir->asset_dir = asset_dir;
dir->curr_index = 0;
printf("AAssetManager_openDir called for %s\n", dirpath);
return dir;
2022-11-04 21:18:44 +05:00
}
const char * AAssetDir_getNextFileName(struct AAssetDir *dir)
{
size_t index = dir->curr_index;
const size_t max = AssetDir_getFileCount(dir->asset_dir);
/* skip non-regular files */
while ((index < max) && AssetDir_getFileType(dir->asset_dir, index) != FILE_TYPE_REGULAR)
index++;
2022-11-04 21:18:44 +05:00
if (index >= max) {
dir->curr_index = index;
return NULL;
}
2022-11-02 16:40:15 +05:00
dir->curr_index = index + 1;
return AssetDir_getFileName(dir->asset_dir, index);
2022-11-04 21:18:44 +05:00
}
void AAssetDir_close(struct AAssetDir *dir)
2022-11-04 21:18:44 +05:00
{
AssetDir_delete(dir->asset_dir);
free(dir);
2022-11-02 16:40:15 +05:00
}
struct AssetManager * AAssetManager_fromJava(JNIEnv *env, jobject asset_manager)
{
return _PTR(_GET_LONG_FIELD(asset_manager, "mObject"));
}