From 63d23bdedaa2c7967c9663f404d7e51593afe926 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 16 Jan 2014 18:27:05 +0900 Subject: [PATCH] [WIP] add generic file object constructor (pic_funopen) --- include/picrin/port.h | 2 ++ src/port.c | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/include/picrin/port.h b/include/picrin/port.h index 7604ed48..fbd0c884 100644 --- a/include/picrin/port.h +++ b/include/picrin/port.h @@ -41,4 +41,6 @@ struct pic_port *pic_stdin(pic_state *); struct pic_port *pic_stdout(pic_state *); struct pic_port *pic_stderr(pic_state *); +pic_file *pic_funopen(void *cookie, int (*read)(void *, char *, int), int (*write)(void *, const char *, int), fpos_t (*seek)(void *, fpos_t, int), int (*close)(void *)); + #endif diff --git a/src/port.c b/src/port.c index a36af6a8..4586f18c 100644 --- a/src/port.c +++ b/src/port.c @@ -37,6 +37,28 @@ pic_stdout(pic_state *pic) return pic_port_ptr(pic_apply(pic, proc, pic_nil_value())); } +pic_file * +pic_funopen(void *cookie, + int (*read)(void *, char *, int), + int (*write)(void *, const char *, int), + fpos_t (*seek)(void *, fpos_t, int), + int (*close)(void *)) +{ + pic_file *file; + + file = (pic_file *)malloc(sizeof(pic_file)); + if (! file) { + return NULL; + } + file->vtable.cookie = cookie; + file->vtable.read = read; + file->vtable.write = write; + file->vtable.seek = seek; + file->vtable.close = close; + + return file; +} + static void write_pair(pic_state *pic, struct pic_pair *pair); static void write_str(pic_state *pic, struct pic_string *str);