User:Monster Iestyn/Source Code Documentation/byteptr.h

From SRB2 Wiki
Jump to navigation Jump to search
Online link GitHub entry
File type C header file
#include guard none

This header file defines macros for reading from/writing to a UINT8 pointer. They are used to save to and load data from various files used by the game – e.g.: gamedata files, Single Player savegame files, or $$$.sav for netgames.

Includes

Overview

Data type(s) Size (bytes) Macro(s) for writing Macro(s) for reading
UINT8,
boolean1
1 WRITEUINT8(p,b) READUINT8(p)
SINT8 1 WRITESINT8(p,b) READSINT8(p)
INT16 2 WRITEINT16(p,b) READINT16(p)
UINT16 2 WRITEUINT16(p,b) READUINT16(p)
INT32 4 WRITEINT32(p,b) READINT32(p)
UINT32 4 WRITEUINT32(p,b) READUINT32(p)
char 1 WRITECHAR(p,b) READCHAR(p)
fixed_t 4 WRITEFIXED(p,b) READFIXED(p)
angle_t 4 WRITEANGLE(p,b) READANGLE(p)
string (a char array) varies WRITESTRING(p,s)
WRITESTRINGN(p,s,n)
READSTRING(p,s)
READSTRINGN(p,s,n)
SKIPSTRING(p)
raw memory varies WRITEMEM(p,s,n) READMEM(p,s,n)

1 in SRB2's source code, booleans are typically written to/read from a UINT8 * using the UINT8 macros, for convenience.

Inline functions (Big-endian only)

Function name Return type Params Attributes Description
writeshort void void *ptr – destination,
INT32 val – value to write
FUNCINLINE,
static,
ATTRINLINE
Writes a 16-bit integer to a little-endian, unaligned destination.
writelong void void *ptr – destination,
INT32 val – value to write
FUNCINLINE,
static,
ATTRINLINE
Writes a 32-bit integer to a little-endian, unaligned destination.
readshort INT16 void *ptr – data to read from FUNCINLINE,
static,
ATTRINLINE
Reads a INT16 from little-endian, unaligned data.
readushort UINT16 void *ptr – data to read from FUNCINLINE,
static,
ATTRINLINE
Reads a UINT16 from little-endian, unaligned data.
readlong INT16 void *ptr – data to read from FUNCINLINE,
static,
ATTRINLINE
Reads a INT32 from little-endian, unaligned data.
readulong UINT16 void *ptr – data to read from FUNCINLINE,
static,
ATTRINLINE
Reads a UINT32 from little-endian, unaligned data.

Macros

Macro Description
DEALIGNED Defined only if any of the following macros are also defined:

If any of the above architectures or compilers are used, the "de-aligned" versions of the read/write macros must be used.

Note: this macro will be undefined again by the end of the file.

Writing macros

Macro Defined as Description
WRITEUINT8(p,b) Little-endian, DEALIGNED defined:
do {   UINT8 *p_tmp = (void *)p; const   UINT8 tv = (  UINT8)(b); memcpy(p, &tv, sizeof(  UINT8)); p_tmp++; p = (void *)p_tmp; } while (0)

Little-endian, other:

do {   UINT8 *p_tmp = (  UINT8 *)p; *p_tmp = (  UINT8)(b); p_tmp++; p = (void *)p_tmp; } while (0)

Big-endian:

do {  UINT8 *p_tmp = (  UINT8 *)p; *p_tmp       = (  UINT8)(b) ; p_tmp++; p = (void *)p_tmp;} while (0)
Writes the UINT8 value b to p
WRITESINT8(p,b) Little-endian, DEALIGNED defined:
do {   SINT8 *p_tmp = (void *)p; const   SINT8 tv = (  UINT8)(b); memcpy(p, &tv, sizeof(  UINT8)); p_tmp++; p = (void *)p_tmp; } while (0)

Little-endian, other:

do {   SINT8 *p_tmp = (  SINT8 *)p; *p_tmp = (  SINT8)(b); p_tmp++; p = (void *)p_tmp; } while (0)

Big-endian:

do {  SINT8 *p_tmp = (  SINT8 *)p; *p_tmp       = (  SINT8)(b) ; p_tmp++; p = (void *)p_tmp;} while (0)
Writes the SINT8 value b to p
WRITEINT16(p,b) Little-endian, DEALIGNED defined:
do {   INT16 *p_tmp = (void *)p; const   INT16 tv = (  INT16)(b); memcpy(p, &tv, sizeof(  INT16)); p_tmp++; p = (void *)p_tmp; } while (0)

Little-endian, other:

do {   INT16 *p_tmp = (  INT16 *)p; *p_tmp = (  INT16)(b); p_tmp++; p = (void *)p_tmp; } while (0)

Big-endian:

do {  INT16 *p_tmp = (  INT16 *)p; writeshort (p, (  INT16)(b)); p_tmp++; p = (void *)p_tmp;} while (0)
Writes the INT16 value b to p
WRITEUINT16(p,b) Little-endian, DEALIGNED defined:
do {  UINT16 *p_tmp = (void *)p; const  UINT16 tv = ( UINT16)(b); memcpy(p, &tv, sizeof( UINT16)); p_tmp++; p = (void *)p_tmp; } while (0)

Little-endian, other:

do {  UINT16 *p_tmp = ( UINT16 *)p; *p_tmp = ( UINT16)(b); p_tmp++; p = (void *)p_tmp; } while (0)

Big-endian:

do { UINT16 *p_tmp = ( UINT16 *)p; writeshort (p, ( UINT16)(b)); p_tmp++; p = (void *)p_tmp;} while (0)
Writes the UINT16 value b to p
WRITEINT32(p,b) Little-endian, DEALIGNED defined:
do {   INT32 *p_tmp = (void *)p; const   INT32 tv = (  INT32)(b); memcpy(p, &tv, sizeof(  INT32)); p_tmp++; p = (void *)p_tmp; } while (0)

Little-endian, other:

do {   INT32 *p_tmp = (  INT32 *)p; *p_tmp = (  INT32)(b); p_tmp++; p = (void *)p_tmp; } while (0)

Big-endian:

do {  INT32 *p_tmp = (  INT32 *)p; writelong  (p, (  INT32)(b)); p_tmp++; p = (void *)p_tmp;} while (0)
Writes the INT32 value b to p
WRITEUINT32(p,b) Little-endian, DEALIGNED defined:
do {  UINT32 *p_tmp = (void *)p; const  UINT32 tv = ( UINT32)(b); memcpy(p, &tv, sizeof( UINT32)); p_tmp++; p = (void *)p_tmp; } while (0)

Little-endian, other:

do {  UINT32 *p_tmp = ( UINT32 *)p; *p_tmp = ( UINT32)(b); p_tmp++; p = (void *)p_tmp; } while (0)

Big-endian:

do { UINT32 *p_tmp = ( UINT32 *)p; writelong  (p, ( UINT32)(b)); p_tmp++; p = (void *)p_tmp;} while (0)
Writes the UINT32 value b to p
WRITECHAR(p,b) Little-endian, DEALIGNED defined:
do {    char *p_tmp = (void *)p; const    char tv = (   char)(b); memcpy(p, &tv, sizeof(   char)); p_tmp++; p = (void *)p_tmp; } while (0)

Little-endian, other:

do {    char *p_tmp = (   char *)p; *p_tmp = (   char)(b); p_tmp++; p = (void *)p_tmp; } while (0)

Big-endian:

do {   char *p_tmp = (   char *)p; *p_tmp       = (   char)(b) ; p_tmp++; p = (void *)p_tmp;} while (0)
Writes the char b to p
WRITEFIXED(p,b) Little-endian, DEALIGNED defined:
do { fixed_t *p_tmp = (void *)p; const fixed_t tv = (fixed_t)(b); memcpy(p, &tv, sizeof(fixed_t)); p_tmp++; p = (void *)p_tmp; } while (0)

Little-endian, other:

do { fixed_t *p_tmp = (fixed_t *)p; *p_tmp = (fixed_t)(b); p_tmp++; p = (void *)p_tmp; } while (0)

Big-endian:

do {fixed_t *p_tmp = (fixed_t *)p; writelong  (p, (fixed_t)(b)); p_tmp++; p = (void *)p_tmp;} while (0)
Writes the fixed_t value b to p
WRITEANGLE(p,b) Little-endian, DEALIGNED defined:
do { angle_t *p_tmp = (void *)p; const angle_t tv = (angle_t)(b); memcpy(p, &tv, sizeof(angle_t)); p_tmp++; p = (void *)p_tmp; } while (0)

Little-endian, other:

do { angle_t *p_tmp = (angle_t *)p; *p_tmp = (angle_t)(b); p_tmp++; p = (void *)p_tmp; } while (0)

Big-endian:

do {angle_t *p_tmp = (angle_t *)p; writelong  (p, (angle_t)(b)); p_tmp++; p = (void *)p_tmp;} while (0)
Writes the angle_t value b to p
WRITESTRINGN(p,s,n)
do { size_t tmp_i = 0; for (; tmp_i < n && s[tmp_i] != '\0'; tmp_i++) WRITECHAR(p, s[tmp_i]); if (tmp_i < n) WRITECHAR(p, '\0');} while (0)
Writes n characters of the string s to p
WRITESTRING(p,s)
do { size_t tmp_i = 0; for (; s[tmp_i] != '\0'; tmp_i++) WRITECHAR(p, s[tmp_i]); WRITECHAR(p, '\0');} while (0)
Writes the string s to p
WRITEMEM(p,s,n)
do { memcpy(p, s, n); p += n; } while (0)
Writes n bytes of raw memory s to p

Reading macros

Macro Defined as Description
READUINT8(p) Little-endian, GCC, DEALIGNED defined:
({   UINT8 *p_tmp = (void *)p;   UINT8 b; memcpy(&b, p, sizeof(  UINT8)); p_tmp++; p = (void *)p_tmp; b; })

Little-endian, GCC, other:

({   UINT8 *p_tmp = (  UINT8 *)p;   UINT8 b = *p_tmp; p_tmp++; p = (void *)p_tmp; b; })

Little-endian, other:

*((  UINT8 *)p)++

Big-endian:

({   UINT8 *p_tmp = (  UINT8 *)p;   UINT8 b =        *p_tmp; p_tmp++; p = (void *)p_tmp; b; })
Reads a UINT8 value from p
READSINT8(p) Little-endian, GCC, DEALIGNED defined:
({   SINT8 *p_tmp = (void *)p;   SINT8 b; memcpy(&b, p, sizeof(  SINT8)); p_tmp++; p = (void *)p_tmp; b; })

Little-endian, GCC, other:

({   SINT8 *p_tmp = (  SINT8 *)p;   SINT8 b = *p_tmp; p_tmp++; p = (void *)p_tmp; b; })

Little-endian, other:

*((  SINT8 *)p)++

Big-endian:

({   SINT8 *p_tmp = (  SINT8 *)p;   SINT8 b =        *p_tmp; p_tmp++; p = (void *)p_tmp; b; })
Reads a SINT8 value from p
READINT16(p) Little-endian, GCC, DEALIGNED defined:
({   INT16 *p_tmp = (void *)p;   INT16 b; memcpy(&b, p, sizeof(  INT16)); p_tmp++; p = (void *)p_tmp; b; })

Little-endian, GCC, other:

({   INT16 *p_tmp = (  INT16 *)p;   INT16 b = *p_tmp; p_tmp++; p = (void *)p_tmp; b; })

Little-endian, other:

*((  INT16 *)p)++

Big-endian:

({   INT16 *p_tmp = (  INT16 *)p;   INT16 b =  readshort(p); p_tmp++; p = (void *)p_tmp; b; })
Reads a INT16 value from p
READUINT16(p) Little-endian, GCC, DEALIGNED defined:
({  UINT16 *p_tmp = (void *)p;  UINT16 b; memcpy(&b, p, sizeof( UINT16)); p_tmp++; p = (void *)p_tmp; b; })

Little-endian, GCC, other:

({  UINT16 *p_tmp = ( UINT16 *)p;  UINT16 b = *p_tmp; p_tmp++; p = (void *)p_tmp; b; })

Little-endian, other:

*(( UINT16 *)p)++

Big-endian:

({  UINT16 *p_tmp = ( UINT16 *)p;  UINT16 b = readushort(p); p_tmp++; p = (void *)p_tmp; b; })
Reads a UINT16 value from p
READINT32(p) Little-endian, GCC, DEALIGNED defined:
({   INT32 *p_tmp = (void *)p;   INT32 b; memcpy(&b, p, sizeof(  INT32)); p_tmp++; p = (void *)p_tmp; b; })

Little-endian, GCC, other:

({   INT32 *p_tmp = (  INT32 *)p;   INT32 b = *p_tmp; p_tmp++; p = (void *)p_tmp; b; })

Little-endian, other:

*((  INT32 *)p)++

Big-endian:

({   INT32 *p_tmp = (  INT32 *)p;   INT32 b =   readlong(p); p_tmp++; p = (void *)p_tmp; b; })
Reads a INT32 value from p
READUINT32(p) Little-endian, GCC, DEALIGNED defined:
({  UINT32 *p_tmp = (void *)p;  UINT32 b; memcpy(&b, p, sizeof( UINT32)); p_tmp++; p = (void *)p_tmp; b; })

Little-endian, GCC, other:

({  UINT32 *p_tmp = ( UINT32 *)p;  UINT32 b = *p_tmp; p_tmp++; p = (void *)p_tmp; b; })

Little-endian, other:

*(( UINT32 *)p)++

Big-endian:

({  UINT32 *p_tmp = ( UINT32 *)p;  UINT32 b =  readulong(p); p_tmp++; p = (void *)p_tmp; b; })
Reads a UINT32 value from p
READCHAR(p) Little-endian, GCC, DEALIGNED defined:
({    char *p_tmp = (void *)p;    char b; memcpy(&b, p, sizeof(   char)); p_tmp++; p = (void *)p_tmp; b; })

Little-endian, GCC, other:

({    char *p_tmp = (   char *)p;    char b = *p_tmp; p_tmp++; p = (void *)p_tmp; b; })

Little-endian, other:

*((   char *)p)++

Big-endian:

({    char *p_tmp = (   char *)p;    char b =        *p_tmp; p_tmp++; p = (void *)p_tmp; b; })
Reads a char from p
READFIXED(p) Little-endian, GCC, DEALIGNED defined:
({ fixed_t *p_tmp = (void *)p; fixed_t b; memcpy(&b, p, sizeof(fixed_t)); p_tmp++; p = (void *)p_tmp; b; })

Little-endian, GCC, other:

({ fixed_t *p_tmp = (fixed_t *)p; fixed_t b = *p_tmp; p_tmp++; p = (void *)p_tmp; b; })

Little-endian, other:

*((fixed_t *)p)++

Big-endian:

({ angle_t *p_tmp = (angle_t *)p; angle_t b =  readulong(p); p_tmp++; p = (void *)p_tmp; b; })
Reads a fixed_t value from p
READANGLE(p) Little-endian, GCC, DEALIGNED defined:
({ angle_t *p_tmp = (void *)p; angle_t b; memcpy(&b, p, sizeof(angle_t)); p_tmp++; p = (void *)p_tmp; b; })

Little-endian, GCC, other:

({ angle_t *p_tmp = (angle_t *)p; angle_t b = *p_tmp; p_tmp++; p = (void *)p_tmp; b; })

Little-endian, other:

*((angle_t *)p)++

Big-endian:

({ angle_t *p_tmp = (angle_t *)p; angle_t b =  readulong(p); p_tmp++; p = (void *)p_tmp; b; })
Reads a angle_t value from p
SKIPSTRING(p)
while (READCHAR(p) != '\0')
Skips the next string found in p
READSTRINGN(p,s,n)
({ size_t tmp_i = 0; for (; tmp_i < n && (s[tmp_i] = READCHAR(p)) != '\0'; tmp_i++); s[tmp_i] = '\0';})
Reads n characters of a string from p and assigns it to s
READSTRING(p,s)
({ size_t tmp_i = 0; for (; (s[tmp_i] = READCHAR(p)) != '\0'; tmp_i++); s[tmp_i] = '\0';})
Reads a string from p and assigns it to s
READMEM(p,s,n)
({ memcpy(s, p, n); p += n; })
Reads n bytes of raw memory from p and assigns it to s