impl pic_fwrite with simple buffering

This commit is contained in:
Yuichi Nishiwaki 2014-01-16 18:39:03 +09:00
parent 14b73501c4
commit ce21353d50
2 changed files with 27 additions and 0 deletions

View File

@ -19,6 +19,7 @@ typedef struct {
char *buf;
int mode;
int bufsiz;
char *s, *c, *e;
struct {
void *cookie;
int (*read)(void *, char *, int);

View File

@ -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)
{