From ce21353d506e8cc397382fd9bc267e8327115d6c Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 16 Jan 2014 18:39:03 +0900 Subject: [PATCH] impl pic_fwrite with simple buffering --- include/picrin/port.h | 1 + src/port.c | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) 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) {