Mercurial > hg > th-libs
annotate th_ioctx_mem.c @ 681:81d1a3f53b87
Cosmetic.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 04 Mar 2020 19:47:21 +0200 |
parents | 7e207f1023d9 |
children | ea6bcbfb9d18 |
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 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
11 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
|
12 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
13 size_t grow; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
14 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
15 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
|
16 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
17 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
|
18 return FALSE; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
19 } |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 if (newSize < ctx->memAlloc) |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
22 return TRUE; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
23 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
24 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
|
25 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
|
26 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
|
27 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
28 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
|
29 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
30 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
|
31 return FALSE; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
32 } |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
33 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
34 ctx->memAlloc += grow; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
35 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
|
36 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 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
|
38 return FALSE; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
39 } |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
40 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
41 ctx->memSize = newSize; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
42 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
43 return TRUE; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 } |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
45 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
47 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
|
48 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 ctx->memOffset = 0; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 return THERR_OK; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 } |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
53 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
54 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
|
55 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
56 return ctx->status; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 } |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
58 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
59 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 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
|
61 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 off_t newPos; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
64 // 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
|
65 switch (whence) |
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 case SEEK_SET: |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
68 newPos = offset; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
69 break; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
70 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
71 case SEEK_CUR: |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
72 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
|
73 break; |
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 case SEEK_END: |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
76 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
|
77 break; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
78 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
79 default: |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
80 return -1; |
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 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
83 // 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
|
84 ctx->memOffset = newPos; |
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 // 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
|
87 if (newPos < 0) |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
88 return -1; |
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 //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
|
91 // return -1; |
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 return 0; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
94 } |
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 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
97 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
|
98 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
99 return ctx->memSize; |
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 |
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 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
|
104 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
105 return ctx->memOffset; |
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 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
109 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
|
110 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
111 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
|
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 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
115 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
|
116 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
117 // Check for EOF |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
118 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
|
119 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
|
120 else |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
121 return EOF; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
122 } |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
123 |
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 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
|
126 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
127 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
|
128 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
129 // 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
|
130 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
|
131 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
132 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
|
133 length = size * nmemb; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
134 } |
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 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
|
137 ctx->memOffset += length; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
138 return nmemb; |
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 |
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 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
|
143 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
144 // Check for EOF |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
145 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
|
146 return EOF; |
681 | 147 |
678
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
148 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
|
149 return ch; |
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 |
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 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
|
154 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
155 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
|
156 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
157 // 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
|
158 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
|
159 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
160 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
|
161 length = size * nmemb; |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
162 } |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
163 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
164 if (length > 0) |
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 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
|
167 ctx->memOffset += length; |
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 nmemb; |
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 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
|
174 { |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
175 "MemIO", |
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 NULL, |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
178 NULL, |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
179 |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
180 th_mem_freset, |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
181 th_mem_ferror, |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
182 th_mem_fseek, |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
183 th_mem_fsize, |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
184 th_mem_ftell, |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
185 th_mem_feof, |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
186 th_mem_fgetc, |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
187 th_mem_fputc, |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
188 th_mem_fread, |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
189 th_mem_fwrite, |
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 NULL, |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
192 NULL, |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
193 NULL |
7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
194 }; |