Debian Codegraph @

prboom2/src/memio.c

raw
1 // Emacs style mode select -*- C -*-
2 //-----------------------------------------------------------------------------
3 //
4 // Copyright(C) 1993-1996 Id Software, Inc.
5 // Copyright(C) 2005 Simon Howard
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 // 02111-1307, USA.
21 //
22 // Emulates the IO functions in C stdio.h reading and writing to
23 // memory.
24 //
25
26 // e6y
27 // All tabs are replaced with spaces.
28 // Fixed eol style of files.
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "memio.h"
35
36 #include "z_zone.h"
37
38 typedef enum {
39 MODE_READ,
40 MODE_WRITE,
41 } memfile_mode_t;
42
43 struct _MEMFILE {
44 unsigned char *buf;
45 size_t buflen;
46 size_t alloced;
47 unsigned int position;
48 memfile_mode_t mode;
49 };
50
51 // Open a memory area for reading
52
53 MEMFILE *mem_fopen_read(const void *buf, size_t buflen)
54 {
55 MEMFILE *file;
56
57 file = Z_Malloc(sizeof(MEMFILE));
58
59 // The MEMFILE struct is overloaded for both read-only and write use cases
60 // This is required to interface with 3rd party libraries (I think)
61 #pragma GCC diagnostic push
62 #pragma GCC diagnostic ignored "-Wcast-qual"
63 file->buf = (unsigned char *) buf;
64 #pragma GCC diagnostic pop
65 file->buflen = buflen;
66 file->position = 0;
67 file->mode = MODE_READ;
68
69 return file;
70 }
71
72 // Read bytes
73
74 size_t mem_fread(void *buf, size_t size, size_t nmemb, MEMFILE *stream)
75 {
76 size_t items;
77
78 if (stream->mode != MODE_READ)
79 {
80 printf("not a read stream\n");
81 return -1;
82 }
83
84 // Trying to read more bytes than we have left?
85
86 items = nmemb;
87
88 if (items * size > stream->buflen - stream->position)
89 {
90 items = (stream->buflen - stream->position) / size;
91 }
92
93 // Copy bytes to buffer
94
95 memcpy(buf, stream->buf + stream->position, items * size);
96
97 // Update position
98
99 stream->position += items * size;
100
101 return items;
102 }
103
104 // Open a memory area for writing
105
106 MEMFILE *mem_fopen_write(void)
107 {
108 MEMFILE *file;
109
110 file = Z_Malloc(sizeof(MEMFILE));
111
112 file->alloced = 1024;
113 file->buf = Z_Malloc(file->alloced);
114 file->buflen = 0;
115 file->position = 0;
116 file->mode = MODE_WRITE;
117
118 return file;
119 }
120
121 // Write bytes to stream
122
123 size_t mem_fwrite(const void *ptr, size_t size, size_t nmemb, MEMFILE *stream)
124 {
125 size_t bytes;
126
127 if (stream->mode != MODE_WRITE)
128 {
129 return -1;
130 }
131
132 // More bytes than can fit in the buffer?
133 // If so, reallocate bigger.
134
135 bytes = size * nmemb;
136
137 while (bytes > stream->alloced - stream->position)
138 {
139 unsigned char *newbuf;
140
141 newbuf = Z_Malloc(stream->alloced * 2);
142 memcpy(newbuf, stream->buf, stream->alloced);
143 Z_Free(stream->buf);
144 stream->buf = newbuf;
145 stream->alloced *= 2;
146 }
147
148 // Copy into buffer
149
150 memcpy(stream->buf + stream->position, ptr, bytes);
151 stream->position += bytes;
152
153 if (stream->position > stream->buflen)
154 stream->buflen = stream->position;
155
156 return nmemb;
157 }
158
159 void mem_get_buf(MEMFILE *stream, void **buf, size_t *buflen)
160 {
161 *buf = stream->buf;
162 *buflen = stream->buflen;
163 }
164
165 void mem_fclose(MEMFILE *stream)
166 {
167 if (stream->mode == MODE_WRITE)
168 {
169 Z_Free(stream->buf);
170 }
171
172 Z_Free(stream);
173 }
174
175 long mem_ftell(MEMFILE *stream)
176 {
177 return stream->position;
178 }
179
180 int mem_fseek(MEMFILE *stream, signed long position, mem_rel_t whence)
181 {
182 unsigned int newpos;
183
184 switch (whence)
185 {
186 case MEM_SEEK_SET:
187 newpos = (int) position;
188 break;
189
190 case MEM_SEEK_CUR:
191 newpos = (int) (stream->position + position);
192 break;
193
194 case MEM_SEEK_END:
195 newpos = (int) (stream->buflen + position);
196 break;
197 default:
198 return -1;
199 }
200
201 if (newpos <= stream->buflen)
202 {
203 stream->position = newpos;
204 return 0;
205 }
206 else
207 {
208 printf("Error seeking to %i\n", newpos);
209 return -1;
210 }
211 }