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.2 2002/05/28 06:28:20 theoddone33
15 * Filesystem mods, actually reads some data files now
17 * Revision 1.1.1.1 2002/05/03 03:28:08 root
21 * 6 9/08/99 10:01p Dave
22 * Make sure game won't run in a drive's root directory. Make sure
23 * standalone routes suqad war messages properly to the host.
25 * 5 9/03/99 1:31a Dave
26 * CD checking by act. Added support to play 2 cutscenes in a row
27 * seamlessly. Fixed super low level cfile bug related to files in the
28 * root directory of a CD. Added cheat code to set campaign mission # in
31 * 4 2/22/99 10:31p Andsager
32 * Get rid of unneeded includes.
34 * 3 10/13/98 9:19a Andsager
35 * Add localization support to cfile. Optional parameter with cfopen that
36 * looks for localized files.
38 * 2 10/07/98 10:52a Dave
41 * 1 10/07/98 10:48a Dave
43 * 14 8/31/98 2:06p Dave
44 * Make cfile sort the ordering or vp files. Added support/checks for
45 * recognizing "mission disk" players.
47 * 13 6/23/98 4:18p Hoffoss
48 * Fixed some bugs with AC release build.
50 * 12 5/20/98 10:46p John
51 * Added code that doesn't include duplicate filenames in any file list
54 * 11 5/14/98 2:14p Lawrance2
55 * Use filespec filtering for packfiles
57 * 10 5/03/98 11:53a John
58 * Fixed filename case mangling.
60 * 9 5/02/98 11:06p Allender
61 * correctly deal with pack pathnames
63 * 8 5/01/98 11:41a Allender
64 * Fixed bug with mission saving in Fred.
66 * 7 5/01/98 10:21a John
67 * Added code to find all pack files in all trees. Added code to create
68 * any directories that we write to.
70 * 6 4/30/98 10:21p John
71 * Added code to cleanup cfilesystem
73 * 5 4/30/98 10:18p John
74 * added source safe header
87 #include <winbase.h> /* needed for memory mapping of file functions */
89 #include <sys/types.h>
99 #include "cfilesystem.h"
100 #include "localize.h"
103 #define CF_ROOTTYPE_PATH 0
104 #define CF_ROOTTYPE_PACK 1
107 // specifying hard drive tree
108 // searching for pack files on hard drive // Found by searching all known paths
109 // specifying cd-rom tree
110 // searching for pack files on CD-rom tree
111 typedef struct cf_root {
112 char path[CF_MAX_PATHNAME_LENGTH]; // Contains something like c:\projects\freespace or c:\projects\freespace\freespace.vp
113 int roottype; // CF_ROOTTYPE_PATH = Path, CF_ROOTTYPE_PACK =Pack file
116 // convenient type for sorting (see cf_build_pack_list())
117 typedef struct cf_root_sort {
118 char path[CF_MAX_PATHNAME_LENGTH];
123 #define CF_NUM_ROOTS_PER_BLOCK 32
124 #define CF_MAX_ROOT_BLOCKS 256 // Can store 32*256 = 8192 Roots
125 #define CF_MAX_ROOTS (CF_NUM_ROOTS_PER_BLOCK * CF_MAX_ROOT_BLOCKS)
127 typedef struct cf_root_block {
128 cf_root roots[CF_NUM_ROOTS_PER_BLOCK];
131 static int Num_roots = 0;
132 static cf_root_block *Root_blocks[CF_MAX_ROOT_BLOCKS];
135 // Created by searching all roots in order. This means Files is then sorted by precedence.
136 typedef struct cf_file {
137 char name_ext[CF_MAX_FILENAME_LENGTH]; // Filename and extension
138 int root_index; // Where in Roots this is located
139 int pathtype_index; // Where in Paths this is located
140 time_t write_time; // When it was last written
141 int size; // How big it is in bytes
142 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.
145 #define CF_NUM_FILES_PER_BLOCK 256
146 #define CF_MAX_FILE_BLOCKS 128 // Can store 256*128 = 32768 files
148 typedef struct cf_file_block {
149 cf_file files[CF_NUM_FILES_PER_BLOCK];
152 static int Num_files = 0;
153 static cf_file_block *File_blocks[CF_MAX_FILE_BLOCKS];
156 // Return a pointer to to file 'index'.
157 cf_file *cf_get_file(int index)
159 int block = index / CF_NUM_FILES_PER_BLOCK;
160 int offset = index % CF_NUM_FILES_PER_BLOCK;
162 return &File_blocks[block]->files[offset];
165 // Create a new file and return a pointer to it.
166 cf_file *cf_create_file()
168 int block = Num_files / CF_NUM_FILES_PER_BLOCK;
169 int offset = Num_files % CF_NUM_FILES_PER_BLOCK;
171 if ( File_blocks[block] == NULL ) {
172 File_blocks[block] = (cf_file_block *)malloc( sizeof(cf_file_block) );
173 Assert( File_blocks[block] != NULL);
178 return &File_blocks[block]->files[offset];
181 extern int cfile_inited;
183 // Create a new root and return a pointer to it. The structure is assumed unitialized.
184 cf_root *cf_get_root(int n)
186 int block = n / CF_NUM_ROOTS_PER_BLOCK;
187 int offset = n % CF_NUM_ROOTS_PER_BLOCK;
192 return &Root_blocks[block]->roots[offset];
196 // Create a new root and return a pointer to it. The structure is assumed unitialized.
197 cf_root *cf_create_root()
199 int block = Num_roots / CF_NUM_ROOTS_PER_BLOCK;
200 int offset = Num_roots % CF_NUM_ROOTS_PER_BLOCK;
202 if ( Root_blocks[block] == NULL ) {
203 Root_blocks[block] = (cf_root_block *)malloc( sizeof(cf_root_block) );
204 Assert(Root_blocks[block] != NULL);
209 return &Root_blocks[block]->roots[offset];
212 // return the # of packfiles which exist
213 int cf_get_packfile_count(cf_root *root)
215 char filespec[MAX_PATH_LEN];
219 // count up how many packfiles we're gonna have
221 for (i=CF_TYPE_ROOT; i<CF_MAX_PATH_TYPES; i++ ) {
223 strcpy( filespec, root->path );
225 if(strlen(Pathtypes[i].path)){
226 strcat( filespec, Pathtypes[i].path );
227 strcat( filespec, "/" );
233 dirp = opendir (filespec);
235 while ((dir = readdir (dirp)) != NULL)
237 if (!fnmatch ("*.vp", dir->d_name, 0))
242 strcpy( filespec, root->path );
244 if(strlen(Pathtypes[i].path)){
245 strcat( filespec, Pathtypes[i].path );
246 strcat( filespec, "\\" );
249 strcat( filespec, "*.vp" );
254 find_handle = _findfirst( filespec, &find );
256 if (find_handle != -1) {
258 if (!(find.attrib & _A_SUBDIR)) {
262 } while (!_findnext(find_handle, &find));
264 _findclose( find_handle );
269 return packfile_count;
272 // packfile sort function
273 int cf_packfile_sort_func(const void *elem1, const void *elem2)
275 cf_root_sort *r1, *r2;
276 r1 = (cf_root_sort*)elem1;
277 r2 = (cf_root_sort*)elem2;
279 // if the 2 directory types are the same, do a string compare
280 if(r1->cf_type == r2->cf_type){
281 return stricmp(r1->path, r2->path);
284 // otherwise return them in order of CF_TYPE_* precedence
285 return (r1->cf_type < r2->cf_type) ? -1 : 1;
288 // Go through a root and look for pack files
289 void cf_build_pack_list( cf_root *root )
291 char filespec[MAX_PATH_LEN];
293 cf_root_sort *temp_roots_sort, *rptr_sort;
294 int temp_root_count, root_index;
296 // determine how many packfiles there are
297 temp_root_count = cf_get_packfile_count(root);
298 if(temp_root_count <= 0){
302 // allocate a temporary array of temporary roots so we can easily sort them
303 temp_roots_sort = (cf_root_sort*)malloc(sizeof(cf_root_sort) * temp_root_count);
304 if(temp_roots_sort == NULL){
309 // now just setup all the root info
311 for (i=CF_TYPE_ROOT; i<CF_MAX_PATH_TYPES; i++ ) {
314 strcpy( filespec, root->path );
316 if(strlen(Pathtypes[i].path)){
317 strcat( filespec, Pathtypes[i].path );
318 strcat( filespec, "/" );
320 fprintf (stderr, "DDOI: searching |%s|\n", filespec);
325 dirp = opendir (filespec);
327 while ((dir = readdir (dirp)) != NULL)
329 if (!fnmatch ("*.vp", dir->d_name, 0))
331 Assert(root_index < temp_root_count);
333 // get a temp pointer
334 rptr_sort = &temp_roots_sort[root_index++];
336 // fill in all the proper info
337 strcpy(rptr_sort->path, root->path);
339 if(strlen(Pathtypes[i].path)){
340 strcat(rptr_sort->path, Pathtypes[i].path );
341 strcat(rptr_sort->path, "/");
344 strcat(rptr_sort->path, dir->d_name );
345 rptr_sort->roottype = CF_ROOTTYPE_PACK;
346 rptr_sort->cf_type = i;
351 strcpy( filespec, root->path );
353 if(strlen(Pathtypes[i].path)){
354 strcat( filespec, Pathtypes[i].path );
355 strcat( filespec, "\\" );
357 strcat( filespec, "*.vp" );
361 find_handle = _findfirst( filespec, &find );
363 if (find_handle != -1) {
366 if (!(find.attrib & _A_SUBDIR)) {
367 Assert(root_index < temp_root_count);
369 // get a temp pointer
370 rptr_sort = &temp_roots_sort[root_index++];
372 // fill in all the proper info
373 strcpy(rptr_sort->path, root->path);
375 if(strlen(Pathtypes[i].path)){
376 strcat(rptr_sort->path, Pathtypes[i].path );
377 strcat(rptr_sort->path, "\\");
380 strcat(rptr_sort->path, find.name );
381 rptr_sort->roottype = CF_ROOTTYPE_PACK;
382 rptr_sort->cf_type = i;
385 } while (!_findnext(find_handle, &find));
387 _findclose( find_handle );
392 // these should always be the same
393 Assert(root_index == temp_root_count);
396 qsort(temp_roots_sort, temp_root_count, sizeof(cf_root_sort), cf_packfile_sort_func);
398 // now insert them all into the real root list properly
400 for(i=0; i<temp_root_count; i++){
401 new_root = cf_create_root();
402 strcpy( new_root->path, root->path );
404 // mwa -- 4/2/98 put in the next 2 lines because the path name needs to be there
405 // to find the files.
406 strcpy(new_root->path, temp_roots_sort[i].path);
407 new_root->roottype = CF_ROOTTYPE_PACK;
410 // free up the temp list
411 free(temp_roots_sort);
415 void cf_build_root_list(char *cdrom_dir)
421 //======================================================
422 // First, check the current directory.
423 // strcpy( root->path, "d:\\projects\\freespace\\" );
425 root = cf_create_root();
427 if ( !_getcwd(root->path, CF_MAX_PATHNAME_LENGTH ) ) {
428 Error(LOCATION, "Can't get current working directory -- %d", errno );
431 // do we already have a slash? as in the case of a root directory install
433 if(strlen(root->path) && (root->path[strlen(root->path)-1] != '/')){
434 strcat(root->path, "/"); // put trailing backslash on for easier path construction
436 if(strlen(root->path) && (root->path[strlen(root->path)-1] != '\\')){
437 strcat(root->path, "\\"); // put trailing backslash on for easier path construction
440 root->roottype = CF_ROOTTYPE_PATH;
442 //======================================================
443 // Next, check any VP files under the current directory.
444 cf_build_pack_list(root);
447 //======================================================
448 // Check the real CD if one...
449 if ( cdrom_dir && strlen(cdrom_dir) ) {
450 root = cf_create_root();
451 strcpy( root->path, cdrom_dir );
452 root->roottype = CF_ROOTTYPE_PATH;
454 //======================================================
455 // Next, check any VP files in the CD-ROM directory.
456 cf_build_pack_list(root);
462 // Given a lower case list of file extensions
463 // separated by spaces, return zero if ext is
465 int is_ext_in_list( char *ext_list, char *ext )
469 strncpy( tmp_ext, ext, 127 );
471 if ( strstr(ext_list, tmp_ext )) {
478 void cf_search_root_path(int root_index)
482 cf_root *root = cf_get_root(root_index);
484 mprintf(( "Searching root '%s'\n", root->path ));
486 char search_path[CF_MAX_PATHNAME_LENGTH];
488 for (i=CF_TYPE_ROOT; i<CF_MAX_PATH_TYPES; i++ ) {
494 strcpy( search_path, root->path );
496 if(strlen(Pathtypes[i].path)){
497 strcat( search_path, Pathtypes[i].path );
498 strcat( search_path, "/" );
501 fprintf (stderr, "DDOI: we're searching |%s|\n", 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);