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/include/VFS.h

131 lines
4.3 KiB
C
Raw Normal View History

2016-07-31 15:55:50 +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: VFS.h *
* Last rev: 5/2/88 *
* mods: *
* comments: Virtual File System Access for YAP *
* *
*************************************************************************/
#ifndef VFS_H
#define VFS_H 1
#include <string.h>
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef _WIN32
#include <stdint.h>
#ifndef uid_t
#define uid_t int
#endif
#ifndef gid_t
#define gid_t int
#endif
2016-07-31 15:55:50 +01:00
#endif
typedef struct {
dev_t st_dev; /* ID of device containing file */
mode_t st_mode; /* Mode of file (see below) */
uid_t st_uid; /* User ID of the file */
gid_t st_gid; /* Group ID of the file */
struct timespec st_atimespec; /* time of last access */
struct timespec st_mtimespec; /* time of last data modification */
struct timespec st_ctimespec; /* time of last status change */
struct timespec st_birthtimespec; /* time of file creation(birth) */
#if __ANDROID__
off64_t st_size; /* file size, in bytes */
#else
off_t st_size; /* file size, in bytes */
#endif
} vfs_stat;
typedef enum vfs_flags {
VFS_CAN_WRITE = 0x1, /// we can write to files in this space
VFS_CAN_EXEC = 0x2, /// we can execute files in this space
VFS_CAN_SEEK = 0x4, /// we can seek within files in this space
VFS_HAS_PREFIX = 0x8, /// has a prefix that identifies a file in this space
VFS_HAS_SUFFIX = 0x10, /// has a suffix that describes the file.
2017-05-27 22:54:00 +01:00
VFS_HAS_FUNCTION = 0x20 /// has a suffix that describes the file.
2016-07-31 15:55:50 +01:00
} vfs_flags_t;
typedef union {
struct vfs *vfs;
uintptr_t cell;
size_t sz;
void *pt;
uintptr_t scalar;
#if __ANDROID__
AAssetManager *mgr;
AAsset *asset;
#endif
} cell_size_t;
typedef struct vfs {
const char *name; /// A text that explains the file system
uintptr_t vflags; /// the main flags describing the operation of the Fs.
/// a way to identify a file in this VFS: two special cases, prefix and suffix
const char *prefix;
const char *suffix;
bool (*id)(struct vfs *me, const char *s);
/** operations */
2017-05-27 22:54:00 +01:00
void *(*open)(const char *s,
const char *io_mode); /// open an object
2016-07-31 15:55:50 +01:00
/// in this space, usual w,r,a,b flags plus B (store in a buffer)
2017-05-27 22:54:00 +01:00
bool (*close)(int sno); /// close the object
int (*get_char)(int sno); /// get an octet to the stream
int (*put_char)(int sno, wchar_t ch); /// output an octet to the stream
void (*flush)(int sno); /// flush a stream
int64_t (*seek)(int sno, int64_t offset,
2016-07-31 15:55:50 +01:00
int whence); /// jump around the stream
2017-05-27 22:54:00 +01:00
void *(*opendir)(const char *s); /// open a directory object, if one exists
const char *(*nextdir)(void *d); /// walk to the next entry in a directory object
2016-07-31 15:55:50 +01:00
void (*closedir)(void *d);
; /// close access a directory object
2017-05-27 22:54:00 +01:00
bool (*stat)(const char *s,
2016-07-31 15:55:50 +01:00
vfs_stat *); /// obtain size, age, permissions of a file.
2017-05-27 22:54:00 +01:00
bool (*isdir)(const char *s); /// verify whether is directory.
bool (*exists)(const char *s); /// verify whether a file exists.
bool (*chdir)(const char *s); /// set working directory (may be virtual).
encoding_t enc; /// default file encoded.
YAP_Term (*parsers)(int sno); // a set of parsers that can read the
2016-07-31 15:55:50 +01:00
// stream and generate a YAP_Term
2017-05-27 22:54:00 +01:00
int (*writers)(int ch, int sno );
/// convert a YAP_Term into this space
2016-07-31 15:55:50 +01:00
const char *virtual_cwd;
/** VFS dep
endent area */
cell_size_t priv[4];
struct vfs *next;
} VFS_t;
extern VFS_t *GLOBAL_VFS;
static inline VFS_t *vfs_owner(const char *fname) {
VFS_t *me = GLOBAL_VFS;
int d;
2017-05-27 22:54:00 +01:00
size_t sz0 = strlen(fname), sz;
2016-07-31 15:55:50 +01:00
while (me) {
2017-05-27 22:54:00 +01:00
if ((me->vflags & VFS_HAS_PREFIX) && strstr(fname, me->prefix) == fname)
2016-07-31 15:55:50 +01:00
return me;
2017-05-27 22:54:00 +01:00
if (me->vflags & VFS_HAS_SUFFIX && (sz = strlen(me->suffix)) && (d = (sz0 - sz)) >= 0 &&
2016-07-31 15:55:50 +01:00
strcmp(fname + d, me->suffix) == 0)
return me;
if (me->vflags & VFS_HAS_FUNCTION && (me->id(me, fname))) {
return me;
}
me = me->next;
}
return NULL;
}
#endif