# HG changeset patch # User Matti Hamalainen # Date 1583772877 -7200 # Node ID a622d21833e1eae83d8b44e17640423503d13dcd # Parent ea6bcbfb9d183873a868c877dfdb05fb68de7184 Implement fopen() in mem ioctx, and simplistic read-only/write flag support. diff -r ea6bcbfb9d18 -r a622d21833e1 th_ioctx.h --- a/th_ioctx.h Mon Mar 09 17:59:06 2020 +0200 +++ b/th_ioctx.h Mon Mar 09 18:54:37 2020 +0200 @@ -49,6 +49,7 @@ size_t minAlloc; ///< Minimum allocation increase (0 = default) off_t memOffset; ///< Current offset in data uint8_t *memData; ///< Pointer to data + BOOL memWrite; ///< TRUE = memory can be written to / expanded etc. FALSE = no // Message functions void (*error)(struct th_ioctx *ctx, const int err, const char *msg); diff -r ea6bcbfb9d18 -r a622d21833e1 th_ioctx_mem.c --- a/th_ioctx_mem.c Mon Mar 09 17:59:06 2020 +0200 +++ b/th_ioctx_mem.c Mon Mar 09 18:54:37 2020 +0200 @@ -8,10 +8,52 @@ #include "th_ioctx.h" +static int th_mem_fopen(th_ioctx *ctx) +{ + if (ctx->mode == NULL) + return THERR_NULLPTR; + + // Setup some things based on the mode string + switch (ctx->mode[0]) + { + case 'a': + // Append + ctx->memWrite = TRUE; + ctx->memOffset = ctx->memSize; + break; + + case 'r': + // Read or read-write + ctx->memWrite = ctx->mode[1] == '+'; + ctx->memOffset = 0; + break; + + case 'w': + // Write, so truncate size + ctx->memOffset = 0; + ctx->memSize = 0; + ctx->memWrite = TRUE; + break; + + default: + return THERR_INVALID_ARGS; + } + + return THERR_OK; +} + + static BOOL th_mem_realloc(th_ioctx *ctx, const size_t newSize) { size_t grow; + // Check write flag + if (!ctx->memWrite) + { + ctx->status = THERR_FWRITE; + return FALSE; + } + // Check against max size if (ctx->maxSize > 0 && newSize > ctx->maxSize) { @@ -179,7 +221,7 @@ { "MemIO", - NULL, + th_mem_fopen, NULL, th_mem_freset,