annotate th_ioctx_mem.c @ 719:d51c277bea6b

Set th_ioctx::memAlloc to zero in th_mem_fclose() if memory is freed. This fixes the issue if file is reopened and th_mem_realloc() fails to notice that we have no allocated memory.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 11 Nov 2020 13:35:03 +0200
parents ffd97ca9dfce
children ff724f45162a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
678
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * Simple I/O abstraction and context handling layer
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Programmed and designed by Matti 'ccr' Hamalainen
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 * (C) Copyright 2012-2020 Tecnic Software productions (TNSP)
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 *
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 * Please read file 'COPYING' for information on license and distribution.
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 */
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 #include "th_ioctx.h"
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10
693
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
11 static int th_mem_fopen(th_ioctx *ctx)
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
12 {
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
13 if (ctx->mode == NULL)
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
14 return THERR_NULLPTR;
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
15
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
16 // Setup some things based on the mode string
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
17 switch (ctx->mode[0])
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
18 {
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
19 case 'a':
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
20 // Append
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
21 ctx->memWrite = TRUE;
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
22 ctx->memOffset = ctx->memSize;
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
23 break;
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
24
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
25 case 'r':
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
26 // Read or read-write
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
27 ctx->memWrite = ctx->mode[1] == '+';
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
28 ctx->memOffset = 0;
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
29 break;
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
30
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
31 case 'w':
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
32 // Write, so truncate size
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
33 ctx->memOffset = 0;
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
34 ctx->memSize = 0;
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
35 ctx->memWrite = TRUE;
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
36 break;
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
37
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
38 default:
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
39 return THERR_INVALID_ARGS;
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
40 }
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
41
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
42 return THERR_OK;
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
43 }
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
44
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
45
694
0fc5ddaccc57 Implement fclose() in mem ioctx, with an context flag for freeing the memory
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
46 static void th_mem_fclose(th_ioctx *ctx)
0fc5ddaccc57 Implement fclose() in mem ioctx, with an context flag for freeing the memory
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
47 {
0fc5ddaccc57 Implement fclose() in mem ioctx, with an context flag for freeing the memory
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
48 if (ctx->memFree)
719
d51c277bea6b Set th_ioctx::memAlloc to zero in th_mem_fclose() if memory is freed. This fixes the issue if file is reopened and th_mem_realloc() fails to notice that we have no allocated memory.
Matti Hamalainen <ccr@tnsp.org>
parents: 718
diff changeset
49 {
694
0fc5ddaccc57 Implement fclose() in mem ioctx, with an context flag for freeing the memory
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
50 th_free_r(&ctx->memData);
719
d51c277bea6b Set th_ioctx::memAlloc to zero in th_mem_fclose() if memory is freed. This fixes the issue if file is reopened and th_mem_realloc() fails to notice that we have no allocated memory.
Matti Hamalainen <ccr@tnsp.org>
parents: 718
diff changeset
51 ctx->memAlloc = 0;
d51c277bea6b Set th_ioctx::memAlloc to zero in th_mem_fclose() if memory is freed. This fixes the issue if file is reopened and th_mem_realloc() fails to notice that we have no allocated memory.
Matti Hamalainen <ccr@tnsp.org>
parents: 718
diff changeset
52 ctx->memSize = 0;
d51c277bea6b Set th_ioctx::memAlloc to zero in th_mem_fclose() if memory is freed. This fixes the issue if file is reopened and th_mem_realloc() fails to notice that we have no allocated memory.
Matti Hamalainen <ccr@tnsp.org>
parents: 718
diff changeset
53 }
694
0fc5ddaccc57 Implement fclose() in mem ioctx, with an context flag for freeing the memory
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
54 }
0fc5ddaccc57 Implement fclose() in mem ioctx, with an context flag for freeing the memory
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
55
0fc5ddaccc57 Implement fclose() in mem ioctx, with an context flag for freeing the memory
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
56
678
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 static BOOL th_mem_realloc(th_ioctx *ctx, const size_t newSize)
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 size_t grow;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60
693
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
61 // Check write flag
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
62 if (!ctx->memWrite)
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
63 {
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
64 ctx->status = THERR_FWRITE;
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
65 return FALSE;
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
66 }
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
67
692
ea6bcbfb9d18 Comments and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 681
diff changeset
68 // Check against max size
678
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 if (ctx->maxSize > 0 && newSize > ctx->maxSize)
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 ctx->status = THERR_BOUNDS;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 return FALSE;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 }
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74
692
ea6bcbfb9d18 Comments and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 681
diff changeset
75 // New size is smaller than old
678
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 if (newSize < ctx->memAlloc)
692
ea6bcbfb9d18 Comments and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 681
diff changeset
77 goto out;
678
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78
692
ea6bcbfb9d18 Comments and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 681
diff changeset
79 // Compute the allocation grow amount
678
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 grow = (ctx->minAlloc > 0) ? ctx->minAlloc : 8 * 1024;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 if (newSize - ctx->memAlloc > grow)
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 grow += newSize - ctx->memAlloc;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 if (ctx->maxSize > 0 && ctx->memAlloc + grow >= ctx->maxSize)
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 ctx->status = THERR_BOUNDS;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 return FALSE;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 }
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89
692
ea6bcbfb9d18 Comments and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 681
diff changeset
90 // Grow the buffer
678
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 ctx->memAlloc += grow;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 if ((ctx->memData = th_realloc(ctx->memData, ctx->memAlloc)) == NULL)
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 ctx->status = THERR_MALLOC;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 return FALSE;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 }
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97
692
ea6bcbfb9d18 Comments and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 681
diff changeset
98 out:
678
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 ctx->memSize = newSize;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 return TRUE;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 }
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 static int th_mem_freset(th_ioctx *ctx)
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 ctx->memOffset = 0;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 return THERR_OK;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 }
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 static int th_mem_ferror(th_ioctx *ctx)
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 return ctx->status;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115 }
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 static int th_mem_fseek(th_ioctx *ctx, const off_t offset, const int whence)
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 off_t newPos;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 // Calculate the new position
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 switch (whence)
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 case SEEK_SET:
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 newPos = offset;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 break;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 case SEEK_CUR:
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 newPos = ctx->memOffset + offset;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 break;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 case SEEK_END:
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 newPos = ctx->memSize + offset;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 break;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137 default:
717
e73200c4584a Add some error status setting in memio.
Matti Hamalainen <ccr@tnsp.org>
parents: 694
diff changeset
138 ctx->status = THERR_FSEEK;
678
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 return -1;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 }
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 // Set the new position
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 ctx->memOffset = newPos;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145 // Check the new position
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146 if (newPos < 0)
717
e73200c4584a Add some error status setting in memio.
Matti Hamalainen <ccr@tnsp.org>
parents: 694
diff changeset
147 {
e73200c4584a Add some error status setting in memio.
Matti Hamalainen <ccr@tnsp.org>
parents: 694
diff changeset
148 ctx->status = THERR_FSEEK;
678
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149 return -1;
717
e73200c4584a Add some error status setting in memio.
Matti Hamalainen <ccr@tnsp.org>
parents: 694
diff changeset
150 }
678
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 //if (!th_mem_realloc(ctx, newPos))
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153 // return -1;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
154
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155 return 0;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
156 }
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
157
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
158
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159 static off_t th_mem_fsize(th_ioctx *ctx)
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
161 return ctx->memSize;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
162 }
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
163
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165 static off_t th_mem_ftell(th_ioctx *ctx)
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
166 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167 return ctx->memOffset;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
168 }
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
169
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
170
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
171 static BOOL th_mem_feof(th_ioctx *ctx)
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173 return ((size_t) ctx->memOffset) >= ctx->memSize;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
174 }
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
177 static int th_mem_fgetc(th_ioctx *ctx)
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
178 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
179 // Check for EOF
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
180 if ((size_t) ctx->memOffset < ctx->memSize)
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181 return ctx->memData[ctx->memOffset++];
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
182 else
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
183 return EOF;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
184 }
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
185
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
186
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
187 static size_t th_mem_fread(void *buf, size_t size, size_t nmemb, th_ioctx *ctx)
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
188 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
189 size_t length = size * nmemb;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191 // Check if we can read the whole chunk
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
192 if (((size_t) ctx->memOffset + length) >= ctx->memSize)
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
193 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
194 nmemb = (ctx->memSize - ctx->memOffset) / size;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
195 length = size * nmemb;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
196 }
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
197
718
ffd97ca9dfce Check for length > 0.
Matti Hamalainen <ccr@tnsp.org>
parents: 717
diff changeset
198 if (length > 0)
ffd97ca9dfce Check for length > 0.
Matti Hamalainen <ccr@tnsp.org>
parents: 717
diff changeset
199 {
ffd97ca9dfce Check for length > 0.
Matti Hamalainen <ccr@tnsp.org>
parents: 717
diff changeset
200 memcpy(buf, ctx->memData + ctx->memOffset, length);
ffd97ca9dfce Check for length > 0.
Matti Hamalainen <ccr@tnsp.org>
parents: 717
diff changeset
201 ctx->memOffset += length;
ffd97ca9dfce Check for length > 0.
Matti Hamalainen <ccr@tnsp.org>
parents: 717
diff changeset
202 }
ffd97ca9dfce Check for length > 0.
Matti Hamalainen <ccr@tnsp.org>
parents: 717
diff changeset
203
678
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
204 return nmemb;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
205 }
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
206
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
207
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
208 static int th_mem_fputc(int ch, th_ioctx *ctx)
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
209 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210 // Check for EOF
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
211 if (!th_mem_realloc(ctx, ctx->memOffset + 1))
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
212 return EOF;
681
81d1a3f53b87 Cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
213
678
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
214 ctx->memData[ctx->memOffset++] = ch;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
215 return ch;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
216 }
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
217
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
218
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
219 static size_t th_mem_fwrite(const void *buf, size_t size, size_t nmemb, th_ioctx *ctx)
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
220 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
221 size_t length = size * nmemb;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
222
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
223 // Check if we can write the whole chunk
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
224 if (!th_mem_realloc(ctx, ctx->memOffset + length))
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
225 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
226 nmemb = (ctx->memSize - ctx->memOffset) / size;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
227 length = size * nmemb;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
228 }
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
229
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
230 if (length > 0)
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
231 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
232 memcpy(ctx->memData + ctx->memOffset, buf, length);
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
233 ctx->memOffset += length;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
234 }
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
235 return nmemb;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
236 }
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
237
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
238
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
239 const th_ioctx_ops th_mem_io_ops =
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
240 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
241 "MemIO",
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
242
693
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
243 th_mem_fopen,
694
0fc5ddaccc57 Implement fclose() in mem ioctx, with an context flag for freeing the memory
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
244 th_mem_fclose,
678
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
245
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
246 th_mem_freset,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 th_mem_ferror,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
248 th_mem_fseek,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
249 th_mem_fsize,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
250 th_mem_ftell,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
251 th_mem_feof,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
252 th_mem_fgetc,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
253 th_mem_fputc,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
254 th_mem_fread,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
255 th_mem_fwrite,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
256
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
257 NULL,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258 NULL,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259 NULL
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260 };