Make byte stream manipulation code easier to read

This commit is contained in:
LJ Sonic 2022-01-10 18:56:42 +01:00
parent 1377824799
commit a668546088

View file

@ -150,27 +150,58 @@ FUNCINLINE static ATTRINLINE UINT32 readulong(void *ptr)
#undef DEALIGNED
#define 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)
#define 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)
#define WRITEMEM(p,s,n) do { memcpy(p, s, n); p += n; } while (0)
#define WRITESTRINGN(p, s, n) ({ \
size_t tmp_i; \
\
for (tmp_i = 0; tmp_i < n && s[tmp_i] != '\0'; tmp_i++) \
WRITECHAR(p, s[tmp_i]); \
\
if (tmp_i < n) \
WRITECHAR(p, '\0'); \
})
#define SKIPSTRING(p) while (READCHAR(p) != '\0')
#define SKIPSTRINGN(p,n) ({ size_t tmp_i = 0; for (; tmp_i < n && READCHAR(p) != '\0'; tmp_i++); })
#define WRITESTRING(p, s) ({ \
size_t tmp_i; \
\
for (tmp_i = 0; s[tmp_i] != '\0'; tmp_i++) \
WRITECHAR(p, s[tmp_i]); \
\
WRITECHAR(p, '\0'); \
})
#define 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';})
#define READSTRING(p,s) ({ size_t tmp_i = 0; for (; (s[tmp_i] = READCHAR(p)) != '\0'; tmp_i++); s[tmp_i] = '\0';})
#define READMEM(p,s,n) ({ memcpy(s, p, n); p += n; })
#define WRITEMEM(p, s, n) ({ \
memcpy(p, s, n); \
p += n; \
})
#if 0 // old names
#define WRITEBYTE(p,b) WRITEUINT8(p,b)
#define WRITESHORT(p,b) WRITEINT16(p,b)
#define WRITEUSHORT(p,b) WRITEUINT16(p,b)
#define WRITELONG(p,b) WRITEINT32(p,b)
#define WRITEULONG(p,b) WRITEUINT32(p,b)
#define SKIPSTRING(p) while (READCHAR(p) != '\0')
#define READBYTE(p) READUINT8(p)
#define READSHORT(p) READINT16(p)
#define READUSHORT(p) READUINT16(p)
#define READLONG(p) READINT32(p)
#define READULONG(p) READUINT32(p)
#endif
#define SKIPSTRINGN(p, n) ({ \
size_t tmp_i = 0; \
\
while (tmp_i < n && READCHAR(p) != '\0') \
tmp_i++; \
})
#define READSTRINGN(p, s, n) ({ \
size_t tmp_i = 0; \
\
while (tmp_i < n && (s[tmp_i] = READCHAR(p)) != '\0') \
tmp_i++; \
\
s[tmp_i] = '\0'; \
})
#define READSTRING(p, s) ({ \
size_t tmp_i = 0; \
\
while ((s[tmp_i] = READCHAR(p)) != '\0') \
tmp_i++; \
\
s[tmp_i] = '\0'; \
})
#define READMEM(p, s, n) ({ \
memcpy(s, p, n); \
p += n; \
})