]> icculus.org git repositories - taylor/freespace2.git/blob - src/cfile/cfilearchive.cpp
clean up Windows #include's and use winsock2
[taylor/freespace2.git] / src / cfile / cfilearchive.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/CFile/CfileArchive.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Low-level code for reading data out of large archive files or normal files.  All
16  * reads/seeks come through here.
17  *
18  * $Log$
19  * Revision 1.2  2002/06/09 04:41:15  relnev
20  * added copyright header
21  *
22  * Revision 1.1.1.1  2002/05/03 03:28:08  root
23  * Initial import.
24  *
25  * 
26  * 4     2/22/99 10:31p Andsager
27  * Get rid of unneeded includes.
28  * 
29  * 3     1/06/99 2:24p Dave
30  * Stubs and release build fixes.
31  * 
32  * 2     10/07/98 10:52a Dave
33  * Initial checkin.
34  * 
35  * 1     10/07/98 10:48a Dave
36  * 
37  * 12    5/14/98 8:10p Lawrance
38  * Fix bug with trying to read past the end of a file (when from a
39  * packfile).
40  * 
41  * 11    5/11/98 11:48a John
42  * fixed bug with memory mapped files
43  * 
44  * 10    5/11/98 10:59a John
45  * added in debug code
46  * 
47  * 9     5/11/98 10:59a John
48  * Moved the low-level file reading code into cfilearchive.cpp.
49  * 
50  * 8     4/30/98 4:53p John
51  * Restructured and cleaned up cfile code.  Added capability to read off
52  * of CD-ROM drive and out of multiple pack files.
53  * 
54  * 7     4/20/98 11:49a Hoffoss
55  * Fixed bug with directory name getting.
56  * 
57  * 6     4/10/98 12:18a Hoffoss
58  * Make pilot image search in pack file possible.
59  * 
60  * 5     1/19/98 9:37p Allender
61  * Great Compiler Warning Purge of Jan, 1998.  Used pragma's in a couple
62  * of places since I was unsure of what to do with code.
63  * 
64  * 4     12/30/97 5:31p Lawrance
65  * fixed problem for people that didn't have a VP file
66  * 
67  * 3     12/30/97 4:31p Sandeep
68  * Changed pakfile format
69  * 
70  * 2     12/28/97 12:42p John
71  * Put in support for reading archive files; Made missionload use the
72  * cf_get_file_list function.   Moved demos directory out of data tree.
73  * 
74  * 1     12/28/97 11:48a John
75  *
76  * $NoKeywords: $
77  */
78
79 #define _CFILE_INTERNAL 
80
81 #include <stdlib.h>
82 #include <string.h>
83 #include <stdio.h>
84
85 #include "pstypes.h"
86 #include "cfile.h"
87 //#include "outwnd.h"
88 //#include "vecmat.h"
89 //#include "timer.h"
90 #include "cfilearchive.h"
91
92 #define CHECK_POSITION
93
94 // Called once to setup the low-level reading code.
95
96 void cf_init_lowlevel_read_code( CFILE * cfile, int offset, int size )
97 {
98         SDL_assert(cfile != NULL);
99
100         Cfile_block *cb;
101         SDL_assert(cfile->id >= 0 && cfile->id < MAX_CFILE_BLOCKS);
102         cb = &Cfile_block_list[cfile->id];      
103
104         cb->lib_offset = offset;
105         cb->raw_position = 0;
106         cb->size = size;
107
108         if ( cb->fp )   {
109                 if ( cb->lib_offset )   {
110                         fseek( cb->fp, cb->lib_offset, SEEK_SET );
111                 }
112
113                 #if defined(CHECK_POSITION) && !defined(NDEBUG)
114                         int raw_position = ftell(cb->fp) - cb->lib_offset;
115                         SDL_assert(raw_position == cb->raw_position);
116                 #endif
117         }
118 }
119
120
121
122 // cfeof() Tests for end-of-file on a stream
123 //
124 // returns a nonzero value after the first read operation that attempts to read
125 // past the end of the file. It returns 0 if the current position is not end of file.
126 // There is no error return.
127
128 int cfeof(CFILE *cfile)
129 {
130         SDL_assert(cfile != NULL);
131
132         Cfile_block *cb;
133         SDL_assert(cfile->id >= 0 && cfile->id < MAX_CFILE_BLOCKS);
134         cb = &Cfile_block_list[cfile->id];      
135
136         int result = 0;
137
138         SDL_assert(cb->fp != NULL);
139
140         #if defined(CHECK_POSITION) && !defined(NDEBUG)
141         int raw_position = ftell(cb->fp) - cb->lib_offset;
142         SDL_assert(raw_position == cb->raw_position);
143         #endif
144                 
145         if (cb->raw_position >= cb->size ) {
146                 result = 1;
147         } else {
148                 result = 0;
149         }
150
151         return result;  
152 }
153
154 // cftell() returns offset into file
155 //
156 // returns:  success ==> offset into the file
157 //           error   ==> -1
158 //
159 int cftell( CFILE * cfile )
160 {
161         SDL_assert(cfile != NULL);
162         Cfile_block *cb;
163         SDL_assert(cfile->id >= 0 && cfile->id < MAX_CFILE_BLOCKS);
164         cb = &Cfile_block_list[cfile->id];      
165
166         SDL_assert(cb->fp != NULL);
167
168         #if defined(CHECK_POSITION) && !defined(NDEBUG)
169         int raw_position = ftell(cb->fp) - cb->lib_offset;
170         SDL_assert(raw_position == cb->raw_position);
171         #endif
172
173         return cb->raw_position;
174 }
175
176
177 // cfseek() moves the file pointer
178 //
179 // returns:   success ==> 0
180 //            error   ==> non-zero
181 //
182 int cfseek( CFILE *cfile, int offset, int where )
183 {
184
185         SDL_assert(cfile != NULL);
186         Cfile_block *cb;
187         SDL_assert(cfile->id >= 0 && cfile->id < MAX_CFILE_BLOCKS);
188         cb = &Cfile_block_list[cfile->id];      
189
190         SDL_assert( cb->fp != NULL );
191         
192         int goal_position;
193
194         switch( where ) {
195         case CF_SEEK_SET:
196                 goal_position = offset+cb->lib_offset;
197                 break;
198         case CF_SEEK_CUR:       
199                 {
200                         goal_position = cb->raw_position+offset+cb->lib_offset;
201                 }
202                 break;
203         case CF_SEEK_END:
204                 goal_position = cb->size+offset+cb->lib_offset;
205                 break;
206         default:
207                 Int3();
208                 return 1;
209         }       
210
211         int result = fseek(cb->fp, goal_position, SEEK_SET );
212         cb->raw_position = goal_position - cb->lib_offset;
213
214         #if defined(CHECK_POSITION) && !defined(NDEBUG)
215                 int tmp_offset = ftell(cb->fp) - cb->lib_offset;
216                 SDL_assert(tmp_offset==cb->raw_position);
217         #endif
218
219         return result;  
220 }
221
222
223 // cfread() reads from a file
224 //
225 // returns:   returns the number of full elements read
226 //            
227 //
228 int cfread(void *buf, int elsize, int nelem, CFILE *cfile)
229 {
230         SDL_assert(cfile != NULL);
231         SDL_assert(buf != NULL);
232         SDL_assert(cfile->id >= 0 && cfile->id < MAX_CFILE_BLOCKS);
233
234         Cfile_block *cb;
235         cb = &Cfile_block_list[cfile->id];      
236
237         SDL_assert(cb->fp != NULL);
238
239         int size = elsize*nelem;
240
241         SDL_assert(nelem > 0);
242         SDL_assert(elsize > 0);
243         SDL_assert(size > 0);
244
245         if ( (cb->raw_position+size) > cb->size ) {
246                 size = cb->size - cb->raw_position;
247                 if ( size < 1 ) {
248                         return 0;
249                 }
250                 //mprintf(( "CFILE: EOF encountered in file\n" ));
251         }
252
253         int bytes_read = fread( buf, 1, size, cb->fp );
254         if ( bytes_read > 0 )   {
255                 cb->raw_position += bytes_read;
256         }               
257
258         #if defined(CHECK_POSITION) && !defined(NDEBUG)
259                 int tmp_offset = ftell(cb->fp) - cb->lib_offset;
260                 SDL_assert(tmp_offset==cb->raw_position);
261         #endif
262
263         return bytes_read / elsize;
264
265 }
266