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/CfileSystem.cpp $
15 * Functions to keep track of and find files that can exist
16 * on the harddrive, cd-rom, or in a pack file on either of those.
17 * This keeps a list of all the files in packfiles or on CD-rom
18 * and when you need a file you call one function which then searches
19 * all those locations, inherently enforcing precedence orders.
22 * Revision 1.11 2004/07/04 11:27:29 taylor
23 * cleanup CFILE code a little, warning fixes, remove redundant dir checks, amd64 support
25 * Revision 1.10 2004/06/11 00:29:22 tigital
26 * byte-swapping changes for bigendian systems
28 * Revision 1.9 2003/05/27 03:03:11 taylor
29 * fix second root (gamedir) searching
31 * Revision 1.8 2003/02/20 17:41:07 theoddone33
32 * Userdir patch from Taylor Richards
34 * Revision 1.7 2002/06/22 23:57:39 relnev
35 * remove writable strings.
37 * fix compile for intel compiler.
39 * Revision 1.6 2002/06/09 04:41:15 relnev
40 * added copyright header
42 * Revision 1.5 2002/06/05 04:03:32 relnev
43 * finished cfilesystem.
45 * removed some old code.
47 * fixed mouse save off-by-one.
51 * Revision 1.4 2002/05/28 17:26:57 theoddone33
52 * Fill in some timer and palette setting stubs. Still no display
54 * Revision 1.3 2002/05/28 06:45:38 theoddone33
57 * Revision 1.2 2002/05/28 06:28:20 theoddone33
58 * Filesystem mods, actually reads some data files now
60 * Revision 1.1.1.1 2002/05/03 03:28:08 root
64 * 6 9/08/99 10:01p Dave
65 * Make sure game won't run in a drive's root directory. Make sure
66 * standalone routes suqad war messages properly to the host.
68 * 5 9/03/99 1:31a Dave
69 * CD checking by act. Added support to play 2 cutscenes in a row
70 * seamlessly. Fixed super low level cfile bug related to files in the
71 * root directory of a CD. Added cheat code to set campaign mission # in
74 * 4 2/22/99 10:31p Andsager
75 * Get rid of unneeded includes.
77 * 3 10/13/98 9:19a Andsager
78 * Add localization support to cfile. Optional parameter with cfopen that
79 * looks for localized files.
81 * 2 10/07/98 10:52a Dave
84 * 1 10/07/98 10:48a Dave
86 * 14 8/31/98 2:06p Dave
87 * Make cfile sort the ordering or vp files. Added support/checks for
88 * recognizing "mission disk" players.
90 * 13 6/23/98 4:18p Hoffoss
91 * Fixed some bugs with AC release build.
93 * 12 5/20/98 10:46p John
94 * Added code that doesn't include duplicate filenames in any file list
97 * 11 5/14/98 2:14p Lawrance2
98 * Use filespec filtering for packfiles
100 * 10 5/03/98 11:53a John
101 * Fixed filename case mangling.
103 * 9 5/02/98 11:06p Allender
104 * correctly deal with pack pathnames
106 * 8 5/01/98 11:41a Allender
107 * Fixed bug with mission saving in Fred.
109 * 7 5/01/98 10:21a John
110 * Added code to find all pack files in all trees. Added code to create
111 * any directories that we write to.
113 * 6 4/30/98 10:21p John
114 * Added code to cleanup cfilesystem
116 * 5 4/30/98 10:18p John
117 * added source safe header
130 #include <winbase.h> /* needed for memory mapping of file functions */
132 #include <sys/types.h>
135 #include <sys/stat.h>
140 //#include "outwnd.h"
141 //#include "vecmat.h"
144 #include "cfilesystem.h"
145 #include "localize.h"
148 #define CF_ROOTTYPE_PATH 0
149 #define CF_ROOTTYPE_PACK 1
152 // specifying hard drive tree
153 // searching for pack files on hard drive // Found by searching all known paths
154 // specifying cd-rom tree
155 // searching for pack files on CD-rom tree
156 typedef struct cf_root {
157 char path[CF_MAX_PATHNAME_LENGTH]; // Contains something like c:\projects\freespace or c:\projects\freespace\freespace.vp
158 int roottype; // CF_ROOTTYPE_PATH = Path, CF_ROOTTYPE_PACK =Pack file
161 // convenient type for sorting (see cf_build_pack_list())
162 typedef struct cf_root_sort {
163 char path[CF_MAX_PATHNAME_LENGTH];
168 #define CF_NUM_ROOTS_PER_BLOCK 32
169 #define CF_MAX_ROOT_BLOCKS 256 // Can store 32*256 = 8192 Roots
170 #define CF_MAX_ROOTS (CF_NUM_ROOTS_PER_BLOCK * CF_MAX_ROOT_BLOCKS)
172 typedef struct cf_root_block {
173 cf_root roots[CF_NUM_ROOTS_PER_BLOCK];
176 static int Num_roots = 0;
177 static cf_root_block *Root_blocks[CF_MAX_ROOT_BLOCKS];
180 // Created by searching all roots in order. This means Files is then sorted by precedence.
181 typedef struct cf_file {
182 char name_ext[CF_MAX_FILENAME_LENGTH]; // Filename and extension
183 int root_index; // Where in Roots this is located
184 int pathtype_index; // Where in Paths this is located
185 time_t write_time; // When it was last written
186 int size; // How big it is in bytes
187 int pack_offset; // For pack files, where it is at. 0 if not in a pack file. This can be used to tell if in a pack file.
190 #define CF_NUM_FILES_PER_BLOCK 256
191 #define CF_MAX_FILE_BLOCKS 128 // Can store 256*128 = 32768 files
193 typedef struct cf_file_block {
194 cf_file files[CF_NUM_FILES_PER_BLOCK];
197 static int Num_files = 0;
198 static cf_file_block *File_blocks[CF_MAX_FILE_BLOCKS];
201 // Return a pointer to to file 'index'.
202 cf_file *cf_get_file(int index)
204 int block = index / CF_NUM_FILES_PER_BLOCK;
205 int offset = index % CF_NUM_FILES_PER_BLOCK;
207 return &File_blocks[block]->files[offset];
210 // Create a new file and return a pointer to it.
211 cf_file *cf_create_file()
213 int block = Num_files / CF_NUM_FILES_PER_BLOCK;
214 int offset = Num_files % CF_NUM_FILES_PER_BLOCK;
216 if ( File_blocks[block] == NULL ) {
217 File_blocks[block] = (cf_file_block *)malloc( sizeof(cf_file_block) );
218 Assert( File_blocks[block] != NULL);
223 return &File_blocks[block]->files[offset];
226 extern int cfile_inited;
228 // Create a new root and return a pointer to it. The structure is assumed unitialized.
229 cf_root *cf_get_root(int n)
231 int block = n / CF_NUM_ROOTS_PER_BLOCK;
232 int offset = n % CF_NUM_ROOTS_PER_BLOCK;
237 return &Root_blocks[block]->roots[offset];
241 // Create a new root and return a pointer to it. The structure is assumed unitialized.
242 cf_root *cf_create_root()
244 int block = Num_roots / CF_NUM_ROOTS_PER_BLOCK;
245 int offset = Num_roots % CF_NUM_ROOTS_PER_BLOCK;
247 if ( Root_blocks[block] == NULL ) {
248 Root_blocks[block] = (cf_root_block *)malloc( sizeof(cf_root_block) );
249 Assert(Root_blocks[block] != NULL);
254 return &Root_blocks[block]->roots[offset];
257 // return the # of packfiles which exist
258 int cf_get_packfile_count(cf_root *root)
260 char filespec[MAX_PATH_LEN];
264 // count up how many packfiles we're gonna have
266 for (i=CF_TYPE_ROOT; i<CF_MAX_PATH_TYPES; i++ ) {
268 strcpy( filespec, root->path );
270 if(strlen(Pathtypes[i].path)){
271 strcat( filespec, Pathtypes[i].path );
272 strcat( filespec, "/" );
278 dirp = opendir (filespec);
280 while ((dir = readdir (dirp)) != NULL)
282 if (!fnmatch ("*.vp", dir->d_name, 0))
288 strcpy( filespec, root->path );
290 if(strlen(Pathtypes[i].path)){
291 strcat( filespec, Pathtypes[i].path );
292 strcat( filespec, "\\" );
295 strcat( filespec, "*.vp" );
300 find_handle = _findfirst( filespec, &find );
302 if (find_handle != -1) {
304 if (!(find.attrib & _A_SUBDIR)) {
308 } while (!_findnext(find_handle, &find));
310 _findclose( find_handle );
315 return packfile_count;
318 // packfile sort function
319 int cf_packfile_sort_func(const void *elem1, const void *elem2)
321 cf_root_sort *r1, *r2;
322 r1 = (cf_root_sort*)elem1;
323 r2 = (cf_root_sort*)elem2;
325 // if the 2 directory types are the same, do a string compare
326 if(r1->cf_type == r2->cf_type){
327 return stricmp(r1->path, r2->path);
330 // otherwise return them in order of CF_TYPE_* precedence
331 return (r1->cf_type < r2->cf_type) ? -1 : 1;
334 // Go through a root and look for pack files
335 void cf_build_pack_list( cf_root *root )
337 char filespec[MAX_PATH_LEN];
339 cf_root_sort *temp_roots_sort, *rptr_sort;
340 int temp_root_count, root_index;
342 // determine how many packfiles there are
343 temp_root_count = cf_get_packfile_count(root);
344 if(temp_root_count <= 0){
348 // allocate a temporary array of temporary roots so we can easily sort them
349 temp_roots_sort = (cf_root_sort*)malloc(sizeof(cf_root_sort) * temp_root_count);
350 if(temp_roots_sort == NULL){
355 // now just setup all the root info
357 for (i=CF_TYPE_ROOT; i<CF_MAX_PATH_TYPES; i++ ) {
360 strcpy( filespec, root->path );
362 if(strlen(Pathtypes[i].path)){
363 strcat( filespec, Pathtypes[i].path );
364 strcat( filespec, "/" );
370 dirp = opendir (filespec);
372 while ((dir = readdir (dirp)) != NULL)
374 if (!fnmatch ("*.vp", dir->d_name, 0))
376 Assert(root_index < temp_root_count);
379 snprintf(fn, MAX_PATH-1, "%s/%s", filespec, dir->d_name);
383 if (stat(fn, &buf) == -1) {
387 if (!S_ISREG(buf.st_mode)) {
391 // get a temp pointer
392 rptr_sort = &temp_roots_sort[root_index++];
394 // fill in all the proper info
395 strcpy(rptr_sort->path, root->path);
397 if(strlen(Pathtypes[i].path)){
398 strcat(rptr_sort->path, Pathtypes[i].path );
399 strcat(rptr_sort->path, "/");
402 strcat(rptr_sort->path, dir->d_name );
403 rptr_sort->roottype = CF_ROOTTYPE_PACK;
404 rptr_sort->cf_type = i;
410 strcpy( filespec, root->path );
412 if(strlen(Pathtypes[i].path)){
413 strcat( filespec, Pathtypes[i].path );
414 strcat( filespec, "\\" );
416 strcat( filespec, "*.vp" );
420 find_handle = _findfirst( filespec, &find );
422 if (find_handle != -1) {
425 if (!(find.attrib & _A_SUBDIR)) {
426 Assert(root_index < temp_root_count);
428 // get a temp pointer
429 rptr_sort = &temp_roots_sort[root_index++];
431 // fill in all the proper info
432 strcpy(rptr_sort->path, root->path);
434 if(strlen(Pathtypes[i].path)){
435 strcat(rptr_sort->path, Pathtypes[i].path );
436 strcat(rptr_sort->path, "\\");
439 strcat(rptr_sort->path, find.name );
440 rptr_sort->roottype = CF_ROOTTYPE_PACK;
441 rptr_sort->cf_type = i;
444 } while (!_findnext(find_handle, &find));
446 _findclose( find_handle );
451 // these should always be the same
452 Assert(root_index == temp_root_count);
455 qsort(temp_roots_sort, temp_root_count, sizeof(cf_root_sort), cf_packfile_sort_func);
457 // now insert them all into the real root list properly
459 for(i=0; i<temp_root_count; i++){
460 new_root = cf_create_root();
461 strcpy( new_root->path, root->path );
463 // mwa -- 4/2/98 put in the next 2 lines because the path name needs to be there
464 // to find the files.
465 strcpy(new_root->path, temp_roots_sort[i].path);
466 new_root->roottype = CF_ROOTTYPE_PACK;
469 // free up the temp list
470 free(temp_roots_sort);
474 void cf_build_root_list(const char *extras_dir)
480 // ================================================================
481 // have user's writable directory as default for loading and saving files
482 root = cf_create_root();
483 strcpy( root->path, Cfile_user_dir );
485 // do we already have a slash? as in the case of a root directory install
486 if(strlen(root->path) && (root->path[strlen(root->path)-1] != DIR_SEPARATOR_CHAR)){
487 strcat(root->path, DIR_SEPARATOR_STR); // put trailing backslash on for easier path construction
489 root->roottype = CF_ROOTTYPE_PATH;
491 //======================================================
492 // then check any VP files under the directory.
493 cf_build_pack_list(root);
495 //======================================================
496 // Next, use the executable's directory for game data
497 root = cf_create_root();
498 strcpy( root->path, Cfile_root_dir );
500 // do we already have a slash? as in the case of a root directory install
501 if(strlen(root->path) && (root->path[strlen(root->path)-1] != DIR_SEPARATOR_CHAR)){
502 strcat(root->path, DIR_SEPARATOR_STR); // put trailing backslash on for easier path construction
504 root->roottype = CF_ROOTTYPE_PATH;
506 //======================================================
507 // then check any VP files under the current directory.
508 cf_build_pack_list(root);
511 //======================================================
512 // Check the real CD if one...
513 if ( extras_dir && strlen(extras_dir) ) {
514 root = cf_create_root();
515 strcpy( root->path, extras_dir );
516 root->roottype = CF_ROOTTYPE_PATH;
518 //======================================================
519 // Next, check any VP files in the CD-ROM directory.
520 cf_build_pack_list(root);
524 // Given a lower case list of file extensions
525 // separated by spaces, return zero if ext is
527 int is_ext_in_list( const char *ext_list, char *ext )
531 strncpy( tmp_ext, ext, 127 );
533 if ( strstr(ext_list, tmp_ext )) {
540 void cf_search_root_path(int root_index)
544 cf_root *root = cf_get_root(root_index);
546 mprintf(( "Searching root '%s'\n", root->path ));
548 char search_path[CF_MAX_PATHNAME_LENGTH];
550 for (i=CF_TYPE_ROOT; i<CF_MAX_PATH_TYPES; i++ ) {
552 strcpy( search_path, root->path );
554 if(strlen(Pathtypes[i].path)){
555 strcat( search_path, Pathtypes[i].path );
556 strcat( search_path, DIR_SEPARATOR_STR );
563 // only get pilots from the primary root as there could be nasty
564 // permission issues otherwise
565 if ( (root_index > 0) && ((Pathtypes[i].index == CF_TYPE_SINGLE_PLAYERS)
566 || (Pathtypes[i].index == CF_TYPE_MULTI_PLAYERS)) ) {
570 dirp = opendir (search_path);
572 while ((dir = readdir (dirp)) != NULL)
574 if (!fnmatch ("*.*", dir->d_name, 0))
577 snprintf(fn, MAX_PATH-1, "%s/%s", search_path, dir->d_name);
581 if (stat(fn, &buf) == -1) {
585 if (!S_ISREG(buf.st_mode)) {
589 char *ext = strchr( dir->d_name, '.' );
591 if ( is_ext_in_list( Pathtypes[i].extensions, ext ) ) {
593 cf_file *file = cf_create_file();
595 strcpy( file->name_ext, dir->d_name );
596 file->root_index = root_index;
597 file->pathtype_index = i;
600 file->write_time = buf.st_mtime;
601 file->size = buf.st_size;
603 file->pack_offset = 0; // Mark as a non-packed file
605 //mprintf(( "Found file '%s'\n", file->name_ext ));
613 strcat( search_path, "*.*" );
618 find_handle = _findfirst( search_path, &find );
620 if (find_handle != -1) {
622 if (!(find.attrib & _A_SUBDIR)) {
624 char *ext = strchr( find.name, '.' );
626 if ( is_ext_in_list( Pathtypes[i].extensions, ext ) ) {
628 cf_file *file = cf_create_file();
630 strcpy( file->name_ext, find.name );
631 file->root_index = root_index;
632 file->pathtype_index = i;
633 file->write_time = find.time_write;
634 file->size = find.size;
635 file->pack_offset = 0; // Mark as a non-packed file
637 //mprintf(( "Found file '%s'\n", file->name_ext ));
644 } while (!_findnext(find_handle, &find));
646 _findclose( find_handle );
654 typedef struct VP_FILE_HEADER {
661 typedef struct VP_FILE {
665 fs_time_t write_time;
668 void cf_search_root_pack(int root_index)
672 cf_root *root = cf_get_root(root_index);
674 //mprintf(( "Searching root pack '%s'\n", root->path ));
678 FILE *fp = fopen( root->path, "rb" );
679 // Read the file header
684 VP_FILE_HEADER VP_header;
686 Assert( sizeof(VP_header) == 16 );
687 fread(&VP_header, 1, sizeof(VP_header), fp);
689 VP_header.version = INTEL_INT( VP_header.version);
690 VP_header.index_offset = INTEL_INT( VP_header.index_offset);
691 VP_header.num_files = INTEL_INT( VP_header.num_files);
694 fseek(fp, VP_header.index_offset, SEEK_SET);
696 char search_path[CF_MAX_PATHNAME_LENGTH];
698 strcpy( search_path, "" );
700 // Go through all the files
701 for (i=0; i<VP_header.num_files; i++ ) {
704 fread( &find, sizeof(VP_FILE), 1, fp );
706 find.offset = INTEL_INT( find.offset );
707 find.size = INTEL_INT( find.size );
708 find.write_time = INTEL_INT(find.write_time);
710 if ( find.size == 0 ) {
711 if ( !stricmp( find.filename, ".." )) {
712 int l = strlen(search_path);
713 char *p = &search_path[l-1];
714 while( (p > search_path) && (*p != DIR_SEPARATOR_CHAR) ) {
719 if ( strlen(search_path) ) {
720 strcat( search_path, DIR_SEPARATOR_STR );
722 strcat( search_path, find.filename );
725 //mprintf(( "Current dir = '%s'\n", search_path ));
729 for (j=CF_TYPE_ROOT; j<CF_MAX_PATH_TYPES; j++ ) {
731 if ( !stricmp( search_path, Pathtypes[j].path )) {
733 char *ext = strchr( find.filename, '.' );
735 if ( is_ext_in_list( Pathtypes[j].extensions, ext ) ) {
737 cf_file *file = cf_create_file();
739 strcpy( file->name_ext, find.filename );
740 file->root_index = root_index;
741 file->pathtype_index = j;
742 file->write_time = find.write_time;
743 file->size = find.size;
744 file->pack_offset = find.offset; // Mark as a non-packed file
746 //mprintf(( "Found pack file '%s'\n", file->name_ext ));
760 void cf_build_file_list()
766 // For each root, find all files...
767 for (i=1; i<Num_roots; i++ ) {
768 cf_root *root = cf_get_root(i);
769 if ( root->roottype == CF_ROOTTYPE_PATH ) {
770 cf_search_root_path(i);
771 } else if ( root->roottype == CF_ROOTTYPE_PACK ) {
772 cf_search_root_pack(i);
779 void cf_build_secondary_filelist(const char *extras_dir)
787 // Init the path types
788 for (i=0; i<CF_MAX_PATH_TYPES; i++ ) {
789 Assert( Pathtypes[i].index == i );
790 #if 0 /* they are already lowercased -- SBF */
791 if ( Pathtypes[i].extensions ) {
792 strlwr(Pathtypes[i].extensions);
797 // Init the root blocks
798 for (i=0; i<CF_MAX_ROOT_BLOCKS; i++ ) {
799 Root_blocks[i] = NULL;
802 // Init the file blocks
803 for (i=0; i<CF_MAX_FILE_BLOCKS; i++ ) {
804 File_blocks[i] = NULL;
807 mprintf(( "Building file index...\n" ));
809 // build the list of searchable roots
810 cf_build_root_list(extras_dir);
812 // build the list of files themselves
813 cf_build_file_list();
815 mprintf(( "Found %d roots and %d files.\n", Num_roots, Num_files ));
818 void cf_free_secondary_filelist()
822 // Free the root blocks
823 for (i=0; i<CF_MAX_ROOT_BLOCKS; i++ ) {
824 if ( Root_blocks[i] ) {
825 free( Root_blocks[i] );
826 Root_blocks[i] = NULL;
831 // Init the file blocks
832 for (i=0; i<CF_MAX_FILE_BLOCKS; i++ ) {
833 if ( File_blocks[i] ) {
834 free( File_blocks[i] );
835 File_blocks[i] = NULL;
841 // Searches for a file. Follows all rules and precedence and searches
842 // CD's and pack files.
843 // Input: filespace - Filename & extension
844 // pathtype - See CF_TYPE_ defines in CFILE.H
845 // Output: pack_filename - Absolute path and filename of this file. Could be a packfile or the actual file.
847 // offset - Offset into pack file. 0 if not a packfile.
848 // Returns: If not found returns 0.
849 int cf_find_file_location( const char *filespec, int pathtype, char *pack_filename, int *size, int *offset, bool localize )
853 Assert(filespec && strlen(filespec));
855 // see if we have something other than just a filename
856 // our current rules say that any file that specifies a direct
857 // path will try to be opened on that path. If that open
858 // fails, then we will open the file based on the extension
861 // NOTE: full path should also include localization, if so desired
863 if ( strpbrk(filespec, "/") ) {
865 if ( strpbrk(filespec,"/\\:") ) { // do we have a full path already?
867 FILE *fp = fopen(filespec, "rb" );
869 if ( size ) *size = filelength(fileno(fp));
870 if ( offset ) *offset = 0;
871 if ( pack_filename ) {
872 strcpy( pack_filename, filespec );
878 return 0; // If they give a full path, fail if not found.
881 // Search the hard drive for files first.
882 int num_search_dirs = 0;
883 int search_order[CF_MAX_PATH_TYPES];
885 if ( CF_TYPE_SPECIFIED(pathtype) ) {
886 search_order[num_search_dirs++] = pathtype;
889 for (i=CF_TYPE_ROOT; i<CF_MAX_PATH_TYPES; i++) {
890 if ( i != pathtype ) {
891 search_order[num_search_dirs++] = i;
895 for (i=0; i<num_search_dirs; i++ ) {
896 char longname[MAX_PATH_LEN];
898 cf_create_default_path_string( longname, search_order[i], filespec, localize );
900 FILE *fp = fopen(longname, "rb" );
902 if ( size ) *size = filelength(fileno(fp));
903 if ( offset ) *offset = 0;
904 if ( pack_filename ) {
905 strcpy( pack_filename, longname );
912 // Search the pak files and CD-ROM.
914 for (i=0; i<Num_files; i++ ) {
915 cf_file * f = cf_get_file(i);
917 // only search paths we're supposed to...
918 if ( (pathtype != CF_TYPE_ANY) && (pathtype != f->pathtype_index) ) {
923 // create localized filespec
924 char loc_filespec[MAX_PATH_LEN];
925 strcpy(loc_filespec, filespec);
926 lcl_add_dir_to_path_with_filename(loc_filespec);
928 if ( !stricmp(loc_filespec, f->name_ext) ) {
929 if ( size ) *size = f->size;
930 if ( offset ) *offset = f->pack_offset;
931 if ( pack_filename ) {
932 cf_root * r = cf_get_root(f->root_index);
934 strcpy( pack_filename, r->path );
935 if ( f->pack_offset < 1 ) {
936 strcat( pack_filename, Pathtypes[f->pathtype_index].path );
937 strcat( pack_filename, DIR_SEPARATOR_STR );
938 strcat( pack_filename, f->name_ext );
945 // file either not localized or localized version not found
946 if ( !stricmp(filespec, f->name_ext) ) {
947 if ( size ) *size = f->size;
948 if ( offset ) *offset = f->pack_offset;
949 if ( pack_filename ) {
950 cf_root * r = cf_get_root(f->root_index);
952 strcpy( pack_filename, r->path );
953 if ( f->pack_offset < 1 ) {
955 if(strlen(Pathtypes[f->pathtype_index].path)){
956 strcat( pack_filename, Pathtypes[f->pathtype_index].path );
957 strcat( pack_filename, DIR_SEPARATOR_STR );
960 strcat( pack_filename, f->name_ext );
971 // Returns true if filename matches filespec, else zero if not
972 int cf_matches_spec(const char *filespec, const char *filename)
974 const char *src_ext, *dst_ext;
976 src_ext = strchr(filespec, '.');
982 dst_ext = strchr(filename, '.');
986 return !stricmp(dst_ext, src_ext);
989 int (*Get_file_list_filter)(const char *filename) = NULL;
990 int Skip_packfile_search = 0;
992 int cf_file_already_in_list( int num_files, char **list, char *filename )
996 char name_no_extension[MAX_PATH_LEN];
998 strcpy(name_no_extension, filename );
999 char *p = strchr( name_no_extension, '.' );
1002 for (i=0; i<num_files; i++ ) {
1003 if ( !stricmp(list[i], name_no_extension ) ) {
1012 // An alternative cf_get_file_list(), dynamic list version.
1013 // This one has a 'type', which is a CF_TYPE_* value. Because this specifies the directory
1014 // location, 'filter' only needs to be the filter itself, with no path information.
1015 // See above descriptions of cf_get_file_list() for more information about how it all works.
1016 int cf_get_file_list( int max, char **list, int pathtype, const char *filter, int sort, file_list_info *info )
1019 int i, l, num_files = 0, own_flag = 0;
1026 Get_file_list_filter = NULL;
1032 if (!info && (sort == CF_SORT_TIME)) {
1033 info = (file_list_info *) malloc(sizeof(file_list_info) * max);
1037 char filespec[MAX_PATH_LEN];
1040 cf_create_default_path_string( filespec, pathtype, NULL );
1045 dirp = opendir (filespec);
1047 while ((dir = readdir (dirp)) != NULL)
1049 if (num_files >= max)
1052 if (fnmatch(filter, dir->d_name, 0) != 0)
1056 snprintf(fn, MAX_PATH-1, "%s/%s", filespec, dir->d_name);
1060 if (stat(fn, &buf) == -1) {
1064 if (!S_ISREG(buf.st_mode)) {
1068 if ( !Get_file_list_filter || (*Get_file_list_filter)(dir->d_name) ) {
1069 ptr = strrchr(dir->d_name, '.');
1071 l = ptr - dir->d_name;
1073 l = strlen(dir->d_name);
1075 list[num_files] = (char *)malloc(l + 1);
1076 strncpy(list[num_files], dir->d_name, l);
1077 list[num_files][l] = 0;
1079 info[num_files].write_time = buf.st_mtime;
1088 cf_create_default_path_string( filespec, pathtype, filter );
1090 find_handle = _findfirst( filespec, &find );
1091 if (find_handle != -1) {
1093 if (num_files >= max)
1096 if (!(find.attrib & _A_SUBDIR)) {
1097 if ( !Get_file_list_filter || (*Get_file_list_filter)(find.name) ) {
1098 ptr = strrchr(find.name, '.');
1100 l = ptr - find.name;
1102 l = strlen(find.name);
1104 list[num_files] = (char *)malloc(l + 1);
1105 strncpy(list[num_files], find.name, l);
1106 list[num_files][l] = 0;
1108 info[num_files].write_time = find.time_write;
1114 } while (!_findnext(find_handle, &find));
1116 _findclose( find_handle );
1120 // Search all the packfiles and CD.
1121 if ( !Skip_packfile_search ) {
1122 for (i=0; i<Num_files; i++ ) {
1123 cf_file * f = cf_get_file(i);
1125 // only search paths we're supposed to...
1126 if ( (pathtype != CF_TYPE_ANY) && (pathtype != f->pathtype_index) ) {
1130 if (num_files >= max)
1133 if ( !cf_matches_spec( filter,f->name_ext)) {
1137 if ( cf_file_already_in_list(num_files,list,f->name_ext)) {
1141 if ( !Get_file_list_filter || (*Get_file_list_filter)(f->name_ext) ) {
1143 //mprintf(( "Found '%s' in root %d path %d\n", f->name_ext, f->root_index, f->pathtype_index ));
1145 ptr = strrchr(f->name_ext, '.');
1147 l = ptr - f->name_ext;
1149 l = strlen(f->name_ext);
1151 list[num_files] = (char *)malloc(l + 1);
1152 strncpy(list[num_files], f->name_ext, l);
1153 list[num_files][l] = 0;
1156 info[num_files].write_time = f->write_time;
1166 if (sort != CF_SORT_NONE) {
1167 cf_sort_filenames( num_files, list, sort, info );
1174 Get_file_list_filter = NULL;
1178 int cf_file_already_in_list_preallocated( int num_files, char arr[][MAX_FILENAME_LEN], const char *filename )
1182 char name_no_extension[MAX_PATH_LEN];
1184 strcpy(name_no_extension, filename );
1185 char *p = strchr( name_no_extension, '.' );
1188 for (i=0; i<num_files; i++ ) {
1189 if ( !stricmp(arr[i], name_no_extension ) ) {
1198 // An alternative cf_get_file_list(), fixed array version.
1199 // This one has a 'type', which is a CF_TYPE_* value. Because this specifies the directory
1200 // location, 'filter' only needs to be the filter itself, with no path information.
1201 // See above descriptions of cf_get_file_list() for more information about how it all works.
1202 int cf_get_file_list_preallocated( int max, char arr[][MAX_FILENAME_LEN], char **list, int pathtype, const char *filter, int sort, file_list_info *info )
1204 int i, num_files = 0, own_flag = 0;
1207 Get_file_list_filter = NULL;
1212 for (i=0; i<max; i++) {
1216 sort = CF_SORT_NONE; // sorting of array directly not supported. Sorting done on list only
1219 if (!info && (sort == CF_SORT_TIME)) {
1220 info = (file_list_info *) malloc(sizeof(file_list_info) * max);
1224 char filespec[MAX_PATH_LEN];
1226 // Search the default directories
1228 cf_create_default_path_string( filespec, pathtype, NULL );
1233 dirp = opendir (filespec);
1235 while ((dir = readdir (dirp)) != NULL)
1237 if (num_files >= max)
1240 if (fnmatch(filter, dir->d_name, 0) != 0)
1244 snprintf(fn, MAX_PATH-1, "%s/%s", filespec, dir->d_name);
1248 if (stat(fn, &buf) == -1) {
1252 if (!S_ISREG(buf.st_mode)) {
1256 if ( !Get_file_list_filter || (*Get_file_list_filter)(dir->d_name) ) {
1258 strncpy(arr[num_files], dir->d_name, MAX_FILENAME_LEN - 1 );
1259 char *ptr = strrchr(arr[num_files], '.');
1265 info[num_files].write_time = buf.st_mtime;
1274 cf_create_default_path_string( filespec, pathtype, filter );
1279 find_handle = _findfirst( filespec, &find );
1280 if (find_handle != -1) {
1282 if (num_files >= max)
1285 if (!(find.attrib & _A_SUBDIR)) {
1287 if ( !Get_file_list_filter || (*Get_file_list_filter)(find.name) ) {
1289 strncpy(arr[num_files], find.name, MAX_FILENAME_LEN - 1 );
1290 char *ptr = strrchr(arr[num_files], '.');
1296 info[num_files].write_time = find.time_write;
1303 } while (!_findnext(find_handle, &find));
1305 _findclose( find_handle );
1310 // Search all the packfiles and CD.
1311 if ( !Skip_packfile_search ) {
1312 for (i=0; i<Num_files; i++ ) {
1313 cf_file * f = cf_get_file(i);
1315 // only search paths we're supposed to...
1316 if ( (pathtype != CF_TYPE_ANY) && (pathtype != f->pathtype_index) ) {
1320 if (num_files >= max)
1323 if ( !cf_matches_spec( filter,f->name_ext)) {
1327 if ( cf_file_already_in_list_preallocated( num_files, arr, f->name_ext )) {
1331 if ( !Get_file_list_filter || (*Get_file_list_filter)(f->name_ext) ) {
1333 //mprintf(( "Found '%s' in root %d path %d\n", f->name_ext, f->root_index, f->pathtype_index ));
1335 strncpy(arr[num_files], f->name_ext, MAX_FILENAME_LEN - 1 );
1336 char *ptr = strrchr(arr[num_files], '.');
1342 info[num_files].write_time = f->write_time;
1351 if (sort != CF_SORT_NONE) {
1353 cf_sort_filenames( num_files, list, sort, info );
1360 Get_file_list_filter = NULL;
1364 // Returns the default storage path for files given a
1365 // particular pathtype. In other words, the path to
1366 // the unpacked, non-cd'd, stored on hard drive path.
1367 // If filename isn't null it will also tack the filename
1368 // on the end, creating a completely valid filename.
1369 // Input: pathtype - CF_TYPE_??
1370 // filename - optional, if set, tacks the filename onto end of path.
1371 // Output: path - Fully qualified pathname.
1372 void cf_create_default_path_string( char *path, int pathtype, const char *filename, bool localize )
1375 if ( filename && strpbrk(filename, "/") ) {
1377 if ( filename && strpbrk(filename,"/\\:") ) {
1379 // Already has full path
1380 strcpy( path, filename );
1383 if ( cfile_init_paths() ) {
1384 strcpy(path, filename);
1388 Assert(CF_TYPE_SPECIFIED(pathtype));
1390 strcpy(path, Cfile_user_dir);
1391 strcat(path, Pathtypes[pathtype].path);
1393 // Don't add slash for root directory
1394 if (Pathtypes[pathtype].path[0] != '\0') {
1395 strcat(path, DIR_SEPARATOR_STR);
1400 strcat(path, filename);
1402 // localize filename
1404 // create copy of path
1405 char temp_path[MAX_PATH_LEN];
1406 strcpy(temp_path, path);
1408 // localize the path
1409 lcl_add_dir_to_path_with_filename(path);
1411 // verify localized path
1412 FILE *fp = fopen(path, "rb");
1416 strcpy(path, temp_path);