Mercurial > hg > th-libs
annotate th_ioctx_mem.c @ 693:a622d21833e1
Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 09 Mar 2020 18:54:37 +0200 |
parents | ea6bcbfb9d18 |
children | 0fc5ddaccc57 |
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 |
678
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 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
|
47 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
48 size_t grow; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 |
693
a622d21833e1
Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents:
692
diff
changeset
|
50 // 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
|
51 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
|
52 { |
a622d21833e1
Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents:
692
diff
changeset
|
53 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
|
54 return FALSE; |
a622d21833e1
Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents:
692
diff
changeset
|
55 } |
a622d21833e1
Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents:
692
diff
changeset
|
56 |
692 | 57 // 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
|
58 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
|
59 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 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
|
61 return FALSE; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 } |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 |
692 | 64 // 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
|
65 if (newSize < ctx->memAlloc) |
692 | 66 goto out; |
678
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
67 |
692 | 68 // 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
|
69 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
|
70 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
|
71 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
|
72 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
73 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
|
74 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
75 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
|
76 return FALSE; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
77 } |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
78 |
692 | 79 // 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
|
80 ctx->memAlloc += grow; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
81 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
|
82 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
83 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
|
84 return FALSE; |
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 |
692 | 87 out: |
678
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
88 ctx->memSize = newSize; |
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 return TRUE; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
91 } |
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 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
94 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
|
95 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
96 ctx->memOffset = 0; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
97 return THERR_OK; |
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_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
|
102 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
103 return ctx->status; |
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 |
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 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
|
108 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
109 off_t newPos; |
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 // 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
|
112 switch (whence) |
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 case SEEK_SET: |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
115 newPos = offset; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
116 break; |
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 case SEEK_CUR: |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
119 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
|
120 break; |
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 case SEEK_END: |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
123 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
|
124 break; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
125 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
126 default: |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
127 return -1; |
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 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
130 // 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
|
131 ctx->memOffset = newPos; |
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 // 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
|
134 if (newPos < 0) |
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 //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
|
138 // return -1; |
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 return 0; |
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 |
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 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
|
145 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
146 return ctx->memSize; |
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 |
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 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
|
151 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
152 return ctx->memOffset; |
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 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
156 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
|
157 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
158 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
|
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 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
162 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
|
163 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
164 // Check for EOF |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
165 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
|
166 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
|
167 else |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
168 return EOF; |
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 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
172 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
|
173 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
174 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
|
175 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
176 // 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
|
177 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
|
178 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
179 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
|
180 length = size * nmemb; |
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 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
|
184 ctx->memOffset += length; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
185 return 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 |
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 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
|
190 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
191 // Check for EOF |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
192 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
|
193 return EOF; |
681 | 194 |
678
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
195 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
|
196 return ch; |
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 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
|
201 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
202 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
|
203 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
204 // 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
|
205 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
|
206 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
207 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
|
208 length = size * nmemb; |
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 if (length > 0) |
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 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
|
214 ctx->memOffset += length; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
215 } |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
216 return nmemb; |
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 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
220 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
|
221 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
222 "MemIO", |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
223 |
693
a622d21833e1
Implement fopen() in mem ioctx, and simplistic read-only/write flag support.
Matti Hamalainen <ccr@tnsp.org>
parents:
692
diff
changeset
|
224 th_mem_fopen, |
678
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
225 NULL, |
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 th_mem_freset, |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
228 th_mem_ferror, |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
229 th_mem_fseek, |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
230 th_mem_fsize, |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
231 th_mem_ftell, |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
232 th_mem_feof, |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
233 th_mem_fgetc, |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
234 th_mem_fputc, |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
235 th_mem_fread, |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
236 th_mem_fwrite, |
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 NULL, |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
239 NULL, |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
240 NULL |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
241 }; |