annotate th_ioctx_mem.c @ 694:0fc5ddaccc57

Implement fclose() in mem ioctx, with an context flag for freeing the memory on fclose().
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 09 Mar 2020 18:55:54 +0200
parents a622d21833e1
children e73200c4584a
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)
0fc5ddaccc57 Implement fclose() in mem ioctx, with an context flag for freeing the memory
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
49 th_free_r(&ctx->memData);
0fc5ddaccc57 Implement fclose() in mem ioctx, with an context flag for freeing the memory
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
50 }
0fc5ddaccc57 Implement fclose() in mem ioctx, with an context flag for freeing the memory
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
51
0fc5ddaccc57 Implement fclose() in mem ioctx, with an context flag for freeing the memory
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
52
678
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 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
54 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 size_t grow;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56
693
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
57 // 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
58 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
59 {
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
60 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
61 return FALSE;
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
62 }
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
63
692
ea6bcbfb9d18 Comments and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 681
diff changeset
64 // 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
65 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
66 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 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
68 return FALSE;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 }
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70
692
ea6bcbfb9d18 Comments and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 681
diff changeset
71 // 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
72 if (newSize < ctx->memAlloc)
692
ea6bcbfb9d18 Comments and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 681
diff changeset
73 goto out;
678
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 // 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
76 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
77 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
78 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
79
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 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
81 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 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
83 return FALSE;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 }
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85
692
ea6bcbfb9d18 Comments and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 681
diff changeset
86 // 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
87 ctx->memAlloc += grow;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 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
89 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 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
91 return FALSE;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 }
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93
692
ea6bcbfb9d18 Comments and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 681
diff changeset
94 out:
678
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 ctx->memSize = newSize;
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 return TRUE;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 }
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99
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 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
102 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 ctx->memOffset = 0;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 return THERR_OK;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 }
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
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 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
109 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 return ctx->status;
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
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 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
115 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 off_t newPos;
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 // 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
119 switch (whence)
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 case SEEK_SET:
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 newPos = offset;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 break;
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_CUR:
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 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
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_END:
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 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
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 default:
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 return -1;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 }
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 // 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
138 ctx->memOffset = newPos;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 // 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
141 if (newPos < 0)
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 return -1;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 //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
145 // return -1;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147 return 0;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 }
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151 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
152 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153 return ctx->memSize;
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
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 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
158 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159 return ctx->memOffset;
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
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 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
164 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165 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
166 }
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167
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 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
170 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
171 // Check for EOF
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172 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
173 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
174 else
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175 return EOF;
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
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 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
180 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181 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
182
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
183 // 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
184 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
185 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
186 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
187 length = size * nmemb;
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
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190 memcpy(buf, ctx->memData + ctx->memOffset, length);
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191 ctx->memOffset += length;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
192 return nmemb;
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
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
195
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
196 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
197 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
198 // Check for EOF
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
199 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
200 return EOF;
681
81d1a3f53b87 Cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
201
678
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
202 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
203 return ch;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
204 }
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 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
208 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
209 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
210
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
211 // 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
212 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
213 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
214 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
215 length = size * nmemb;
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 if (length > 0)
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
219 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
220 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
221 ctx->memOffset += length;
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 return nmemb;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
224 }
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
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
227 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
228 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
229 "MemIO",
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
230
693
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
231 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
232 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
233
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
234 th_mem_freset,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
235 th_mem_ferror,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
236 th_mem_fseek,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
237 th_mem_fsize,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
238 th_mem_ftell,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
239 th_mem_feof,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
240 th_mem_fgetc,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
241 th_mem_fputc,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
242 th_mem_fread,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
243 th_mem_fwrite,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
244
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
245 NULL,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
246 NULL,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 NULL
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
248 };