2 * $Logfile: /Freespace2/code/CFile/CfileSystem.cpp $
7 * Functions to keep track of and find files that can exist
8 * on the harddrive, cd-rom, or in a pack file on either of those.
9 * This keeps a list of all the files in packfiles or on CD-rom
10 * and when you need a file you call one function which then searches
11 * all those locations, inherently enforcing precedence orders.
14 * Revision 1.3 2002/05/28 06:45:38 theoddone33
17 * Revision 1.2 2002/05/28 06:28:20 theoddone33
18 * Filesystem mods, actually reads some data files now
20 * Revision 1.1.1.1 2002/05/03 03:28:08 root
24 * 6 9/08/99 10:01p Dave
25 * Make sure game won't run in a drive's root directory. Make sure
26 * standalone routes suqad war messages properly to the host.
28 * 5 9/03/99 1:31a Dave
29 * CD checking by act. Added support to play 2 cutscenes in a row
30 * seamlessly. Fixed super low level cfile bug related to files in the
31 * root directory of a CD. Added cheat code to set campaign mission # in
34 * 4 2/22/99 10:31p Andsager
35 * Get rid of unneeded includes.
37 * 3 10/13/98 9:19a Andsager
38 * Add localization support to cfile. Optional parameter with cfopen that
39 * looks for localized files.
41 * 2 10/07/98 10:52a Dave
44 * 1 10/07/98 10:48a Dave
46 * 14 8/31/98 2:06p Dave
47 * Make cfile sort the ordering or vp files. Added support/checks for
48 * recognizing "mission disk" players.
50 * 13 6/23/98 4:18p Hoffoss
51 * Fixed some bugs with AC release build.
53 * 12 5/20/98 10:46p John
54 * Added code that doesn't include duplicate filenames in any file list
57 * 11 5/14/98 2:14p Lawrance2
58 * Use filespec filtering for packfiles
60 * 10 5/03/98 11:53a John
61 * Fixed filename case mangling.
63 * 9 5/02/98 11:06p Allender
64 * correctly deal with pack pathnames
66 * 8 5/01/98 11:41a Allender
67 * Fixed bug with mission saving in Fred.
69 * 7 5/01/98 10:21a John
70 * Added code to find all pack files in all trees. Added code to create
71 * any directories that we write to.
73 * 6 4/30/98 10:21p John
74 * Added code to cleanup cfilesystem
76 * 5 4/30/98 10:18p John
77 * added source safe header
90 #include <winbase.h> /* needed for memory mapping of file functions */
92 #include <sys/types.h>
102 #include "cfilesystem.h"
103 #include "localize.h"
106 #define CF_ROOTTYPE_PATH 0
107 #define CF_ROOTTYPE_PACK 1
110 // specifying hard drive tree
111 // searching for pack files on hard drive // Found by searching all known paths
112 // specifying cd-rom tree
113 // searching for pack files on CD-rom tree
114 typedef struct cf_root {
115 char path[CF_MAX_PATHNAME_LENGTH]; // Contains something like c:\projects\freespace or c:\projects\freespace\freespace.vp
116 int roottype; // CF_ROOTTYPE_PATH = Path, CF_ROOTTYPE_PACK =Pack file
119 // convenient type for sorting (see cf_build_pack_list())
120 typedef struct cf_root_sort {
121 char path[CF_MAX_PATHNAME_LENGTH];
126 #define CF_NUM_ROOTS_PER_BLOCK 32
127 #define CF_MAX_ROOT_BLOCKS 256 // Can store 32*256 = 8192 Roots
128 #define CF_MAX_ROOTS (CF_NUM_ROOTS_PER_BLOCK * CF_MAX_ROOT_BLOCKS)
130 typedef struct cf_root_block {
131 cf_root roots[CF_NUM_ROOTS_PER_BLOCK];
134 static int Num_roots = 0;
135 static cf_root_block *Root_blocks[CF_MAX_ROOT_BLOCKS];
138 // Created by searching all roots in order. This means Files is then sorted by precedence.
139 typedef struct cf_file {
140 char name_ext[CF_MAX_FILENAME_LENGTH]; // Filename and extension
141 int root_index; // Where in Roots this is located
142 int pathtype_index; // Where in Paths this is located
143 time_t write_time; // When it was last written
144 int size; // How big it is in bytes
145 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.
148 #define CF_NUM_FILES_PER_BLOCK 256
149 #define CF_MAX_FILE_BLOCKS 128 // Can store 256*128 = 32768 files
151 typedef struct cf_file_block {
152 cf_file files[CF_NUM_FILES_PER_BLOCK];
155 static int Num_files = 0;
156 static cf_file_block *File_blocks[CF_MAX_FILE_BLOCKS];
159 // Return a pointer to to file 'index'.
160 cf_file *cf_get_file(int index)
162 int block = index / CF_NUM_FILES_PER_BLOCK;
163 int offset = index % CF_NUM_FILES_PER_BLOCK;
165 return &File_blocks[block]->files[offset];
168 // Create a new file and return a pointer to it.
169 cf_file *cf_create_file()
171 int block = Num_files / CF_NUM_FILES_PER_BLOCK;
172 int offset = Num_files % CF_NUM_FILES_PER_BLOCK;
174 if ( File_blocks[block] == NULL ) {
175 File_blocks[block] = (cf_file_block *)malloc( sizeof(cf_file_block) );
176 Assert( File_blocks[block] != NULL);
181 return &File_blocks[block]->files[offset];
184 extern int cfile_inited;
186 // Create a new root and return a pointer to it. The structure is assumed unitialized.
187 cf_root *cf_get_root(int n)
189 int block = n / CF_NUM_ROOTS_PER_BLOCK;
190 int offset = n % CF_NUM_ROOTS_PER_BLOCK;
195 return &Root_blocks[block]->roots[offset];
199 // Create a new root and return a pointer to it. The structure is assumed unitialized.
200 cf_root *cf_create_root()
202 int block = Num_roots / CF_NUM_ROOTS_PER_BLOCK;
203 int offset = Num_roots % CF_NUM_ROOTS_PER_BLOCK;
205 if ( Root_blocks[block] == NULL ) {
206 Root_blocks[block] = (cf_root_block *)malloc( sizeof(cf_root_block) );
207 Assert(Root_blocks[block] != NULL);
212 return &Root_blocks[block]->roots[offset];
215 // return the # of packfiles which exist
216 int cf_get_packfile_count(cf_root *root)
218 char filespec[MAX_PATH_LEN];
222 // count up how many packfiles we're gonna have
224 for (i=CF_TYPE_ROOT; i<CF_MAX_PATH_TYPES; i++ ) {
226 strcpy( filespec, root->path );
228 if(strlen(Pathtypes[i].path)){
229 strcat( filespec, Pathtypes[i].path );
230 strcat( filespec, "/" );
236 dirp = opendir (filespec);
238 while ((dir = readdir (dirp)) != NULL)
240 if (!fnmatch ("*.vp", dir->d_name, 0))
245 strcpy( filespec, root->path );
247 if(strlen(Pathtypes[i].path)){
248 strcat( filespec, Pathtypes[i].path );
249 strcat( filespec, "\\" );
252 strcat( filespec, "*.vp" );
257 find_handle = _findfirst( filespec, &find );
259 if (find_handle != -1) {
261 if (!(find.attrib & _A_SUBDIR)) {
265 } while (!_findnext(find_handle, &find));
267 _findclose( find_handle );
272 return packfile_count;
275 // packfile sort function
276 int cf_packfile_sort_func(const void *elem1, const void *elem2)
278 cf_root_sort *r1, *r2;
279 r1 = (cf_root_sort*)elem1;
280 r2 = (cf_root_sort*)elem2;
282 // if the 2 directory types are the same, do a string compare
283 if(r1->cf_type == r2->cf_type){
284 return stricmp(r1->path, r2->path);
287 // otherwise return them in order of CF_TYPE_* precedence
288 return (r1->cf_type < r2->cf_type) ? -1 : 1;
291 // Go through a root and look for pack files
292 void cf_build_pack_list( cf_root *root )
294 char filespec[MAX_PATH_LEN];
296 cf_root_sort *temp_roots_sort, *rptr_sort;
297 int temp_root_count, root_index;
299 // determine how many packfiles there are
300 temp_root_count = cf_get_packfile_count(root);
301 if(temp_root_count <= 0){
305 // allocate a temporary array of temporary roots so we can easily sort them
306 temp_roots_sort = (cf_root_sort*)malloc(sizeof(cf_root_sort) * temp_root_count);
307 if(temp_roots_sort == NULL){
312 // now just setup all the root info
314 for (i=CF_TYPE_ROOT; i<CF_MAX_PATH_TYPES; i++ ) {
317 strcpy( filespec, root->path );
319 if(strlen(Pathtypes[i].path)){
320 strcat( filespec, Pathtypes[i].path );
321 strcat( filespec, "/" );
327 dirp = opendir (filespec);
329 while ((dir = readdir (dirp)) != NULL)
331 if (!fnmatch ("*.vp", dir->d_name, 0))
333 Assert(root_index < temp_root_count);
335 // get a temp pointer
336 rptr_sort = &temp_roots_sort[root_index++];
338 // fill in all the proper info
339 strcpy(rptr_sort->path, root->path);
341 if(strlen(Pathtypes[i].path)){
342 strcat(rptr_sort->path, Pathtypes[i].path );
343 strcat(rptr_sort->path, "/");
346 strcat(rptr_sort->path, dir->d_name );
347 rptr_sort->roottype = CF_ROOTTYPE_PACK;
348 rptr_sort->cf_type = i;
353 strcpy( filespec, root->path );
355 if(strlen(Pathtypes[i].path)){
356 strcat( filespec, Pathtypes[i].path );
357 strcat( filespec, "\\" );
359 strcat( filespec, "*.vp" );
363 find_handle = _findfirst( filespec, &find );
365 if (find_handle != -1) {
368 if (!(find.attrib & _A_SUBDIR)) {
369 Assert(root_index < temp_root_count);
371 // get a temp pointer
372 rptr_sort = &temp_roots_sort[root_index++];
374 // fill in all the proper info
375 strcpy(rptr_sort->path, root->path);
377 if(strlen(Pathtypes[i].path)){
378 strcat(rptr_sort->path, Pathtypes[i].path );
379 strcat(rptr_sort->path, "\\");
382 strcat(rptr_sort->path, find.name );
383 rptr_sort->roottype = CF_ROOTTYPE_PACK;
384 rptr_sort->cf_type = i;
387 } while (!_findnext(find_handle, &find));
389 _findclose( find_handle );
394 // these should always be the same
395 Assert(root_index == temp_root_count);
398 qsort(temp_roots_sort, temp_root_count, sizeof(cf_root_sort), cf_packfile_sort_func);
400 // now insert them all into the real root list properly
402 for(i=0; i<temp_root_count; i++){
403 new_root = cf_create_root();
404 strcpy( new_root->path, root->path );
406 // mwa -- 4/2/98 put in the next 2 lines because the path name needs to be there
407 // to find the files.
408 strcpy(new_root->path, temp_roots_sort[i].path);
409 new_root->roottype = CF_ROOTTYPE_PACK;
412 // free up the temp list
413 free(temp_roots_sort);
417 void cf_build_root_list(char *cdrom_dir)
423 //======================================================
424 // First, check the current directory.
425 // strcpy( root->path, "d:\\projects\\freespace\\" );
427 root = cf_create_root();
429 if ( !_getcwd(root->path, CF_MAX_PATHNAME_LENGTH ) ) {
430 Error(LOCATION, "Can't get current working directory -- %d", errno );
433 // do we already have a slash? as in the case of a root directory install
435 if(strlen(root->path) && (root->path[strlen(root->path)-1] != '/')){
436 strcat(root->path, "/"); // put trailing backslash on for easier path construction
438 if(strlen(root->path) && (root->path[strlen(root->path)-1] != '\\')){
439 strcat(root->path, "\\"); // put trailing backslash on for easier path construction
442 root->roottype = CF_ROOTTYPE_PATH;
444 //======================================================
445 // Next, check any VP files under the current directory.
446 cf_build_pack_list(root);
449 //======================================================
450 // Check the real CD if one...
451 if ( cdrom_dir && strlen(cdrom_dir) ) {
452 root = cf_create_root();
453 strcpy( root->path, cdrom_dir );
454 root->roottype = CF_ROOTTYPE_PATH;
456 //======================================================
457 // Next, check any VP files in the CD-ROM directory.
458 cf_build_pack_list(root);
464 // Given a lower case list of file extensions
465 // separated by spaces, return zero if ext is
467 int is_ext_in_list( char *ext_list, char *ext )
471 strncpy( tmp_ext, ext, 127 );
473 if ( strstr(ext_list, tmp_ext )) {
480 void cf_search_root_path(int root_index)
484 cf_root *root = cf_get_root(root_index);
486 mprintf(( "Searching root '%s'\n", root->path ));
488 char search_path[CF_MAX_PATHNAME_LENGTH];
490 for (i=CF_TYPE_ROOT; i<CF_MAX_PATH_TYPES; i++ ) {
496 strcpy( search_path, root->path );
498 if(strlen(Pathtypes[i].path)){
499 strcat( search_path, Pathtypes[i].path );
500 strcat( search_path, "/" );
503 dirp = opendir (search_path);
505 while ((dir = readdir (dirp)) != NULL)
507 if (!fnmatch ("*.*", dir->d_name, 0))
509 char *ext = strchr( dir->d_name, '.' );
511 if ( is_ext_in_list( Pathtypes[i].extensions, ext ) ) {
513 cf_file *file = cf_create_file();
515 strcpy( file->name_ext, dir->d_name );
516 file->root_index = root_index;
517 file->pathtype_index = i;
519 file->write_time = find.time_write;
520 file->size = find.size;
524 file->pack_offset = 0; // Mark as a non-packed file
526 //mprintf(( "Found file '%s'\n", file->name_ext ));
533 strcpy( search_path, root->path );
535 if(strlen(Pathtypes[i].path)){
536 strcat( search_path, Pathtypes[i].path );
537 strcat( search_path, "\\" );
540 strcat( search_path, "*.*" );
545 find_handle = _findfirst( search_path, &find );
547 if (find_handle != -1) {
549 if (!(find.attrib & _A_SUBDIR)) {
551 char *ext = strchr( find.name, '.' );
553 if ( is_ext_in_list( Pathtypes[i].extensions, ext ) ) {
555 cf_file *file = cf_create_file();
557 strcpy( file->name_ext, find.name );
558 file->root_index = root_index;
559 file->pathtype_index = i;
560 file->write_time = find.time_write;
561 file->size = find.size;
562 file->pack_offset = 0; // Mark as a non-packed file
564 //mprintf(( "Found file '%s'\n", file->name_ext ));
571 } while (!_findnext(find_handle, &find));
573 _findclose( find_handle );
581 typedef struct VP_FILE_HEADER {
588 typedef struct VP_FILE {
595 void cf_search_root_pack(int root_index)
599 cf_root *root = cf_get_root(root_index);
601 //mprintf(( "Searching root pack '%s'\n", root->path ));
605 FILE *fp = fopen( root->path, "rb" );
606 // Read the file header
611 VP_FILE_HEADER VP_header;
613 Assert( sizeof(VP_header) == 16 );
614 fread(&VP_header, 1, sizeof(VP_header), fp);
617 fseek(fp, VP_header.index_offset, SEEK_SET);
619 char search_path[CF_MAX_PATHNAME_LENGTH];
621 strcpy( search_path, "" );
623 // Go through all the files
624 for (i=0; i<VP_header.num_files; i++ ) {
627 fread( &find, sizeof(VP_FILE), 1, fp );
629 if ( find.size == 0 ) {
630 if ( !stricmp( find.filename, ".." )) {
631 int l = strlen(search_path);
632 char *p = &search_path[l-1];
634 while( (p > search_path) && (*p != '/') ) {
636 while( (p > search_path) && (*p != '\\') ) {
642 if ( strlen(search_path) ) {
644 strcat( search_path, "/" );
646 strcat( search_path, "\\" );
649 strcat( search_path, find.filename );
652 //mprintf(( "Current dir = '%s'\n", search_path ));
656 for (j=CF_TYPE_ROOT; j<CF_MAX_PATH_TYPES; j++ ) {
658 if ( !stricmp( search_path, Pathtypes[j].path )) {
660 char *ext = strchr( find.filename, '.' );
662 if ( is_ext_in_list( Pathtypes[j].extensions, ext ) ) {
664 cf_file *file = cf_create_file();
666 strcpy( file->name_ext, find.filename );
667 file->root_index = root_index;
668 file->pathtype_index = j;
669 file->write_time = find.write_time;
670 file->size = find.size;
671 file->pack_offset = find.offset; // Mark as a non-packed file
673 //mprintf(( "Found pack file '%s'\n", file->name_ext ));
687 void cf_build_file_list()
693 // For each root, find all files...
694 for (i=1; i<Num_roots; i++ ) {
695 cf_root *root = cf_get_root(i);
696 if ( root->roottype == CF_ROOTTYPE_PATH ) {
697 cf_search_root_path(i);
698 } else if ( root->roottype == CF_ROOTTYPE_PACK ) {
699 cf_search_root_pack(i);
706 void cf_build_secondary_filelist(char *cdrom_dir)
714 // Init the path types
715 for (i=0; i<CF_MAX_PATH_TYPES; i++ ) {
716 Assert( Pathtypes[i].index == i );
717 if ( Pathtypes[i].extensions ) {
718 strlwr(Pathtypes[i].extensions);
722 // Init the root blocks
723 for (i=0; i<CF_MAX_ROOT_BLOCKS; i++ ) {
724 Root_blocks[i] = NULL;
727 // Init the file blocks
728 for (i=0; i<CF_MAX_FILE_BLOCKS; i++ ) {
729 File_blocks[i] = NULL;
732 mprintf(( "Building file index...\n" ));
734 // build the list of searchable roots
735 cf_build_root_list(cdrom_dir);
737 // build the list of files themselves
738 cf_build_file_list();
740 mprintf(( "Found %d roots and %d files.\n", Num_roots, Num_files ));
743 void cf_free_secondary_filelist()
747 // Free the root blocks
748 for (i=0; i<CF_MAX_ROOT_BLOCKS; i++ ) {
749 if ( Root_blocks[i] ) {
750 free( Root_blocks[i] );
751 Root_blocks[i] = NULL;
756 // Init the file blocks
757 for (i=0; i<CF_MAX_FILE_BLOCKS; i++ ) {
758 if ( File_blocks[i] ) {
759 free( File_blocks[i] );
760 File_blocks[i] = NULL;
766 // Searches for a file. Follows all rules and precedence and searches
767 // CD's and pack files.
768 // Input: filespace - Filename & extension
769 // pathtype - See CF_TYPE_ defines in CFILE.H
770 // Output: pack_filename - Absolute path and filename of this file. Could be a packfile or the actual file.
772 // offset - Offset into pack file. 0 if not a packfile.
773 // Returns: If not found returns 0.
774 int cf_find_file_location( char *filespec, int pathtype, char *pack_filename, int *size, int *offset, bool localize )
778 Assert(filespec && strlen(filespec));
780 // see if we have something other than just a filename
781 // our current rules say that any file that specifies a direct
782 // path will try to be opened on that path. If that open
783 // fails, then we will open the file based on the extension
786 // NOTE: full path should also include localization, if so desired
787 if ( strpbrk(filespec,"/\\:") ) { // do we have a full path already?
788 FILE *fp = fopen(filespec, "rb" );
790 if ( size ) *size = filelength(fileno(fp));
791 if ( offset ) *offset = 0;
792 if ( pack_filename ) {
793 strcpy( pack_filename, filespec );
799 return 0; // If they give a full path, fail if not found.
802 // Search the hard drive for files first.
803 int num_search_dirs = 0;
804 int search_order[CF_MAX_PATH_TYPES];
806 if ( CF_TYPE_SPECIFIED(pathtype) ) {
807 search_order[num_search_dirs++] = pathtype;
810 for (i=CF_TYPE_ROOT; i<CF_MAX_PATH_TYPES; i++) {
811 if ( i != pathtype ) {
812 search_order[num_search_dirs++] = i;
816 for (i=0; i<num_search_dirs; i++ ) {
817 char longname[MAX_PATH_LEN];
819 cf_create_default_path_string( longname, search_order[i], filespec, localize );
821 FILE *fp = fopen(longname, "rb" );
823 if ( size ) *size = filelength(fileno(fp));
824 if ( offset ) *offset = 0;
825 if ( pack_filename ) {
826 strcpy( pack_filename, longname );
833 // Search the pak files and CD-ROM.
835 for (i=0; i<Num_files; i++ ) {
836 cf_file * f = cf_get_file(i);
838 // only search paths we're supposed to...
839 if ( (pathtype != CF_TYPE_ANY) && (pathtype != f->pathtype_index) ) {
844 // create localized filespec
845 char temp[MAX_PATH_LEN];
846 strcpy(temp, filespec);
847 lcl_add_dir_to_path_with_filename(filespec);
849 if ( !stricmp(filespec, f->name_ext) ) {
850 if ( size ) *size = f->size;
851 if ( offset ) *offset = f->pack_offset;
852 if ( pack_filename ) {
853 cf_root * r = cf_get_root(f->root_index);
855 strcpy( pack_filename, r->path );
856 if ( f->pack_offset < 1 ) {
857 strcat( pack_filename, Pathtypes[f->pathtype_index].path );
859 strcat( pack_filename, "/" );
861 strcat( pack_filename, "\\" );
863 strcat( pack_filename, f->name_ext );
868 // restore original filespec
869 strcpy(filespec, temp);
872 // file either not localized or localized version not found
873 if ( !stricmp(filespec, f->name_ext) ) {
874 if ( size ) *size = f->size;
875 if ( offset ) *offset = f->pack_offset;
876 if ( pack_filename ) {
877 cf_root * r = cf_get_root(f->root_index);
879 strcpy( pack_filename, r->path );
880 if ( f->pack_offset < 1 ) {
882 if(strlen(Pathtypes[f->pathtype_index].path)){
883 strcat( pack_filename, Pathtypes[f->pathtype_index].path );
885 strcat( pack_filename, "/" );
887 strcat( pack_filename, "\\" );
891 strcat( pack_filename, f->name_ext );
902 // Returns true if filename matches filespec, else zero if not
903 int cf_matches_spec(char *filespec, char *filename)
905 char *src_ext, *dst_ext;
907 src_ext = strchr(filespec, '.');
913 dst_ext = strchr(filename, '.');
917 return !stricmp(dst_ext, src_ext);
920 int (*Get_file_list_filter)(char *filename) = NULL;
921 int Skip_packfile_search = 0;
923 int cf_file_already_in_list( int num_files, char **list, char *filename )
927 char name_no_extension[MAX_PATH_LEN];
929 strcpy(name_no_extension, filename );
930 char *p = strchr( name_no_extension, '.' );
933 for (i=0; i<num_files; i++ ) {
934 if ( !stricmp(list[i], name_no_extension ) ) {
943 // An alternative cf_get_file_list(), dynamic list version.
944 // This one has a 'type', which is a CF_TYPE_* value. Because this specifies the directory
945 // location, 'filter' only needs to be the filter itself, with no path information.
946 // See above descriptions of cf_get_file_list() for more information about how it all works.
947 int cf_get_file_list( int max, char **list, int pathtype, char *filter, int sort, file_list_info *info )
950 int i, l, find_handle, num_files = 0, own_flag = 0;
956 Get_file_list_filter = NULL;
962 if (!info && (sort == CF_SORT_TIME)) {
963 info = (file_list_info *) malloc(sizeof(file_list_info) * max);
967 char filespec[MAX_PATH_LEN];
969 cf_create_default_path_string( filespec, pathtype, filter );
974 find_handle = _findfirst( filespec, &find );
975 if (find_handle != -1) {
977 if (num_files >= max)
980 if (!(find.attrib & _A_SUBDIR)) {
981 if ( !Get_file_list_filter || (*Get_file_list_filter)(find.name) ) {
982 ptr = strrchr(find.name, '.');
986 l = strlen(find.name);
988 list[num_files] = (char *)malloc(l + 1);
989 strncpy(list[num_files], find.name, l);
990 list[num_files][l] = 0;
992 info[num_files].write_time = find.time_write;
998 } while (!_findnext(find_handle, &find));
1000 _findclose( find_handle );
1005 // Search all the packfiles and CD.
1006 if ( !Skip_packfile_search ) {
1007 for (i=0; i<Num_files; i++ ) {
1008 cf_file * f = cf_get_file(i);
1010 // only search paths we're supposed to...
1011 if ( (pathtype != CF_TYPE_ANY) && (pathtype != f->pathtype_index) ) {
1015 if (num_files >= max)
1018 if ( !cf_matches_spec( filter,f->name_ext)) {
1022 if ( cf_file_already_in_list(num_files,list,f->name_ext)) {
1026 if ( !Get_file_list_filter || (*Get_file_list_filter)(f->name_ext) ) {
1028 //mprintf(( "Found '%s' in root %d path %d\n", f->name_ext, f->root_index, f->pathtype_index ));
1030 ptr = strrchr(f->name_ext, '.');
1032 l = ptr - f->name_ext;
1034 l = strlen(f->name_ext);
1036 list[num_files] = (char *)malloc(l + 1);
1037 strncpy(list[num_files], f->name_ext, l);
1038 list[num_files][l] = 0;
1041 info[num_files].write_time = f->write_time;
1051 if (sort != CF_SORT_NONE) {
1052 cf_sort_filenames( num_files, list, sort, info );
1059 Get_file_list_filter = NULL;
1063 int cf_file_already_in_list_preallocated( int num_files, char arr[][MAX_FILENAME_LEN], char *filename )
1067 char name_no_extension[MAX_PATH_LEN];
1069 strcpy(name_no_extension, filename );
1070 char *p = strchr( name_no_extension, '.' );
1073 for (i=0; i<num_files; i++ ) {
1074 if ( !stricmp(arr[i], name_no_extension ) ) {
1083 // An alternative cf_get_file_list(), fixed array version.
1084 // This one has a 'type', which is a CF_TYPE_* value. Because this specifies the directory
1085 // location, 'filter' only needs to be the filter itself, with no path information.
1086 // See above descriptions of cf_get_file_list() for more information about how it all works.
1087 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 )
1089 int i, num_files = 0, own_flag = 0;
1092 Get_file_list_filter = NULL;
1097 for (i=0; i<max; i++) {
1101 sort = CF_SORT_NONE; // sorting of array directly not supported. Sorting done on list only
1104 if (!info && (sort == CF_SORT_TIME)) {
1105 info = (file_list_info *) malloc(sizeof(file_list_info) * max);
1109 char filespec[MAX_PATH_LEN];
1111 cf_create_default_path_string( filespec, pathtype, filter );
1113 // Search the default directories
1120 find_handle = _findfirst( filespec, &find );
1121 if (find_handle != -1) {
1123 if (num_files >= max)
1126 if (!(find.attrib & _A_SUBDIR)) {
1128 if ( !Get_file_list_filter || (*Get_file_list_filter)(find.name) ) {
1130 strncpy(arr[num_files], find.name, MAX_FILENAME_LEN - 1 );
1131 char *ptr = strrchr(arr[num_files], '.');
1137 info[num_files].write_time = find.time_write;
1144 } while (!_findnext(find_handle, &find));
1146 _findclose( find_handle );
1151 // Search all the packfiles and CD.
1152 if ( !Skip_packfile_search ) {
1153 for (i=0; i<Num_files; i++ ) {
1154 cf_file * f = cf_get_file(i);
1156 // only search paths we're supposed to...
1157 if ( (pathtype != CF_TYPE_ANY) && (pathtype != f->pathtype_index) ) {
1161 if (num_files >= max)
1164 if ( !cf_matches_spec( filter,f->name_ext)) {
1168 if ( cf_file_already_in_list_preallocated( num_files, arr, f->name_ext )) {
1172 if ( !Get_file_list_filter || (*Get_file_list_filter)(f->name_ext) ) {
1174 //mprintf(( "Found '%s' in root %d path %d\n", f->name_ext, f->root_index, f->pathtype_index ));
1176 strncpy(arr[num_files], f->name_ext, MAX_FILENAME_LEN - 1 );
1177 char *ptr = strrchr(arr[num_files], '.');
1183 info[num_files].write_time = f->write_time;
1192 if (sort != CF_SORT_NONE) {
1194 cf_sort_filenames( num_files, list, sort, info );
1201 Get_file_list_filter = NULL;
1205 // Returns the default storage path for files given a
1206 // particular pathtype. In other words, the path to
1207 // the unpacked, non-cd'd, stored on hard drive path.
1208 // If filename isn't null it will also tack the filename
1209 // on the end, creating a completely valid filename.
1210 // Input: pathtype - CF_TYPE_??
1211 // filename - optional, if set, tacks the filename onto end of path.
1212 // Output: path - Fully qualified pathname.
1213 void cf_create_default_path_string( char *path, int pathtype, char *filename, bool localize )
1216 if ( filename && strpbrk(filename,"/") ) {
1218 if ( filename && strpbrk(filename,"/\\:") ) {
1220 // Already has full path
1221 strcpy( path, filename );
1224 cf_root *root = cf_get_root(0);
1227 strcpy(path, filename);
1231 Assert(CF_TYPE_SPECIFIED(pathtype));
1233 strcpy(path, root->path);
1234 strcat(path, Pathtypes[pathtype].path);
1236 // Don't add slash for root directory
1237 if (Pathtypes[pathtype].path[0] != '\0') {
1247 strcat(path, filename);
1249 // localize filename
1251 // create copy of path
1252 char temp_path[MAX_PATH_LEN];
1253 strcpy(temp_path, path);
1255 // localize the path
1256 lcl_add_dir_to_path_with_filename(path);
1258 // verify localized path
1259 FILE *fp = fopen(path, "rb");
1263 strcpy(path, temp_path);