diff --git a/extlib/benz/file.c b/extlib/benz/file.c index d5dc60b9..5032b480 100644 --- a/extlib/benz/file.c +++ b/extlib/benz/file.c @@ -35,6 +35,18 @@ int xfclose(pic_state *pic, xFILE *fp) { return fp->vtable.close(pic, fp->vtable.cookie); } +void xclearerr(pic_state PIC_UNUSED(*pic), xFILE *fp) { + fp->flag &= ~(X_EOF | X_ERR); +} + +int xfeof(pic_state PIC_UNUSED(*pic), xFILE *fp) { + return (fp->flag & X_EOF) != 0; +} + +int xferror(pic_state PIC_UNUSED(*pic), xFILE *fp) { + return (fp->flag & X_ERR) != 0; +} + int x_fillbuf(pic_state *pic, xFILE *fp) { int bufsize; @@ -187,7 +199,7 @@ char *xfgets(pic_state *pic, char *s, int size, xFILE *stream) { return (c == EOF && buf == s) ? NULL : s; } -int xungetc(int c, xFILE *fp) { +int xungetc(pic_state PIC_UNUSED(*pic), int c, xFILE *fp) { unsigned char uc = c; if (c == EOF || fp->base == fp->ptr) { @@ -211,7 +223,7 @@ size_t xfread(pic_state *pic, void *ptr, size_t size, size_t count, xFILE *fp) { if ((c = x_fillbuf(pic, fp)) == EOF) { return (size * count - nbytes) / size; } else { - xungetc(c, fp); + xungetc(pic, c, fp); } } memcpy(bptr, fp->ptr, nbytes); @@ -240,6 +252,10 @@ size_t xfwrite(pic_state *pic, const void *ptr, size_t size, size_t count, xFILE return count; } +#define XSEEK_CUR 0 +#define XSEEK_END 1 +#define XSEEK_SET 2 + long xfseek(pic_state *pic, xFILE *fp, long offset, int whence) { long s; @@ -535,17 +551,3 @@ xFILE *xfopen_null(pic_state PIC_UNUSED(*pic), const char *mode) { return xfunopen(pic, 0, 0, null_write, null_seek, null_close); } } - -#if 0 -int main() -{ - char buf[256]; - - xgets(buf); - - xprintf("%s\n", buf); - xprintf("hello\n"); - xprintf("hello\n"); - // xfflush(0); -} -#endif diff --git a/extlib/benz/include/picrin/file.h b/extlib/benz/include/picrin/file.h index 2118934c..657fa2e6 100644 --- a/extlib/benz/include/picrin/file.h +++ b/extlib/benz/include/picrin/file.h @@ -25,10 +25,6 @@ typedef struct { int flag; /* mode of the file access */ } xFILE; -#define xstdin (&pic->files[0]) -#define xstdout (&pic->files[1]) -#define xstderr (&pic->files[2]) - enum { X_READ = 01, X_WRITE = 02, @@ -38,31 +34,28 @@ enum { X_LNBUF = 040 }; -#define xclearerr(p) ((p)->flag &= ~(X_EOF | X_ERR)) -#define xfeof(p) (((p)->flag & X_EOF) != 0) -#define xferror(p) (((p)->flag & X_ERR) != 0) +#define xstdin (&pic->files[0]) +#define xstdout (&pic->files[1]) +#define xstderr (&pic->files[2]) xFILE *xfunopen(pic_state *, void *cookie, int (*read)(pic_state *, void *, char *, int), int (*write)(pic_state *, void *, const char *, int), long (*seek)(pic_state *, void *, long, int), int (*close)(pic_state *, void *)); +size_t xfread(pic_state *, void *ptr, size_t size, size_t count, xFILE *fp); +size_t xfwrite(pic_state *, const void *ptr, size_t size, size_t count, xFILE *fp); +long xfseek(pic_state *, xFILE *fp, long offset, int whence); /* 0:cur, 1:end, 2:set */ +int xfclose(pic_state *, xFILE *fp); -enum { - XSEEK_CUR, - XSEEK_END, - XSEEK_SET -}; +void xclearerr(pic_state *, xFILE *fp); +int xfeof(pic_state *, xFILE *fp); +int xferror(pic_state *, xFILE *fp); -size_t xfread(pic_state *, void *, size_t, size_t, xFILE *); -size_t xfwrite(pic_state *, const void *, size_t, size_t, xFILE *); -long xfseek(pic_state *, xFILE *, long, int); -int xfflush(pic_state *, xFILE *); -int xfclose(pic_state *, xFILE *); - -int xfputc(pic_state *, int, xFILE *); -int xfgetc(pic_state *, xFILE *); -int xfputs(pic_state *, const char *, xFILE *); -char *xfgets(pic_state *, char *, int, xFILE *); -int xungetc(int, xFILE *); -int xfprintf(pic_state *, xFILE *, const char *, ...); -int xvfprintf(pic_state *, xFILE *, const char *, va_list); +int xfputc(pic_state *, int c, xFILE *fp); +int xfgetc(pic_state *, xFILE *fp); +int xfputs(pic_state *, const char *s, xFILE *fp); +char *xfgets(pic_state *, char *s, int size, xFILE *fp); +int xungetc(pic_state *, int c, xFILE *fp); +int xfflush(pic_state *, xFILE *fp); +int xfprintf(pic_state *, xFILE *fp, const char *fmt, ...); +int xvfprintf(pic_state *, xFILE *fp, const char *fmt, va_list); #if PIC_ENABLE_STDIO xFILE *xfopen_file(pic_state *, FILE *, const char *mode); diff --git a/extlib/benz/port.c b/extlib/benz/port.c index f89abe56..69a564ff 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -199,7 +199,7 @@ pic_port_peek_u8(pic_state *pic) return pic_eof_object(pic); } else { - xungetc(c, pic_fileno(pic, port)); + xungetc(pic, c, pic_fileno(pic, port)); return pic_int_value(pic, c); } } diff --git a/extlib/benz/read.c b/extlib/benz/read.c index ac3deb9f..6d3803fa 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -39,7 +39,7 @@ peek(pic_state *pic, xFILE *file) { int c; - xungetc((c = xfgetc(pic, file)), file); + xungetc(pic, (c = xfgetc(pic, file)), file); return c; }