2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
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
10 * $Logfile: /Freespace2/code/CFile/cfile.h $
15 * <insert description of file here>
18 * Revision 1.2 2002/06/09 04:41:12 relnev
19 * added copyright header
21 * Revision 1.1.1.1 2002/05/03 03:28:11 root
25 * 11 9/08/99 12:03a Dave
26 * Make squad logos render properly in D3D all the time. Added intel anim
29 * 10 8/31/99 9:46a Dave
30 * Support for new cfile cbanims directory.
32 * 9 5/19/99 4:07p Dave
33 * Moved versioning code into a nice isolated common place. Fixed up
34 * updating code on the pxo screen. Fixed several stub problems.
36 * 8 3/28/99 5:58p Dave
37 * Added early demo code. Make objects move. Nice and framerate
38 * independant, but not much else. Don't use yet unless you're me :)
40 * 7 3/24/99 4:05p Dave
41 * Put in support for assigning the player to a specific squadron with a
42 * specific logo. Preliminary work for doing pos/orient checksumming in
43 * multiplayer to reduce bandwidth.
45 * 6 1/12/99 3:15a Dave
46 * Barracks screen support for selecting squad logos. We need real artwork
49 * 5 10/29/98 10:41a Dave
50 * Change the way cfile initializes exe directory.
52 * 4 10/13/98 9:19a Andsager
53 * Add localization support to cfile. Optional parameter with cfopen that
54 * looks for localized files.
56 * 3 10/12/98 9:54a Dave
57 * Fixed a few file organization things.
59 * 2 10/07/98 10:52a Dave
62 * 1 10/07/98 10:48a Dave
64 * 84 9/09/98 5:53p Dave
65 * Put in new tracker packets in API. Change cfile to be able to checksum
68 * 83 8/12/98 4:53p Dave
69 * Put in 32 bit checksumming for PXO missions. No validation on the
70 * actual tracker yet, though.
72 * 82 5/19/98 1:19p Allender
73 * new low level reliable socket reading code. Make all missions/campaign
74 * load/save to data missions folder (i.e. we are rid of the player
77 * 81 5/13/98 10:22p John
78 * Added cfile functions to read/write rle compressed blocks of data.
79 * Made palman use it for .clr files. Made alphacolors calculate on the
80 * fly rather than caching to/from disk.
82 * 80 5/01/98 10:21a John
83 * Added code to find all pack files in all trees. Added code to create
84 * any directories that we write to.
86 * 79 4/30/98 10:29p John
87 * Added code to refresh filelist if cd-rom changed or packfiles created
90 * 78 4/30/98 10:06p John
91 * Started adding code for splitting the maps data tree for hardware
94 * 77 4/30/98 9:43p John
95 * Restructured some stuff.
97 * 76 4/30/98 8:23p John
98 * Fixed some bugs with Fred caused by my new cfile code.
100 * 75 4/30/98 4:53p John
101 * Restructured and cleaned up cfile code. Added capability to read off
102 * of CD-ROM drive and out of multiple pack files.
104 * 74 4/20/98 6:04p Dave
105 * Implement multidata cache flushing and xferring mission files to
106 * multidata. Make sure observers can't change hud config. Fix pilot image
107 * viewing in popup. Put in game status field. Tweaked multi options.
109 * 73 4/01/98 6:06p Hoffoss
110 * Added a command briefing directory for those voice files.
112 * 72 3/31/98 4:51p Dave
113 * Removed medals screen and multiplayer buttons from demo version. Put in
114 * new pilot popup screen. Make ships in mp team vs. team have proper team
115 * ids. Make mp respawns a permanent option saved in the player file.
117 * 71 3/26/98 6:01p Dave
118 * Put in file checksumming routine in cfile. Made pilot pic xferring more
119 * robust. Cut header size of voice data packets in half. Put in
120 * restricted game host query system.
122 * 70 3/10/98 2:27p Hoffoss
123 * Added change directory with history stack functions, so you can switch
124 * to a new directory, do something, and restore to what it was. useful
127 * 69 3/07/98 3:48p Lawrance
128 * get save game working, allow restore from main menu
130 * 68 2/26/98 10:07p Hoffoss
131 * Rewrote state saving and restoring to fix bugs and simplify the code.
133 * 67 2/20/98 4:43p Dave
134 * Finished support for multiplayer player data files. Split off
135 * multiplayer campaign functionality.
137 * 66 2/19/98 6:26p Dave
138 * Fixed a few file xfer bugs. Tweaked mp team select screen. Put in
139 * initial support for player data uploading.
141 * 65 2/06/98 3:47p Allender
142 * subtitling for movies
144 * 64 2/05/98 10:15p Lawrance
145 * Add support for .svg filenames.
147 * 63 1/12/98 10:07p Hoffoss
148 * Made tab cycle properly in debriefing screen and put in aux. pilot
151 * 62 1/11/98 2:45p John
152 * Changed .lst to .clt
154 * 61 1/11/98 2:14p John
155 * Changed a lot of stuff that had to do with bitmap loading. Made cfile
156 * not do callbacks, I put that in global code. Made only bitmaps that
157 * need to load for a level load.
159 * 60 1/02/98 4:41p Allender
160 * added new mission folder for "player" missions
162 * 59 12/28/97 12:42p John
163 * Put in support for reading archive files; Made missionload use the
164 * cf_get_file_list function. Moved demos directory out of data tree.
166 * 58 12/17/97 10:16p Allender
167 * implemented a "no callback" flag to tell the cfile code not to use the
168 * cf_callback on files which specify this flag
170 * 57 12/08/97 6:23p Lawrance
173 * 56 12/07/97 4:30p John
174 * Fixed bug with cfile versioning if two files use it at once. Added
175 * code to cfile so I could display a loading box while loading.
177 * 55 11/24/97 9:28a Hoffoss
178 * Moved define of CF_TYPE_MULTI_PLAYERS so it's in order. I started to
179 * add it because I couldn't find it, so that's indication enough it
182 * 54 11/20/97 1:07a Lawrance
183 * add support for voice/debriefings directory
185 * 53 11/19/97 7:27p Hoffoss
186 * Added version checking read functions.
188 * 52 11/18/97 10:53a Hoffoss
190 * 51 11/17/97 6:07p Hoffoss
191 * Make get_file_list() allow filtering via a callback function.
193 * 50 11/16/97 2:29p John
194 * added versioning to nebulas; put nebula code into freespace.
196 * 49 11/15/97 6:10p Lawrance
197 * add in support for new voice directories
199 * 48 11/11/97 4:54p Dave
200 * Put in support for single vs. multiplayer pilots. Put in initial player
201 * selection screen (no command line option yet). Started work on
202 * multiplayer campaign file save gaming.
204 * 47 11/07/97 4:00p Hoffoss
205 * Capitalized directory names, changed the player/image directory to work
206 * like the rest, and forced the player/images directory to be created.
208 * 46 11/06/97 5:38p Hoffoss
209 * Added a new player image directory and added support for it.
211 * 45 11/04/97 7:46p Lawrance
212 * Add support for data\interface\HUD directory
214 * 44 10/29/97 6:22p Hoffoss
215 * Added some new file listing functions.
217 * 43 10/28/97 10:54a Lawrance
218 * support for 8b22k and 16b11k directories under the sound directory
220 * 42 10/14/97 11:34p Lawrance
221 * add function to get full path for a given filename
223 * 41 9/24/97 5:30p Lawrance
224 * add directory for voices
226 * 40 9/20/97 8:16a John
227 * Made .clr files go into the Cache directory. Replaced cfopen(name,NULL)
228 * to delete a file with cf_delete.
230 * 39 9/09/97 6:50p Hoffoss
231 * Fixed bug with mission saving.
233 * 38 9/05/97 4:52p Lawrance
234 * fix prototype for cfread_angles() and cfwrite_angles()
236 * 37 8/29/97 4:47p Dave
237 * Added an extension for state transfer status files.
239 * 36 8/21/97 12:14p Dave
240 * Changed demo file extension from .keg to .fsd
242 * 35 8/19/97 5:51p Hoffoss
243 * Fixes to cfopen to not check the default directory first, but check it
244 * last. Also tracks info as to where a file is located when opened.
246 * 34 8/17/97 10:22p Hoffoss
247 * Fixed several bugs in Fred with Undo feature. In the process, recoded
248 * a lot of CFile.cpp.
250 * 33 8/17/97 12:47p Hoffoss
251 * Changed code so I can force missions to load from the missions
252 * directory regardless of it's extension.
254 * 32 8/13/97 1:39p Adam
255 * make .ani files available in the data/effects directory
257 * 31 8/13/97 12:24p Lawrance
258 * Add support for effects directory
260 * 30 7/30/97 5:23p Dave
261 * Added file extensions for demo stuff
263 * 29 7/28/97 10:42p Lawrance
264 * added ctmpfile(), analog to tmpfile()
266 * 28 7/16/97 5:29p John
267 * added palette table caching and made scaler and liner no light tmapper
268 * do alpha blending in 8 bpp mode.
270 * 27 6/05/97 4:53p John
271 * First rev of new antialiased font stuff.
273 * 26 6/05/97 1:22p Allender
274 * added .ntl as an extension for nettest program
276 * 25 4/25/97 11:31a Allender
277 * Campaign state now saved in campaign save file in player directory.
278 * Made some global variables follow naming convention. Solidified
279 * continuing campaigns based on new structure
281 * 24 4/17/97 9:01p Allender
282 * start of campaign stuff. Campaigns now stored in external file (no
283 * filenames in code). Continuing campaign won't work at this point
285 * 23 4/03/97 4:26p Lawrance
286 * adding .wav search to music directory
288 * 22 4/01/97 9:26a Allender
289 * added support for descent style fonts although they are not used in the
292 * 21 3/04/97 8:17a John
293 * Fixed movie code to not require a file handle. Used CFILE instead.
294 * Took cfile_get_handle or whatever out.
296 * 20 3/03/97 8:57a Lawrance
297 * took out cf_returnfp()
299 * 19 3/01/97 2:09p Lawrance
300 * supporting memory mapped files, moved cfile implementation details into
303 * 18 2/17/97 3:00p Lawrance
304 * added .ani type to MAPS_EXT and INTERFACE_EXT
306 * 17 2/07/97 9:00a Lawrance
307 * added cfread_uint() and cwrite_uint()
309 * 16 2/04/97 9:29a Allender
310 * added cfwrite* functions
312 * 15 1/22/97 10:48a Lawrance
313 * supporting AVI playback
315 * 14 12/23/96 10:56a John
316 * Totally restructured the POF stuff to support multiple
317 * detail levels in one POF file.
320 * 13 11/20/96 10:00a Hoffoss
321 * Added cfile_chdir() function.
323 * 12 11/13/96 10:14a Allender
324 * added small routines to read basic data types. Also changed code to
325 * try to find extensions in more than 1 directory.
327 * 11 11/11/96 3:21p Allender
328 * added extension for movies
341 #define CF_SEEK_SET (0)
342 #define CF_SEEK_CUR (1)
343 #define CF_SEEK_END (2)
345 typedef struct CFILE {
346 int id; // Index into cfile.cpp specific structure
347 int version; // version of this file
350 // extra info that can be returned when getting a file listing
356 #define CF_MAX_FILENAME_LENGTH 32 // Includes null terminater, so real length is 31
357 #define CF_MAX_PATHNAME_LENGTH 256 // Includes null terminater, so real length is 255
359 #define CF_TYPE_ANY -1 // Used to check in any directory
361 #define CF_TYPE_INVALID 0
362 #define CF_TYPE_ROOT 1 // Root must be 1!!
363 #define CF_TYPE_DATA 2
364 #define CF_TYPE_MAPS 3
365 #define CF_TYPE_TEXT 4
366 #define CF_TYPE_MISSIONS 5
367 #define CF_TYPE_MODELS 6
368 #define CF_TYPE_TABLES 7
369 #define CF_TYPE_SOUNDS 8
370 #define CF_TYPE_SOUNDS_8B22K 9
371 #define CF_TYPE_SOUNDS_16B11K 10
372 #define CF_TYPE_VOICE 11
373 #define CF_TYPE_VOICE_BRIEFINGS 12
374 #define CF_TYPE_VOICE_CMD_BRIEF 13
375 #define CF_TYPE_VOICE_DEBRIEFINGS 14
376 #define CF_TYPE_VOICE_PERSONAS 15
377 #define CF_TYPE_VOICE_SPECIAL 16
378 #define CF_TYPE_VOICE_TRAINING 17
379 #define CF_TYPE_MUSIC 18
380 #define CF_TYPE_MOVIES 19
381 #define CF_TYPE_INTERFACE 20
382 #define CF_TYPE_FONT 21
383 #define CF_TYPE_EFFECTS 22
384 #define CF_TYPE_HUD 23
385 #define CF_TYPE_PLAYER_MAIN 24
386 #define CF_TYPE_PLAYER_IMAGES_MAIN 25
387 #define CF_TYPE_CACHE 26
388 #define CF_TYPE_PLAYERS 27
389 #define CF_TYPE_SINGLE_PLAYERS 28
390 #define CF_TYPE_MULTI_PLAYERS 29
391 #define CF_TYPE_MULTI_CACHE 30
392 #define CF_TYPE_CONFIG 31
393 #define CF_TYPE_SQUAD_IMAGES_MAIN 32
394 #define CF_TYPE_DEMOS 33
395 #define CF_TYPE_CBANIMS 34
396 #define CF_TYPE_INTEL_ANIMS 35
398 #define CF_MAX_PATH_TYPES 36 // Can be as high as you'd like
400 // TRUE if type is specified and valid
401 #define CF_TYPE_SPECIFIED(path_type) (((path_type)>CF_TYPE_INVALID) && ((path_type)<CF_MAX_PATH_TYPES))
403 // #define's for the type parameter in cfopen.
404 #define CFILE_NORMAL 0 // open file normally
405 #define CFILE_MEMORY_MAPPED (1<<0) // open file as a memory-mapped file
407 #define CF_SORT_NONE 0
408 #define CF_SORT_NAME 1
409 #define CF_SORT_TIME 2
411 #define cfread_fix(file) (fix)cfread_int(file)
412 #define cfwrite_fix(i,file) cfwrite_int(i,file)
414 // callback function used for get_file_list() to filter files to be added to list. Return 1
415 // to add file to list, or 0 to not add it.
416 extern int (*Get_file_list_filter)(char *filename);
418 // cfile directory. valid after cfile_init() returns successfully
419 #define CFILE_ROOT_DIRECTORY_LEN 256
420 extern char Cfile_root_dir[CFILE_ROOT_DIRECTORY_LEN];
422 //================= LOW-LEVEL FUNCTIONS ==================
423 // Call this once at the beginning of the program
424 int cfile_init(char *exe_dir,char *cdrom_dir=NULL);
426 // Call this if pack files got added or removed or the
427 // cdrom changed. This will refresh the list of filenames
428 // stored in packfiles and on the cdrom.
429 void cfile_refresh();
431 // add an extension to a filename if it doesn't already have it
432 char *cf_add_ext(char *filename, char *ext);
434 // return CF_TYPE (directory location type) of a CFILE you called cfopen() successfully on.
435 int cf_get_dir_type(CFILE *cfile);
437 // Opens the file. If no path is given, use the extension to look into the
438 // default path. If mode is NULL, delete the file.
439 CFILE *cfopen(char *filename, char *mode, int type = CFILE_NORMAL, int dir_type = CF_TYPE_ANY, bool localize = false);
441 // Flush the open file buffer
442 int cflush(CFILE *cfile);
444 // version number of opened file. Will be 0 unless you put something else here after you
445 // open a file. Once set, you can use minimum version numbers with the read functions.
446 void cf_set_version( CFILE * cfile, int version );
449 void cf_delete( char *filename, int dir_type );
451 // Same as _access function to read a file's access bits
452 int cf_access( char *filename, int dir_type, int mode );
454 // Returns 1 if file exists, 0 if not.
455 int cf_exist( char *filename, int dir_type );
457 // ctmpfile() opens a temporary file stream. File is deleted automatically when closed
461 int cfclose(CFILE *cfile);
463 // Returns size of file...
464 int cfilelength(CFILE *fp);
467 int cfread(void *buf, int elsize, int nelem, CFILE *fp);
469 // cfwrite() writes to the file
470 int cfwrite(void *buf, int elsize, int nelem, CFILE *cfile);
472 // Reads/writes RLE compressed data.
473 int cfread_compressed(void *buf, int elsize, int nelem, CFILE *cfile);
474 int cfwrite_compressed(void *param_buf, int param_elsize, int param_nelem, CFILE *cfile);
476 // Moves the file pointer
477 int cfseek(CFILE *fp, int offset, int where);
479 // Returns current position of file.
480 int cftell(CFILE *fp);
482 // cfputc() writes a character to a file
483 int cfputc(int c, CFILE *cfile);
485 // cfputs() writes a string to a file
486 int cfputs(char *str, CFILE *cfile);
488 // cfgetc() reads a character to a file
489 int cfgetc(CFILE *cfile);
491 // cfgets() reads a string from a file
492 char *cfgets(char *buf, int n, CFILE *cfile);
494 // cfeof() Tests for end-of-file on a stream
495 int cfeof(CFILE *cfile);
497 // Return the data pointer associated with the CFILE structure (for memory mapped files)
498 void *cf_returndata(CFILE *cfile);
500 // get the 2 byte checksum of the passed filename - return 0 if operation failed, 1 if succeeded
501 int cf_chksum_short(char *filename, ushort *chksum, int max_size = -1, int cf_type = CF_TYPE_ANY );
503 // get the 2 byte checksum of the passed file - return 0 if operation failed, 1 if succeeded
504 // NOTE : preserves current file position
505 int cf_chksum_short(CFILE *file, ushort *chksum, int max_size = -1);
507 // get the 32 bit CRC checksum of the passed filename - return 0 if operation failed, 1 if succeeded
508 int cf_chksum_long(char *filename, uint *chksum, int max_size = -1, int cf_type = CF_TYPE_ANY );
510 // get the 32 bit CRC checksum of the passed file - return 0 if operation failed, 1 if succeeded
511 // NOTE : preserves current file position
512 int cf_chksum_long(CFILE *file, uint *chksum, int max_size = -1);
514 // convenient for misc checksumming purposes ------------------------------------------
516 // update cur_chksum with the chksum of the new_data of size new_data_size
517 ushort cf_add_chksum_short(ushort seed, char *buffer, int size);
519 // update cur_chksum with the chksum of the new_data of size new_data_size
520 unsigned long cf_add_chksum_long(unsigned long seed, char *buffer, int size);
522 // convenient for misc checksumming purposes ------------------------------------------
524 //================= HIGH LEVEL FUNCTIONS ==================
525 int cfexist(char *filename); // Returns true if file exists on disk (1) or in hog (2).
527 // rename a file, utilizing the extension to determine where file is.
528 #define CF_RENAME_SUCCESS 0 // successfully renamed the file
529 #define CF_RENAME_FAIL_ACCESS 1 // new name could not be created
530 #define CF_RENAME_FAIL_EXIST 2 // old name does not exist
531 int cf_rename(char *old_name, char *name, int type = CF_TYPE_ANY );
533 // changes the attributes of a file
534 void cf_attrib(char *name, int set, int clear, int type);
536 // flush (delete all files in) the passed directory (by type), return the # of files deleted
537 // NOTE : WILL NOT DELETE READ-ONLY FILES
538 int cfile_flush_dir(int type);
540 // functions for reading from cfile
541 // These are all high level, built up from
543 int cfgetc(CFILE *fp);
544 char *cfgets(char *buf, size_t n, CFILE *fp);
545 char cfread_char(CFILE *file, int ver = 0, char deflt = 0);
546 ubyte cfread_ubyte(CFILE *file, int ver = 0, ubyte deflt = 0);
547 short cfread_short(CFILE *file, int ver = 0, short deflt = 0);
548 ushort cfread_ushort(CFILE *file, int ver = 0, ushort deflt = 0);
549 int cfread_int(CFILE *file, int ver = 0, int deflt = 0);
550 uint cfread_uint(CFILE *file, int ver = 0, uint deflt = 0);
551 float cfread_float(CFILE *file, int ver = 0, float deflt = 0.0f);
552 void cfread_vector(vector *vec, CFILE *file, int ver = 0, vector *deflt = NULL);
553 void cfread_angles(angles *ang, CFILE *file, int ver = 0, angles *deflt = NULL);
555 // Reads variable length, null-termined string. Will only read up
557 void cfread_string(char *buf,int n, CFILE *file);
558 // Read a fixed length that is null-terminatedm, and has the length
560 void cfread_string_len(char *buf,int n, CFILE *file);
562 // functions for writing cfiles
563 int cfwrite_char(char c, CFILE *file);
564 int cfwrite_float(float f, CFILE *file);
565 int cfwrite_int(int i, CFILE *file);
566 int cfwrite_uint(uint i, CFILE *file);
567 int cfwrite_short(short s, CFILE *file);
568 int cfwrite_ushort(ushort s, CFILE *file);
569 int cfwrite_ubyte(ubyte u, CFILE *file);
570 int cfwrite_vector(vector *vec, CFILE *file);
571 int cfwrite_angles(angles *ang, CFILE *file);
573 // writes variable length, null-termined string.
574 int cfwrite_string(char *buf, CFILE *file);
576 // write a fixed length that is null-terminatedm, and has the length
578 int cfwrite_string_len(char *buf, CFILE *file);
580 int cf_get_file_list( int max, char **list, int type, char *filter, int sort = CF_SORT_NONE, file_list_info *info = NULL );
581 int cf_get_file_list_preallocated( int max, char arr[][MAX_FILENAME_LEN], char **list, int type, char *filter, int sort = CF_SORT_NONE, file_list_info *info = NULL );
582 void cf_sort_filenames( int n, char **list, int sort, file_list_info *info = NULL );
584 // Searches for a file. Follows all rules and precedence and searches
585 // CD's and pack files.
586 // Input: filespace - Filename & extension
587 // pathtype - See CF_TYPE_ defines in CFILE.H
588 // Output: pack_filename - Absolute path and filename of this file. Could be a packfile or the actual file.
590 // offset - Offset into pack file. 0 if not a packfile.
591 // Returns: If not found returns 0.
592 int cf_find_file_location( char *filespec, int pathtype, char *pack_filename, int *size, int *offset, bool localize = false);
594 // Functions to change directories
595 int cfile_chdir(char *dir);
596 int cfile_chdrive(int DriveNum, int flag);
598 // push current directory on a 'stack' (so we can restore it) and change the directory
599 int cfile_push_chdir(int type);
601 // restore directory on top of the stack
605 #endif /* __CFILE_H__ */