1 /*
2     DSDL
3     Copyright (C) 2025 Inochi2D Project <luna@foxgirls.gay>
4 
5     This software is provided 'as-is', without any express or implied
6     warranty.  In no event will the authors be held liable for any damages
7     arising from the use of this software.
8 
9     Permission is granted to anyone to use this software for any purpose,
10     including commercial applications, and to alter it and redistribute it
11     freely, subject to the following restrictions:
12 
13     1. The origin of this software must not be misrepresented; you must not
14         claim that you wrote the original software. If you use this software
15         in a product, an acknowledgment in the product documentation would be
16         appreciated but is not required.
17     2. Altered source versions must be plainly marked as such, and must not be
18         misrepresented as being the original software.
19     3. This notice may not be removed or altered from any source distribution.
20 
21     ==========================================================================
22 
23     Simple DirectMedia Layer
24     Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
25 
26     This software is provided 'as-is', without any express or implied
27     warranty.  In no event will the authors be held liable for any damages
28     arising from the use of this software.
29 
30     Permission is granted to anyone to use this software for any purpose,
31     including commercial applications, and to alter it and redistribute it
32     freely, subject to the following restrictions:
33 
34     1. The origin of this software must not be misrepresented; you must not
35         claim that you wrote the original software. If you use this software
36         in a product, an acknowledgment in the product documentation would be
37         appreciated but is not required.
38     2. Altered source versions must be plainly marked as such, and must not be
39         misrepresented as being the original software.
40     3. This notice may not be removed or altered from any source distribution.
41 */
42 
43 /**
44     SDL IOStream
45 
46     See_Also:
47         $(LINK2 https://wiki.libsdl.org/SDL3/CategoryIOStream, SDL3 IOStream Documentation)
48     
49     Copyright: © 2025 Inochi2D Project, © 1997-2025 Sam Lantinga
50     License: Subject to the terms of the Zlib License, as written in the LICENSE file.
51     Authors: 
52         Luna Nielsen
53 */
54 module sdl.iostream;
55 import sdl.stdc;
56 import sdl.properties;
57 
58 extern(C) nothrow @nogc:
59 
60 /**
61     SDL_IOStream status, set by a read or write operation.
62 */
63 enum SDL_IOStatus {
64     
65     /**
66         Everything is ready (no errors and not EOF).
67     */
68     SDL_IO_STATUS_READY,     
69     
70     /**
71         Read or write I/O error
72     */
73     SDL_IO_STATUS_ERROR,     
74     
75     /**
76         End of file
77     */
78     SDL_IO_STATUS_EOF,       
79     
80     /**
81         Non blocking I/O, not ready
82     */
83     SDL_IO_STATUS_NOT_READY, 
84     
85     /**
86         Tried to write a read-only buffer
87     */
88     SDL_IO_STATUS_READONLY,  
89     
90     /**
91         Tried to read a write-only buffer
92     */
93     SDL_IO_STATUS_WRITEONLY  
94 }
95 
96 /**
97     Possible `whence` values for SDL_IOStream seeking.
98 
99     These map to the same "whence" concept that `fseek` or `lseek` use in the
100     standard C runtime.
101 
102 */
103 enum SDL_IOWhence {
104     
105     /**
106         Seek from the beginning of data
107     */
108     SDL_IO_SEEK_SET,  
109     
110     /**
111         Seek relative to current read point
112     */
113     SDL_IO_SEEK_CUR,  
114     
115     /**
116         Seek relative to the end of data
117     */
118     SDL_IO_SEEK_END   
119 }
120 
121 /**
122     The function pointers that drive an SDL_IOStream.
123 
124     Applications can provide this struct to SDL_OpenIO() to create their own
125     implementation of SDL_IOStream. This is not necessarily required, as SDL
126     already offers several common types of I/O streams, via functions like
127     SDL_IOFromFile() and SDL_IOFromMem().
128 
129     This structure should be initialized using SDL_INIT_INTERFACE()
130 
131     See_Also:
132         $(D SDL_INIT_INTERFACE)
133 */
134 struct SDL_IOStreamInterface {
135     /*
136         The version of this interface
137     */
138     Uint32 version_;
139 
140     /**
141         Return the number of bytes in this SDL_IOStream
142 
143         Returns:
144             The total size of the data stream, or -1 on error.
145     */
146     Sint64 function(void* userdata) size;
147 
148     /**
149         Seek to `offset` relative to `whence`, one of stdio's whence values:
150         SDL_IO_SEEK_SET, SDL_IO_SEEK_CUR, SDL_IO_SEEK_END
151 
152         Returns:
153             The final offset in the data stream, or -1 on error.
154     */
155     Sint64 function(void* userdata, Sint64 offset, SDL_IOWhence whence) seek;
156 
157     /**
158         Read up to `size` bytes from the data stream to the area pointed
159         at by `ptr`.
160 
161         On an incomplete read, you should set `*status` to a value from the
162         SDL_IOStatus enum. You do not have to explicitly set this on
163         a complete, successful read.
164 
165         Returns:
166             The number of bytes read
167     */
168     size_t function(void* userdata, void* ptr, size_t size, SDL_IOStatus* status) read;
169 
170     /**
171         Write exactly `size` bytes from the area pointed at by `ptr`
172         to data stream.
173 
174         On an incomplete write, you should set `*status` to a value from the
175         SDL_IOStatus enum. You do not have to explicitly set this on
176         a complete, successful write.
177 
178         Returns:
179             The number of bytes written
180     */
181     size_t function(void* userdata, const(void)* ptr, size_t size, SDL_IOStatus* status) write;
182 
183     /**
184         If the stream is buffering, make sure the data is written out.
185 
186         On failure, you should set `*status` to a value from the
187         SDL_IOStatus enum. You do not have to explicitly set this on
188         a successful flush.
189 
190         Returns:
191             true if successful or false on write error when flushing data.
192     */
193     bool function(void* userdata, SDL_IOStatus* status) flush;
194 
195     /**
196         Close and free any allocated resources.
197 
198         This does not guarantee file writes will sync to physical media; they
199         can be in the system's file cache, waiting to go to disk.
200 
201         The SDL_IOStream is still destroyed even if this fails, so clean up anything
202         even if flushing buffers, etc, returns an error.
203 
204         Returns:
205             true if successful or false on write error when flushing data.
206     */
207     bool function(void* userdata) close;
208 }
209 
210 /**
211     The read/write operation structure.
212 
213     This operates as an opaque handle. There are several APIs to create various
214     types of I/O streams, or an app can supply an SDL_IOStreamInterface to
215     SDL_OpenIO() to provide their own stream implementation behind this
216     struct's abstract interface.
217 */
218 struct SDL_IOStream;
219 
220 /**
221     Use this function to create a new SDL_IOStream structure for reading from
222     and/or writing to a named file.
223 
224     The `mode` string is treated roughly the same as in a call to the C
225     library's fopen(), even if SDL doesn't happen to use fopen() behind the
226     scenes.
227 
228     Available `mode` strings:
229 
230     -   "r": Open a file for reading. The file must exist.
231     -   "w": Create an empty file for writing. If a file with the same name
232         already exists its content is erased and the file is treated as a new
233         empty file.
234     -   "a": Append to a file. Writing operations append data at the end of the
235         file. The file is created if it does not exist.
236     -   "r+": Open a file for update both reading and writing. The file must
237         exist.
238     -   "w+": Create an empty file for both reading and writing. If a file with
239         the same name already exists its content is erased and the file is
240         treated as a new empty file.
241     -   "a+": Open a file for reading and appending. All writing operations are
242         performed at the end of the file, protecting the previous content to be
243         overwritten. You can reposition (fseek, rewind) the internal pointer to
244         anywhere in the file for reading, but writing operations will move it
245         back to the end of file. The file is created if it does not exist.
246 
247     **NOTE**: In order to open a file as a binary file, a "b" character has to
248     be included in the `mode` string. This additional "b" character can either
249     be appended at the end of the string (thus making the following compound
250     modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") or be inserted between the
251     letter and the "+" sign for the mixed modes ("rb+", "wb+", "ab+").
252     Additional characters may follow the sequence, although they should have no
253     effect. For example, "t" is sometimes appended to make explicit the file is
254     a text file.
255 
256     This function supports Unicode filenames, but they must be encoded in UTF-8
257     format, regardless of the underlying operating system.
258 
259     In Android, SDL_IOFromFile() can be used to open content:// URIs. As a
260     fallback, SDL_IOFromFile() will transparently open a matching filename in
261     the app's `assets`.
262 
263     Closing the SDL_IOStream will close SDL's internal file handle.
264 
265     The following properties may be set at creation time by SDL:
266 
267     -   `SDL_PROP_IOSTREAM_WINDOWS_HANDLE_POINTER`: a pointer, that can be cast
268         to a win32 `HANDLE`, that this SDL_IOStream is using to access the
269         filesystem. If the program isn't running on Windows, or SDL used some
270         other method to access the filesystem, this property will not be set.
271     -   `SDL_PROP_IOSTREAM_STDIO_FILE_POINTER`: a pointer, that can be cast to a
272         stdio `FILE* `, that this SDL_IOStream is using to access the filesystem.
273         If SDL used some other method to access the filesystem, this property
274         will not be set. PLEASE NOTE that if SDL is using a different C runtime
275         than your app, trying to use this pointer will almost certainly result in
276         a crash! This is mostly a problem on Windows; make sure you build SDL and
277         your app with the same compiler and settings to avoid it.
278     -   `SDL_PROP_IOSTREAM_FILE_DESCRIPTOR_NUMBER`: a file descriptor that this
279         SDL_IOStream is using to access the filesystem.
280     -   `SDL_PROP_IOSTREAM_ANDROID_AASSET_POINTER`: a pointer, that can be cast
281         to an Android NDK `AAsset* `, that this SDL_IOStream is using to access
282         the filesystem. If SDL used some other method to access the filesystem,
283         this property will not be set.
284 
285     Params:
286         file =  a UTF-8 string representing the filename to open.
287         mode =  an ASCII string representing the mode to be used for opening
288                 the file.
289     
290     Returns:
291         A pointer to the SDL_IOStream structure that is created or NULL on
292         failure; call SDL_GetError() for more information.
293 
294     Threadsafety:
295         This function is not thread safe.
296 
297     See_Also:
298         $(D SDL_CloseIO)
299         $(D SDL_FlushIO)
300         $(D SDL_ReadIO)
301         $(D SDL_SeekIO)
302         $(D SDL_TellIO)
303         $(D SDL_WriteIO)
304 */
305 extern SDL_IOStream*  SDL_IOFromFile(const(char)* file, const(char)* mode);
306 
307 enum SDL_PROP_IOSTREAM_WINDOWS_HANDLE_POINTER =    "SDL.iostream.windows.handle";
308 enum SDL_PROP_IOSTREAM_STDIO_FILE_POINTER =        "SDL.iostream.stdio.file";
309 enum SDL_PROP_IOSTREAM_FILE_DESCRIPTOR_NUMBER =    "SDL.iostream.file_descriptor";
310 enum SDL_PROP_IOSTREAM_ANDROID_AASSET_POINTER =    "SDL.iostream.android.aasset";
311 
312 /**
313     Use this function to prepare a read-write memory buffer for use with
314     SDL_IOStream.
315 
316     This function sets up an SDL_IOStream struct based on a memory area of a
317     certain size, for both read and write access.
318 
319     This memory buffer is not copied by the SDL_IOStream; the pointer you
320     provide must remain valid until you close the stream. Closing the stream
321     will not free the original buffer.
322 
323     If you need to make sure the SDL_IOStream never writes to the memory
324     buffer, you should use SDL_IOFromConstMem() with a read-only buffer of
325     memory instead.
326 
327     The following properties will be set at creation time by SDL:
328 
329     -   `SDL_PROP_IOSTREAM_MEMORY_POINTER`: this will be the `mem` parameter that
330         was passed to this function.
331     -   `SDL_PROP_IOSTREAM_MEMORY_SIZE_NUMBER`: this will be the `size` parameter
332         that was passed to this function.
333 
334     Params:
335         mem =   a pointer to a buffer to feed an SDL_IOStream stream.
336         size =  the buffer size, in bytes.
337     
338     Returns:
339         A pointer to a new SDL_IOStream structure or NULL on failure; call
340         SDL_GetError() for more information.
341 
342     Threadsafety:
343         It is safe to call this function from any thread.
344 
345     See_Also:
346         $(D SDL_IOFromConstMem)
347         $(D SDL_CloseIO)
348         $(D SDL_FlushIO)
349         $(D SDL_ReadIO)
350         $(D SDL_SeekIO)
351         $(D SDL_TellIO)
352         $(D SDL_WriteIO)
353 */
354 extern SDL_IOStream*  SDL_IOFromMem(void* mem, size_t size);
355 
356 enum SDL_PROP_IOSTREAM_MEMORY_POINTER = "SDL.iostream.memory.base";
357 enum SDL_PROP_IOSTREAM_MEMORY_SIZE_NUMBER =  "SDL.iostream.memory.size";
358 
359 /**
360     Use this function to prepare a read-only memory buffer for use with
361     SDL_IOStream.
362     
363     This function sets up an SDL_IOStream struct based on a memory area of a
364     certain size. It assumes the memory area is not writable.
365     
366     Attempting to write to this SDL_IOStream stream will report an error
367     without writing to the memory buffer.
368     
369     This memory buffer is not copied by the SDL_IOStream; the pointer you
370     provide must remain valid until you close the stream. Closing the stream
371     will not free the original buffer.
372     
373     If you need to write to a memory buffer, you should use SDL_IOFromMem()
374     with a writable buffer of memory instead.
375     
376     The following properties will be set at creation time by SDL:
377 
378     -   `SDL_PROP_IOSTREAM_MEMORY_POINTER`: this will be the `mem` parameter that
379         was passed to this function.
380     -   `SDL_PROP_IOSTREAM_MEMORY_SIZE_NUMBER`: this will be the `size` parameter
381         that was passed to this function.
382     
383 
384     Params:
385         mem =   a pointer to a read-only buffer to feed an SDL_IOStream stream.
386         size =  the buffer size, in bytes.
387     
388     Returns:
389         A pointer to a new SDL_IOStream structure or NULL on failure; call
390         SDL_GetError() for more information.
391     
392     Threadsafety:
393         It is safe to call this function from any thread.
394     
395     See_Also:
396         $(D SDL_IOFromMem)
397         $(D SDL_CloseIO)
398         $(D SDL_ReadIO)
399         $(D SDL_SeekIO)
400         $(D SDL_TellIO)
401 */
402 extern SDL_IOStream*  SDL_IOFromConstMem(const(void)* mem, size_t size);
403 
404 /**
405     Use this function to create an SDL_IOStream that is backed by dynamically
406     allocated memory.
407 
408     This supports the following properties to provide access to the memory and
409     control over allocations:
410 
411     -   `SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER`: a pointer to the internal
412         memory of the stream. This can be set to NULL to transfer ownership of
413         the memory to the application, which should free the memory with
414         SDL_free(). If this is done, the next operation on the stream must be
415         SDL_CloseIO().
416     -   `SDL_PROP_IOSTREAM_DYNAMIC_CHUNKSIZE_NUMBER`: memory will be allocated in
417         multiples of this size, defaulting to 1024.
418 
419     Returns:
420         A pointer to a new SDL_IOStream structure or NULL on failure; call
421         SDL_GetError() for more information.
422 
423     Threadsafety:
424         It is safe to call this function from any thread.
425 
426     See_Also:
427         $(D SDL_CloseIO)
428         $(D SDL_ReadIO)
429         $(D SDL_SeekIO)
430         $(D SDL_TellIO)
431         $(D SDL_WriteIO)
432 */
433 extern SDL_IOStream*  SDL_IOFromDynamicMem();
434 
435 enum SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER =    "SDL.iostream.dynamic.memory";
436 enum SDL_PROP_IOSTREAM_DYNAMIC_CHUNKSIZE_NUMBER =  "SDL.iostream.dynamic.chunksize";
437 
438 /**
439     Create a custom SDL_IOStream.
440 
441     Applications do not need to use this function unless they are providing
442     their own SDL_IOStream implementation. If you just need an SDL_IOStream to
443     read/write a common data source, you should use the built-in
444     implementations in SDL, like SDL_IOFromFile() or SDL_IOFromMem(), etc.
445 
446     This function makes a copy of `iface` and the caller does not need to keep
447     it around after this call.
448 
449     Params:
450         iface =     the interface that implements this SDL_IOStream, initialized
451                     using SDL_INIT_INTERFACE().
452         userdata =  the pointer that will be passed to the interface functions.
453     
454     Returns:
455         A pointer to the allocated memory on success or NULL on failure;
456         call SDL_GetError() for more information.
457 
458     Threadsafety:
459         It is safe to call this function from any thread.
460 
461     See_Also:
462         $(D SDL_CloseIO)
463         $(D SDL_INIT_INTERFACE)
464         $(D SDL_IOFromConstMem)
465         $(D SDL_IOFromFile)
466         $(D SDL_IOFromMem)
467 */
468 extern SDL_IOStream*  SDL_OpenIO(const(SDL_IOStreamInterface)* iface, void* userdata);
469 
470 /**
471     Close and free an allocated SDL_IOStream structure.
472 
473     SDL_CloseIO() closes and cleans up the SDL_IOStream stream. It releases any
474     resources used by the stream and frees the SDL_IOStream itself. This
475     returns true on success, or false if the stream failed to flush to its
476     output (e.g. to disk).
477 
478     Note that if this fails to flush the stream for any reason, this function
479     reports an error, but the SDL_IOStream is still invalid once this function
480     returns.
481 
482     This call flushes any buffered writes to the operating system, but there
483     are no guarantees that those writes have gone to physical media; they might
484     be in the OS's file cache, waiting to go to disk later. If it's absolutely
485     crucial that writes go to disk immediately, so they are definitely stored
486     even if the power fails before the file cache would have caught up, one
487     should call SDL_FlushIO() before closing. Note that flushing takes time and
488     makes the system and your app operate less efficiently, so do so sparingly.
489 
490     Params:
491         context = SDL_IOStream structure to close.
492     
493     Returns:
494         true on success or false on failure; call SDL_GetError() for more
495         information.
496 
497     Threadsafety:
498         This function is not thread safe.
499 
500     See_Also:
501         $(D SDL_OpenIO)
502 */
503 extern bool SDL_CloseIO(SDL_IOStream* context);
504 
505 /**
506     Get the properties associated with an SDL_IOStream.
507 
508     \param context a pointer to an SDL_IOStream structure.
509     
510     Returns:
511         a valid property ID on success or 0 on failure; call
512             SDL_GetError() for more information.
513 
514     Threadsafety:
515         This function is not thread safe.
516 
517 */
518 extern SDL_PropertiesID SDL_GetIOProperties(SDL_IOStream* context);
519 
520 /**
521     Query the stream status of an SDL_IOStream.
522 
523     This information can be useful to decide if a short read or write was due
524     to an error, an EOF, or a non-blocking operation that isn't yet ready to
525     complete.
526 
527     An SDL_IOStream's status is only expected to change after a SDL_ReadIO or
528     SDL_WriteIO call; don't expect it to change if you just call this query
529     function in a tight loop.
530 
531     \param context the SDL_IOStream to query.
532     
533     Returns:
534         an SDL_IOStatus enum with the current state.
535 
536     Threadsafety:
537         This function is not thread safe.
538 
539 */
540 extern SDL_IOStatus SDL_GetIOStatus(SDL_IOStream* context);
541 
542 /**
543     Use this function to get the size of the data stream in an SDL_IOStream.
544 
545     \param context the SDL_IOStream to get the size of the data stream from.
546     
547     Returns:
548         the size of the data stream in the SDL_IOStream on success or a
549             negative error code on failure; call SDL_GetError() for more
550             information.
551 
552     Threadsafety:
553         This function is not thread safe.
554 
555 */
556 extern Sint64 SDL_GetIOSize(SDL_IOStream* context);
557 
558 /**
559     Seek within an SDL_IOStream data stream.
560 
561     This function seeks to byte `offset`, relative to `whence`.
562 
563     `whence` may be any of the following values:
564 
565     - `SDL_IO_SEEK_SET`: seek from the beginning of data
566     - `SDL_IO_SEEK_CUR`: seek relative to current read point
567     - `SDL_IO_SEEK_END`: seek relative to the end of data
568 
569     If this stream can not seek, it will return -1.
570 
571     Params:
572         context =   a pointer to an SDL_IOStream structure.
573         offset =    an offset in bytes, relative to `whence` location; can be
574                     negative.
575         whence =    any of `SDL_IO_SEEK_SET`, `SDL_IO_SEEK_CUR`,
576                     `SDL_IO_SEEK_END`.
577     
578     Returns:
579         The final offset in the data stream after the seek or -1 on
580         failure; call SDL_GetError() for more information.
581 
582     Threadsafety:
583         This function is not thread safe.
584 
585     See_Also:
586         $(D SDL_TellIO)
587 */
588 extern Sint64 SDL_SeekIO(SDL_IOStream* context, Sint64 offset, SDL_IOWhence whence);
589 
590 /**
591     Determine the current read/write offset in an SDL_IOStream data stream.
592 
593     SDL_TellIO is actually a wrapper function that calls the SDL_IOStream's
594     `seek` method, with an offset of 0 bytes from `SDL_IO_SEEK_CUR`, to
595     simplify application development.
596 
597     Params:
598         context =   an SDL_IOStream data stream object from which to get the
599                     current offset.
600     
601     Returns:
602         The current offset in the stream, or -1 if the information can not
603         be determined.
604 
605     Threadsafety:
606         This function is not thread safe.
607 
608     See_Also:
609         $(D SDL_SeekIO)
610 */
611 extern Sint64 SDL_TellIO(SDL_IOStream* context);
612 
613 /**
614     Read from a data source.
615 
616     This function reads up `size` bytes from the data source to the area
617     pointed at by `ptr`. This function may read less bytes than requested.
618 
619     This function will return zero when the data stream is completely read, and
620     SDL_GetIOStatus() will return SDL_IO_STATUS_EOF. If zero is returned and
621     the stream is not at EOF, SDL_GetIOStatus() will return a different error
622     value and SDL_GetError() will offer a human-readable message.
623 
624     Params:
625         context =   a pointer to an SDL_IOStream structure.
626         ptr =       a pointer to a buffer to read data into.
627         size =      the number of bytes to read from the data source.
628     
629     Returns:
630         The number of bytes read, or 0 on end of file or other failure;
631         call SDL_GetError() for more information.
632 
633     Threadsafety:
634         This function is not thread safe.
635 
636     See_Also:
637         $(D SDL_WriteIO)
638         $(D SDL_GetIOStatus)
639 */
640 extern size_t SDL_ReadIO(SDL_IOStream* context, void* ptr, size_t size);
641 
642 /**
643     Write to an SDL_IOStream data stream.
644 
645     This function writes exactly `size` bytes from the area pointed at by `ptr`
646     to the stream. If this fails for any reason, it'll return less than `size`
647     to demonstrate how far the write progressed. On success, it returns `size`.
648 
649     On error, this function still attempts to write as much as possible, so it
650     might return a positive value less than the requested write size.
651 
652     The caller can use SDL_GetIOStatus() to determine if the problem is
653     recoverable, such as a non-blocking write that can simply be retried later,
654     or a fatal error.
655 
656     Params:
657         context =   a pointer to an SDL_IOStream structure.
658         ptr =       a pointer to a buffer containing data to write.
659         size =      the number of bytes to write.
660     
661     Returns:
662         The number of bytes written, which will be less than `size` on
663         failure; call SDL_GetError() for more information.
664 
665     Threadsafety:
666         This function is not thread safe.
667 
668     See_Also:
669         $(D SDL_IOprintf)
670         $(D SDL_ReadIO)
671         $(D SDL_SeekIO)
672         $(D SDL_FlushIO)
673         $(D SDL_GetIOStatus)
674 */
675 extern size_t SDL_WriteIO(SDL_IOStream* context, const(void)* ptr, size_t size);
676 
677 /**
678     Print to an SDL_IOStream data stream.
679 
680     This function does formatted printing to the stream.
681 
682     Params:
683         context =   a pointer to an SDL_IOStream structure.
684         fmt =       a printf() style format string.
685         ... =       additional parameters matching % tokens in the `fmt` string, if
686                     any.
687     
688     Returns:
689         The number of bytes written or 0 on failure; call SDL_GetError()
690         for more information.
691 
692     Threadsafety:
693         This function is not thread safe.
694 
695     See_Also:
696         $(D SDL_IOvprintf)
697         $(D SDL_WriteIO)
698 */
699 extern size_t SDL_IOprintf(SDL_IOStream* context, const(char)* fmt, ...);
700 
701 /**
702     Flush any buffered data in the stream.
703 
704     This function makes sure that any buffered data is written to the stream.
705     Normally this isn't necessary but if the stream is a pipe or socket it
706     guarantees that any pending data is sent.
707 
708     Params:
709         context = SDL_IOStream structure to flush.
710     
711     Returns:
712         true on success or false on failure; call SDL_GetError() for more
713         information.
714 
715     Threadsafety:
716         This function is not thread safe.
717 
718 
719         $(D SDL_OpenIO)
720         $(D SDL_WriteIO)
721 */
722 extern bool SDL_FlushIO(SDL_IOStream* context);
723 
724 /**
725     Load all the data from an SDL data stream.
726 
727     The data is allocated with a zero byte at the end (null terminated) for
728     convenience. This extra byte is not included in the value reported via
729     `datasize`.
730 
731     The data should be freed with SDL_free().
732 
733     Params:
734         src =       the SDL_IOStream to read all available data from.
735         datasize =  a pointer filled in with the number of bytes read, may be
736                     NULL.
737         closeio =   if true, calls SDL_CloseIO() on `src` before returning, even
738                     in the case of an error.
739     
740     Returns:
741         the data or NULL on failure; call SDL_GetError() for more
742         information.
743 
744     Threadsafety:
745         This function is not thread safe.
746 
747     See_Also:
748         $(D SDL_LoadFile)
749         $(D SDL_SaveFile_IO)
750 */
751 extern void*  SDL_LoadFile_IO(SDL_IOStream* src, size_t* datasize, bool closeio);
752 
753 /**
754     Load all the data from a file path.
755 
756     The data is allocated with a zero byte at the end (null terminated) for
757     convenience. This extra byte is not included in the value reported via
758     `datasize`.
759 
760     The data should be freed with SDL_free().
761 
762     Params:
763         file =      the path to read all available data from.
764         datasize =  if not NULL, will store the number of bytes read.
765     
766     Returns:
767         the data or NULL on failure; call SDL_GetError() for more
768             information.
769 
770     Threadsafety:
771         This function is not thread safe.
772 
773     See_Also:
774         $(D SDL_LoadFile_IO)
775         $(D SDL_SaveFile)
776 */
777 extern void*  SDL_LoadFile(const(char)* file, size_t* datasize);
778 
779 /**
780     Save all the data into an SDL data stream.
781 
782     Params:
783         src =       the SDL_IOStream to write all data to.
784         data =      the data to be written. If datasize is 0, may be NULL or a
785                     invalid pointer.
786         datasize =  the number of bytes to be written.
787         closeio =   if true, calls SDL_CloseIO() on `src` before returning, even
788                     in the case of an error.
789     
790     Returns:
791         true on success or false on failure; call SDL_GetError() for more
792             information.
793 
794     Threadsafety:
795         This function is not thread safe.
796 
797     See_Also:
798         $(D SDL_SaveFile)
799         $(D SDL_LoadFile_IO)
800 */
801 extern bool SDL_SaveFile_IO(SDL_IOStream* src, const(void)* data, size_t datasize, bool closeio);
802 
803 /**
804     Save all the data into a file path.
805 
806     Params:
807         file =      the path to write all available data into.
808         data =      the data to be written. If datasize is 0, may be NULL or a
809                     invalid pointer.
810         datasize =  the number of bytes to be written.
811     
812     Returns:
813         true on success or false on failure; call SDL_GetError() for more
814             information.
815 
816     Threadsafety:
817         This function is not thread safe.
818 
819     See_Also:
820         $(D SDL_SaveFile_IO)
821         $(D SDL_LoadFile)
822 */
823 extern bool SDL_SaveFile(const(char)* file, const(void)* data, size_t datasize);
824 
825 /**
826     Use this function to read a byte from an SDL_IOStream.
827 
828     This function will return false when the data stream is completely read,
829     and SDL_GetIOStatus() will return SDL_IO_STATUS_EOF. If false is returned
830     and the stream is not at EOF, SDL_GetIOStatus() will return a different
831     error value and SDL_GetError() will offer a human-readable message.
832 
833     Params:
834         src =   the SDL_IOStream to read from.
835         value = a pointer filled in with the data read.
836     
837     Returns:
838         true on success or false on failure or EOF; call SDL_GetError()
839             for more information.
840 
841     Threadsafety:
842         This function is not thread safe.
843 */
844 extern bool SDL_ReadU8(SDL_IOStream* src, Uint8* value);
845 
846 /**
847     Use this function to read a signed byte from an SDL_IOStream.
848 
849     This function will return false when the data stream is completely read,
850     and SDL_GetIOStatus() will return SDL_IO_STATUS_EOF. If false is returned
851     and the stream is not at EOF, SDL_GetIOStatus() will return a different
852     error value and SDL_GetError() will offer a human-readable message.
853 
854     Params:
855         src =   the SDL_IOStream to read from.
856         value = a pointer filled in with the data read.
857     
858     Returns:
859         true on success or false on failure; call SDL_GetError() for more
860         information.
861 
862     Threadsafety:
863         This function is not thread safe.
864 */
865 extern bool SDL_ReadS8(SDL_IOStream* src, Sint8* value);
866 
867 /**
868     Use this function to read 16 bits of little-endian data from an
869     SDL_IOStream and return in native format.
870 
871     SDL byteswaps the data only if necessary, so the data returned will be in
872     the native byte order.
873 
874     This function will return false when the data stream is completely read,
875     and SDL_GetIOStatus() will return SDL_IO_STATUS_EOF. If false is returned
876     and the stream is not at EOF, SDL_GetIOStatus() will return a different
877     error value and SDL_GetError() will offer a human-readable message.
878 
879     Params:
880         src =   the stream from which to read data.
881         value = a pointer filled in with the data read.
882     
883     Returns:
884         true on successful write or false on failure; call SDL_GetError()
885         for more information.
886 
887     Threadsafety:
888         This function is not thread safe.
889 
890 */
891 extern bool SDL_ReadU16LE(SDL_IOStream* src, Uint16* value);
892 
893 /**
894     Use this function to read 16 bits of little-endian data from an
895     SDL_IOStream and return in native format.
896 
897     SDL byteswaps the data only if necessary, so the data returned will be in
898     the native byte order.
899 
900     This function will return false when the data stream is completely read,
901     and SDL_GetIOStatus() will return SDL_IO_STATUS_EOF. If false is returned
902     and the stream is not at EOF, SDL_GetIOStatus() will return a different
903     error value and SDL_GetError() will offer a human-readable message.
904 
905     Params:
906         src =   the stream from which to read data.
907         value = a pointer filled in with the data read.
908     
909     Returns:
910         true on successful write or false on failure; call SDL_GetError()
911         for more information.
912 
913     Threadsafety:
914         This function is not thread safe.
915 */
916 extern bool SDL_ReadS16LE(SDL_IOStream* src, Sint16* value);
917 
918 /**
919     Use this function to read 16 bits of big-endian data from an SDL_IOStream
920     and return in native format.
921 
922     SDL byteswaps the data only if necessary, so the data returned will be in
923     the native byte order.
924 
925     This function will return false when the data stream is completely read,
926     and SDL_GetIOStatus() will return SDL_IO_STATUS_EOF. If false is returned
927     and the stream is not at EOF, SDL_GetIOStatus() will return a different
928     error value and SDL_GetError() will offer a human-readable message.
929 
930     Params:
931         src =   the stream from which to read data.
932         value = a pointer filled in with the data read.
933     
934     Returns:
935         true on successful write or false on failure; call SDL_GetError()
936         for more information.
937 
938     Threadsafety:
939         This function is not thread safe.
940 */
941 extern bool SDL_ReadU16BE(SDL_IOStream* src, Uint16* value);
942 
943 /**
944     Use this function to read 16 bits of big-endian data from an SDL_IOStream
945     and return in native format.
946 
947     SDL byteswaps the data only if necessary, so the data returned will be in
948     the native byte order.
949 
950     This function will return false when the data stream is completely read,
951     and SDL_GetIOStatus() will return SDL_IO_STATUS_EOF. If false is returned
952     and the stream is not at EOF, SDL_GetIOStatus() will return a different
953     error value and SDL_GetError() will offer a human-readable message.
954 
955     Params:
956         src =   the stream from which to read data.
957         value = a pointer filled in with the data read.
958     
959     Returns:
960         true on successful write or false on failure; call SDL_GetError()
961         for more information.
962 
963     Threadsafety:
964         This function is not thread safe.
965 */
966 extern bool SDL_ReadS16BE(SDL_IOStream* src, Sint16* value);
967 
968 /**
969     Use this function to read 32 bits of little-endian data from an
970     SDL_IOStream and return in native format.
971 
972     SDL byteswaps the data only if necessary, so the data returned will be in
973     the native byte order.
974 
975     This function will return false when the data stream is completely read,
976     and SDL_GetIOStatus() will return SDL_IO_STATUS_EOF. If false is returned
977     and the stream is not at EOF, SDL_GetIOStatus() will return a different
978     error value and SDL_GetError() will offer a human-readable message.
979 
980     Params:
981         src =   the stream from which to read data.
982         value = a pointer filled in with the data read.
983     
984     Returns:
985         true on successful write or false on failure; call SDL_GetError()
986         for more information.
987 
988     Threadsafety:
989         This function is not thread safe.
990 */
991 extern bool SDL_ReadU32LE(SDL_IOStream* src, Uint32* value);
992 
993 /**
994     Use this function to read 32 bits of little-endian data from an
995     SDL_IOStream and return in native format.
996 
997     SDL byteswaps the data only if necessary, so the data returned will be in
998     the native byte order.
999 
1000     This function will return false when the data stream is completely read,
1001     and SDL_GetIOStatus() will return SDL_IO_STATUS_EOF. If false is returned
1002     and the stream is not at EOF, SDL_GetIOStatus() will return a different
1003     error value and SDL_GetError() will offer a human-readable message.
1004 
1005     Params:
1006         src =   the stream from which to read data.
1007         value = a pointer filled in with the data read.
1008     
1009     Returns:
1010         true on successful write or false on failure; call SDL_GetError()
1011         for more information.
1012 
1013     Threadsafety:
1014         This function is not thread safe.
1015 */
1016 extern bool SDL_ReadS32LE(SDL_IOStream* src, Sint32* value);
1017 
1018 /**
1019     Use this function to read 32 bits of big-endian data from an SDL_IOStream
1020     and return in native format.
1021 
1022     SDL byteswaps the data only if necessary, so the data returned will be in
1023     the native byte order.
1024 
1025     This function will return false when the data stream is completely read,
1026     and SDL_GetIOStatus() will return SDL_IO_STATUS_EOF. If false is returned
1027     and the stream is not at EOF, SDL_GetIOStatus() will return a different
1028     error value and SDL_GetError() will offer a human-readable message.
1029 
1030     Params:
1031         src =   the stream from which to read data.
1032         value = a pointer filled in with the data read.
1033     
1034     Returns:
1035         true on successful write or false on failure; call SDL_GetError()
1036         for more information.
1037 
1038     Threadsafety:
1039         This function is not thread safe.
1040 */
1041 extern bool SDL_ReadU32BE(SDL_IOStream* src, Uint32* value);
1042 
1043 /**
1044     Use this function to read 32 bits of big-endian data from an SDL_IOStream
1045     and return in native format.
1046 
1047     SDL byteswaps the data only if necessary, so the data returned will be in
1048     the native byte order.
1049 
1050     This function will return false when the data stream is completely read,
1051     and SDL_GetIOStatus() will return SDL_IO_STATUS_EOF. If false is returned
1052     and the stream is not at EOF, SDL_GetIOStatus() will return a different
1053     error value and SDL_GetError() will offer a human-readable message.
1054 
1055     Params:
1056         src =   the stream from which to read data.
1057         value = a pointer filled in with the data read.
1058     
1059     Returns:
1060         true on successful write or false on failure; call SDL_GetError()
1061         for more information.
1062 
1063     Threadsafety:
1064         This function is not thread safe.
1065 */
1066 extern bool SDL_ReadS32BE(SDL_IOStream* src, Sint32* value);
1067 
1068 /**
1069     Use this function to read 64 bits of little-endian data from an
1070     SDL_IOStream and return in native format.
1071 
1072     SDL byteswaps the data only if necessary, so the data returned will be in
1073     the native byte order.
1074 
1075     This function will return false when the data stream is completely read,
1076     and SDL_GetIOStatus() will return SDL_IO_STATUS_EOF. If false is returned
1077     and the stream is not at EOF, SDL_GetIOStatus() will return a different
1078     error value and SDL_GetError() will offer a human-readable message.
1079 
1080     Params:
1081         src =   the stream from which to read data.
1082         value = a pointer filled in with the data read.
1083     
1084     Returns:
1085         true on successful write or false on failure; call SDL_GetError()
1086         for more information.
1087 
1088     Threadsafety:
1089         This function is not thread safe.
1090 */
1091 extern bool SDL_ReadU64LE(SDL_IOStream* src, Uint64* value);
1092 
1093 /**
1094     Use this function to read 64 bits of little-endian data from an
1095     SDL_IOStream and return in native format.
1096 
1097     SDL byteswaps the data only if necessary, so the data returned will be in
1098     the native byte order.
1099 
1100     This function will return false when the data stream is completely read,
1101     and SDL_GetIOStatus() will return SDL_IO_STATUS_EOF. If false is returned
1102     and the stream is not at EOF, SDL_GetIOStatus() will return a different
1103     error value and SDL_GetError() will offer a human-readable message.
1104 
1105     Params:
1106         src =   the stream from which to read data.
1107         value = a pointer filled in with the data read.
1108     
1109     Returns:
1110         true on successful write or false on failure; call SDL_GetError()
1111         for more information.
1112 
1113     Threadsafety:
1114         This function is not thread safe.
1115 */
1116 extern bool SDL_ReadS64LE(SDL_IOStream* src, Sint64* value);
1117 
1118 /**
1119     Use this function to read 64 bits of big-endian data from an SDL_IOStream
1120     and return in native format.
1121 
1122     SDL byteswaps the data only if necessary, so the data returned will be in
1123     the native byte order.
1124 
1125     This function will return false when the data stream is completely read,
1126     and SDL_GetIOStatus() will return SDL_IO_STATUS_EOF. If false is returned
1127     and the stream is not at EOF, SDL_GetIOStatus() will return a different
1128     error value and SDL_GetError() will offer a human-readable message.
1129 
1130     Params:
1131         src =   the stream from which to read data.
1132         value = a pointer filled in with the data read.
1133     
1134     Returns:
1135         true on successful write or false on failure; call SDL_GetError()
1136         for more information.
1137 
1138     Threadsafety:
1139         This function is not thread safe.
1140 */
1141 extern bool SDL_ReadU64BE(SDL_IOStream* src, Uint64* value);
1142 
1143 /**
1144     Use this function to read 64 bits of big-endian data from an SDL_IOStream
1145     and return in native format.
1146 
1147     SDL byteswaps the data only if necessary, so the data returned will be in
1148     the native byte order.
1149 
1150     This function will return false when the data stream is completely read,
1151     and SDL_GetIOStatus() will return SDL_IO_STATUS_EOF. If false is returned
1152     and the stream is not at EOF, SDL_GetIOStatus() will return a different
1153     error value and SDL_GetError() will offer a human-readable message.
1154 
1155     Params:
1156         src =   the stream from which to read data.
1157         value = a pointer filled in with the data read.
1158     
1159     Returns:
1160         true on successful write or false on failure; call SDL_GetError()
1161         for more information.
1162 
1163     Threadsafety:
1164         This function is not thread safe.
1165 */
1166 extern bool SDL_ReadS64BE(SDL_IOStream* src, Sint64* value);
1167 
1168 /**
1169     Use this function to write a byte to an SDL_IOStream.
1170 
1171     Params:
1172         dst =   the SDL_IOStream to write to.
1173         value = the byte value to write.
1174     
1175     Returns:
1176         true on successful write or false on failure; call SDL_GetError()
1177         for more information.
1178 
1179     Threadsafety:
1180         This function is not thread safe.
1181 */
1182 extern bool SDL_WriteU8(SDL_IOStream* dst, Uint8 value);
1183 
1184 /**
1185     Use this function to write a signed byte to an SDL_IOStream.
1186 
1187     Params:
1188         dst =   the SDL_IOStream to write to.
1189         value = the byte value to write.
1190     
1191     Returns:
1192         true on successful write or false on failure; call SDL_GetError()
1193         for more information.
1194 
1195     Threadsafety:
1196         This function is not thread safe.
1197 */
1198 extern bool SDL_WriteS8(SDL_IOStream* dst, Sint8 value);
1199 
1200 /**
1201     Use this function to write 16 bits in native format to an SDL_IOStream as
1202     little-endian data.
1203 
1204     SDL byteswaps the data only if necessary, so the application always
1205     specifies native format, and the data written will be in little-endian
1206     format.
1207 
1208     Params:
1209         dst =   the stream to which data will be written.
1210         value = the data to be written, in native format.
1211     
1212     Returns:
1213         true on successful write or false on failure; call SDL_GetError()
1214         for more information.
1215 
1216     Threadsafety:
1217         This function is not thread safe.
1218 */
1219 extern bool SDL_WriteU16LE(SDL_IOStream* dst, Uint16 value);
1220 
1221 /**
1222     Use this function to write 16 bits in native format to an SDL_IOStream as
1223     little-endian data.
1224 
1225     SDL byteswaps the data only if necessary, so the application always
1226     specifies native format, and the data written will be in little-endian
1227     format.
1228 
1229     Params:
1230         dst =   the stream to which data will be written.
1231         value = the data to be written, in native format.
1232     
1233     Returns:
1234         true on successful write or false on failure; call SDL_GetError()
1235         for more information.
1236 
1237     Threadsafety:
1238         This function is not thread safe.
1239 
1240 */
1241 extern bool SDL_WriteS16LE(SDL_IOStream* dst, Sint16 value);
1242 
1243 /**
1244     Use this function to write 16 bits in native format to an SDL_IOStream as
1245     big-endian data.
1246 
1247     SDL byteswaps the data only if necessary, so the application always
1248     specifies native format, and the data written will be in big-endian format.
1249 
1250     Params:
1251         dst =   the stream to which data will be written.
1252         value = the data to be written, in native format.
1253     
1254     Returns:
1255         true on successful write or false on failure; call SDL_GetError()
1256         for more information.
1257 
1258     Threadsafety:
1259         This function is not thread safe.
1260 
1261 */
1262 extern bool SDL_WriteU16BE(SDL_IOStream* dst, Uint16 value);
1263 
1264 /**
1265     Use this function to write 16 bits in native format to an SDL_IOStream as
1266     big-endian data.
1267 
1268     SDL byteswaps the data only if necessary, so the application always
1269     specifies native format, and the data written will be in big-endian format.
1270 
1271     Params:
1272         dst =   the stream to which data will be written.
1273         value = the data to be written, in native format.
1274     
1275     Returns:
1276         true on successful write or false on failure; call SDL_GetError()
1277         for more information.
1278 
1279     Threadsafety:
1280         This function is not thread safe.
1281 
1282 */
1283 extern bool SDL_WriteS16BE(SDL_IOStream* dst, Sint16 value);
1284 
1285 /**
1286     Use this function to write 32 bits in native format to an SDL_IOStream as
1287     little-endian data.
1288 
1289     SDL byteswaps the data only if necessary, so the application always
1290     specifies native format, and the data written will be in little-endian
1291     format.
1292 
1293     Params:
1294         dst =   the stream to which data will be written.
1295         value = the data to be written, in native format.
1296     
1297     Returns:
1298         true on successful write or false on failure; call SDL_GetError()
1299         for more information.
1300 
1301     Threadsafety:
1302         This function is not thread safe.
1303 
1304 */
1305 extern bool SDL_WriteU32LE(SDL_IOStream* dst, Uint32 value);
1306 
1307 /**
1308     Use this function to write 32 bits in native format to an SDL_IOStream as
1309     little-endian data.
1310 
1311     SDL byteswaps the data only if necessary, so the application always
1312     specifies native format, and the data written will be in little-endian
1313     format.
1314 
1315     Params:
1316         dst =   the stream to which data will be written.
1317         value = the data to be written, in native format.
1318     
1319     Returns:
1320         true on successful write or false on failure; call SDL_GetError()
1321         for more information.
1322 
1323     Threadsafety:
1324         This function is not thread safe.
1325 
1326 */
1327 extern bool SDL_WriteS32LE(SDL_IOStream* dst, Sint32 value);
1328 
1329 /**
1330     Use this function to write 32 bits in native format to an SDL_IOStream as
1331     big-endian data.
1332 
1333     SDL byteswaps the data only if necessary, so the application always
1334     specifies native format, and the data written will be in big-endian format.
1335 
1336     Params:
1337         dst =   the stream to which data will be written.
1338         value = the data to be written, in native format.
1339     
1340     Returns:
1341         true on successful write or false on failure; call SDL_GetError()
1342         for more information.
1343 
1344     Threadsafety:
1345         This function is not thread safe.
1346 
1347 */
1348 extern bool SDL_WriteU32BE(SDL_IOStream* dst, Uint32 value);
1349 
1350 /**
1351     Use this function to write 32 bits in native format to an SDL_IOStream as
1352     big-endian data.
1353 
1354     SDL byteswaps the data only if necessary, so the application always
1355     specifies native format, and the data written will be in big-endian format.
1356 
1357     Params:
1358         dst =   the stream to which data will be written.
1359         value = the data to be written, in native format.
1360     
1361     Returns:
1362         true on successful write or false on failure; call SDL_GetError()
1363         for more information.
1364 
1365     Threadsafety:
1366         This function is not thread safe.
1367 
1368 */
1369 extern bool SDL_WriteS32BE(SDL_IOStream* dst, Sint32 value);
1370 
1371 /**
1372     Use this function to write 64 bits in native format to an SDL_IOStream as
1373     little-endian data.
1374 
1375     SDL byteswaps the data only if necessary, so the application always
1376     specifies native format, and the data written will be in little-endian
1377     format.
1378 
1379     Params:
1380         dst =   the stream to which data will be written.
1381         value = the data to be written, in native format.
1382     
1383     Returns:
1384         true on successful write or false on failure; call SDL_GetError()
1385         for more information.
1386 
1387     Threadsafety:
1388         This function is not thread safe.
1389 
1390 */
1391 extern bool SDL_WriteU64LE(SDL_IOStream* dst, Uint64 value);
1392 
1393 /**
1394     Use this function to write 64 bits in native format to an SDL_IOStream as
1395     little-endian data.
1396 
1397     SDL byteswaps the data only if necessary, so the application always
1398     specifies native format, and the data written will be in little-endian
1399     format.
1400 
1401     Params:
1402         dst =   the stream to which data will be written.
1403         value = the data to be written, in native format.
1404     
1405     Returns:
1406         true on successful write or false on failure; call SDL_GetError()
1407         for more information.
1408 
1409     Threadsafety:
1410         This function is not thread safe.
1411 
1412 */
1413 extern bool SDL_WriteS64LE(SDL_IOStream* dst, Sint64 value);
1414 
1415 /**
1416     Use this function to write 64 bits in native format to an SDL_IOStream as
1417     big-endian data.
1418 
1419     SDL byteswaps the data only if necessary, so the application always
1420     specifies native format, and the data written will be in big-endian format.
1421 
1422     Params:
1423         dst =   the stream to which data will be written.
1424         value = the data to be written, in native format.
1425     
1426     Returns:
1427         true on successful write or false on failure; call SDL_GetError()
1428         for more information.
1429 
1430     Threadsafety:
1431         This function is not thread safe.
1432 
1433 */
1434 extern bool SDL_WriteU64BE(SDL_IOStream* dst, Uint64 value);
1435 
1436 /**
1437     Use this function to write 64 bits in native format to an SDL_IOStream as
1438     big-endian data.
1439 
1440     SDL byteswaps the data only if necessary, so the application always
1441     specifies native format, and the data written will be in big-endian format.
1442 
1443     Params:
1444         dst =   the stream to which data will be written.
1445         value = the data to be written, in native format.
1446     
1447     Returns:
1448         true on successful write or false on failure; call SDL_GetError()
1449         for more information.
1450 
1451     Threadsafety:
1452         This function is not thread safe.
1453 */
1454 extern bool SDL_WriteS64BE(SDL_IOStream* dst, Sint64 value);