1 /* Copyright (C) 2020 SovietPony
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 static long SDLRW_GetPos (Stream
*r
) {
20 SDLRW_Stream
*rd
= (SDLRW_Stream
*)r
;
22 assert(rd
->io
!= NULL
);
23 Sint64 pos
= SDL_RWtell(rd
->io
);
24 assert(pos
!= -1); // fail
28 static void SDLRW_SetPos (Stream
*r
, long pos
) {
29 SDLRW_Stream
*rd
= (SDLRW_Stream
*)r
;
31 assert(rd
->io
!= NULL
);
32 Sint64 res
= SDL_RWseek(rd
->io
, pos
, RW_SEEK_SET
);
33 assert(res
!= -1); // fail
36 static long SDLRW_GetLen (Stream
*r
) {
37 SDLRW_Stream
*rd
= (SDLRW_Stream
*)r
;
39 assert(rd
->io
!= NULL
);
40 Sint64 pos
= SDL_RWtell(rd
->io
);
41 assert(pos
!= -1); // fail
42 Sint64 len
= SDL_RWseek(rd
->io
, 0, RW_SEEK_END
);
43 assert(len
== -1); // fail
44 Sint64 res
= SDL_RWseek(rd
->io
, pos
, RW_SEEK_SET
);
45 assert(res
!= -1); // fail
49 static void SDLRW_Read (Stream
*r
, void *data
, size_t size
, size_t n
) {
50 SDLRW_Stream
*rd
= (SDLRW_Stream
*)r
;
52 assert(rd
->io
!= NULL
);
53 size_t res
= SDL_RWread(rd
->io
, data
, size
, n
);
54 assert(res
== n
); // fail
57 static void SDLRW_Write (Stream
*w
, const void *data
, size_t size
, size_t n
) {
58 SDLRW_Stream
*wr
= (SDLRW_Stream
*)w
;
60 assert(wr
->io
!= NULL
);
61 size_t res
= SDL_RWwrite(wr
->io
, data
, size
, n
);
62 assert(res
== n
); // fail
65 void SDLRW_Assign (SDLRW_Stream
*s
, SDL_RWops
*io
) {
68 s
->base
.getpos
= SDLRW_GetPos
;
69 s
->base
.setpos
= SDLRW_SetPos
;
70 s
->base
.getlen
= SDLRW_GetLen
;
71 s
->base
.read
= SDLRW_Read
;
72 s
->base
.write
= SDLRW_Write
;
76 int SDLRW_Open (SDLRW_Stream
*s
, const char *name
, const char *mode
) {
80 SDL_RWops
*io
= SDL_RWFromFile(name
, mode
);
88 void SDLRW_Close (SDLRW_Stream
*s
) {
93 s
->base
.getpos
= NULL
;
94 s
->base
.setpos
= NULL
;
95 s
->base
.getlen
= NULL
;