18static char SccsId[] =
"%W% %G%";
40#include <android/asset_manager.h>
41#include <android/native_activity.h>
45Java_pt_up_yap_yapdroid_YAPDroid_loadAssetManager(JNIEnv *env, jclass clazz, jobject assetManager);
50AAssetManager *Yap_assetManager(
void)
52 return AAssetManager_fromJava(Yap_env, Yap_aref);
56Java_pt_up_yap_yapdroid_YAPDroid_loadAssetManager(JNIEnv *env, jclass clazz, jobject assetManager) {
57 Yap_aref = (*env)->NewGlobalRef(env,assetManager);
63open_asset(
VFS_t *me,
const char *fname,
const char *io_mode,
int sno) {
69 if (strchr(io_mode,
'B')) {
70 mode = AASSET_MODE_BUFFER;
72 mode = AASSET_MODE_UNKNOWN;
74 GLOBAL_Stream[sno].name = Yap_LookupAtom(fname);
75 fname += strlen(me->
prefix)+1;
80 am = AAssetManager_open(Yap_assetManager(), fname, AASSET_MODE_UNKNOWN);
83 __android_log_print(ANDROID_LOG_INFO,
"YAPDroid",
"open %s <%s>", fname, io_mode);
92 __android_log_print(ANDROID_LOG_INFO,
"YAPDroid",
"failed %s <%s>", fname, io_mode);
97 off64_t sz = AAsset_getLength64(am), sz0 = 0;
100 if ((buf = AAsset_getBuffer(am))) {
102 char *bf = malloc(sz);
103 memmove(bf, buf, sz);
104 bool rc = Yap_set_stream_to_buf(st, bf, sz);
105 if (rc) AAsset_close(am);
108 st->status = InMemory_Stream_f|Seekable_Stream_f|Input_Stream_f;
110 }
else if ((fd = AAsset_openFileDescriptor64(am, &sz0, &sz)) >= 0) {
112 st->file = fdopen(fd,
"r");
115 st->status = Seekable_Stream_f|Input_Stream_f;
119 GLOBAL_Stream[sno].vfs_handle = am;
121 st->status = Input_Stream_f;
127close_asset(
int sno) {
128 AAsset_close(GLOBAL_Stream[sno].vfs_handle);
132static int64_t seek64(
int sno, int64_t offset,
int whence) {
133 return AAsset_seek64(GLOBAL_Stream[sno].vfs_handle, offset, whence);
136static int getc_asset(
int sno) {
138 if (AAsset_read(GLOBAL_Stream[sno].vfs_handle, &ch, 1))
144static void *opendir_a(
VFS_t *me,
const char *dirName) {
145 dirName += strlen(me->
prefix) + 1;
146 return (
void *) AAssetManager_openDir(Yap_assetManager(), dirName);
149static const char *readdir_a(
void *dirHandle) {
150 return AAssetDir_getNextFileName((AAssetDir *) dirHandle);
153static bool closedir_a(
void *dirHandle) {
154 AAssetDir_close((AAssetDir *) dirHandle);
159static bool stat_a(
VFS_t *me,
const char *fname,
vfs_stat *out) {
161 fname += strlen(me->
prefix) + 1;
162 if (stat(
"/assets", &bf)) {
163 out->st_mode = me ->
vflags ;
164 out->st_dev = bf.st_dev;
165 out->st_uid = bf.st_uid;
166 out->st_gid = bf.st_gid;
167 memmove(&out->st_atimespec, (
const void *) &bf.st_atim,
sizeof(
struct timespec));
168 memmove(&out->st_mtimespec, (
const void *) &bf.st_mtim,
sizeof(
struct timespec));
169 memmove(&out->st_ctimespec, (
const void *) &bf.st_ctim,
sizeof(
struct timespec));
170 memmove(&out->st_birthtimespec, (
const void *) &bf.st_ctim,
171 sizeof(
struct timespec));
173 AAsset *a = AAssetManager_open(Yap_assetManager(), fname, AASSET_MODE_UppppppppNKNOWN);
177 out->st_size = AAsset_getLength64(a);
184bool is_dir_a(
VFS_t *me,
const char *dirName) {
185 dirName += strlen(me->
prefix)+1;
186 if (dirName[0] ==
'\0')
189 AAssetDir *d = AAssetManager_openDir(Yap_assetManager(), dirName);
190 if (d == NULL || AAssetDir_getNextFileName(d) == NULL)
192 (AAssetDir_close(d));
199bool exists_a(
VFS_t *me,
const char *dirName) {
200 dirName += strlen(me->
prefix) + 1;
202 AAsset *d = AAssetManager_open(Yap_assetManager(), dirName, AASSET_MODE_UNKNOWN);
213static bool set_cwd(
VFS_t *me,
const char *dirName) {
219 if (!is_dir_a(me,dirName))
220 dirName = dirname(dirName);
221 GLOBAL_cwd = malloc(strlen(dirName)+1);
222 strcpy(GLOBAL_cwd, dirName);
236 me = (
VFS_t *) Yap_AllocCodeSpace(
sizeof(
struct vfs));
237 me->name =
"/assets";
238 me->
vflags = VFS_CAN_EXEC | VFS_CAN_SEEK | VFS_CAN_READ |
242 me->
open = open_asset;
243 me->
close = close_asset;
251 me->
isdir = is_dir_a;
254 me->
enc = ENC_ISO_UTF8;
259 me->next = GLOBAL_VFS;
VFS_t * Yap_InitAssetManager(void)
void * vfs_handle
stream belongs to a space
struct vfs * vfs
function the stream uses for reading a character
void *(* open)(struct vfs *, const char *fname, const char *io_mode, int sno)
operations
void *(* opendir)(struct vfs *, const char *s)
jump around the stream
bool(* stat)(struct vfs *, const char *s, vfs_stat *)
close access a directory object
int64_t(* seek)(int sno, int64_t offset, int whence)
flush a stream
bool(* close)(int sno)
open an object
YAP_Term(* parsers)(int sno)
default file encoded
const char * prefix
the main flags describing the operation of the Fs
bool(* exists)(struct vfs *, const char *s)
verify whether is directory
int(* put_char)(int sno, int ch)
unget an octet from the stream
bool(* chdir)(struct vfs *, const char *s)
verify whether a file exists
const char *(* nextdir)(void *d)
open a directory object, if one exists
int(* get_char)(int sno)
close the object
uintptr_t vflags
A text that explains the file system.
bool(* isdir)(struct vfs *, const char *s)
obtain size, age, permissions of a file
bool(* closedir)(void *d)
walk to the next entry in a directory object
encoding_t enc
set working directory (may be virtual)