From 392549b315c259cef03824487da4b9c0c43527c5 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 16 Jan 2014 18:32:05 +0900 Subject: [PATCH] replace old stdio functions with picrin's wrapper impls --- include/picrin/port.h | 2 ++ src/file.c | 8 ++++---- src/port.c | 14 +++++++------- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/include/picrin/port.h b/include/picrin/port.h index 07eba86c..0b89e373 100644 --- a/include/picrin/port.h +++ b/include/picrin/port.h @@ -41,6 +41,8 @@ struct pic_port *pic_stdin(pic_state *); struct pic_port *pic_stdout(pic_state *); struct pic_port *pic_stderr(pic_state *); +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_fopen(const char *, const char *); diff --git a/src/file.c b/src/file.c index eced2448..8dfcd9c1 100644 --- a/src/file.c +++ b/src/file.c @@ -7,15 +7,15 @@ static pic_value generic_open_file(pic_state *pic, const char *fname, char *mode, short flags) { struct pic_port *port; - FILE *fp; + pic_file *file; - fp = fopen(fname, mode); - if (! fp) { + file = pic_fopen(fname, mode); + if (! file) { pic_error(pic, "could not open file"); } port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TT_PORT); - port->file = fp; + port->file = file; port->flags = flags; port->status = PIC_PORT_OPEN; diff --git a/src/port.c b/src/port.c index 4586f18c..17bec870 100644 --- a/src/port.c +++ b/src/port.c @@ -359,7 +359,7 @@ pic_port_close_port(pic_state *pic) pic_get_args(pic, "p", &port); - if (fclose(port->file) == EOF) { + if (pic_fclose(port->file) == EOF) { pic_error(pic, "close-port: failure"); } port->status = PIC_PORT_CLOSE; @@ -404,7 +404,7 @@ pic_port_read_char(pic_state *pic) assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, PIC_PORT_OPEN, "read-char"); - if ((c = fgetc(port->file)) == EOF) { + if ((c = pic_fgetc(port->file)) == EOF) { return pic_eof_object(); } else { @@ -422,11 +422,11 @@ pic_port_peek_char(pic_state *pic) assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, PIC_PORT_OPEN, "peek-char"); - if ((c = fgetc(port->file)) == EOF) { + if ((c = pic_fgetc(port->file)) == EOF) { return pic_eof_object(); } else { - ungetc(c, port->file); + pic_ungetc(c, port->file); return pic_char_value(c); } } @@ -450,7 +450,7 @@ pic_port_newline(pic_state *pic) assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_TEXT, PIC_PORT_OPEN, "newline"); - fputs("\n", port->file); + pic_fputs("\n", port->file); return pic_none_value(); } @@ -464,7 +464,7 @@ pic_port_write_char(pic_state *pic) assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_TEXT, PIC_PORT_OPEN, "write-char"); - fputc(c, port->file); + pic_fputc(c, port->file); return pic_none_value(); } @@ -477,7 +477,7 @@ pic_port_flush(pic_state *pic) assert_port_profile(port, PIC_PORT_OUT, PIC_PORT_OPEN, "flush-output-port"); - fflush(port->file); + pic_fflush(port->file); return pic_none_value(); }