7 static long FILE_Reader_GetPos (Reader
*r
) {
8 FILE_Reader
*rd
= (FILE_Reader
*)r
;
10 assert(rd
->fp
!= NULL
);
11 long pos
= ftell(rd
->fp
);
12 assert(pos
!= -1); // fail
16 static void FILE_Reader_SetPos (Reader
*r
, long pos
) {
17 FILE_Reader
*rd
= (FILE_Reader
*)r
;
19 assert(rd
->fp
!= NULL
);
21 int res
= fseek(rd
->fp
, pos
, SEEK_SET
);
22 assert(res
== 0); // fail
25 static void FILE_Reader_Read (Reader
*r
, void *data
, size_t size
, size_t n
) {
26 FILE_Reader
*rd
= (FILE_Reader
*)r
;
28 assert(rd
->fp
!= NULL
);
29 size_t res
= fread(data
, size
, n
, rd
->fp
);
30 assert(res
== n
); // fail
33 void FILE_AssignReader (FILE_Reader
*r
, FILE *fp
) {
36 r
->base
.getpos
= FILE_Reader_GetPos
;
37 r
->base
.setpos
= FILE_Reader_SetPos
;
38 r
->base
.read
= FILE_Reader_Read
;
42 int FILE_OpenReader (FILE_Reader
*r
, const char *name
) {
45 FILE *fp
= fopen(name
, "rb");
47 FILE_AssignReader(r
, fp
);
52 void FILE_CloseReader (FILE_Reader
*r
) {
57 r
->base
.getpos
= NULL
;
58 r
->base
.setpos
= NULL
;
63 static long FILE_Writer_GetPos (Writer
*w
) {
64 FILE_Writer
*wr
= (FILE_Writer
*)w
;
66 assert(wr
->fp
!= NULL
);
67 long pos
= ftell(wr
->fp
);
68 assert(pos
!= -1); // fail
72 static void FILE_Writer_SetPos (Writer
*w
, long pos
) {
73 FILE_Writer
*wr
= (FILE_Writer
*)w
;
75 assert(wr
->fp
!= NULL
);
77 int res
= fseek(wr
->fp
, pos
, SEEK_SET
);
78 assert(res
== 0); // fail
81 static void FILE_Writer_Write (Writer
*w
, const void *data
, size_t size
, size_t n
) {
82 FILE_Writer
*wr
= (FILE_Writer
*)w
;
84 assert(wr
->fp
!= NULL
);
85 size_t res
= fwrite(data
, size
, n
, wr
->fp
);
86 assert(res
== n
); // fail
89 void FILE_AssignWriter (FILE_Writer
*w
, FILE *fp
) {
92 w
->base
.getpos
= FILE_Writer_GetPos
;
93 w
->base
.setpos
= FILE_Writer_SetPos
;
94 w
->base
.write
= FILE_Writer_Write
;
98 int FILE_OpenWriter (FILE_Writer
*w
, const char *name
) {
100 assert(name
!= NULL
);
101 FILE *fp
= fopen(name
, "wb");
103 FILE_AssignWriter(w
, fp
);
108 void FILE_CloseWriter (FILE_Writer
*w
) {
113 w
->base
.getpos
= NULL
;
114 w
->base
.setpos
= NULL
;
115 w
->base
.write
= NULL
;