From e43a9c78818a64ca08504ff232891a1eab92e5e6 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Jun 2015 02:29:17 +0900 Subject: [PATCH] pass pic_state object to vtable functions --- extlib/benz/file.c | 24 ++++++++++++++---------- extlib/benz/include/picrin/file.h | 10 +++++----- extlib/benz/port.c | 18 ++++++++---------- 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/extlib/benz/file.c b/extlib/benz/file.c index 7a4908f3..23a78f45 100644 --- a/extlib/benz/file.c +++ b/extlib/benz/file.c @@ -1,6 +1,7 @@ #include "picrin.h" -static int file_read(void *cookie, char *ptr, int size) { +static int +file_read(pic_state PIC_UNUSED(*pic), void *cookie, char *ptr, int size) { FILE *file = cookie; int r; @@ -16,7 +17,8 @@ static int file_read(void *cookie, char *ptr, int size) { return r; } -static int file_write(void *cookie, const char *ptr, int size) { +static int +file_write(pic_state PIC_UNUSED(*pic), void *cookie, const char *ptr, int size) { FILE *file = cookie; int r; @@ -28,7 +30,8 @@ static int file_write(void *cookie, const char *ptr, int size) { return r; } -static long file_seek(void *cookie, long pos, int whence) { +static long +file_seek(pic_state PIC_UNUSED(*pic), void *cookie, long pos, int whence) { switch (whence) { case XSEEK_CUR: whence = SEEK_CUR; @@ -43,7 +46,8 @@ static long file_seek(void *cookie, long pos, int whence) { return fseek(cookie, pos, whence); } -static int file_close(void *cookie) { +static int +file_close(pic_state PIC_UNUSED(*pic), void *cookie) { return fclose(cookie); } @@ -70,7 +74,7 @@ xFILE x_iob[XOPEN_MAX] = { { { 0 }, 0, NULL, NULL, FILE_VTABLE, X_WRITE | X_UNBUF } }; -xFILE *xfunopen(void *cookie, int (*read)(void *, char *, int), int (*write)(void *, const char *, int), long (*seek)(void *, long, int), int (*close)(void *)) { +xFILE *xfunopen(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 *)) { xFILE *fp; for (fp = x_iob; fp < x_iob + XOPEN_MAX; fp++) @@ -98,7 +102,7 @@ int xfclose(pic_state *pic, xFILE *fp) { fp->flag = 0; if (fp->base != fp->buf) pic_free(pic, fp->base); - return fp->vtable.close(fp->vtable.cookie); + return fp->vtable.close(pic, fp->vtable.cookie); } int x_fillbuf(pic_state *pic, xFILE *fp) { @@ -121,7 +125,7 @@ int x_fillbuf(pic_state *pic, xFILE *fp) { bufsize = (fp->flag & X_UNBUF) ? sizeof(fp->buf) : XBUFSIZ; fp->ptr = fp->base; - fp->cnt = fp->vtable.read(fp->vtable.cookie, fp->ptr, bufsize); + fp->cnt = fp->vtable.read(pic, fp->vtable.cookie, fp->ptr, bufsize); if (--fp->cnt < 0) { if (fp->cnt == -1) @@ -157,7 +161,7 @@ int x_flushbuf(pic_state *pic, int x, xFILE *fp) { fp->cnt = 0; if (x == EOF) return EOF; - num_written = fp->vtable.write(fp->vtable.cookie, (const char *) &c, 1); + num_written = fp->vtable.write(pic, fp->vtable.cookie, (const char *) &c, 1); bufsize = 1; } else { /* buffered write */ @@ -168,7 +172,7 @@ int x_flushbuf(pic_state *pic, int x, xFILE *fp) { bufsize = (int)(fp->ptr - fp->base); while(bufsize - num_written > 0) { int t; - t = fp->vtable.write(fp->vtable.cookie, fp->base + num_written, bufsize - num_written); + t = fp->vtable.write(pic, fp->vtable.cookie, fp->base + num_written, bufsize - num_written); if (t < 0) break; num_written += t; @@ -334,7 +338,7 @@ long xfseek(pic_state *pic, xFILE *fp, long offset, int whence) { fp->ptr = fp->base; fp->cnt = 0; - if ((s = fp->vtable.seek(fp->vtable.cookie, offset, whence)) != 0) + if ((s = fp->vtable.seek(pic, fp->vtable.cookie, offset, whence)) != 0) return s; fp->flag &= ~X_EOF; return 0; diff --git a/extlib/benz/include/picrin/file.h b/extlib/benz/include/picrin/file.h index 189bd3de..2b761184 100644 --- a/extlib/benz/include/picrin/file.h +++ b/extlib/benz/include/picrin/file.h @@ -23,10 +23,10 @@ struct xFILE { /* operators */ struct { void *cookie; - int (*read)(void *, char *, int); - int (*write)(void *, const char *, int); - long (*seek)(void *, long, int); - int (*close)(void *); + 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 *); } vtable; int flag; /* mode of the file access */ }; @@ -63,7 +63,7 @@ enum _flags { #define xputchar(pic, x) xputc((pic), (x), xstdout) /* resource aquisition */ -xFILE *xfunopen(void *cookie, int (*read)(void *, char *, int), int (*write)(void *, const char *, int), long (*seek)(void *, long, int), int (*close)(void *)); +xFILE *xfunopen(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 *)); xFILE *xfopen(const char *, const char *); int xfclose(pic_state *, xFILE *); diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 8a0d7df4..867626f8 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -54,13 +54,12 @@ DEFINE_STANDARD_PORT_ACCESSOR(pic_stdout, "current-output-port") DEFINE_STANDARD_PORT_ACCESSOR(pic_stderr, "current-error-port") struct strfile { - pic_state *pic; char *buf; long pos, end, capa; }; static int -string_read(void *cookie, char *ptr, int size) +string_read(pic_state PIC_UNUSED(*pic), void *cookie, char *ptr, int size) { struct strfile *m = cookie; @@ -72,13 +71,13 @@ string_read(void *cookie, char *ptr, int size) } static int -string_write(void *cookie, const char *ptr, int size) +string_write(pic_state *pic, void *cookie, const char *ptr, int size) { struct strfile *m = cookie; if (m->pos + size >= m->capa) { m->capa = (m->pos + size) * 2; - m->buf = pic_realloc(m->pic, m->buf, (size_t)m->capa); + m->buf = pic_realloc(pic, m->buf, (size_t)m->capa); } memcpy(m->buf + m->pos, ptr, size); m->pos += size; @@ -88,7 +87,7 @@ string_write(void *cookie, const char *ptr, int size) } static long -string_seek(void *cookie, long pos, int whence) +string_seek(pic_state PIC_UNUSED(*pic), void *cookie, long pos, int whence) { struct strfile *m = cookie; @@ -108,12 +107,12 @@ string_seek(void *cookie, long pos, int whence) } static int -string_close(void *cookie) +string_close(pic_state *pic, void *cookie) { struct strfile *m = cookie; - pic_free(m->pic, m->buf); - pic_free(m->pic, m); + pic_free(pic, m->buf); + pic_free(pic, m); return 0; } @@ -124,7 +123,6 @@ string_open(pic_state *pic, const char *data, size_t size) xFILE *file; m = pic_malloc(pic, sizeof(struct strfile)); - m->pic = pic; m->buf = pic_malloc(pic, size); m->pos = 0; m->end = size; @@ -139,7 +137,7 @@ string_open(pic_state *pic, const char *data, size_t size) } if (file == NULL) { - string_close(m); + string_close(pic, m); pic_error(pic, "could not open new output string/bytevector port", pic_nil_value()); } return file;