rwops

rwops.nim

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

Types

RWopsAndroidio* = object
  asset*: pointer
RWopsWindowsioBuffer* = object
  data*: pointer
  size*: csize_t
  left*: csize_t
RWopsWindowsio* = object
  append*: bool
  h*: pointer
  buffer*: RWopsWindowsioBuffer
RWopsStdio* = object
  autoclose*: bool
  fp*: File
RWopsMemio* = object
  base*: ptr uint8
  here*: ptr uint8
  stop*: ptr uint8
RWopsUnknownio* = object
  data1*: pointer
  data2*: pointer
RWopsType* = RWopsKind
RWopsKind* {...}{.union.} = object
  androidio*: RWopsAndroidio
  windowsio*: RWopsWindowsio
  stdio*: RWopsStdio
  mem*: RWopsMemio
  unknown*: RWopsUnknownio
RWops* = object
  size*: proc (context: ptr RWops): int64 {...}{.cdecl.} ## ``Return`` the size of the file in this rwops, or `-1` if unknown
  seek*: proc (context: ptr RWops; offset: int64; whence: cint): int64 {...}{.cdecl.} ## Seek to ``offset`` relative to ``whence``,
                                                                                 ## one of stdio's whence values:
                                                                                 ## 
                                                                                 ## `RW_SEEK_SET`, `RW_SEEK_CUR`, `RW_SEEK_END`
                                                                                 ## 
                                                                                 ## ``Return`` the final offset in the data stream, or `-1` on error.
  read*: proc (context: ptr RWops; p: pointer; size: csize_t; maxnum: csize_t): csize_t {...}{.
      cdecl.} ## Read up to ``maxnum`` objects each of size ``size`` from the data
              ## stream to the area pointed at by ``p``.
              ## 
              ## ``Return`` the number of objects read, or `0` at error or end of file.
  write*: proc (context: ptr RWops; p: pointer; size: csize_t; num: csize_t): csize_t {...}{.
      cdecl.} ## Write exactly ``num`` objects each of size ``size`` from the area
              ## pointed at by ``p`` to data stream.
              ## 
              ## ``Return`` the number of objects written,
              ## or `0` at error or end of file.
  close*: proc (context: ptr RWops): cint {...}{.cdecl.} ## Close and free an allocated RWops object.
                                                    ## 
                                                    ## ``Return`` `0` if successful,
                                                    ## or `-1` on write error when flushing data.
  kind*: uint32
  mem*: RWopsKind
This is the read/write operation structure -- very basic.

Consts

RWOPS_UNKNOWN* = 0
Unknown stream type
RWOPS_WINFILE* = 1
Win32 file
RWOPS_STDFILE* = 2
Stdio file
RWOPS_JNIFILE* = 3
Android asset
RWOPS_MEMORY* = 4
Memory stream
RWOPS_MEMORY_RO* = 5
Read-Only memory stream
RW_SEEK_SET* = 0
Seek from the beginning of data
RW_SEEK_CUR* = 1
Seek relative to current read point
RW_SEEK_END* = 2
Seek relative to the end of data

Procs

proc rwFromFile*(file: cstring; mode: cstring): ptr RWops {...}{.cdecl,
    importc: "SDL_RWFromFile", dynlib: SDL2_LIB.}
proc rwFromFP*(fp: File; autoclose: bool): ptr RWops {...}{.cdecl,
    importc: "SDL_RWFromFP", dynlib: SDL2_LIB.}
proc rwFromMem*(mem: pointer; size: cint): ptr RWops {...}{.cdecl,
    importc: "SDL_RWFromMem", dynlib: SDL2_LIB.}
proc rwFromConstMem*(mem: pointer; size: cint): ptr RWops {...}{.cdecl,
    importc: "SDL_RWFromConstMem", dynlib: SDL2_LIB.}
proc allocRW*(): ptr RWops {...}{.cdecl, importc: "SDL_AllocRW", dynlib: SDL2_LIB.}
proc freeRW*(area: ptr RWops) {...}{.cdecl, importc: "SDL_FreeRW", dynlib: SDL2_LIB.}
proc rwSize*(context: ptr RWops): int64 {...}{.cdecl, importc: "SDL_RWsize",
    dynlib: SDL2_LIB.}
Return the size of the file in this rwops, or -1 if unknown.
proc rwSeek*(context: ptr RWops; offset: int64; whence: cint): int64 {...}{.cdecl,
    importc: "SDL_RWseek", dynlib: SDL2_LIB.}

Seek to offset relative to whence, one of stdio's whence values: RW_SEEK_SET, RW_SEEK_CUR, RW_SEEK_END.

Return the final offset in the data stream, or -1 on error.

proc rwTell*(context: ptr RWops): int64 {...}{.cdecl, importc: "SDL_RWtell",
    dynlib: SDL2_LIB.}
Return the current offset in the data stream, or -1 on error.
proc rwRead*(context: ptr RWops; p: pointer; size: csize_t; maxnum: csize_t): csize_t {...}{.
    cdecl, importc: "SDL_RWread", dynlib: SDL2_LIB.}

Read up to maxnum objects each of size size from the data stream to the area pointed at by p.

Return the number of objects read, or 0 at error or end of file.

proc rwWrite*(context: ptr RWops; p: pointer; size: csize_t; num: csize_t): csize_t {...}{.
    cdecl, importc: "SDL_RWwrite", dynlib: SDL2_LIB.}

Write exactly num objects each of size size from the area pointed at by p to data stream.

Return the number of objects written, or 0 at error or end of file.

proc rwClose*(context: ptr RWops): cint {...}{.cdecl, importc: "SDL_RWclose",
    dynlib: SDL2_LIB.}

Close and free an allocated sdl.RWops structure.

Return 0 if successful or -1 on write error when flushing data.

proc loadFileRW*(src: ptr RWops; datasize: ptr csize_t; freesrc: cint): pointer {...}{.
    cdecl, importc: "SDL_LoadFile_RW", dynlib: SDL2_LIB.}

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 nil, 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 free().

Return the data, or nil if there was an error.

proc loadFile*(file: cstring; datasize: ptr csize_t): pointer {...}{.cdecl,
    importc: "SDL_LoadFile", dynlib: SDL2_LIB.}

Load an entire file.

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

If datasize is not nil, 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().

Return the data, or nil if there was an error.

proc readU8*(src: ptr RWops): uint8 {...}{.cdecl, importc: "SDL_ReadU8",
                                      dynlib: SDL2_LIB.}
proc readLE16*(src: ptr RWops): uint16 {...}{.cdecl, importc: "SDL_ReadLE16",
    dynlib: SDL2_LIB.}
proc readBE16*(src: ptr RWops): uint16 {...}{.cdecl, importc: "SDL_ReadBE16",
    dynlib: SDL2_LIB.}
proc readLE32*(src: ptr RWops): uint32 {...}{.cdecl, importc: "SDL_ReadLE32",
    dynlib: SDL2_LIB.}
proc readBE32*(src: ptr RWops): uint32 {...}{.cdecl, importc: "SDL_ReadBE32",
    dynlib: SDL2_LIB.}
proc readLE64*(src: ptr RWops): uint64 {...}{.cdecl, importc: "SDL_ReadLE64",
    dynlib: SDL2_LIB.}
proc readBE64*(src: ptr RWops): uint64 {...}{.cdecl, importc: "SDL_ReadBE64",
    dynlib: SDL2_LIB.}
proc writeU8*(dst: ptr RWops; value: uint8): csize_t {...}{.cdecl,
    importc: "SDL_WriteU8", dynlib: SDL2_LIB.}
proc writeLE16*(dst: ptr RWops; value: uint16): csize_t {...}{.cdecl,
    importc: "SDL_WriteLE16", dynlib: SDL2_LIB.}
proc writeBE16*(dst: ptr RWops; value: uint16): csize_t {...}{.cdecl,
    importc: "SDL_WriteBE16", dynlib: SDL2_LIB.}
proc writeLE32*(dst: ptr RWops; value: uint32): csize_t {...}{.cdecl,
    importc: "SDL_WriteLE32", dynlib: SDL2_LIB.}
proc writeBE32*(dst: ptr RWops; value: uint32): csize_t {...}{.cdecl,
    importc: "SDL_WriteBE32", dynlib: SDL2_LIB.}
proc writeLE64*(dst: ptr RWops; value: uint64): csize_t {...}{.cdecl,
    importc: "SDL_WriteLE64", dynlib: SDL2_LIB.}
proc writeBE64*(dst: ptr RWops; value: uint64): csize_t {...}{.cdecl,
    importc: "SDL_WriteBE64", dynlib: SDL2_LIB.}

Templates

template loadFileRW*(src: ptr RWops; datasize: ptr csize_t; freesrc: bool): pointer