annotate th_ioctx_mem.c @ 717:e73200c4584a

Add some error status setting in memio.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 26 Oct 2020 12:48:37 +0200
parents 0fc5ddaccc57
children ffd97ca9dfce
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:
717
e73200c4584a Add some error status setting in memio.
Matti Hamalainen <ccr@tnsp.org>
parents: 694
diff changeset
134 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
135 return -1;
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
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 // 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
139 ctx->memOffset = newPos;
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 // 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
142 if (newPos < 0)
717
e73200c4584a Add some error status setting in memio.
Matti Hamalainen <ccr@tnsp.org>
parents: 694
diff changeset
143 {
e73200c4584a Add some error status setting in memio.
Matti Hamalainen <ccr@tnsp.org>
parents: 694
diff changeset
144 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
145 return -1;
717
e73200c4584a Add some error status setting in memio.
Matti Hamalainen <ccr@tnsp.org>
parents: 694
diff changeset
146 }
678
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 //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
149 // return -1;
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 return 0;
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
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 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
156 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
157 return ctx->memSize;
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
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 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
162 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
163 return ctx->memOffset;
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
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 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
168 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
169 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
170 }
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
171
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 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
174 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175 // Check for EOF
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176 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
177 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
178 else
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
179 return EOF;
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
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 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
184 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
185 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
186
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
187 // 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
188 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
189 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190 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
191 length = size * nmemb;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
192 }
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 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
195 ctx->memOffset += length;
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
196 return nmemb;
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
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
199
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200 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
201 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
202 // Check for EOF
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
203 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
204 return EOF;
681
81d1a3f53b87 Cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
205
678
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
206 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
207 return ch;
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
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 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
212 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
213 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
214
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
215 // 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
216 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
217 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
218 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
219 length = size * nmemb;
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
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
222 if (length > 0)
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
223 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
224 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
225 ctx->memOffset += length;
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 return 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
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
231 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
232 {
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
233 "MemIO",
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
234
693
a622d21833e1 Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
235 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
236 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
237
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
238 th_mem_freset,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
239 th_mem_ferror,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
240 th_mem_fseek,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
241 th_mem_fsize,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
242 th_mem_ftell,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
243 th_mem_feof,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
244 th_mem_fgetc,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
245 th_mem_fputc,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
246 th_mem_fread,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 th_mem_fwrite,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
248
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
249 NULL,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
250 NULL,
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
251 NULL
7e207f1023d9 Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
252 };