comparison th_ioctx_mem.c @ 771:c17eadc60c3d

Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 20 Feb 2023 23:33:45 +0200
parents 31bc1ed07cf5
children 1959ba53e9dc
comparison
equal deleted inserted replaced
770:79f888e4616f 771:c17eadc60c3d
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) 11 static int th_mem_fopen(th_ioctx_t *ctx)
12 { 12 {
13 if (ctx->mode == NULL) 13 if (ctx->mode == NULL)
14 return THERR_NULLPTR; 14 return THERR_NULLPTR;
15 15
16 // Setup some things based on the mode string 16 // Setup some things based on the mode string
41 41
42 return THERR_OK; 42 return THERR_OK;
43 } 43 }
44 44
45 45
46 static void th_mem_fclose(th_ioctx *ctx) 46 static void th_mem_fclose(th_ioctx_t *ctx)
47 { 47 {
48 if (ctx->memFree) 48 if (ctx->memFree)
49 { 49 {
50 th_free_r(&ctx->memData); 50 th_free_r(&ctx->memData);
51 ctx->memAlloc = 0; 51 ctx->memAlloc = 0;
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_t *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)
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_t *ctx)
109 { 109 {
110 ctx->memOffset = 0; 110 ctx->memOffset = 0;
111 return THERR_OK; 111 return THERR_OK;
112 } 112 }
113 113
114 114
115 static int th_mem_ferror(th_ioctx *ctx) 115 static int th_mem_ferror(th_ioctx_t *ctx)
116 { 116 {
117 return ctx->status; 117 return ctx->status;
118 } 118 }
119 119
120 120
121 static int th_mem_fseek(th_ioctx *ctx, const off_t offset, const int whence) 121 static int th_mem_fseek(th_ioctx_t *ctx, const off_t offset, const int whence)
122 { 122 {
123 off_t newPos; 123 off_t newPos;
124 124
125 // Calculate the new position 125 // Calculate the new position
126 switch (whence) 126 switch (whence)
157 157
158 return 0; 158 return 0;
159 } 159 }
160 160
161 161
162 static off_t th_mem_fsize(th_ioctx *ctx) 162 static off_t th_mem_fsize(th_ioctx_t *ctx)
163 { 163 {
164 return ctx->memSize; 164 return ctx->memSize;
165 } 165 }
166 166
167 167
168 static off_t th_mem_ftell(th_ioctx *ctx) 168 static off_t th_mem_ftell(th_ioctx_t *ctx)
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_t *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
180 static int th_mem_fgetc(th_ioctx *ctx) 180 static int th_mem_fgetc(th_ioctx_t *ctx)
181 { 181 {
182 // Check for EOF 182 // Check for EOF
183 if ((size_t) ctx->memOffset < ctx->memSize) 183 if ((size_t) ctx->memOffset < ctx->memSize)
184 return ctx->memData[ctx->memOffset++]; 184 return ctx->memData[ctx->memOffset++];
185 else 185 else
186 return EOF; 186 return EOF;
187 } 187 }
188 188
189 189
190 static size_t th_mem_fread(void *buf, size_t size, size_t nmemb, th_ioctx *ctx) 190 static size_t th_mem_fread(void *buf, size_t size, size_t nmemb, th_ioctx_t *ctx)
191 { 191 {
192 size_t length = size * nmemb; 192 size_t length = size * nmemb;
193 193
194 // Check if we can read the whole chunk 194 // Check if we can read the whole chunk
195 if (((size_t) ctx->memOffset + length) >= ctx->memSize) 195 if (((size_t) ctx->memOffset + length) >= ctx->memSize)
206 206
207 return nmemb; 207 return nmemb;
208 } 208 }
209 209
210 210
211 static int th_mem_fputc(int ch, th_ioctx *ctx) 211 static int th_mem_fputc(int ch, th_ioctx_t *ctx)
212 { 212 {
213 // Check for EOF 213 // Check for EOF
214 if (!th_mem_realloc(ctx, ctx->memOffset + 1)) 214 if (!th_mem_realloc(ctx, ctx->memOffset + 1))
215 return EOF; 215 return EOF;
216 216
217 ctx->memData[ctx->memOffset++] = ch; 217 ctx->memData[ctx->memOffset++] = ch;
218 return ch; 218 return ch;
219 } 219 }
220 220
221 221
222 static size_t th_mem_fwrite(const void *buf, size_t size, size_t nmemb, th_ioctx *ctx) 222 static size_t th_mem_fwrite(const void *buf, size_t size, size_t nmemb, th_ioctx_t *ctx)
223 { 223 {
224 size_t length = size * nmemb; 224 size_t length = size * nmemb;
225 225
226 // Check if we can write the whole chunk 226 // Check if we can write the whole chunk
227 if (!th_mem_realloc(ctx, ctx->memOffset + length)) 227 if (!th_mem_realloc(ctx, ctx->memOffset + length))
237 } 237 }
238 return nmemb; 238 return nmemb;
239 } 239 }
240 240
241 241
242 const th_ioctx_ops th_mem_io_ops = 242 const th_ioctx_t_ops th_mem_io_ops =
243 { 243 {
244 "MemIO", 244 "MemIO",
245 245
246 th_mem_fopen, 246 th_mem_fopen,
247 th_mem_fclose, 247 th_mem_fclose,