handle end-of-file while reading file object

This commit is contained in:
Yuichi Nishiwaki 2014-01-16 19:56:45 +09:00
parent 74be43195d
commit 6f32cbdea3
2 changed files with 15 additions and 0 deletions

View File

@ -17,7 +17,12 @@ enum pic_port_status {
#define PIC_UBUFSIZ 3 #define PIC_UBUFSIZ 3
enum pic_file_flags {
PIC_FILE_EOF = 1,
};
typedef struct { typedef struct {
short flags;
/* buffered IO */ /* buffered IO */
char *buf; char *buf;
int mode; int mode;

View File

@ -72,6 +72,7 @@ pic_fflush(pic_file *file)
int r; int r;
r = file->vtable.write(file->vtable.cookie, file->s, file->c - file->s); r = file->vtable.write(file->vtable.cookie, file->s, file->c - file->s);
/* TODO: error handling (r == -1 or r < file->c - file->s)*/
file->c -= r; file->c -= r;
return r; return r;
} }
@ -82,6 +83,10 @@ pic_ffill(pic_file *file)
int r; int r;
r = file->vtable.read(file->vtable.cookie, file->c, file->e - file->c); r = file->vtable.read(file->vtable.cookie, file->c, file->e - file->c);
/* TODO: error handling (r == -1) */
if (r < file->e - file->c) {
file->flags |= PIC_FILE_EOF;
}
file->c += r; file->c += r;
return r; return r;
} }
@ -194,9 +199,14 @@ pic_fread(void *ptr, size_t block, size_t nitems, pic_file *file)
file->c = file->s; file->c = file->s;
size -= avail; size -= avail;
dst += avail; dst += avail;
if ((file->flags & PIC_FILE_EOF) != 0)
break;
pic_ffill(file); pic_ffill(file);
} }
} }
/* handle end-of-file */
*dst = EOF;
return block * nitems - size;
} }
size_t size_t