diff --git a/include/picrin/port.h b/include/picrin/port.h index 857d2deb..32550231 100644 --- a/include/picrin/port.h +++ b/include/picrin/port.h @@ -19,6 +19,7 @@ typedef struct { char *buf; int mode; int bufsiz; + char *s, *c, *e; struct { void *cookie; int (*read)(void *, char *, int); diff --git a/src/port.c b/src/port.c index ebcd6dbb..a7df04e8 100644 --- a/src/port.c +++ b/src/port.c @@ -147,6 +147,32 @@ pic_fclose(pic_file *file) return 0; } +size_t +pic_fwrite(const void *ptr, size_t block, size_t nitems, pic_file *file) +{ + int size, room; + char *dst = (char *)ptr; + + size = block * nitems; /* TODO: optimize block write */ + while (1) { + room = file->e - file->c; + if (room < size) { + memcpy(file->c, dst, room); + file->c += room; + size -= room; + dst += room; + pic_fflush(file); + } + else { + memcpy(file->c, dst, size); + file->c += size; + break; + } + } + + return block * nitems; +} + int pic_fgetc(pic_file *file) {