comparison th_ioctx_mem.c @ 693:a622d21833e1

Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 09 Mar 2020 18:54:37 +0200
parents ea6bcbfb9d18
children 0fc5ddaccc57
comparison
equal deleted inserted replaced
692:ea6bcbfb9d18 693:a622d21833e1
6 * Please read file 'COPYING' for information on license and distribution. 6 * Please read file 'COPYING' for information on license and distribution.
7 */ 7 */
8 #include "th_ioctx.h" 8 #include "th_ioctx.h"
9 9
10 10
11 static int th_mem_fopen(th_ioctx *ctx)
12 {
13 if (ctx->mode == NULL)
14 return THERR_NULLPTR;
15
16 // Setup some things based on the mode string
17 switch (ctx->mode[0])
18 {
19 case 'a':
20 // Append
21 ctx->memWrite = TRUE;
22 ctx->memOffset = ctx->memSize;
23 break;
24
25 case 'r':
26 // Read or read-write
27 ctx->memWrite = ctx->mode[1] == '+';
28 ctx->memOffset = 0;
29 break;
30
31 case 'w':
32 // Write, so truncate size
33 ctx->memOffset = 0;
34 ctx->memSize = 0;
35 ctx->memWrite = TRUE;
36 break;
37
38 default:
39 return THERR_INVALID_ARGS;
40 }
41
42 return THERR_OK;
43 }
44
45
11 static BOOL th_mem_realloc(th_ioctx *ctx, const size_t newSize) 46 static BOOL th_mem_realloc(th_ioctx *ctx, const size_t newSize)
12 { 47 {
13 size_t grow; 48 size_t grow;
49
50 // Check write flag
51 if (!ctx->memWrite)
52 {
53 ctx->status = THERR_FWRITE;
54 return FALSE;
55 }
14 56
15 // Check against max size 57 // Check against max size
16 if (ctx->maxSize > 0 && newSize > ctx->maxSize) 58 if (ctx->maxSize > 0 && newSize > ctx->maxSize)
17 { 59 {
18 ctx->status = THERR_BOUNDS; 60 ctx->status = THERR_BOUNDS;
177 219
178 const th_ioctx_ops th_mem_io_ops = 220 const th_ioctx_ops th_mem_io_ops =
179 { 221 {
180 "MemIO", 222 "MemIO",
181 223
182 NULL, 224 th_mem_fopen,
183 NULL, 225 NULL,
184 226
185 th_mem_freset, 227 th_mem_freset,
186 th_mem_ferror, 228 th_mem_ferror,
187 th_mem_fseek, 229 th_mem_fseek,