comparison th_ioctx_mem.c @ 735:31bc1ed07cf5

Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 07 Dec 2022 12:14:39 +0200
parents 29e44a58bc73
children c17eadc60c3d
comparison
equal deleted inserted replaced
734:2ae1045f6c18 735:31bc1ed07cf5
16 // Setup some things based on the mode string 16 // Setup some things based on the mode string
17 switch (ctx->mode[0]) 17 switch (ctx->mode[0])
18 { 18 {
19 case 'a': 19 case 'a':
20 // Append 20 // Append
21 ctx->memWrite = TRUE; 21 ctx->memWrite = true;
22 ctx->memOffset = ctx->memSize; 22 ctx->memOffset = ctx->memSize;
23 break; 23 break;
24 24
25 case 'r': 25 case 'r':
26 // Read or read-write 26 // Read or read-write
30 30
31 case 'w': 31 case 'w':
32 // Write, so truncate size 32 // Write, so truncate size
33 ctx->memOffset = 0; 33 ctx->memOffset = 0;
34 ctx->memSize = 0; 34 ctx->memSize = 0;
35 ctx->memWrite = TRUE; 35 ctx->memWrite = true;
36 break; 36 break;
37 37
38 default: 38 default:
39 return THERR_INVALID_ARGS; 39 return THERR_INVALID_ARGS;
40 } 40 }
52 ctx->memSize = 0; 52 ctx->memSize = 0;
53 } 53 }
54 } 54 }
55 55
56 56
57 static BOOL th_mem_realloc(th_ioctx *ctx, const size_t newSize) 57 static bool th_mem_realloc(th_ioctx *ctx, const size_t newSize)
58 { 58 {
59 size_t grow; 59 size_t grow;
60 60
61 // Check write flag 61 // Check write flag
62 if (!ctx->memWrite) 62 if (!ctx->memWrite)
63 { 63 {
64 ctx->status = THERR_FWRITE; 64 ctx->status = THERR_FWRITE;
65 return FALSE; 65 return false;
66 } 66 }
67 67
68 if (ctx->memData == NULL) 68 if (ctx->memData == NULL)
69 ctx->memAlloc = 0; 69 ctx->memAlloc = 0;
70 70
71 // Check against max size 71 // Check against max size
72 if (ctx->maxSize > 0 && newSize > ctx->maxSize) 72 if (ctx->maxSize > 0 && newSize > ctx->maxSize)
73 { 73 {
74 ctx->status = THERR_BOUNDS; 74 ctx->status = THERR_BOUNDS;
75 return FALSE; 75 return false;
76 } 76 }
77 77
78 // New size is smaller than old 78 // New size is smaller than old
79 if (newSize < ctx->memAlloc) 79 if (newSize < ctx->memAlloc)
80 goto out; 80 goto out;
85 grow += newSize - ctx->memAlloc; 85 grow += newSize - ctx->memAlloc;
86 86
87 if (ctx->maxSize > 0 && ctx->memAlloc + grow >= ctx->maxSize) 87 if (ctx->maxSize > 0 && ctx->memAlloc + grow >= ctx->maxSize)
88 { 88 {
89 ctx->status = THERR_BOUNDS; 89 ctx->status = THERR_BOUNDS;
90 return FALSE; 90 return false;
91 } 91 }
92 92
93 // Grow the buffer 93 // Grow the buffer
94 ctx->memAlloc += grow; 94 ctx->memAlloc += grow;
95 if ((ctx->memData = th_realloc(ctx->memData, ctx->memAlloc)) == NULL) 95 if ((ctx->memData = th_realloc(ctx->memData, ctx->memAlloc)) == NULL)
96 { 96 {
97 ctx->status = THERR_MALLOC; 97 ctx->status = THERR_MALLOC;
98 return FALSE; 98 return false;
99 } 99 }
100 100
101 out: 101 out:
102 ctx->memSize = newSize; 102 ctx->memSize = newSize;
103 103
104 return TRUE; 104 return true;
105 } 105 }
106 106
107 107
108 static int th_mem_freset(th_ioctx *ctx) 108 static int th_mem_freset(th_ioctx *ctx)
109 { 109 {
169 { 169 {
170 return ctx->memOffset; 170 return ctx->memOffset;
171 } 171 }
172 172
173 173
174 static BOOL th_mem_feof(th_ioctx *ctx) 174 static bool th_mem_feof(th_ioctx *ctx)
175 { 175 {
176 return ((size_t) ctx->memOffset) >= ctx->memSize; 176 return ((size_t) ctx->memOffset) >= ctx->memSize;
177 } 177 }
178 178
179 179