pass pic_state object to vtable functions
This commit is contained in:
parent
3df7d1dd71
commit
e43a9c7881
|
@ -1,6 +1,7 @@
|
||||||
#include "picrin.h"
|
#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;
|
FILE *file = cookie;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
|
@ -16,7 +17,8 @@ static int file_read(void *cookie, char *ptr, int size) {
|
||||||
return r;
|
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;
|
FILE *file = cookie;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
|
@ -28,7 +30,8 @@ static int file_write(void *cookie, const char *ptr, int size) {
|
||||||
return r;
|
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) {
|
switch (whence) {
|
||||||
case XSEEK_CUR:
|
case XSEEK_CUR:
|
||||||
whence = SEEK_CUR;
|
whence = SEEK_CUR;
|
||||||
|
@ -43,7 +46,8 @@ static long file_seek(void *cookie, long pos, int whence) {
|
||||||
return fseek(cookie, pos, 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);
|
return fclose(cookie);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,7 +74,7 @@ xFILE x_iob[XOPEN_MAX] = {
|
||||||
{ { 0 }, 0, NULL, NULL, FILE_VTABLE, X_WRITE | X_UNBUF }
|
{ { 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;
|
xFILE *fp;
|
||||||
|
|
||||||
for (fp = x_iob; fp < x_iob + XOPEN_MAX; 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;
|
fp->flag = 0;
|
||||||
if (fp->base != fp->buf)
|
if (fp->base != fp->buf)
|
||||||
pic_free(pic, fp->base);
|
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) {
|
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;
|
bufsize = (fp->flag & X_UNBUF) ? sizeof(fp->buf) : XBUFSIZ;
|
||||||
|
|
||||||
fp->ptr = fp->base;
|
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 < 0) {
|
||||||
if (fp->cnt == -1)
|
if (fp->cnt == -1)
|
||||||
|
@ -157,7 +161,7 @@ int x_flushbuf(pic_state *pic, int x, xFILE *fp) {
|
||||||
fp->cnt = 0;
|
fp->cnt = 0;
|
||||||
if (x == EOF)
|
if (x == EOF)
|
||||||
return 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;
|
bufsize = 1;
|
||||||
} else {
|
} else {
|
||||||
/* buffered write */
|
/* buffered write */
|
||||||
|
@ -168,7 +172,7 @@ int x_flushbuf(pic_state *pic, int x, xFILE *fp) {
|
||||||
bufsize = (int)(fp->ptr - fp->base);
|
bufsize = (int)(fp->ptr - fp->base);
|
||||||
while(bufsize - num_written > 0) {
|
while(bufsize - num_written > 0) {
|
||||||
int t;
|
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)
|
if (t < 0)
|
||||||
break;
|
break;
|
||||||
num_written += t;
|
num_written += t;
|
||||||
|
@ -334,7 +338,7 @@ long xfseek(pic_state *pic, xFILE *fp, long offset, int whence) {
|
||||||
fp->ptr = fp->base;
|
fp->ptr = fp->base;
|
||||||
fp->cnt = 0;
|
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;
|
return s;
|
||||||
fp->flag &= ~X_EOF;
|
fp->flag &= ~X_EOF;
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -23,10 +23,10 @@ struct xFILE {
|
||||||
/* operators */
|
/* operators */
|
||||||
struct {
|
struct {
|
||||||
void *cookie;
|
void *cookie;
|
||||||
int (*read)(void *, char *, int);
|
int (*read)(pic_state *, void *, char *, int);
|
||||||
int (*write)(void *, const char *, int);
|
int (*write)(pic_state *, void *, const char *, int);
|
||||||
long (*seek)(void *, long, int);
|
long (*seek)(pic_state *, void *, long, int);
|
||||||
int (*close)(void *);
|
int (*close)(pic_state *, void *);
|
||||||
} vtable;
|
} vtable;
|
||||||
int flag; /* mode of the file access */
|
int flag; /* mode of the file access */
|
||||||
};
|
};
|
||||||
|
@ -63,7 +63,7 @@ enum _flags {
|
||||||
#define xputchar(pic, x) xputc((pic), (x), xstdout)
|
#define xputchar(pic, x) xputc((pic), (x), xstdout)
|
||||||
|
|
||||||
/* resource aquisition */
|
/* 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 *);
|
xFILE *xfopen(const char *, const char *);
|
||||||
int xfclose(pic_state *, xFILE *);
|
int xfclose(pic_state *, xFILE *);
|
||||||
|
|
||||||
|
|
|
@ -54,13 +54,12 @@ DEFINE_STANDARD_PORT_ACCESSOR(pic_stdout, "current-output-port")
|
||||||
DEFINE_STANDARD_PORT_ACCESSOR(pic_stderr, "current-error-port")
|
DEFINE_STANDARD_PORT_ACCESSOR(pic_stderr, "current-error-port")
|
||||||
|
|
||||||
struct strfile {
|
struct strfile {
|
||||||
pic_state *pic;
|
|
||||||
char *buf;
|
char *buf;
|
||||||
long pos, end, capa;
|
long pos, end, capa;
|
||||||
};
|
};
|
||||||
|
|
||||||
static int
|
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;
|
struct strfile *m = cookie;
|
||||||
|
|
||||||
|
@ -72,13 +71,13 @@ string_read(void *cookie, char *ptr, int size)
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
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;
|
struct strfile *m = cookie;
|
||||||
|
|
||||||
if (m->pos + size >= m->capa) {
|
if (m->pos + size >= m->capa) {
|
||||||
m->capa = (m->pos + size) * 2;
|
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);
|
memcpy(m->buf + m->pos, ptr, size);
|
||||||
m->pos += size;
|
m->pos += size;
|
||||||
|
@ -88,7 +87,7 @@ string_write(void *cookie, const char *ptr, int size)
|
||||||
}
|
}
|
||||||
|
|
||||||
static long
|
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;
|
struct strfile *m = cookie;
|
||||||
|
|
||||||
|
@ -108,12 +107,12 @@ string_seek(void *cookie, long pos, int whence)
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
string_close(void *cookie)
|
string_close(pic_state *pic, void *cookie)
|
||||||
{
|
{
|
||||||
struct strfile *m = cookie;
|
struct strfile *m = cookie;
|
||||||
|
|
||||||
pic_free(m->pic, m->buf);
|
pic_free(pic, m->buf);
|
||||||
pic_free(m->pic, m);
|
pic_free(pic, m);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,7 +123,6 @@ string_open(pic_state *pic, const char *data, size_t size)
|
||||||
xFILE *file;
|
xFILE *file;
|
||||||
|
|
||||||
m = pic_malloc(pic, sizeof(struct strfile));
|
m = pic_malloc(pic, sizeof(struct strfile));
|
||||||
m->pic = pic;
|
|
||||||
m->buf = pic_malloc(pic, size);
|
m->buf = pic_malloc(pic, size);
|
||||||
m->pos = 0;
|
m->pos = 0;
|
||||||
m->end = size;
|
m->end = size;
|
||||||
|
@ -139,7 +137,7 @@ string_open(pic_state *pic, const char *data, size_t size)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (file == NULL) {
|
if (file == NULL) {
|
||||||
string_close(m);
|
string_close(pic, m);
|
||||||
pic_error(pic, "could not open new output string/bytevector port", pic_nil_value());
|
pic_error(pic, "could not open new output string/bytevector port", pic_nil_value());
|
||||||
}
|
}
|
||||||
return file;
|
return file;
|
||||||
|
|
Loading…
Reference in New Issue