This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/os/assets.c

235 lines
6.4 KiB
C
Raw Normal View History

2016-07-31 16:28:05 +01:00
/*************************************************************************
* *
* YAP Prolog *
* *
* Yap Prolog was developed at NCCUP - Universidade do Porto *
* *
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
* *
**************************************************************************
* *
* File: assets.c *
* Last rev: 5/2/88 *
* mods: *
* comments: Asset Support in ANDROID *
* *
*************************************************************************/
#ifdef SCCSA
static char SccsId[] = "%W% %G%";
#endif
/**
* @file assets.c
* @author VITOR SANTOS COSTA <vsc@VITORs-MBP.lan>
* @date Thu Nov 19 10:53:20 2015
*
* @brief File Aliases
*
*
*/
#include <stdbool.h>
#include "sysbits.h"
// for native asset manager
#include <sys/types.h>
2017-11-21 15:44:43 +00:00
#if __ANDROID__
#include <android/asset_manager.h>
#include <android/native_activity.h>
2016-07-31 16:28:05 +01:00
2017-11-21 15:44:43 +00:00
AAssetManager * Yap_assetManager;
2016-07-31 16:28:05 +01:00
2017-11-27 13:36:19 +00:00
jboolean Java_pt_up_yap_app_YAPDroid_setAssetManager(JNIEnv* env, jclass clazz, jobject assetManager)
2017-11-21 15:44:43 +00:00
{
Yap_assetManager = AAssetManager_fromJava(env, assetManager);
2017-11-27 13:36:19 +00:00
return true;
2016-07-31 16:28:05 +01:00
}
2017-11-21 15:44:43 +00:00
static void *
2017-11-27 13:36:19 +00:00
open_asset__( int sno, const char *fname, const char *io_mode)
2016-07-31 16:28:05 +01:00
{
int mode;
const void *buf;
2017-11-21 15:44:43 +00:00
VFS_t *me = GLOBAL_Stream[sno].vfs;
2016-07-31 16:28:05 +01:00
if (strstr(fname,"/assets") == fname) {
2017-11-21 15:44:43 +00:00
// we're in
if ( Yap_assetManager == NULL) {
2017-11-27 13:36:19 +00:00
PlIOError(PERMISSION_ERROR_OPEN_SOURCE_SINK, TermNil,
2017-11-21 15:44:43 +00:00
"asset manager",
fname);
2017-11-27 13:36:19 +00:00
return NULL;
2016-07-31 16:28:05 +01:00
}
2017-11-21 15:44:43 +00:00
if (strchr(io_mode, 'w') || strchr(io_mode, 'a')) {
2017-11-27 13:36:19 +00:00
PlIOError(PERMISSION_ERROR_OPEN_SOURCE_SINK, TermNil,
2017-11-21 15:44:43 +00:00
"%s: no writing but flags are %s",
fname, io_mode);
2017-11-27 13:36:19 +00:00
return NULL;
2017-11-21 15:44:43 +00:00
}
if (strchr(io_mode, 'B'))
mode = AASSET_MODE_BUFFER;
else {
mode = AASSET_MODE_UNKNOWN;
}
AAsset *a = AAssetManager_open( Yap_assetManager, fname, mode);
// try not to use it as an asset
off64_t sz = AAsset_getLength64(a), sz0 = 0;
int fd;
StreamDesc *st = GLOBAL_Stream+sno;
if ((fd = AAsset_openFileDescriptor64(a, &sz0, &sz)) >= 0) {
// can use it as red-only file
st->file = fdopen(fd, "r");
st->vfs = me;
st->vfs_handle = a;
return a;
} else if ((buf = AAsset_getBuffer(a))) {
// copy to memory
bool rc = Yap_set_stream_to_buf(st, buf, sz);
2017-11-27 13:36:19 +00:00
if (rc) AAsset_close(a);
return st;
2017-11-21 15:44:43 +00:00
}
// should be done, but if not
GLOBAL_Stream[sno].vfs_handle= a;
2016-07-31 16:28:05 +01:00
st->vfs = me;
2017-11-27 13:36:19 +00:00
return a;
2016-07-31 16:28:05 +01:00
}
return NULL;
}
static bool
2017-11-21 15:44:43 +00:00
close_asset(int sno)
2016-07-31 16:28:05 +01:00
{
2017-11-21 15:44:43 +00:00
AAsset_close(GLOBAL_Stream[sno].vfs_handle);
2016-07-31 16:28:05 +01:00
return true;
}
2017-11-21 15:44:43 +00:00
static int64_t seek64(int sno, int64_t offset, int whence)
2016-07-31 16:28:05 +01:00
{
2017-11-21 15:44:43 +00:00
return AAsset_seek64(GLOBAL_Stream[sno].vfs_handle, offset, whence);
2016-07-31 16:28:05 +01:00
}
2017-11-21 15:44:43 +00:00
static int getc_asset(int sno)
2016-07-31 16:28:05 +01:00
{
int ch;
2017-11-21 15:44:43 +00:00
if ( AAsset_read (GLOBAL_Stream[sno].vfs_handle, &ch, 1) )
2016-07-31 16:28:05 +01:00
return ch;
return -1;
}
2017-11-21 15:44:43 +00:00
static void *opendir_a( VFS_t *me, const char *dirName)
2016-07-31 16:28:05 +01:00
{
2017-11-21 15:44:43 +00:00
return (void *)AAssetManager_openDir (Yap_assetManager, dirName);
2016-07-31 16:28:05 +01:00
}
static const char *readdir_a(void *dirHandle)
{
return AAssetDir_getNextFileName ((AAssetDir *)dirHandle);
}
static bool closedir_a(void *dirHandle)
{
AAssetDir_close ((AAssetDir *)dirHandle);
return true;
}
2017-11-21 15:44:43 +00:00
static bool stat_a(VFS_t *me, const char *fname, vfs_stat *out)
2016-07-31 16:28:05 +01:00
{
2017-11-21 15:44:43 +00:00
struct stat bf;
if (stat( "/assets", &bf)) {
2016-07-31 16:28:05 +01:00
out->st_dev = bf.st_dev;
out->st_uid = bf.st_uid;
out->st_gid = bf.st_gid;
memcpy(&out->st_atimespec, (const void *)&out->st_atimespec, sizeof(struct timespec));
memcpy(&out->st_mtimespec,(const void *) &out->st_mtimespec, sizeof(struct timespec));
memcpy(&out->st_ctimespec, (const void *)&out->st_ctimespec, sizeof(struct timespec));
memcpy(&out->st_birthtimespec, (const void *)&out->st_birthtimespec, sizeof(struct timespec));
}
2017-11-21 15:44:43 +00:00
AAsset *a = AAssetManager_open( Yap_assetManager , fname, AASSET_MODE_UNKNOWN);
2016-07-31 16:28:05 +01:00
// try not to use it as an asset
out->st_size = AAsset_getLength64(a);
AAsset_close(a);
return true;
}
static
2017-11-21 15:44:43 +00:00
bool is_dir_a( VFS_t *me,const char *dirName)
2016-07-31 16:28:05 +01:00
{
bool rc;
// try not to use it as an asset
2017-11-21 15:44:43 +00:00
AAssetDir *d = AAssetManager_openDir ( Yap_assetManager, dirName);
2016-07-31 16:28:05 +01:00
if (d == NULL)
return false;
rc = (AAssetDir_getNextFileName(d) != NULL);
AAssetDir_close(d);
return rc;
}
static
2017-11-27 13:36:19 +00:00
VFS_t *exists_a(VFS_t *me, const char *dirName)
2016-07-31 16:28:05 +01:00
{
// try not to use it as an asset
2017-11-21 15:44:43 +00:00
AAsset *d = AAssetManager_open ( Yap_assetManager, dirName, AASSET_MODE_UNKNOWN);
2016-07-31 16:28:05 +01:00
if (d == NULL)
2017-11-27 13:36:19 +00:00
return NULL;
2016-07-31 16:28:05 +01:00
AAsset_close(d);
2017-11-27 13:36:19 +00:00
return me;
2016-07-31 16:28:05 +01:00
}
2017-11-21 15:44:43 +00:00
static bool set_cwd (VFS_t *me, const char *dirName) {
2016-07-31 16:28:05 +01:00
chdir("/assets");
if (me->virtual_cwd)
2017-11-21 15:44:43 +00:00
free((void*)(me->virtual_cwd));
2016-07-31 16:28:05 +01:00
me->virtual_cwd = malloc( sizeof(dirName) + 1 );
return me!= NULL;
}
#endif
VFS_t *
2017-11-27 13:36:19 +00:00
Yap_InitAssetManager( void )
2016-07-31 16:28:05 +01:00
{
2017-11-21 15:44:43 +00:00
#if __ANDROID__
2016-07-31 16:28:05 +01:00
VFS_t *me;
2017-11-27 13:36:19 +00:00
2016-07-31 16:28:05 +01:00
/* init standard VFS */
me = (VFS_t *)Yap_AllocCodeSpace(sizeof(struct vfs));
me->name = "/assets";
me->vflags = VFS_CAN_EXEC|VFS_CAN_SEEK|VFS_HAS_PREFIX; /// the main flags describing the operation of the Fs.
me->prefix = "/assets";
/** operations */
2017-11-21 15:44:43 +00:00
me->open = open_asset__; /// open an object in this space
2016-07-31 16:28:05 +01:00
me->close= close_asset; /// close the object
me->get_char = getc_asset; /// get an octet to the stream
2017-11-21 15:44:43 +00:00
me->put_char = NULL; /// output an octet to the stream
2016-07-31 16:28:05 +01:00
me->seek = seek64; /// jump around the stream
me->opendir = opendir_a; /// open a directory object, if one exists
me->nextdir = readdir_a; /// open a directory object, if one exists
me->closedir = closedir_a; /// close access a directory object
me->stat = stat_a; /// obtain size, age, permissions of a file.
me->isdir = is_dir_a; /// obtain size, age, permissions of a file.
me->exists = exists_a; /// obtain size, age, permissions of a file.
me->chdir = set_cwd; /// chnage working directory.
me->enc = ENC_ISO_UTF8; /// how the file is encoded.
me->parsers = NULL; /// a set of parsers that can read the stream and generate a term
me->writers = NULL;
LOCK(BGL);
me-> next = GLOBAL_VFS;
GLOBAL_VFS = me;
return me;
UNLOCK(BGL);
return me;
#else
return NULL;
#endif
}