add IO buffering (setvbuf)

This commit is contained in:
Yuichi Nishiwaki 2014-01-16 18:36:47 +09:00
parent 4d0a448a44
commit eca456f875
2 changed files with 30 additions and 0 deletions

View File

@ -16,6 +16,9 @@ enum pic_port_status {
}; };
typedef struct { typedef struct {
char *buf;
int mode;
int bufsiz;
struct { struct {
void *cookie; void *cookie;
int (*read)(void *, char *, int); int (*read)(void *, char *, int);
@ -41,6 +44,7 @@ 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 *);
int pic_setvbuf(pic_file *, char *, int, size_t);
int pic_fflush(pic_file *); int pic_fflush(pic_file *);
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 *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 *));

View File

@ -37,6 +37,32 @@ 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()));
} }
int
pic_setvbuf(pic_file *file, char *buf, int mode, size_t bufsiz)
{
/* FIXME: free old buf */
file->mode = mode;
if (buf) {
file->buf = buf;
file->bufsiz = bufsiz;
}
else {
if (mode == _IONBF) {
file->buf = NULL;
file->bufsiz = 0;
}
else {
assert(bufsiz == 0);
file->buf = (char *)malloc(BUFSIZ);
file->bufsiz = BUFSIZ;
}
}
file->s = file->c = file->buf;
file->e = file->buf + file->bufsiz;
return 0;
}
pic_file * pic_file *
pic_funopen(void *cookie, pic_funopen(void *cookie,
int (*read)(void *, char *, int), int (*read)(void *, char *, int),