publish xfile API

This commit is contained in:
Yuichi Nishiwaki 2016-02-20 18:34:13 +09:00
parent d3b188e44d
commit fcb3182e08
10 changed files with 59 additions and 51 deletions

View File

@ -20,7 +20,7 @@ open_file(pic_state *pic, const char *fname, const char *mode)
if ((fp = fopen(fname, mode)) == NULL) {
file_error(pic, "could not open file...");
}
return pic_make_port(pic, xfopen_file(pic, fp, mode));
return pic_open_port(pic, xfopen_file(pic, fp, mode));
}
pic_value

View File

@ -20,7 +20,7 @@ pic_load_load(pic_state *pic)
pic_errorf(pic, "load: could not open file %s", fn);
}
port = pic_make_port(pic, xfopen_file(pic, fp, "r"));
port = pic_open_port(pic, xfopen_file(pic, fp, "r"));
pic_load(pic, port);

View File

@ -331,7 +331,7 @@ make_socket_port(pic_state *pic, struct pic_socket_t *sock, const char *mode)
fp = xfunopen(pic, sock, 0, xf_socket_write, xf_socket_seek, xf_socket_close);
}
return pic_make_port(pic, fp);
return pic_open_port(pic, fp);
}
static pic_value

View File

@ -252,10 +252,6 @@ size_t xfwrite(pic_state *pic, const void *ptr, size_t size, size_t count, xFILE
return count;
}
#define XSEEK_CUR 0
#define XSEEK_END 1
#define XSEEK_SET 2
long xfseek(pic_state *pic, xFILE *fp, long offset, int whence) {
long s;

View File

@ -107,6 +107,12 @@ pic_value pic_vcall(pic_state *, pic_value proc, int, va_list);
pic_value pic_apply(pic_state *, pic_value proc, int n, pic_value *argv);
pic_value pic_applyk(pic_state *, pic_value proc, int n, pic_value *argv);
typedef struct xFILE xFILE;
pic_value pic_open_port(pic_state *, xFILE *file);
xFILE *pic_fileno(pic_state *, pic_value port);
void pic_close_port(pic_state *, pic_value port);
PIC_INLINE int pic_int(pic_state *, pic_value i);
PIC_INLINE double pic_float(pic_state *, pic_value f);
PIC_INLINE char pic_char(pic_state *, pic_value c);
@ -255,6 +261,35 @@ int pic_str_cmp(pic_state *, pic_value str1, pic_value str2);
int pic_str_hash(pic_state *, pic_value str);
/* External I/O */
#define XSEEK_CUR 0
#define XSEEK_END 1
#define XSEEK_SET 2
xFILE *xfunopen(pic_state *, void *cookie, int (*read)(pic_state *, void *, char *, int), int (*write)(pic_state *, void *, const char *, int), long (*seek)(pic_state *, void *, long, int), int (*close)(pic_state *, void *));
size_t xfread(pic_state *, void *ptr, size_t size, size_t count, xFILE *fp);
size_t xfwrite(pic_state *, const void *ptr, size_t size, size_t count, xFILE *fp);
long xfseek(pic_state *, xFILE *fp, long offset, int whence);
int xfclose(pic_state *, xFILE *fp);
void xclearerr(pic_state *, xFILE *fp);
int xfeof(pic_state *, xFILE *fp);
int xferror(pic_state *, xFILE *fp);
int xfputc(pic_state *, int c, xFILE *fp);
int xfgetc(pic_state *, xFILE *fp);
int xfputs(pic_state *, const char *s, xFILE *fp);
char *xfgets(pic_state *, char *s, int size, xFILE *fp);
int xungetc(pic_state *, int c, xFILE *fp);
int xfflush(pic_state *, xFILE *fp);
int xfprintf(pic_state *, xFILE *fp, const char *fmt, ...);
int xvfprintf(pic_state *, xFILE *fp, const char *fmt, va_list);
/* extra stuff */
@ -272,15 +307,23 @@ void *pic_default_allocf(void *, void *, size_t);
pic_errorf(pic, "expected " #type ", but got ~s", v); \
}
pic_value pic_make_port(pic_state *, xFILE *file);
void pic_close_port(pic_state *, pic_value port);
#define xstdin (&pic->files[0])
#define xstdout (&pic->files[1])
#define xstderr (&pic->files[2])
#if PIC_ENABLE_STDIO
xFILE *xfopen_file(pic_state *, FILE *, const char *mode);
#endif
xFILE *xfopen_buf(pic_state *, const char *buf, int len, const char *mode);
int xfget_buf(pic_state *, xFILE *file, const char **buf, int *len);
xFILE *xfopen_null(pic_state *, const char *mode);
#define pic_void(exec) \
pic_void_(PIC_GENSYM(ai), exec)
#define pic_void_(ai,exec) do { \
size_t ai = pic_enter(pic); \
size_t ai = pic_enter(pic); \
exec; \
pic_leave(pic, ai); \
pic_leave(pic, ai); \
} while (0)
pic_value pic_read(pic_state *, pic_value port);
@ -348,8 +391,6 @@ void pic_print_backtrace(pic_state *, xFILE *);
#define pic_stdout(pic) pic_funcall(pic, "picrin.base", "current-output-port", 0)
#define pic_stderr(pic) pic_funcall(pic, "picrin.base", "current-error-port", 0)
xFILE *pic_fileno(pic_state *, pic_value port);
pic_value pic_write(pic_state *, pic_value); /* returns given obj */
pic_value pic_fwrite(pic_state *, pic_value, xFILE *);
void pic_printf(pic_state *, const char *, ...);

View File

@ -8,7 +8,7 @@ extern "C" {
#define XBUFSIZ 1024
#define XOPEN_MAX 1024
typedef struct {
struct xFILE {
/* buffer */
char buf[1]; /* fallback buffer */
long cnt; /* characters left */
@ -23,7 +23,7 @@ typedef struct {
int (*close)(pic_state *, void *);
} vtable;
int flag; /* mode of the file access */
} xFILE;
};
enum {
X_READ = 01,
@ -34,35 +34,6 @@ enum {
X_LNBUF = 040
};
#define xstdin (&pic->files[0])
#define xstdout (&pic->files[1])
#define xstderr (&pic->files[2])
xFILE *xfunopen(pic_state *, void *cookie, int (*read)(pic_state *, void *, char *, int), int (*write)(pic_state *, void *, const char *, int), long (*seek)(pic_state *, void *, long, int), int (*close)(pic_state *, void *));
size_t xfread(pic_state *, void *ptr, size_t size, size_t count, xFILE *fp);
size_t xfwrite(pic_state *, const void *ptr, size_t size, size_t count, xFILE *fp);
long xfseek(pic_state *, xFILE *fp, long offset, int whence); /* 0:cur, 1:end, 2:set */
int xfclose(pic_state *, xFILE *fp);
void xclearerr(pic_state *, xFILE *fp);
int xfeof(pic_state *, xFILE *fp);
int xferror(pic_state *, xFILE *fp);
int xfputc(pic_state *, int c, xFILE *fp);
int xfgetc(pic_state *, xFILE *fp);
int xfputs(pic_state *, const char *s, xFILE *fp);
char *xfgets(pic_state *, char *s, int size, xFILE *fp);
int xungetc(pic_state *, int c, xFILE *fp);
int xfflush(pic_state *, xFILE *fp);
int xfprintf(pic_state *, xFILE *fp, const char *fmt, ...);
int xvfprintf(pic_state *, xFILE *fp, const char *fmt, va_list);
#if PIC_ENABLE_STDIO
xFILE *xfopen_file(pic_state *, FILE *, const char *mode);
#endif
xFILE *xfopen_buf(pic_state *, const char *buf, int len, const char *mode);
int xfget_buf(pic_state *, xFILE *file, const char **buf, int *len);
xFILE *xfopen_null(pic_state *, const char *mode);
#if defined(__cplusplus)
}

View File

@ -10,9 +10,9 @@ extern "C" {
#endif
#include "picrin/khash.h"
#include "picrin/file.h"
#include "picrin/irep.h"
#include "picrin/file.h"
#include "picrin/read.h"
#include "picrin/gc.h"

View File

@ -20,7 +20,7 @@ pic_load(pic_state *pic, pic_value port)
void
pic_load_cstr(pic_state *pic, const char *str)
{
pic_value port = pic_make_port(pic, xfopen_buf(pic, str, strlen(str), "r"));
pic_value port = pic_open_port(pic, xfopen_buf(pic, str, strlen(str), "r"));
pic_try {
pic_load(pic, port);

View File

@ -9,7 +9,7 @@
#define EOF (-1)
pic_value
pic_make_port(pic_state *pic, xFILE *file)
pic_open_port(pic_state *pic, xFILE *file)
{
struct pic_port *port;
@ -142,7 +142,7 @@ pic_port_open_input_bytevector(pic_state *pic)
buf = pic_blob(pic, blob, &len);
return pic_make_port(pic, xfopen_buf(pic, (char *)buf, len, "r"));
return pic_open_port(pic, xfopen_buf(pic, (char *)buf, len, "r"));
}
static pic_value
@ -150,7 +150,7 @@ pic_port_open_output_bytevector(pic_state *pic)
{
pic_get_args(pic, "");
return pic_make_port(pic, xfopen_buf(pic, NULL, 0, "w"));
return pic_open_port(pic, xfopen_buf(pic, NULL, 0, "w"));
}
static pic_value
@ -337,7 +337,7 @@ coerce_port(pic_state *pic)
}
#define DEFINE_PORT(pic, name, file) \
pic_defvar(pic, name, pic_make_port(pic, file), coerce)
pic_defvar(pic, name, pic_open_port(pic, file), coerce)
void
pic_init_port(pic_state *pic)

View File

@ -844,7 +844,7 @@ pic_read(pic_state *pic, pic_value port)
pic_value
pic_read_cstr(pic_state *pic, const char *str)
{
pic_value port = pic_make_port(pic, xfopen_buf(pic, str, strlen(str), "r"));
pic_value port = pic_open_port(pic, xfopen_buf(pic, str, strlen(str), "r"));
pic_value form;
pic_try {