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);