view src/dmgrowbuf.h @ 2298:b5abfff07ca9

Add new DMGrowBuf helper functions dmGrowBufCopyOffsSize() and dmGrowBufConstCopyOffsSize().
author Matti Hamalainen <>
date Thu, 04 Jul 2019 10:54:16 +0300
parents 39b4e06785f5
line wrap: on
line source

 * DMLib
 * -- Growable buffer implementation
 * Programmed and designed by Matti 'ccr' Hamalainen
 * (C) Copyright 2018-2019 Tecnic Software productions (TNSP)

#include "dmlib.h"

#ifdef __cplusplus
extern "C" {

typedef struct
        *data;      // Actually allocated data pointer

        offs,       // Current offset
        size,       // Actual allocated size
        len,        // Size requested
        mingrow;    // Minimum amount of bytes the allocation size grows by

        backwards,  // TRUE if the buffer grows backwards (e.g. "offs" moves backwards)
        literal,    // TRUE if dmGrowBufPut*() functions stores data "literally" in backwards mode
        is_const;   // TRUE will cause any reallocs etc. modifications to fail
} DMGrowBuf;

int    dmGrowBufInit(DMGrowBuf *buf);
int    dmGrowBufAlloc(DMGrowBuf *buf, const size_t initial, const size_t mingrow);
void   dmGrowBufFree(DMGrowBuf *buf);

DMGrowBuf * dmGrowBufCopy(DMGrowBuf *dst, const DMGrowBuf *src, const size_t enlarge);
DMGrowBuf * dmGrowBufCopyOffs(DMGrowBuf *dst, const DMGrowBuf *src, const size_t offs, const size_t enlarge);
DMGrowBuf * dmGrowBufCopyOffsSize(DMGrowBuf *dst, const DMGrowBuf *src, const size_t offs, const size_t len, const size_t enlarge);

DMGrowBuf * dmGrowBufConstCopy(DMGrowBuf *dst, const DMGrowBuf *src);
DMGrowBuf * dmGrowBufConstCopyOffs(DMGrowBuf *dst, const DMGrowBuf *src, const size_t offs);
DMGrowBuf * dmGrowBufConstCopyOffsSize(DMGrowBuf *dst, const DMGrowBuf *src, const size_t offs, const size_t len);
DMGrowBuf * dmGrowBufConstCreateFrom(DMGrowBuf *buf, Uint8 *data, const size_t len);

BOOL   dmGrowBufGrow(DMGrowBuf *buf, const size_t amount);
BOOL   dmGrowBufCheckGrow(DMGrowBuf *buf, const size_t nsize);

BOOL   dmGrowBufPut(DMGrowBuf *buf, const Uint8 *data, const size_t len);
BOOL   dmGrowBufPutU8(DMGrowBuf *buf, const Uint8 value);
BOOL   dmGrowBufPutU16BE(DMGrowBuf *buf, const Uint16 val);
BOOL   dmGrowBufPutU16LE(DMGrowBuf *buf, const Uint16 val);
BOOL   dmGrowBufPutU32BE(DMGrowBuf *buf, const Uint32 val);
BOOL   dmGrowBufPutU32LE(DMGrowBuf *buf, const Uint32 val);

BOOL   dmGrowBufGetU8(DMGrowBuf *buf, Uint8 *value);

#ifdef __cplusplus

#endif // DMGROWBUF_H