-
Notifications
You must be signed in to change notification settings - Fork 1
/
d_io.cpp
246 lines (221 loc) · 5.13 KB
/
d_io.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Emacs style mode select -*- C++ -*-
//----------------------------------------------------------------------------
//
// Copyright(C) 2013 James Haley
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//----------------------------------------------------------------------------
//
// File/WAD Standard Input Routines
//
// This code was moved here from d_deh.c and generalized to make a consistent
// interface for emulating stdio functions on wad lumps. The structure here
// handles either external files or wad lumps, depending on how it is
// initialized, and emulates stdio regardless.
//
// James Haley
//
//----------------------------------------------------------------------------
#include "z_zone.h"
#include "d_keywds.h"
#include "doomtype.h"
#include "d_io.h"
#include "d_dwfile.h"
#include "m_misc.h"
#include "w_wad.h"
//
// Constructor
//
DWFILE::DWFILE()
: type(0), inp(NULL), lump(NULL), data(NULL), size(0), origsize(0),
lumpnum(0)
{
}
//
// Destructor
//
DWFILE::~DWFILE()
{
if(isOpen())
close();
}
//
// DWFILE::getStr
//
// Equivalent to fgets
//
char *DWFILE::getStr(char *buf, size_t n)
{
// If this is a real file, return regular fgets
if(type == DWF_FILE)
return fgets(buf, n, (FILE *)inp);
// If no more characters
if(!n || !*inp || size <= 0)
return NULL;
if(n == 1)
{
size--, *buf = *inp++;
}
else
{ // copy buffer
char *p = buf;
while(n > 1 && *inp && size &&
(n--, size--, *p++ = *inp++) != '\n')
;
*p = 0;
}
return buf; // Return buffer pointer
}
//
// DWFILE::atEof
//
// Equivalent to feof
//
int DWFILE::atEof() const
{
return (type == DWF_FILE) ?
feof((FILE *)inp) : !*inp || size <= 0;
}
//
// DWFILE::getChar
//
// Equivalent to fgetc
//
int DWFILE::getChar()
{
return (type == DWF_FILE) ?
fgetc((FILE *)inp) : size > 0 ? size--, *inp++ : EOF;
}
//
// DWFILE::unGetChar
//
// Equivalent to ungetc
//
// haleyjd 04/03/03: note that wad lump buffers will not be
// written into by this function -- this is necessary to
// maintain cacheability.
//
int DWFILE::unGetChar(int c)
{
return (type == DWF_FILE) ?
ungetc(c, (FILE *)inp) : size < origsize ? size++, *(--inp) : EOF;
}
//
// DWFILE::openFile
//
// Open a file into the DWFILE instance. Uses standard fopen.
//
void DWFILE::openFile(const char *filename, const char *mode)
{
if(isOpen())
close();
inp = (byte *)fopen(filename, mode);
lumpnum = -1;
type = DWF_FILE;
// zero out fields not used for file reading
data = NULL;
lump = NULL;
origsize = 0;
size = 0;
}
//
// DWFILE::openLump
//
// Open a wad lump into the DWFILE structure. The wad lump will
// be cached in zone memory at static allocation level.
//
void DWFILE::openLump(int p_lumpnum)
{
if(isOpen())
close();
// haleyjd 04/03/03: added origsize field for D_Ungetc
lumpnum = p_lumpnum;
size = origsize = W_LumpLength(lumpnum);
inp = lump = (byte *)(wGlobalDir.cacheLumpNum(lumpnum, PU_STATIC));
type = DWF_LUMP;
// zero out fields not used for lump reading
data = NULL;
}
/*
// TODO: support this properly
void D_OpenData(DWFILE *infile, byte *data, int size)
{
memset(infile, 0, sizeof(*infile));
infile->size = infile->origsize = size;
infile->inp = infile->data = data;
infile->lumpnum = -1;
infile->type = DWF_DATA;
}
*/
//
// DWFILE::close
//
// Closes the file or wad lump. Calls standard fclose for files;
// sets wad lumps to cache allocation level.
//
void DWFILE::close()
{
if(!isOpen())
return;
switch(type)
{
case DWF_LUMP:
Z_ChangeTag(lump, PU_CACHE);
break;
case DWF_DATA:
efree(data);
break;
case DWF_FILE:
fclose((FILE *)inp);
break;
default:
break;
}
inp = lump = data = NULL;
}
//
// DWFILE::read
//
// haleyjd
//
size_t DWFILE::read(void *dest, size_t p_size, size_t p_num)
{
if(type == DWF_FILE)
return fread(dest, p_size, p_num, (FILE *)inp);
else
{
size_t numbytes = p_size * p_num;
size_t numbytesread = 0;
byte *d = (byte *)dest;
while(numbytesread < numbytes && size)
{
*d++ = *inp++;
size--;
numbytesread++;
}
return numbytesread;
}
}
//
// DWFILE::fileLength
//
// haleyjd 03/08/06: returns the length of the file.
//
long DWFILE::fileLength() const
{
return (type == DWF_FILE) ? M_FileLength((FILE *)inp) : origsize;
}
// EOF