SDL  2.0
SDL_rwops.h File Reference
#include "SDL_stdinc.h"
#include "SDL_error.h"
#include "begin_code.h"
#include "close_code.h"
+ Include dependency graph for SDL_rwops.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SDL_RWops

Macros

#define SDL_RWOPS_UNKNOWN   0U
#define SDL_RWOPS_WINFILE   1U
#define SDL_RWOPS_STDFILE   2U
#define SDL_RWOPS_JNIFILE   3U
#define SDL_RWOPS_MEMORY   4U
#define SDL_RWOPS_MEMORY_RO   5U
#define RW_SEEK_SET   0
#define RW_SEEK_CUR   1
#define RW_SEEK_END   2
#define SDL_LoadFile(file, datasize)   SDL_LoadFile_RW(SDL_RWFromFile(file, "rb"), datasize, 1)
Read/write macros

Macros to easily read and write from an SDL_RWops structure.

#define SDL_RWsize(ctx)   (ctx)->size(ctx)
#define SDL_RWseek(ctx, offset, whence)   (ctx)->seek(ctx, offset, whence)
#define SDL_RWtell(ctx)   (ctx)->seek(ctx, 0, RW_SEEK_CUR)
#define SDL_RWread(ctx, ptr, size, n)   (ctx)->read(ctx, ptr, size, n)
#define SDL_RWwrite(ctx, ptr, size, n)   (ctx)->write(ctx, ptr, size, n)
#define SDL_RWclose(ctx)   (ctx)->close(ctx)

Functions

SDL_RWopsSDL_AllocRW (void)
void SDL_FreeRW (SDL_RWops *area)
voidSDL_LoadFile_RW (SDL_RWops *src, size_t *datasize, int freesrc)
RWFrom functions

Functions to create SDL_RWops structures from various data streams.

SDL_RWopsSDL_RWFromFile (const char *file, const char *mode)
SDL_RWopsSDL_RWFromFP (FILE *fp, SDL_bool autoclose)
SDL_RWopsSDL_RWFromMem (void *mem, int size)
SDL_RWopsSDL_RWFromConstMem (const void *mem, int size)
Read endian functions

Read an item of the specified endianness and return in native format.

Uint8 SDL_ReadU8 (SDL_RWops *src)
Uint16 SDL_ReadLE16 (SDL_RWops *src)
Uint16 SDL_ReadBE16 (SDL_RWops *src)
Uint32 SDL_ReadLE32 (SDL_RWops *src)
Uint32 SDL_ReadBE32 (SDL_RWops *src)
Uint64 SDL_ReadLE64 (SDL_RWops *src)
Uint64 SDL_ReadBE64 (SDL_RWops *src)
Write endian functions

Write an item of native format to the specified endianness.

size_t SDL_WriteU8 (SDL_RWops *dst, Uint8 value)
size_t SDL_WriteLE16 (SDL_RWops *dst, Uint16 value)
size_t SDL_WriteBE16 (SDL_RWops *dst, Uint16 value)
size_t SDL_WriteLE32 (SDL_RWops *dst, Uint32 value)
size_t SDL_WriteBE32 (SDL_RWops *dst, Uint32 value)
size_t SDL_WriteLE64 (SDL_RWops *dst, Uint64 value)
size_t SDL_WriteBE64 (SDL_RWops *dst, Uint64 value)

Detailed Description

This file provides a general interface for SDL to read and write data streams. It can easily be extended to files, memory, etc.

Definition in file SDL_rwops.h.

Macro Definition Documentation

#define RW_SEEK_CUR   1

Seek relative to current read point

Definition at line 175 of file SDL_rwops.h.

Referenced by _testGenericRWopsValidations(), main(), mem_seek(), SDL_LoadBMP_RW(), and SDL_LoadWAV_RW().

#define RW_SEEK_END   2

Seek relative to the end of data

Definition at line 176 of file SDL_rwops.h.

Referenced by _testGenericRWopsValidations(), main(), and mem_seek().

#define RW_SEEK_SET   0

Seek from the beginning of data

Definition at line 174 of file SDL_rwops.h.

Referenced by _testGenericRWopsValidations(), main(), mem_seek(), rwops_testFileWriteReadEndian(), SDL_LoadBMP_RW(), and SDL_SaveBMP_RW().

#define SDL_LoadFile (   file,
  datasize 
)    SDL_LoadFile_RW(SDL_RWFromFile(file, "rb"), datasize, 1)

Load an entire file.

Convenience macro.

Definition at line 214 of file SDL_rwops.h.

Referenced by SDL_LoadVIDPIDListFromHint().

#define SDL_RWOPS_JNIFILE   3U

Android asset

Definition at line 45 of file SDL_rwops.h.

Referenced by rwops_testFileRead(), rwops_testFileWrite(), and SDL_RWFromFile().

#define SDL_RWOPS_MEMORY   4U

Memory stream

Definition at line 46 of file SDL_rwops.h.

Referenced by rwops_testMem(), and SDL_RWFromMem().

#define SDL_RWOPS_MEMORY_RO   5U

Read-Only memory stream

Definition at line 47 of file SDL_rwops.h.

Referenced by rwops_testConstMem(), and SDL_RWFromConstMem().

#define SDL_RWOPS_STDFILE   2U

Stdio file

Definition at line 44 of file SDL_rwops.h.

Referenced by rwops_testFileRead(), rwops_testFileWrite(), rwops_testFPRead(), and rwops_testFPWrite().

#define SDL_RWOPS_UNKNOWN   0U

Unknown stream type

Definition at line 42 of file SDL_rwops.h.

Referenced by rwops_testAllocFree(), and SDL_AllocRW().

#define SDL_RWOPS_WINFILE   1U

Win32 file

Definition at line 43 of file SDL_rwops.h.

Referenced by rwops_testFileRead(), rwops_testFileWrite(), and SDL_RWFromFile().

#define SDL_RWseek (   ctx,
  offset,
  whence 
)    (ctx)->seek(ctx, offset, whence)
#define SDL_RWsize (   ctx)    (ctx)->size(ctx)

Definition at line 184 of file SDL_rwops.h.

Referenced by SDL_GameControllerAddMappingsFromRW(), and SDL_LoadFile_RW().

#define SDL_RWtell (   ctx)    (ctx)->seek(ctx, 0, RW_SEEK_CUR)

Definition at line 186 of file SDL_rwops.h.

Referenced by SDL_LoadBMP_RW(), and SDL_SaveBMP_RW().

#define SDL_RWwrite (   ctx,
  ptr,
  size,
  n 
)    (ctx)->write(ctx, ptr, size, n)

Function Documentation

SDL_RWops* SDL_AllocRW ( void  )

Definition at line 682 of file SDL_rwops.c.

References NULL, SDL_malloc, SDL_OutOfMemory, SDL_RWOPS_UNKNOWN, and SDL_RWops::type.

{
SDL_RWops *area;
area = (SDL_RWops *) SDL_malloc(sizeof *area);
if (area == NULL) {
} else {
}
return area;
}
void SDL_FreeRW ( SDL_RWops area)

Definition at line 696 of file SDL_rwops.c.

References SDL_free.

{
SDL_free(area);
}
void* SDL_LoadFile_RW ( SDL_RWops src,
size_t datasize,
int  freesrc 
)

Load all the data from an SDL data stream.

The data is allocated with a zero byte at the end (null terminated)

If datasize is not NULL, it is filled with the size of the data read.

If freesrc is non-zero, the stream will be closed after being read.

The data should be freed with SDL_free().

Returns
the data, or NULL if there was an error.

Definition at line 703 of file SDL_rwops.c.

References done, NULL, SDL_free, SDL_InvalidParamError, SDL_malloc, SDL_OutOfMemory, SDL_realloc, SDL_RWclose, SDL_RWread, and SDL_RWsize.

{
const int FILE_CHUNK_SIZE = 1024;
size_t size_read, size_total;
void *data = NULL, *newdata;
if (!src) {
return NULL;
}
size = SDL_RWsize(src);
if (size < 0) {
size = FILE_CHUNK_SIZE;
}
data = SDL_malloc((size_t)(size + 1));
size_total = 0;
for (;;) {
if ((((Sint64)size_total) + FILE_CHUNK_SIZE) > size) {
size = (size_total + FILE_CHUNK_SIZE);
newdata = SDL_realloc(data, (size_t)(size + 1));
if (!newdata) {
SDL_free(data);
data = NULL;
goto done;
}
data = newdata;
}
size_read = SDL_RWread(src, (char *)data+size_total, 1, (size_t)(size-size_total));
if (size_read == 0) {
break;
}
size_total += size_read;
}
if (datasize) {
*datasize = size_total;
}
((char *)data)[size_total] = '\0';
if (freesrc && src) {
}
return data;
}
Uint16 SDL_ReadBE16 ( SDL_RWops src)

Definition at line 775 of file SDL_rwops.c.

References SDL_RWread, and SDL_SwapBE16.

{
SDL_RWread(src, &value, sizeof (value), 1);
return SDL_SwapBE16(value);
}
Uint32 SDL_ReadBE32 ( SDL_RWops src)

Definition at line 793 of file SDL_rwops.c.

References SDL_RWread, and SDL_SwapBE32.

{
SDL_RWread(src, &value, sizeof (value), 1);
return SDL_SwapBE32(value);
}
Uint64 SDL_ReadBE64 ( SDL_RWops src)

Definition at line 811 of file SDL_rwops.c.

References SDL_RWread, and SDL_SwapBE64.

{
SDL_RWread(src, &value, sizeof (value), 1);
return SDL_SwapBE64(value);
}
Uint16 SDL_ReadLE16 ( SDL_RWops src)

Definition at line 766 of file SDL_rwops.c.

References SDL_RWread, and SDL_SwapLE16.

{
SDL_RWread(src, &value, sizeof (value), 1);
return SDL_SwapLE16(value);
}
Uint32 SDL_ReadLE32 ( SDL_RWops src)

Definition at line 784 of file SDL_rwops.c.

References SDL_RWread, and SDL_SwapLE32.

{
SDL_RWread(src, &value, sizeof (value), 1);
return SDL_SwapLE32(value);
}
Uint64 SDL_ReadLE64 ( SDL_RWops src)

Definition at line 802 of file SDL_rwops.c.

References SDL_RWread, and SDL_SwapLE64.

{
SDL_RWread(src, &value, sizeof (value), 1);
return SDL_SwapLE64(value);
}
Uint8 SDL_ReadU8 ( SDL_RWops src)

Definition at line 757 of file SDL_rwops.c.

References SDL_RWread.

{
Uint8 value = 0;
SDL_RWread(src, &value, sizeof (value), 1);
return value;
}
SDL_RWops* SDL_RWFromConstMem ( const void mem,
int  size 
)

Definition at line 654 of file SDL_rwops.c.

References SDL_RWops::close, SDL_RWops::hidden, SDL_RWops::mem, mem_close(), mem_read(), mem_seek(), mem_size(), mem_writeconst(), NULL, SDL_RWops::read, SDL_AllocRW, SDL_InvalidParamError, SDL_RWOPS_MEMORY_RO, SDL_RWops::seek, SDL_RWops::size, SDL_RWops::type, and SDL_RWops::write.

{
SDL_RWops *rwops = NULL;
if (!mem) {
return rwops;
}
if (!size) {
return rwops;
}
rwops = SDL_AllocRW();
if (rwops != NULL) {
rwops->size = mem_size;
rwops->seek = mem_seek;
rwops->read = mem_read;
rwops->close = mem_close;
rwops->hidden.mem.base = (Uint8 *) mem;
rwops->hidden.mem.here = rwops->hidden.mem.base;
rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
}
return rwops;
}
SDL_RWops* SDL_RWFromFile ( const char *  file,
const char *  mode 
)

Definition at line 511 of file SDL_rwops.c.

References Android_JNI_FileClose(), Android_JNI_FileOpen(), Android_JNI_FileRead(), Android_JNI_FileSeek(), Android_JNI_FileSize(), Android_JNI_FileWrite(), SDL_RWops::close, NULL, SDL_RWops::read, SDL_AllocRW, SDL_AndroidGetInternalStoragePath, SDL_FreeRW, SDL_RWFromFP, SDL_RWOPS_JNIFILE, SDL_RWOPS_WINFILE, SDL_SetError, SDL_snprintf, SDL_stack_alloc, SDL_stack_free, SDL_RWops::seek, SDL_RWops::size, SDL_RWops::type, and SDL_RWops::write.

{
SDL_RWops *rwops = NULL;
if (!file || !*file || !mode || !*mode) {
SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
return NULL;
}
#if defined(__ANDROID__)
#ifdef HAVE_STDIO_H
/* Try to open the file on the filesystem first */
if (*file == '/') {
FILE *fp = fopen(file, mode);
if (fp) {
return SDL_RWFromFP(fp, 1);
}
} else {
/* Try opening it from internal storage if it's a relative path */
char *path;
FILE *fp;
path = SDL_stack_alloc(char, PATH_MAX);
if (path) {
SDL_snprintf(path, PATH_MAX, "%s/%s",
fp = fopen(path, mode);
if (fp) {
return SDL_RWFromFP(fp, 1);
}
}
}
#endif /* HAVE_STDIO_H */
/* Try to open the file from the asset system */
rwops = SDL_AllocRW();
if (!rwops)
return NULL; /* SDL_SetError already setup by SDL_AllocRW() */
if (Android_JNI_FileOpen(rwops, file, mode) < 0) {
SDL_FreeRW(rwops);
return NULL;
}
#elif defined(__WIN32__)
rwops = SDL_AllocRW();
if (!rwops)
return NULL; /* SDL_SetError already setup by SDL_AllocRW() */
if (windows_file_open(rwops, file, mode) < 0) {
SDL_FreeRW(rwops);
return NULL;
}
rwops->size = windows_file_size;
rwops->seek = windows_file_seek;
rwops->read = windows_file_read;
rwops->write = windows_file_write;
rwops->close = windows_file_close;
#elif HAVE_STDIO_H
{
#ifdef __APPLE__
FILE *fp = SDL_OpenFPFromBundleOrFallback(file, mode);
#elif __WINRT__
FILE *fp = NULL;
fopen_s(&fp, file, mode);
#else
FILE *fp = fopen(file, mode);
#endif
if (fp == NULL) {
SDL_SetError("Couldn't open %s", file);
} else {
rwops = SDL_RWFromFP(fp, 1);
}
}
#else
SDL_SetError("SDL not compiled with stdio support");
#endif /* !HAVE_STDIO_H */
return rwops;
}
SDL_RWops* SDL_RWFromFP ( FILE *  fp,
SDL_bool  autoclose 
)
SDL_RWops* SDL_RWFromMem ( void mem,
int  size 
)

Definition at line 626 of file SDL_rwops.c.

References SDL_RWops::close, SDL_RWops::hidden, SDL_RWops::mem, mem_close(), mem_read(), mem_seek(), mem_size(), mem_write(), NULL, SDL_RWops::read, SDL_AllocRW, SDL_InvalidParamError, SDL_RWOPS_MEMORY, SDL_RWops::seek, SDL_RWops::size, SDL_RWops::type, and SDL_RWops::write.

{
SDL_RWops *rwops = NULL;
if (!mem) {
return rwops;
}
if (!size) {
return rwops;
}
rwops = SDL_AllocRW();
if (rwops != NULL) {
rwops->size = mem_size;
rwops->seek = mem_seek;
rwops->read = mem_read;
rwops->write = mem_write;
rwops->close = mem_close;
rwops->hidden.mem.base = (Uint8 *) mem;
rwops->hidden.mem.here = rwops->hidden.mem.base;
rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
}
return rwops;
}
size_t SDL_WriteBE16 ( SDL_RWops dst,
Uint16  value 
)

Definition at line 833 of file SDL_rwops.c.

References SDL_RWwrite, and SDL_SwapBE16.

{
const Uint16 swapped = SDL_SwapBE16(value);
return SDL_RWwrite(dst, &swapped, sizeof (swapped), 1);
}
size_t SDL_WriteBE32 ( SDL_RWops dst,
Uint32  value 
)

Definition at line 847 of file SDL_rwops.c.

References SDL_RWwrite, and SDL_SwapBE32.

{
const Uint32 swapped = SDL_SwapBE32(value);
return SDL_RWwrite(dst, &swapped, sizeof (swapped), 1);
}
size_t SDL_WriteBE64 ( SDL_RWops dst,
Uint64  value 
)

Definition at line 861 of file SDL_rwops.c.

References SDL_RWwrite, and SDL_SwapBE64.

{
const Uint64 swapped = SDL_SwapBE64(value);
return SDL_RWwrite(dst, &swapped, sizeof (swapped), 1);
}
size_t SDL_WriteLE16 ( SDL_RWops dst,
Uint16  value 
)

Definition at line 826 of file SDL_rwops.c.

References SDL_RWwrite, and SDL_SwapLE16.

{
const Uint16 swapped = SDL_SwapLE16(value);
return SDL_RWwrite(dst, &swapped, sizeof (swapped), 1);
}
size_t SDL_WriteLE32 ( SDL_RWops dst,
Uint32  value 
)

Definition at line 840 of file SDL_rwops.c.

References SDL_RWwrite, and SDL_SwapLE32.

{
const Uint32 swapped = SDL_SwapLE32(value);
return SDL_RWwrite(dst, &swapped, sizeof (swapped), 1);
}
size_t SDL_WriteLE64 ( SDL_RWops dst,
Uint64  value 
)

Definition at line 854 of file SDL_rwops.c.

References SDL_RWwrite, and SDL_SwapLE64.

{
const Uint64 swapped = SDL_SwapLE64(value);
return SDL_RWwrite(dst, &swapped, sizeof (swapped), 1);
}
size_t SDL_WriteU8 ( SDL_RWops dst,
Uint8  value 
)

Definition at line 820 of file SDL_rwops.c.

References SDL_RWwrite.

{
return SDL_RWwrite(dst, &value, sizeof (value), 1);
}