1 
2 //          Copyright 2018 - 2021 Michael D. Parker
3 // Distributed under the Boost Software License, Version 1.0.
4 //    (See accompanying file LICENSE_1_0.txt or copy at
5 //          http://www.boost.org/LICENSE_1_0.txt)
6 
7 module bindbc.sdl.bind.sdlrwops;
8 
9 import core.stdc.stdio : FILE;
10 import bindbc.sdl.config;
11 import bindbc.sdl.bind.sdlstdinc : SDL_bool;
12 
13 enum : uint {
14     SDL_RWOPS_UNKNOWN = 0,
15     SDL_RWOPS_WINFILE = 1,
16     SDL_RWOPS_STDFILE = 2,
17     SDL_RWOPS_JNIFILE = 3,
18     SDL_RWOPS_MEMORY = 4,
19     SDL_RWOPS_MEMORY_RO = 5,
20 }
21 
22 struct SDL_RWops {
23     extern(C) @nogc nothrow {
24         long function(SDL_RWops*) size;
25         long function(SDL_RWops*, long, int) seek;
26         size_t function(SDL_RWops*, void*, size_t, size_t) read;
27         size_t function(SDL_RWops*, const(void)*, size_t, size_t) write;
28         int function(SDL_RWops*) close;
29     }
30 
31     uint type;
32 
33     union Hidden {
34         // version(Android)
35         version(Windows) {
36             struct Windowsio {
37                 int append;
38                 void* h;
39                 struct Buffer {
40                     void* data;
41                     size_t size;
42                     size_t left;
43                 }
44                 Buffer buffer;
45             }
46             Windowsio windowsio;
47         }
48 
49         struct Stdio {
50             int autoclose;
51             FILE* fp;
52         }
53         Stdio stdio;
54 
55         struct Mem {
56             ubyte* base;
57             ubyte* here;
58             ubyte* stop;
59         }
60         Mem mem;
61 
62         struct Unknown {
63             void* data1;
64             void* data2;
65         }
66         Unknown unknown;
67     }
68     Hidden hidden;
69 }
70 
71 enum {
72     RW_SEEK_SET = 0,
73     RW_SEEK_CUR = 1,
74     RW_SEEK_END = 2,
75 }
76 
77 static if(sdlSupport < SDLSupport.sdl2010) {
78     @nogc nothrow {
79         long SDL_RWsize(SDL_RWops* ctx) { return ctx.size(ctx); }
80         long SDL_RWseek(SDL_RWops* ctx, long offset, int whence) { return ctx.seek(ctx, offset, whence); }
81         long SDL_RWtell(SDL_RWops* ctx) { return ctx.seek(ctx, 0, RW_SEEK_CUR); }
82         size_t SDL_RWread(SDL_RWops* ctx, void* ptr, size_t size, size_t n) { return ctx.read(ctx, ptr, size, n); }
83         size_t SDL_RWwrite(SDL_RWops* ctx, const(void)* ptr, size_t size, size_t n) { return ctx.write(ctx, ptr, size, n); }
84         int SDL_RWclose(SDL_RWops* ctx) { return ctx.close(ctx); }
85     }
86 }
87 
88 static if(sdlSupport >= SDLSupport.sdl206) {
89     @nogc nothrow
90     void* SDL_LoadFile(const(char)* filename, size_t datasize) {
91         pragma(inline, true);
92         return SDL_LoadFile_RW(SDL_RWFromFile(filename, "rb"), datasize, 1);
93     }
94 }
95 
96 static if(staticBinding)  {
97     extern(C) @nogc nothrow {
98         SDL_RWops* SDL_RWFromFile(const(char)* file, const(char)* mode);
99         SDL_RWops* SDL_RWFromFP(FILE* ffp, SDL_bool autoclose);
100         SDL_RWops* SDL_RWFromMem(void* mem, int size);
101         SDL_RWops* SDL_RWFromConstMem(const(void)* mem, int size);
102         SDL_RWops* SDL_AllocRW();
103         void SDL_FreeRW(SDL_RWops* context);
104         ubyte SDL_ReadU8(SDL_RWops* context);
105         ushort SDL_ReadLE16(SDL_RWops* context);
106         ushort SDL_ReadBE16(SDL_RWops* context);
107         uint SDL_ReadLE32(SDL_RWops* context);
108         uint SDL_ReadBE32(SDL_RWops* context);
109         ulong SDL_ReadLE64(SDL_RWops* context);
110         ulong SDL_ReadBE64(SDL_RWops* context);
111         size_t SDL_WriteU8(SDL_RWops* context,ubyte value);
112         size_t SDL_WriteLE16(SDL_RWops* context,ushort value);
113         size_t SDL_WriteBE16(SDL_RWops* context,ushort value);
114         size_t SDL_WriteLE32(SDL_RWops* context,uint value);
115         size_t SDL_WriteBE32(SDL_RWops* context,uint value);
116         size_t SDL_WriteLE64(SDL_RWops* context,ulong value);
117         size_t SDL_WriteBE64(SDL_RWops* context,ulong value);
118 
119         static if(sdlSupport >= SDLSupport.sdl206) {
120             void* SDL_LoadFile_RW(SDL_RWops* context, size_t datasize, int freesrc);
121         }
122         static if(sdlSupport >= SDLSupport.sdl2010) {
123             long SDL_RWsize(SDL_RWops* context);
124             long SDL_RWseek(SDL_RWops* context, long offset, int whence);
125             long SDL_RWtell(SDL_RWops* context);
126             size_t SDL_RWread(SDL_RWops* context, void* ptr, size_t size, size_t maxnum);
127             size_t SDL_RWwrite(SDL_RWops* context, const(void)* ptr, size_t size, size_t num);
128             int SDL_RWclose(SDL_RWops* context);
129         }
130     }
131 }
132 else {
133     extern(C) @nogc nothrow {
134         alias pSDL_RWFromFile = SDL_RWops* function(const(char)* file, const(char)* mode);
135         alias pSDL_RWFromFP = SDL_RWops* function(FILE* ffp, SDL_bool autoclose);
136         alias pSDL_RWFromMem = SDL_RWops* function(void* mem, int size);
137         alias pSDL_RWFromConstMem = SDL_RWops* function(const(void)* mem, int size);
138         alias pSDL_AllocRW = SDL_RWops* function();
139         alias pSDL_FreeRW = void function(SDL_RWops* context);
140         alias pSDL_ReadU8 = ubyte function(SDL_RWops* context);
141         alias pSDL_ReadLE16 = ushort function(SDL_RWops* context);
142         alias pSDL_ReadBE16 = ushort function(SDL_RWops* context);
143         alias pSDL_ReadLE32 = uint function(SDL_RWops* context);
144         alias pSDL_ReadBE32 = uint function(SDL_RWops* context);
145         alias pSDL_ReadLE64 = ulong function(SDL_RWops* context);
146         alias pSDL_ReadBE64 = ulong function(SDL_RWops* context);
147         alias pSDL_WriteU8 = size_t function(SDL_RWops* context,ubyte value);
148         alias pSDL_WriteLE16 = size_t function(SDL_RWops* context,ushort value);
149         alias pSDL_WriteBE16 = size_t function(SDL_RWops* context,ushort value);
150         alias pSDL_WriteLE32 = size_t function(SDL_RWops* context,uint value);
151         alias pSDL_WriteBE32 = size_t function(SDL_RWops* context,uint value);
152         alias pSDL_WriteLE64 = size_t function(SDL_RWops* context,ulong value);
153         alias pSDL_WriteBE64 = size_t function(SDL_RWops* context,ulong value);
154     }
155     __gshared {
156         pSDL_RWFromFile SDL_RWFromFile;
157         pSDL_RWFromFP SDL_RWFromFP;
158         pSDL_RWFromMem SDL_RWFromMem;
159         pSDL_RWFromConstMem SDL_RWFromConstMem;
160         pSDL_AllocRW SDL_AllocRW;
161         pSDL_FreeRW SDL_FreeRW;
162         pSDL_ReadU8 SDL_ReadU8;
163         pSDL_ReadLE16 SDL_ReadLE16;
164         pSDL_ReadBE16 SDL_ReadBE16;
165         pSDL_ReadLE32 SDL_ReadLE32;
166         pSDL_ReadBE32 SDL_ReadBE32;
167         pSDL_ReadLE64 SDL_ReadLE64;
168         pSDL_ReadBE64 SDL_ReadBE64;
169         pSDL_WriteU8 SDL_WriteU8;
170         pSDL_WriteLE16 SDL_WriteLE16;
171         pSDL_WriteBE16 SDL_WriteBE16;
172         pSDL_WriteLE32 SDL_WriteLE32;
173         pSDL_WriteBE32 SDL_WriteBE32;
174         pSDL_WriteLE64 SDL_WriteLE64;
175         pSDL_WriteBE64 SDL_WriteBE64;
176     }
177     static if(sdlSupport >= SDLSupport.sdl206) {
178         extern(C) @nogc nothrow {
179             alias pSDL_LoadFile_RW = void* function(SDL_RWops* context, size_t datasize, int freesrc);
180         }
181         __gshared {
182             pSDL_LoadFile_RW SDL_LoadFile_RW;
183         }
184     }
185     static if(sdlSupport >= SDLSupport.sdl2010) {
186         extern(C) @nogc nothrow {
187             alias pSDL_RWsize = long function(SDL_RWops* context);
188             alias pSDL_RWseek = long function(SDL_RWops* context, long offset, int whence);
189             alias pSDL_RWtell = long function(SDL_RWops* context);
190             alias pSDL_RWread = size_t function(SDL_RWops* context, void* ptr, size_t size, size_t maxnum);
191             alias pSDL_RWwrite = size_t function(SDL_RWops* context, const(void)* ptr, size_t size, size_t num);
192             alias pSDL_RWclose = int function(SDL_RWops* context);
193         }
194         __gshared {
195             pSDL_RWsize SDL_RWsize;
196             pSDL_RWseek SDL_RWseek;
197             pSDL_RWtell SDL_RWtell;
198             pSDL_RWread SDL_RWread;
199             pSDL_RWwrite SDL_RWwrite;
200             pSDL_RWclose SDL_RWclose;
201         }
202     }
203 }