[WIP] add generic file object constructor (pic_funopen)

This commit is contained in:
Yuichi Nishiwaki 2014-01-16 18:27:05 +09:00
parent 1cb9b0347d
commit 63d23bdeda
2 changed files with 24 additions and 0 deletions

View File

@ -41,4 +41,6 @@ struct pic_port *pic_stdin(pic_state *);
struct pic_port *pic_stdout(pic_state *); struct pic_port *pic_stdout(pic_state *);
struct pic_port *pic_stderr(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 #endif

View File

@ -37,6 +37,28 @@ pic_stdout(pic_state *pic)
return pic_port_ptr(pic_apply(pic, proc, pic_nil_value())); 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_pair(pic_state *pic, struct pic_pair *pair);
static void write_str(pic_state *pic, struct pic_string *str); static void write_str(pic_state *pic, struct pic_string *str);