diff --git a/contrib/20.r7rs/src/file.c b/contrib/20.r7rs/src/file.c index 2c0b080e..5cb99ffc 100644 --- a/contrib/20.r7rs/src/file.c +++ b/contrib/20.r7rs/src/file.c @@ -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 diff --git a/contrib/20.r7rs/src/load.c b/contrib/20.r7rs/src/load.c index cc02bb2c..b7459bb6 100644 --- a/contrib/20.r7rs/src/load.c +++ b/contrib/20.r7rs/src/load.c @@ -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); diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index 0be9f6d9..73272825 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -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 diff --git a/extlib/benz/file.c b/extlib/benz/file.c index 5032b480..66ec825c 100644 --- a/extlib/benz/file.c +++ b/extlib/benz/file.c @@ -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; diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 961297fa..15604e5a 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -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 *, ...); diff --git a/extlib/benz/include/picrin/file.h b/extlib/benz/include/picrin/file.h index 657fa2e6..7595d4fb 100644 --- a/extlib/benz/include/picrin/file.h +++ b/extlib/benz/include/picrin/file.h @@ -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) } diff --git a/extlib/benz/include/picrin/state.h b/extlib/benz/include/picrin/state.h index a6db373d..5246797c 100644 --- a/extlib/benz/include/picrin/state.h +++ b/extlib/benz/include/picrin/state.h @@ -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" diff --git a/extlib/benz/load.c b/extlib/benz/load.c index 28ac61b0..2ceefa11 100644 --- a/extlib/benz/load.c +++ b/extlib/benz/load.c @@ -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); diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 69a564ff..f410f44f 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -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) diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 6d3803fa..b409264b 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -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 {