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.6 2002/06/09 04:41:15 relnev
23 * added copyright header
25 * Revision 1.5 2002/06/05 04:03:32 relnev
26 * finished cfilesystem.
28 * removed some old code.
30 * fixed mouse save off-by-one.
34 * Revision 1.4 2002/05/28 17:26:57 theoddone33
35 * Fill in some timer and palette setting stubs. Still no display
37 * Revision 1.3 2002/05/28 06:45:38 theoddone33
40 * Revision 1.2 2002/05/28 06:28:20 theoddone33
41 * Filesystem mods, actually reads some data files now
43 * Revision 1.1.1.1 2002/05/03 03:28:08 root
47 * 6 9/08/99 10:01p Dave
48 * Make sure game won't run in a drive's root directory. Make sure
49 * standalone routes suqad war messages properly to the host.
51 * 5 9/03/99 1:31a Dave
52 * CD checking by act. Added support to play 2 cutscenes in a row
53 * seamlessly. Fixed super low level cfile bug related to files in the
54 * root directory of a CD. Added cheat code to set campaign mission # in
57 * 4 2/22/99 10:31p Andsager
58 * Get rid of unneeded includes.
60 * 3 10/13/98 9:19a Andsager
61 * Add localization support to cfile. Optional parameter with cfopen that
62 * looks for localized files.
64 * 2 10/07/98 10:52a Dave
67 * 1 10/07/98 10:48a Dave
69 * 14 8/31/98 2:06p Dave
70 * Make cfile sort the ordering or vp files. Added support/checks for
71 * recognizing "mission disk" players.
73 * 13 6/23/98 4:18p Hoffoss
74 * Fixed some bugs with AC release build.
76 * 12 5/20/98 10:46p John
77 * Added code that doesn't include duplicate filenames in any file list
80 * 11 5/14/98 2:14p Lawrance2
81 * Use filespec filtering for packfiles
83 * 10 5/03/98 11:53a John
84 * Fixed filename case mangling.
86 * 9 5/02/98 11:06p Allender
87 * correctly deal with pack pathnames
89 * 8 5/01/98 11:41a Allender
90 * Fixed bug with mission saving in Fred.
92 * 7 5/01/98 10:21a John
93 * Added code to find all pack files in all trees. Added code to create
94 * any directories that we write to.
96 * 6 4/30/98 10:21p John
97 * Added code to cleanup cfilesystem
99 * 5 4/30/98 10:18p John
100 * added source safe header
113 #include <winbase.h> /* needed for memory mapping of file functions */
115 #include <sys/types.h>
118 #include <sys/stat.h>
123 //#include "outwnd.h"
124 //#include "vecmat.h"
127 #include "cfilesystem.h"
128 #include "localize.h"
131 #define CF_ROOTTYPE_PATH 0
132 #define CF_ROOTTYPE_PACK 1
135 // specifying hard drive tree
136 // searching for pack files on hard drive // Found by searching all known paths
137 // specifying cd-rom tree
138 // searching for pack files on CD-rom tree
139 typedef struct cf_root {
140 char path[CF_MAX_PATHNAME_LENGTH]; // Contains something like c:\projects\freespace or c:\projects\freespace\freespace.vp
141 int roottype; // CF_ROOTTYPE_PATH = Path, CF_ROOTTYPE_PACK =Pack file
144 // convenient type for sorting (see cf_build_pack_list())
145 typedef struct cf_root_sort {
146 char path[CF_MAX_PATHNAME_LENGTH];
151 #define CF_NUM_ROOTS_PER_BLOCK 32
152 #define CF_MAX_ROOT_BLOCKS 256 // Can store 32*256 = 8192 Roots
153 #define CF_MAX_ROOTS (CF_NUM_ROOTS_PER_BLOCK * CF_MAX_ROOT_BLOCKS)
155 typedef struct cf_root_block {
156 cf_root roots[CF_NUM_ROOTS_PER_BLOCK];
159 static int Num_roots = 0;
160 static cf_root_block *Root_blocks[CF_MAX_ROOT_BLOCKS];
163 // Created by searching all roots in order. This means Files is then sorted by precedence.
164 typedef struct cf_file {
165 char name_ext[CF_MAX_FILENAME_LENGTH]; // Filename and extension
166 int root_index; // Where in Roots this is located
167 int pathtype_index; // Where in Paths this is located
168 time_t write_time; // When it was last written
169 int size; // How big it is in bytes
170 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.
173 #define CF_NUM_FILES_PER_BLOCK 256
174 #define CF_MAX_FILE_BLOCKS 128 // Can store 256*128 = 32768 files
176 typedef struct cf_file_block {
177 cf_file files[CF_NUM_FILES_PER_BLOCK];
180 static int Num_files = 0;
181 static cf_file_block *File_blocks[CF_MAX_FILE_BLOCKS];
184 // Return a pointer to to file 'index'.
185 cf_file *cf_get_file(int index)
187 int block = index / CF_NUM_FILES_PER_BLOCK;
188 int offset = index % CF_NUM_FILES_PER_BLOCK;
190 return &File_blocks[block]->files[offset];
193 // Create a new file and return a pointer to it.
194 cf_file *cf_create_file()
196 int block = Num_files / CF_NUM_FILES_PER_BLOCK;
197 int offset = Num_files % CF_NUM_FILES_PER_BLOCK;
199 if ( File_blocks[block] == NULL ) {
200 File_blocks[block] = (cf_file_block *)malloc( sizeof(cf_file_block) );
201 Assert( File_blocks[block] != NULL);
206 return &File_blocks[block]->files[offset];
209 extern int cfile_inited;
211 // Create a new root and return a pointer to it. The structure is assumed unitialized.
212 cf_root *cf_get_root(int n)
214 int block = n / CF_NUM_ROOTS_PER_BLOCK;
215 int offset = n % CF_NUM_ROOTS_PER_BLOCK;
220 return &Root_blocks[block]->roots[offset];
224 // Create a new root and return a pointer to it. The structure is assumed unitialized.
225 cf_root *cf_create_root()
227 int block = Num_roots / CF_NUM_ROOTS_PER_BLOCK;
228 int offset = Num_roots % CF_NUM_ROOTS_PER_BLOCK;
230 if ( Root_blocks[block] == NULL ) {
231 Root_blocks[block] = (cf_root_block *)malloc( sizeof(cf_root_block) );
232 Assert(Root_blocks[block] != NULL);
237 return &Root_blocks[block]->roots[offset];
240 // return the # of packfiles which exist
241 int cf_get_packfile_count(cf_root *root)
243 char filespec[MAX_PATH_LEN];
247 // count up how many packfiles we're gonna have
249 for (i=CF_TYPE_ROOT; i<CF_MAX_PATH_TYPES; i++ ) {
251 strcpy( filespec, root->path );
253 if(strlen(Pathtypes[i].path)){
254 strcat( filespec, Pathtypes[i].path );
255 strcat( filespec, "/" );
261 dirp = opendir (filespec);
263 while ((dir = readdir (dirp)) != NULL)
265 if (!fnmatch ("*.vp", dir->d_name, 0))
271 strcpy( filespec, root->path );
273 if(strlen(Pathtypes[i].path)){
274 strcat( filespec, Pathtypes[i].path );
275 strcat( filespec, "\\" );
278 strcat( filespec, "*.vp" );
283 find_handle = _findfirst( filespec, &find );
285 if (find_handle != -1) {
287 if (!(find.attrib & _A_SUBDIR)) {
291 } while (!_findnext(find_handle, &find));
293 _findclose( find_handle );
298 return packfile_count;
301 // packfile sort function
302 int cf_packfile_sort_func(const void *elem1, const void *elem2)
304 cf_root_sort *r1, *r2;
305 r1 = (cf_root_sort*)elem1;
306 r2 = (cf_root_sort*)elem2;
308 // if the 2 directory types are the same, do a string compare
309 if(r1->cf_type == r2->cf_type){
310 return stricmp(r1->path, r2->path);
313 // otherwise return them in order of CF_TYPE_* precedence
314 return (r1->cf_type < r2->cf_type) ? -1 : 1;
317 // Go through a root and look for pack files
318 void cf_build_pack_list( cf_root *root )
320 char filespec[MAX_PATH_LEN];
322 cf_root_sort *temp_roots_sort, *rptr_sort;
323 int temp_root_count, root_index;
325 // determine how many packfiles there are
326 temp_root_count = cf_get_packfile_count(root);
327 if(temp_root_count <= 0){
331 // allocate a temporary array of temporary roots so we can easily sort them
332 temp_roots_sort = (cf_root_sort*)malloc(sizeof(cf_root_sort) * temp_root_count);
333 if(temp_roots_sort == NULL){
338 // now just setup all the root info
340 for (i=CF_TYPE_ROOT; i<CF_MAX_PATH_TYPES; i++ ) {
343 strcpy( filespec, root->path );
345 if(strlen(Pathtypes[i].path)){
346 strcat( filespec, Pathtypes[i].path );
347 strcat( filespec, "/" );
353 dirp = opendir (filespec);
355 while ((dir = readdir (dirp)) != NULL)
357 if (!fnmatch ("*.vp", dir->d_name, 0))
359 Assert(root_index < temp_root_count);
362 snprintf(fn, MAX_PATH-1, "%s/%s", filespec, dir->d_name);
366 if (stat(fn, &buf) == -1) {
370 if (!S_ISREG(buf.st_mode)) {
374 // get a temp pointer
375 rptr_sort = &temp_roots_sort[root_index++];
377 // fill in all the proper info
378 strcpy(rptr_sort->path, root->path);
380 if(strlen(Pathtypes[i].path)){
381 strcat(rptr_sort->path, Pathtypes[i].path );
382 strcat(rptr_sort->path, "/");
385 strcat(rptr_sort->path, dir->d_name );
386 rptr_sort->roottype = CF_ROOTTYPE_PACK;
387 rptr_sort->cf_type = i;
393 strcpy( filespec, root->path );
395 if(strlen(Pathtypes[i].path)){
396 strcat( filespec, Pathtypes[i].path );
397 strcat( filespec, "\\" );
399 strcat( filespec, "*.vp" );
403 find_handle = _findfirst( filespec, &find );
405 if (find_handle != -1) {
408 if (!(find.attrib & _A_SUBDIR)) {
409 Assert(root_index < temp_root_count);
411 // get a temp pointer
412 rptr_sort = &temp_roots_sort[root_index++];
414 // fill in all the proper info
415 strcpy(rptr_sort->path, root->path);
417 if(strlen(Pathtypes[i].path)){
418 strcat(rptr_sort->path, Pathtypes[i].path );
419 strcat(rptr_sort->path, "\\");
422 strcat(rptr_sort->path, find.name );
423 rptr_sort->roottype = CF_ROOTTYPE_PACK;
424 rptr_sort->cf_type = i;
427 } while (!_findnext(find_handle, &find));
429 _findclose( find_handle );
434 // these should always be the same
435 Assert(root_index == temp_root_count);
438 qsort(temp_roots_sort, temp_root_count, sizeof(cf_root_sort), cf_packfile_sort_func);
440 // now insert them all into the real root list properly
442 for(i=0; i<temp_root_count; i++){
443 new_root = cf_create_root();
444 strcpy( new_root->path, root->path );
446 // mwa -- 4/2/98 put in the next 2 lines because the path name needs to be there
447 // to find the files.
448 strcpy(new_root->path, temp_roots_sort[i].path);
449 new_root->roottype = CF_ROOTTYPE_PACK;
452 // free up the temp list
453 free(temp_roots_sort);
457 void cf_build_root_list(char *cdrom_dir)
463 //======================================================
464 // First, check the current directory.
465 // strcpy( root->path, "d:\\projects\\freespace\\" );
467 root = cf_create_root();
469 if ( !_getcwd(root->path, CF_MAX_PATHNAME_LENGTH ) ) {
470 Error(LOCATION, "Can't get current working directory -- %d", errno );
473 // do we already have a slash? as in the case of a root directory install
475 if(strlen(root->path) && (root->path[strlen(root->path)-1] != '/')){
476 strcat(root->path, "/"); // put trailing backslash on for easier path construction
478 if(strlen(root->path) && (root->path[strlen(root->path)-1] != '\\')){
479 strcat(root->path, "\\"); // put trailing backslash on for easier path construction
482 root->roottype = CF_ROOTTYPE_PATH;
484 //======================================================
485 // Next, check any VP files under the current directory.
486 cf_build_pack_list(root);
489 //======================================================
490 // Check the real CD if one...
491 if ( cdrom_dir && strlen(cdrom_dir) ) {
492 root = cf_create_root();
493 strcpy( root->path, cdrom_dir );
494 root->roottype = CF_ROOTTYPE_PATH;
496 //======================================================
497 // Next, check any VP files in the CD-ROM directory.
498 cf_build_pack_list(root);
504 // Given a lower case list of file extensions
505 // separated by spaces, return zero if ext is
507 int is_ext_in_list( char *ext_list, char *ext )
511 strncpy( tmp_ext, ext, 127 );
513 if ( strstr(ext_list, tmp_ext )) {
520 void cf_search_root_path(int root_index)
524 cf_root *root = cf_get_root(root_index);
526 mprintf(( "Searching root '%s'\n", root->path ));
528 char search_path[CF_MAX_PATHNAME_LENGTH];
530 for (i=CF_TYPE_ROOT; i<CF_MAX_PATH_TYPES; i++ ) {
536 strcpy( search_path, root->path );
538 if(strlen(Pathtypes[i].path)){
539 strcat( search_path, Pathtypes[i].path );
540 strcat( search_path, "/" );
543 dirp = opendir (search_path);
545 while ((dir = readdir (dirp)) != NULL)
547 if (!fnmatch ("*.*", dir->d_name, 0))
550 snprintf(fn, MAX_PATH-1, "%s/%s", search_path, dir->d_name);
554 if (stat(fn, &buf) == -1) {
558 if (!S_ISREG(buf.st_mode)) {
562 char *ext = strchr( dir->d_name, '.' );
564 if ( is_ext_in_list( Pathtypes[i].extensions, ext ) ) {
566 cf_file *file = cf_create_file();
568 strcpy( file->name_ext, dir->d_name );
569 file->root_index = root_index;
570 file->pathtype_index = i;
573 file->write_time = buf.st_mtime;
574 file->size = buf.st_size;
576 file->pack_offset = 0; // Mark as a non-packed file
578 //mprintf(( "Found file '%s'\n", file->name_ext ));
586 strcpy( search_path, root->path );
588 if(strlen(Pathtypes[i].path)){
589 strcat( search_path, Pathtypes[i].path );
590 strcat( search_path, "\\" );
593 strcat( search_path, "*.*" );
598 find_handle = _findfirst( search_path, &find );
600 if (find_handle != -1) {
602 if (!(find.attrib & _A_SUBDIR)) {
604 char *ext = strchr( find.name, '.' );
606 if ( is_ext_in_list( Pathtypes[i].extensions, ext ) ) {
608 cf_file *file = cf_create_file();
610 strcpy( file->name_ext, find.name );
611 file->root_index = root_index;
612 file->pathtype_index = i;
613 file->write_time = find.time_write;
614 file->size = find.size;
615 file->pack_offset = 0; // Mark as a non-packed file
617 //mprintf(( "Found file '%s'\n", file->name_ext ));
624 } while (!_findnext(find_handle, &find));
626 _findclose( find_handle );
634 typedef struct VP_FILE_HEADER {
641 typedef struct VP_FILE {
648 void cf_search_root_pack(int root_index)
652 cf_root *root = cf_get_root(root_index);
654 //mprintf(( "Searching root pack '%s'\n", root->path ));
658 FILE *fp = fopen( root->path, "rb" );
659 // Read the file header
664 VP_FILE_HEADER VP_header;
666 Assert( sizeof(VP_header) == 16 );
667 fread(&VP_header, 1, sizeof(VP_header), fp);
670 fseek(fp, VP_header.index_offset, SEEK_SET);
672 char search_path[CF_MAX_PATHNAME_LENGTH];
674 strcpy( search_path, "" );
676 // Go through all the files
677 for (i=0; i<VP_header.num_files; i++ ) {
680 fread( &find, sizeof(VP_FILE), 1, fp );
682 if ( find.size == 0 ) {
683 if ( !stricmp( find.filename, ".." )) {
684 int l = strlen(search_path);
685 char *p = &search_path[l-1];
687 while( (p > search_path) && (*p != '/') ) {
689 while( (p > search_path) && (*p != '\\') ) {
695 if ( strlen(search_path) ) {
697 strcat( search_path, "/" );
699 strcat( search_path, "\\" );
702 strcat( search_path, find.filename );
705 //mprintf(( "Current dir = '%s'\n", search_path ));
709 for (j=CF_TYPE_ROOT; j<CF_MAX_PATH_TYPES; j++ ) {
711 if ( !stricmp( search_path, Pathtypes[j].path )) {
713 char *ext = strchr( find.filename, '.' );
715 if ( is_ext_in_list( Pathtypes[j].extensions, ext ) ) {
717 cf_file *file = cf_create_file();
719 strcpy( file->name_ext, find.filename );
720 file->root_index = root_index;
721 file->pathtype_index = j;
722 file->write_time = find.write_time;
723 file->size = find.size;
724 file->pack_offset = find.offset; // Mark as a non-packed file
726 //mprintf(( "Found pack file '%s'\n", file->name_ext ));
740 void cf_build_file_list()
746 // For each root, find all files...
747 for (i=1; i<Num_roots; i++ ) {
748 cf_root *root = cf_get_root(i);
749 if ( root->roottype == CF_ROOTTYPE_PATH ) {
750 cf_search_root_path(i);
751 } else if ( root->roottype == CF_ROOTTYPE_PACK ) {
752 cf_search_root_pack(i);
759 void cf_build_secondary_filelist(char *cdrom_dir)
767 // Init the path types
768 for (i=0; i<CF_MAX_PATH_TYPES; i++ ) {
769 Assert( Pathtypes[i].index == i );
770 if ( Pathtypes[i].extensions ) {
771 strlwr(Pathtypes[i].extensions);
775 // Init the root blocks
776 for (i=0; i<CF_MAX_ROOT_BLOCKS; i++ ) {
777 Root_blocks[i] = NULL;
780 // Init the file blocks
781 for (i=0; i<CF_MAX_FILE_BLOCKS; i++ ) {
782 File_blocks[i] = NULL;
785 mprintf(( "Building file index...\n" ));
787 // build the list of searchable roots
788 cf_build_root_list(cdrom_dir);
790 // build the list of files themselves
791 cf_build_file_list();
793 mprintf(( "Found %d roots and %d files.\n", Num_roots, Num_files ));
796 void cf_free_secondary_filelist()
800 // Free the root blocks
801 for (i=0; i<CF_MAX_ROOT_BLOCKS; i++ ) {
802 if ( Root_blocks[i] ) {
803 free( Root_blocks[i] );
804 Root_blocks[i] = NULL;
809 // Init the file blocks
810 for (i=0; i<CF_MAX_FILE_BLOCKS; i++ ) {
811 if ( File_blocks[i] ) {
812 free( File_blocks[i] );
813 File_blocks[i] = NULL;
819 // Searches for a file. Follows all rules and precedence and searches
820 // CD's and pack files.
821 // Input: filespace - Filename & extension
822 // pathtype - See CF_TYPE_ defines in CFILE.H
823 // Output: pack_filename - Absolute path and filename of this file. Could be a packfile or the actual file.
825 // offset - Offset into pack file. 0 if not a packfile.
826 // Returns: If not found returns 0.
827 int cf_find_file_location( char *filespec, int pathtype, char *pack_filename, int *size, int *offset, bool localize )
831 Assert(filespec && strlen(filespec));
833 // see if we have something other than just a filename
834 // our current rules say that any file that specifies a direct
835 // path will try to be opened on that path. If that open
836 // fails, then we will open the file based on the extension
839 // NOTE: full path should also include localization, if so desired
840 if ( strpbrk(filespec,"/\\:") ) { // do we have a full path already?
841 FILE *fp = fopen(filespec, "rb" );
843 if ( size ) *size = filelength(fileno(fp));
844 if ( offset ) *offset = 0;
845 if ( pack_filename ) {
846 strcpy( pack_filename, filespec );
852 return 0; // If they give a full path, fail if not found.
855 // Search the hard drive for files first.
856 int num_search_dirs = 0;
857 int search_order[CF_MAX_PATH_TYPES];
859 if ( CF_TYPE_SPECIFIED(pathtype) ) {
860 search_order[num_search_dirs++] = pathtype;
863 for (i=CF_TYPE_ROOT; i<CF_MAX_PATH_TYPES; i++) {
864 if ( i != pathtype ) {
865 search_order[num_search_dirs++] = i;
869 for (i=0; i<num_search_dirs; i++ ) {
870 char longname[MAX_PATH_LEN];
872 cf_create_default_path_string( longname, search_order[i], filespec, localize );
874 FILE *fp = fopen(longname, "rb" );
876 if ( size ) *size = filelength(fileno(fp));
877 if ( offset ) *offset = 0;
878 if ( pack_filename ) {
879 strcpy( pack_filename, longname );
886 // Search the pak files and CD-ROM.
888 for (i=0; i<Num_files; i++ ) {
889 cf_file * f = cf_get_file(i);
891 // only search paths we're supposed to...
892 if ( (pathtype != CF_TYPE_ANY) && (pathtype != f->pathtype_index) ) {
897 // create localized filespec
898 char temp[MAX_PATH_LEN];
899 strcpy(temp, filespec);
900 lcl_add_dir_to_path_with_filename(filespec);
902 if ( !stricmp(filespec, f->name_ext) ) {
903 if ( size ) *size = f->size;
904 if ( offset ) *offset = f->pack_offset;
905 if ( pack_filename ) {
906 cf_root * r = cf_get_root(f->root_index);
908 strcpy( pack_filename, r->path );
909 if ( f->pack_offset < 1 ) {
910 strcat( pack_filename, Pathtypes[f->pathtype_index].path );
912 strcat( pack_filename, "/" );
914 strcat( pack_filename, "\\" );
916 strcat( pack_filename, f->name_ext );
921 // restore original filespec
922 strcpy(filespec, temp);
925 // file either not localized or localized version not found
926 if ( !stricmp(filespec, f->name_ext) ) {
927 if ( size ) *size = f->size;
928 if ( offset ) *offset = f->pack_offset;
929 if ( pack_filename ) {
930 cf_root * r = cf_get_root(f->root_index);
932 strcpy( pack_filename, r->path );
933 if ( f->pack_offset < 1 ) {
935 if(strlen(Pathtypes[f->pathtype_index].path)){
936 strcat( pack_filename, Pathtypes[f->pathtype_index].path );
938 strcat( pack_filename, "/" );
940 strcat( pack_filename, "\\" );
944 strcat( pack_filename, f->name_ext );
955 // Returns true if filename matches filespec, else zero if not
956 int cf_matches_spec(char *filespec, char *filename)
958 char *src_ext, *dst_ext;
960 src_ext = strchr(filespec, '.');
966 dst_ext = strchr(filename, '.');
970 return !stricmp(dst_ext, src_ext);
973 int (*Get_file_list_filter)(char *filename) = NULL;
974 int Skip_packfile_search = 0;
976 int cf_file_already_in_list( int num_files, char **list, char *filename )
980 char name_no_extension[MAX_PATH_LEN];
982 strcpy(name_no_extension, filename );
983 char *p = strchr( name_no_extension, '.' );
986 for (i=0; i<num_files; i++ ) {
987 if ( !stricmp(list[i], name_no_extension ) ) {
996 // An alternative cf_get_file_list(), dynamic list version.
997 // This one has a 'type', which is a CF_TYPE_* value. Because this specifies the directory
998 // location, 'filter' only needs to be the filter itself, with no path information.
999 // See above descriptions of cf_get_file_list() for more information about how it all works.
1000 int cf_get_file_list( int max, char **list, int pathtype, char *filter, int sort, file_list_info *info )
1003 int i, l, num_files = 0, own_flag = 0;
1010 Get_file_list_filter = NULL;
1016 if (!info && (sort == CF_SORT_TIME)) {
1017 info = (file_list_info *) malloc(sizeof(file_list_info) * max);
1021 char filespec[MAX_PATH_LEN];
1024 cf_create_default_path_string( filespec, pathtype, NULL );
1029 dirp = opendir (filespec);
1031 while ((dir = readdir (dirp)) != NULL)
1033 if (num_files >= max)
1036 if (fnmatch(filter, dir->d_name, 0) != 0)
1040 snprintf(fn, MAX_PATH-1, "%s/%s", filespec, dir->d_name);
1044 if (stat(fn, &buf) == -1) {
1048 if (!S_ISREG(buf.st_mode)) {
1052 if ( !Get_file_list_filter || (*Get_file_list_filter)(dir->d_name) ) {
1053 ptr = strrchr(dir->d_name, '.');
1055 l = ptr - dir->d_name;
1057 l = strlen(dir->d_name);
1059 list[num_files] = (char *)malloc(l + 1);
1060 strncpy(list[num_files], dir->d_name, l);
1061 list[num_files][l] = 0;
1063 info[num_files].write_time = buf.st_mtime;
1072 cf_create_default_path_string( filespec, pathtype, filter );
1074 find_handle = _findfirst( filespec, &find );
1075 if (find_handle != -1) {
1077 if (num_files >= max)
1080 if (!(find.attrib & _A_SUBDIR)) {
1081 if ( !Get_file_list_filter || (*Get_file_list_filter)(find.name) ) {
1082 ptr = strrchr(find.name, '.');
1084 l = ptr - find.name;
1086 l = strlen(find.name);
1088 list[num_files] = (char *)malloc(l + 1);
1089 strncpy(list[num_files], find.name, l);
1090 list[num_files][l] = 0;
1092 info[num_files].write_time = find.time_write;
1098 } while (!_findnext(find_handle, &find));
1100 _findclose( find_handle );
1105 // Search all the packfiles and CD.
1106 if ( !Skip_packfile_search ) {
1107 for (i=0; i<Num_files; i++ ) {
1108 cf_file * f = cf_get_file(i);
1110 // only search paths we're supposed to...
1111 if ( (pathtype != CF_TYPE_ANY) && (pathtype != f->pathtype_index) ) {
1115 if (num_files >= max)
1118 if ( !cf_matches_spec( filter,f->name_ext)) {
1122 if ( cf_file_already_in_list(num_files,list,f->name_ext)) {
1126 if ( !Get_file_list_filter || (*Get_file_list_filter)(f->name_ext) ) {
1128 //mprintf(( "Found '%s' in root %d path %d\n", f->name_ext, f->root_index, f->pathtype_index ));
1130 ptr = strrchr(f->name_ext, '.');
1132 l = ptr - f->name_ext;
1134 l = strlen(f->name_ext);
1136 list[num_files] = (char *)malloc(l + 1);
1137 strncpy(list[num_files], f->name_ext, l);
1138 list[num_files][l] = 0;
1141 info[num_files].write_time = f->write_time;
1151 if (sort != CF_SORT_NONE) {
1152 cf_sort_filenames( num_files, list, sort, info );
1159 Get_file_list_filter = NULL;
1163 int cf_file_already_in_list_preallocated( int num_files, char arr[][MAX_FILENAME_LEN], char *filename )
1167 char name_no_extension[MAX_PATH_LEN];
1169 strcpy(name_no_extension, filename );
1170 char *p = strchr( name_no_extension, '.' );
1173 for (i=0; i<num_files; i++ ) {
1174 if ( !stricmp(arr[i], name_no_extension ) ) {
1183 // An alternative cf_get_file_list(), fixed array version.
1184 // This one has a 'type', which is a CF_TYPE_* value. Because this specifies the directory
1185 // location, 'filter' only needs to be the filter itself, with no path information.
1186 // See above descriptions of cf_get_file_list() for more information about how it all works.
1187 int cf_get_file_list_preallocated( int max, char arr[][MAX_FILENAME_LEN], char **list, int pathtype, char *filter, int sort, file_list_info *info )
1189 int i, num_files = 0, own_flag = 0;
1192 Get_file_list_filter = NULL;
1197 for (i=0; i<max; i++) {
1201 sort = CF_SORT_NONE; // sorting of array directly not supported. Sorting done on list only
1204 if (!info && (sort == CF_SORT_TIME)) {
1205 info = (file_list_info *) malloc(sizeof(file_list_info) * max);
1209 char filespec[MAX_PATH_LEN];
1211 // Search the default directories
1213 cf_create_default_path_string( filespec, pathtype, NULL );
1218 dirp = opendir (filespec);
1220 while ((dir = readdir (dirp)) != NULL)
1222 if (num_files >= max)
1225 if (fnmatch(filter, dir->d_name, 0) != 0)
1229 snprintf(fn, MAX_PATH-1, "%s/%s", filespec, dir->d_name);
1233 if (stat(fn, &buf) == -1) {
1237 if (!S_ISREG(buf.st_mode)) {
1241 if ( !Get_file_list_filter || (*Get_file_list_filter)(dir->d_name) ) {
1243 strncpy(arr[num_files], dir->d_name, MAX_FILENAME_LEN - 1 );
1244 char *ptr = strrchr(arr[num_files], '.');
1250 info[num_files].write_time = buf.st_mtime;
1259 cf_create_default_path_string( filespec, pathtype, filter );
1264 find_handle = _findfirst( filespec, &find );
1265 if (find_handle != -1) {
1267 if (num_files >= max)
1270 if (!(find.attrib & _A_SUBDIR)) {
1272 if ( !Get_file_list_filter || (*Get_file_list_filter)(find.name) ) {
1274 strncpy(arr[num_files], find.name, MAX_FILENAME_LEN - 1 );
1275 char *ptr = strrchr(arr[num_files], '.');
1281 info[num_files].write_time = find.time_write;
1288 } while (!_findnext(find_handle, &find));
1290 _findclose( find_handle );
1295 // Search all the packfiles and CD.
1296 if ( !Skip_packfile_search ) {
1297 for (i=0; i<Num_files; i++ ) {
1298 cf_file * f = cf_get_file(i);
1300 // only search paths we're supposed to...
1301 if ( (pathtype != CF_TYPE_ANY) && (pathtype != f->pathtype_index) ) {
1305 if (num_files >= max)
1308 if ( !cf_matches_spec( filter,f->name_ext)) {
1312 if ( cf_file_already_in_list_preallocated( num_files, arr, f->name_ext )) {
1316 if ( !Get_file_list_filter || (*Get_file_list_filter)(f->name_ext) ) {
1318 //mprintf(( "Found '%s' in root %d path %d\n", f->name_ext, f->root_index, f->pathtype_index ));
1320 strncpy(arr[num_files], f->name_ext, MAX_FILENAME_LEN - 1 );
1321 char *ptr = strrchr(arr[num_files], '.');
1327 info[num_files].write_time = f->write_time;
1336 if (sort != CF_SORT_NONE) {
1338 cf_sort_filenames( num_files, list, sort, info );
1345 Get_file_list_filter = NULL;
1349 // Returns the default storage path for files given a
1350 // particular pathtype. In other words, the path to
1351 // the unpacked, non-cd'd, stored on hard drive path.
1352 // If filename isn't null it will also tack the filename
1353 // on the end, creating a completely valid filename.
1354 // Input: pathtype - CF_TYPE_??
1355 // filename - optional, if set, tacks the filename onto end of path.
1356 // Output: path - Fully qualified pathname.
1357 void cf_create_default_path_string( char *path, int pathtype, char *filename, bool localize )
1360 if ( filename && strpbrk(filename,"/") ) {
1362 if ( filename && strpbrk(filename,"/\\:") ) {
1364 // Already has full path
1365 strcpy( path, filename );
1368 cf_root *root = cf_get_root(0);
1371 strcpy(path, filename);
1375 Assert(CF_TYPE_SPECIFIED(pathtype));
1377 strcpy(path, root->path);
1378 strcat(path, Pathtypes[pathtype].path);
1380 // Don't add slash for root directory
1381 if (Pathtypes[pathtype].path[0] != '\0') {
1391 strcat(path, filename);
1393 // localize filename
1395 // create copy of path
1396 char temp_path[MAX_PATH_LEN];
1397 strcpy(temp_path, path);
1399 // localize the path
1400 lcl_add_dir_to_path_with_filename(path);
1402 // verify localized path
1403 FILE *fp = fopen(path, "rb");
1407 strcpy(path, temp_path);