]> icculus.org git repositories - taylor/freespace2.git/blob - src/cfile/cfilesystem.cpp
fill in rest of UI reset function
[taylor/freespace2.git] / src / cfile / cfilesystem.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
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
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/CFile/CfileSystem.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
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.
20  *
21  * $Log$
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
24  *
25  * Revision 1.10  2004/06/11 00:29:22  tigital
26  * byte-swapping changes for bigendian systems
27  *
28  * Revision 1.9  2003/05/27 03:03:11  taylor
29  * fix second root (gamedir) searching
30  *
31  * Revision 1.8  2003/02/20 17:41:07  theoddone33
32  * Userdir patch from Taylor Richards
33  *
34  * Revision 1.7  2002/06/22 23:57:39  relnev
35  * remove writable strings.
36  *
37  * fix compile for intel compiler.
38  *
39  * Revision 1.6  2002/06/09 04:41:15  relnev
40  * added copyright header
41  *
42  * Revision 1.5  2002/06/05 04:03:32  relnev
43  * finished cfilesystem.
44  *
45  * removed some old code.
46  *
47  * fixed mouse save off-by-one.
48  *
49  * sound cleanups.
50  *
51  * Revision 1.4  2002/05/28 17:26:57  theoddone33
52  * Fill in some timer and palette setting stubs.  Still no display
53  *
54  * Revision 1.3  2002/05/28 06:45:38  theoddone33
55  * Cleanup some stuff
56  *
57  * Revision 1.2  2002/05/28 06:28:20  theoddone33
58  * Filesystem mods, actually reads some data files now
59  *
60  * Revision 1.1.1.1  2002/05/03 03:28:08  root
61  * Initial import.
62  *
63  * 
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.
67  * 
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
72  * main hall.
73  * 
74  * 4     2/22/99 10:31p Andsager
75  * Get rid of unneeded includes.
76  * 
77  * 3     10/13/98 9:19a Andsager
78  * Add localization support to cfile.  Optional parameter with cfopen that
79  * looks for localized files.
80  * 
81  * 2     10/07/98 10:52a Dave
82  * Initial checkin.
83  * 
84  * 1     10/07/98 10:48a Dave
85  * 
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.
89  * 
90  * 13    6/23/98 4:18p Hoffoss
91  * Fixed some bugs with AC release build.
92  * 
93  * 12    5/20/98 10:46p John
94  * Added code that doesn't include duplicate filenames in any file list
95  * functions.
96  * 
97  * 11    5/14/98 2:14p Lawrance2
98  * Use filespec filtering for packfiles
99  * 
100  * 10    5/03/98 11:53a John
101  * Fixed filename case mangling.
102  * 
103  * 9     5/02/98 11:06p Allender
104  * correctly deal with pack pathnames
105  * 
106  * 8     5/01/98 11:41a Allender
107  * Fixed bug with mission saving in Fred.
108  * 
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.
112  * 
113  * 6     4/30/98 10:21p John
114  * Added code to cleanup cfilesystem
115  * 
116  * 5     4/30/98 10:18p John
117  * added source safe header
118  *
119  * $NoKeywords: $
120  */
121
122 #include <stdlib.h>
123 #include <string.h>
124 #include <stdio.h>
125 #include <errno.h>
126 #ifndef PLAT_UNIX
127 #include <io.h>
128 #include <direct.h>
129 #include <windows.h>
130 #include <winbase.h>            /* needed for memory mapping of file functions */
131 #else
132 #include <sys/types.h>
133 #include <dirent.h>
134 #include <fnmatch.h>
135 #include <sys/stat.h>
136 #include <unistd.h>
137 #endif
138
139 #include "pstypes.h"
140 //#include "outwnd.h"
141 //#include "vecmat.h"
142 //#include "timer.h"
143 #include "cfile.h"
144 #include "cfilesystem.h"
145 #include "localize.h"
146
147
148 #define CF_ROOTTYPE_PATH 0
149 #define CF_ROOTTYPE_PACK 1
150
151 //  Created by:
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
159 } cf_root;
160
161 // convenient type for sorting (see cf_build_pack_list())
162 typedef struct cf_root_sort { 
163         char                            path[CF_MAX_PATHNAME_LENGTH];
164         int                             roottype;
165         int                             cf_type;
166 } cf_root_sort;
167
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)
171
172 typedef struct cf_root_block {
173         cf_root                 roots[CF_NUM_ROOTS_PER_BLOCK];
174 } cf_root_block;
175
176 static int Num_roots = 0;
177 static cf_root_block  *Root_blocks[CF_MAX_ROOT_BLOCKS];
178   
179
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.
188 } cf_file;
189
190 #define CF_NUM_FILES_PER_BLOCK   256
191 #define CF_MAX_FILE_BLOCKS                      128                                             // Can store 256*128 = 32768 files
192
193 typedef struct cf_file_block {
194         cf_file                                         files[CF_NUM_FILES_PER_BLOCK];
195 } cf_file_block;
196
197 static int Num_files = 0;
198 static cf_file_block  *File_blocks[CF_MAX_FILE_BLOCKS];
199
200
201 // Return a pointer to to file 'index'.
202 cf_file *cf_get_file(int index)
203 {
204         int block = index / CF_NUM_FILES_PER_BLOCK;
205         int offset = index % CF_NUM_FILES_PER_BLOCK;
206
207         return &File_blocks[block]->files[offset];
208 }
209
210 // Create a new file and return a pointer to it.
211 cf_file *cf_create_file()
212 {
213         int block = Num_files / CF_NUM_FILES_PER_BLOCK;
214         int offset = Num_files % CF_NUM_FILES_PER_BLOCK;
215         
216         if ( File_blocks[block] == NULL )       {
217                 File_blocks[block] = (cf_file_block *)malloc( sizeof(cf_file_block) );
218                 SDL_assert( File_blocks[block] != NULL);
219         }
220
221         Num_files++;
222
223         return &File_blocks[block]->files[offset];
224 }
225
226 extern int cfile_inited;
227
228 // Create a new root and return a pointer to it.  The structure is assumed unitialized.
229 cf_root *cf_get_root(int n)
230 {
231         int block = n / CF_NUM_ROOTS_PER_BLOCK;
232         int offset = n % CF_NUM_ROOTS_PER_BLOCK;
233
234         if (!cfile_inited)
235                 return NULL;
236
237         return &Root_blocks[block]->roots[offset];
238 }
239
240
241 // Create a new root and return a pointer to it.  The structure is assumed unitialized.
242 cf_root *cf_create_root()
243 {
244         int block = Num_roots / CF_NUM_ROOTS_PER_BLOCK;
245         int offset = Num_roots % CF_NUM_ROOTS_PER_BLOCK;
246         
247         if ( Root_blocks[block] == NULL )       {
248                 Root_blocks[block] = (cf_root_block *)malloc( sizeof(cf_root_block) );
249                 SDL_assert(Root_blocks[block] != NULL);
250         }
251
252         Num_roots++;
253
254         return &Root_blocks[block]->roots[offset];
255 }
256
257 // return the # of packfiles which exist
258 int cf_get_packfile_count(cf_root *root)
259 {
260         char filespec[MAX_PATH_LEN];
261         int i;
262         int packfile_count;
263
264         // count up how many packfiles we're gonna have
265         packfile_count = 0;
266         for (i=CF_TYPE_ROOT; i<CF_MAX_PATH_TYPES; i++ ) {
267                 SDL_strlcpy( filespec, root->path, SDL_arraysize(filespec) );
268
269                 if(strlen(Pathtypes[i].path)){
270                         SDL_strlcat( filespec, Pathtypes[i].path, SDL_arraysize(filespec) );
271                         SDL_strlcat( filespec, DIR_SEPARATOR_STR, SDL_arraysize(filespec) );
272                 }
273
274 #ifdef PLAT_UNIX
275                 DIR *dirp;
276                 struct dirent *dir;
277
278                 dirp = opendir (filespec);
279                 if ( dirp ) {
280                         while ((dir = readdir (dirp)) != NULL)
281                         {
282                                 if (!fnmatch ("*.vp", dir->d_name, 0))
283                                         packfile_count++;
284                         }
285                         closedir(dirp);
286                 }
287 #else
288                 SDL_strlcat( filespec, "*.vp", SDL_arraysize(filespec) );
289
290                 int find_handle;
291                 _finddata_t find;
292                 
293                 find_handle = _findfirst( filespec, &find );
294
295                 if (find_handle != -1) {
296                         do {
297                                 if (!(find.attrib & _A_SUBDIR)) {
298                                         packfile_count++;
299                                 }
300
301                         } while (!_findnext(find_handle, &find));
302
303                         _findclose( find_handle );
304                 }       
305 #endif
306         }       
307
308         return packfile_count;
309 }
310
311 // packfile sort function
312 int cf_packfile_sort_func(const void *elem1, const void *elem2)
313 {
314         cf_root_sort *r1, *r2;
315         r1 = (cf_root_sort*)elem1;
316         r2 = (cf_root_sort*)elem2;
317
318         // if the 2 directory types are the same, do a string compare
319         if(r1->cf_type == r2->cf_type){
320                 return SDL_strcasecmp(r1->path, r2->path);
321         }
322
323         // otherwise return them in order of CF_TYPE_* precedence
324         return (r1->cf_type < r2->cf_type) ? -1 : 1;
325 }
326
327 // Go through a root and look for pack files
328 void cf_build_pack_list( cf_root *root )
329 {
330         char filespec[MAX_PATH_LEN];
331         int i;
332         cf_root_sort *temp_roots_sort, *rptr_sort;
333         int temp_root_count, root_index;
334
335         // determine how many packfiles there are
336         temp_root_count = cf_get_packfile_count(root);
337         if(temp_root_count <= 0){
338                 return;
339         }
340
341         // allocate a temporary array of temporary roots so we can easily sort them
342         temp_roots_sort = (cf_root_sort*)malloc(sizeof(cf_root_sort) * temp_root_count);
343         if(temp_roots_sort == NULL){
344                 Int3();
345                 return;
346         }
347
348         // now just setup all the root info
349         root_index = 0;
350         for (i=CF_TYPE_ROOT; i<CF_MAX_PATH_TYPES; i++ ) {
351                 SDL_strlcpy( filespec, root->path, SDL_arraysize(filespec) );
352
353                 if(strlen(Pathtypes[i].path)){
354                         SDL_strlcat( filespec, Pathtypes[i].path, SDL_arraysize(filespec) );
355                         SDL_strlcat( filespec, DIR_SEPARATOR_STR, SDL_arraysize(filespec) );
356                 }
357
358 #ifdef PLAT_UNIX
359                 DIR *dirp;
360                 struct dirent *dir;
361
362                 dirp = opendir (filespec);
363                 if ( dirp ) {
364                         while ((dir = readdir (dirp)) != NULL)
365                         {
366                                 if (!fnmatch ("*.vp", dir->d_name, 0))
367                                 {
368                                         SDL_assert(root_index < temp_root_count);
369
370                                         char fn[MAX_PATH_LEN];
371                                         SDL_snprintf(fn, SDL_arraysize(fn), "%s/%s", filespec, dir->d_name);
372                                                         
373                                         struct stat buf;
374                                         if (stat(fn, &buf) == -1) {
375                                                 continue;
376                                         }
377                                         
378                                         if (!S_ISREG(buf.st_mode)) {
379                                                 continue;
380                                         }
381                                         
382                                         // get a temp pointer
383                                         rptr_sort = &temp_roots_sort[root_index++];
384
385                                         // fill in all the proper info
386                                         SDL_strlcpy(rptr_sort->path, root->path, SDL_arraysize(rptr_sort->path));
387
388                                         if(strlen(Pathtypes[i].path)){
389                                                 SDL_strlcat(rptr_sort->path, Pathtypes[i].path, SDL_arraysize(rptr_sort->path));
390                                                 SDL_strlcat(rptr_sort->path, "/", SDL_arraysize(rptr_sort->path));
391                                         }
392
393                                         SDL_strlcat(rptr_sort->path, dir->d_name, SDL_arraysize(rptr_sort->path));
394                                         rptr_sort->roottype = CF_ROOTTYPE_PACK;
395                                         rptr_sort->cf_type = i;
396                                 }
397                         }
398                         closedir(dirp);
399                 }
400 #else
401                 SDL_strlcat( filespec, "*.vp", SDL_arraysize(filespec) );
402
403                 int find_handle;
404                 _finddata_t find;
405                 
406                 find_handle = _findfirst( filespec, &find );
407
408                 if (find_handle != -1) {
409                         do {
410                                 // add the new item
411                                 if (!(find.attrib & _A_SUBDIR)) {                                       
412                                         SDL_assert(root_index < temp_root_count);
413
414                                         // get a temp pointer
415                                         rptr_sort = &temp_roots_sort[root_index++];
416
417                                         // fill in all the proper info
418                                         SDL_strlcpy(rptr_sort->path, root->path, SDL_arraysize(rptr_sort->path));
419
420                                         if(strlen(Pathtypes[i].path)){
421                                                 SDL_strlcat(rptr_sort->path, Pathtypes[i].path, SDL_arraysize(rptr_sort->path) );
422                                                 SDL_strlcat(rptr_sort->path, "\\", SDL_arraysize(rptr_sort->path));
423                                         }
424
425                                         SDL_strlcat(rptr_sort->path, find.name, SDL_arraysize(rptr_sort->path) );
426                                         rptr_sort->roottype = CF_ROOTTYPE_PACK;
427                                         rptr_sort->cf_type = i;
428                                 }
429
430                         } while (!_findnext(find_handle, &find));
431
432                         _findclose( find_handle );
433                 }       
434 #endif
435         }       
436
437         // these should always be the same
438         SDL_assert(root_index == temp_root_count);
439
440         // sort tht roots
441         qsort(temp_roots_sort,  temp_root_count, sizeof(cf_root_sort), cf_packfile_sort_func);
442
443         // now insert them all into the real root list properly
444         cf_root *new_root;
445         for(i=0; i<temp_root_count; i++){               
446                 new_root = cf_create_root();
447                 SDL_strlcpy( new_root->path, root->path, SDL_arraysize(new_root->path) );
448
449                 // mwa -- 4/2/98 put in the next 2 lines because the path name needs to be there
450                 // to find the files.
451                 SDL_strlcpy(new_root->path, temp_roots_sort[i].path, SDL_arraysize(new_root->path));
452                 new_root->roottype = CF_ROOTTYPE_PACK;          
453         }
454
455         // free up the temp list
456         free(temp_roots_sort);
457 }
458
459
460 void cf_build_root_list(const char *extras_dir)
461 {
462         Num_roots = 0;
463
464         cf_root *root;
465
466         // ================================================================
467         // have user's writable directory as default for loading and saving files
468         root = cf_create_root();
469         SDL_strlcpy( root->path, Cfile_user_dir, SDL_arraysize(root->path) );
470
471         // do we already have a slash? as in the case of a root directory install
472         if(strlen(root->path) && (root->path[strlen(root->path)-1] != DIR_SEPARATOR_CHAR)){
473                 SDL_strlcat(root->path, DIR_SEPARATOR_STR, SDL_arraysize(root->path));          // put trailing backslash on for easier path construction
474         }
475         root->roottype = CF_ROOTTYPE_PATH;
476
477         //======================================================
478         // then check any VP files under the directory.
479         cf_build_pack_list(root);
480
481         //======================================================
482         // Next, use the executable's directory for game data
483         root = cf_create_root();
484         SDL_strlcpy( root->path, Cfile_root_dir, SDL_arraysize(root->path) );
485
486         // do we already have a slash? as in the case of a root directory install
487         if(strlen(root->path) && (root->path[strlen(root->path)-1] != DIR_SEPARATOR_CHAR)){
488                 SDL_strlcat(root->path, DIR_SEPARATOR_STR, SDL_arraysize(root->path));          // put trailing backslash on for easier path construction
489         }
490         root->roottype = CF_ROOTTYPE_PATH;
491
492         //======================================================
493         // then check any VP files under the current directory.
494         cf_build_pack_list(root);
495
496
497         //======================================================
498         // Check the real CD if one...
499         if ( extras_dir && strlen(extras_dir) ) {
500                 root = cf_create_root();
501                 SDL_strlcpy( root->path, extras_dir, SDL_arraysize(root->path) );
502                 root->roottype = CF_ROOTTYPE_PATH;
503
504                 //======================================================
505                 // Next, check any VP files in the CD-ROM directory.
506                 cf_build_pack_list(root);
507         }
508 }
509
510 // Given a lower case list of file extensions 
511 // separated by spaces, return zero if ext is
512 // not in the list.
513 int is_ext_in_list( const char *ext_list, char *ext )
514 {
515         char tmp_ext[128];
516
517         SDL_strlcpy( tmp_ext, ext, SDL_arraysize(tmp_ext) );
518         SDL_strlwr(tmp_ext);
519         if ( strstr(ext_list, tmp_ext ))        {
520                 return 1;
521         }       
522
523         return 0;
524 }
525
526 void cf_search_root_path(int root_index)
527 {
528         int i;
529
530         cf_root *root = cf_get_root(root_index);
531
532         mprintf(( "Searching root '%s'\n", root->path ));
533
534         char search_path[CF_MAX_PATHNAME_LENGTH];
535
536         for (i=CF_TYPE_ROOT; i<CF_MAX_PATH_TYPES; i++ ) {
537
538                 SDL_strlcpy( search_path, root->path, SDL_arraysize(search_path) );
539
540                 if(strlen(Pathtypes[i].path)){
541                         SDL_strlcat( search_path, Pathtypes[i].path, SDL_arraysize(search_path) );
542                         SDL_strlcat( search_path, DIR_SEPARATOR_STR, SDL_arraysize(search_path) );
543                 }
544
545 #ifdef PLAT_UNIX
546                 DIR *dirp;
547                 struct dirent *dir;
548
549                 // only get pilots from the primary root as there could be nasty
550                 // permission issues otherwise
551                 if ( (root_index > 0) && ((Pathtypes[i].index == CF_TYPE_SINGLE_PLAYERS)
552                                         || (Pathtypes[i].index == CF_TYPE_MULTI_PLAYERS)) ) {
553                         break;
554                 }
555
556                 dirp = opendir (search_path);
557                 if ( dirp ) {
558                         while ((dir = readdir (dirp)) != NULL)
559                         {
560                                 if (!fnmatch ("*.*", dir->d_name, 0))
561                                 {
562                                         char fn[MAX_PATH_LEN];
563                                         SDL_snprintf(fn, MAX_PATH_LEN, "%s/%s", search_path, dir->d_name);
564                                                         
565                                         struct stat buf;
566                                         if (stat(fn, &buf) == -1) {
567                                                 continue;
568                                         }
569                                         
570                                         if (!S_ISREG(buf.st_mode)) {
571                                                 continue;
572                                         }
573                                         
574                                         char *ext = SDL_strchr( dir->d_name, '.' );
575                                         if ( ext )      {
576                                                 if ( is_ext_in_list( Pathtypes[i].extensions, ext ) )   {
577                                                         // Found a file!!!!
578                                                         cf_file *file = cf_create_file();
579
580                                                         SDL_strlcpy( file->name_ext, dir->d_name, SDL_arraysize(file->name_ext) );
581                                                         file->root_index = root_index;
582                                                         file->pathtype_index = i;
583
584
585                                                         file->write_time = buf.st_mtime;
586                                                         file->size = buf.st_size;
587
588                                                         file->pack_offset = 0;                  // Mark as a non-packed file
589
590                                                         //mprintf(( "Found file '%s'\n", file->name_ext ));
591                                                 }
592                                         }
593                                 }
594                         }
595                         closedir(dirp);
596                 }
597 #else
598                 SDL_strlcat( search_path, "*.*", SDL_arraysize(search_path) );
599
600                 int find_handle;
601                 _finddata_t find;
602                 
603                 find_handle = _findfirst( search_path, &find );
604
605                 if (find_handle != -1) {
606                         do {
607                                 if (!(find.attrib & _A_SUBDIR)) {
608
609                                         char *ext = SDL_strchr( find.name, '.' );
610                                         if ( ext )      {
611                                                 if ( is_ext_in_list( Pathtypes[i].extensions, ext ) )   {
612                                                         // Found a file!!!!
613                                                         cf_file *file = cf_create_file();
614
615                                                         SDL_strlcpy( file->name_ext, find.name, SDL_arraysize(file->name_ext) );
616                                                         file->root_index = root_index;
617                                                         file->pathtype_index = i;
618                                                         file->write_time = find.time_write;
619                                                         file->size = find.size;
620                                                         file->pack_offset = 0;                  // Mark as a non-packed file
621
622                                                         //mprintf(( "Found file '%s'\n", file->name_ext ));
623
624                                                 }
625                                         }
626
627                                 }
628
629                         } while (!_findnext(find_handle, &find));
630
631                         _findclose( find_handle );
632                 }
633 #endif
634
635         }
636 }
637
638
639 typedef struct VP_FILE_HEADER {
640         char id[4];
641         int version;
642         int index_offset;
643         int num_files;
644 } VP_FILE_HEADER;
645
646 typedef struct VP_FILE {
647         int     offset;
648         int     size;
649         char    filename[32];
650         fs_time_t write_time;
651 } VP_FILE;
652
653 void cf_search_root_pack(int root_index)
654 {
655         size_t rc = 0;
656         int i;
657
658         cf_root *root = cf_get_root(root_index);
659
660         //mprintf(( "Searching root pack '%s'\n", root->path ));
661
662         // Open data
663                 
664         FILE *fp = fopen( root->path, "rb" );
665         // Read the file header
666         if (!fp) {
667                 return;
668         }
669
670         VP_FILE_HEADER VP_header;
671
672         SDL_assert( sizeof(VP_header) == 16 );
673         rc = fread(&VP_header, 1, sizeof(VP_header), fp);
674
675         if (rc != sizeof(VP_header)) {
676                 fclose(fp);
677                 return;
678         }
679
680     VP_header.version = INTEL_INT( VP_header.version);
681     VP_header.index_offset = INTEL_INT( VP_header.index_offset);
682     VP_header.num_files = INTEL_INT( VP_header.num_files);
683         
684         // Read index info
685         fseek(fp, VP_header.index_offset, SEEK_SET);
686
687         char search_path[CF_MAX_PATHNAME_LENGTH];
688
689         SDL_strlcpy( search_path, "", SDL_arraysize(search_path) );
690         
691         // Go through all the files
692         for (i=0; i<VP_header.num_files; i++ )  {
693                 VP_FILE find;
694
695                 rc = fread( &find, 1, sizeof(VP_FILE), fp );
696
697                 if (rc != sizeof(VP_FILE)) {
698                         break;
699                 }
700
701         find.offset = INTEL_INT( find.offset );
702         find.size = INTEL_INT( find.size );
703                 find.write_time = INTEL_INT(find.write_time);
704
705                 if ( find.size == 0 )   {
706                         if ( !SDL_strcasecmp( find.filename, ".." ))    {
707                                 int l = strlen(search_path);
708                                 char *p = &search_path[l-1];
709                                 while( (p > search_path) && (*p != DIR_SEPARATOR_CHAR) )        {
710                                         p--;
711                                 }
712                                 *p = 0;
713                         } else {
714                                 if ( strlen(search_path)        )       {
715                                         SDL_strlcat( search_path,       DIR_SEPARATOR_STR, SDL_arraysize(search_path) );
716                                 }
717                                 SDL_strlcat( search_path, find.filename, SDL_arraysize(search_path) );
718                         }
719
720                         //mprintf(( "Current dir = '%s'\n", search_path ));
721                 } else {
722         
723                         int j;
724                         for (j=CF_TYPE_ROOT; j<CF_MAX_PATH_TYPES; j++ ) {
725
726                                 if ( !SDL_strcasecmp( search_path, Pathtypes[j].path )) {
727
728                                         char *ext = SDL_strchr( find.filename, '.' );
729                                         if ( ext )      {
730                                                 if ( is_ext_in_list( Pathtypes[j].extensions, ext ) )   {
731                                                         // Found a file!!!!
732                                                         cf_file *file = cf_create_file();
733                                                         
734                                                         SDL_strlcpy( file->name_ext, find.filename, SDL_arraysize(file->name_ext) );
735                                                         file->root_index = root_index;
736                                                         file->pathtype_index = j;
737                                                         file->write_time = find.write_time;
738                                                         file->size = find.size;
739                                                         file->pack_offset = find.offset;                        // Mark as a non-packed file
740
741                                                         //mprintf(( "Found pack file '%s'\n", file->name_ext ));
742                                                 }
743                                         }
744                                         
745
746                                 }
747                         }
748
749                 }
750         }
751         fclose(fp);
752 }
753
754
755 void cf_build_file_list()
756 {
757         int i;
758
759         Num_files = 0;
760
761         // For each root, find all files...
762         for (i=1; i<Num_roots; i++ )    {
763                 cf_root *root = cf_get_root(i);
764                 if ( root->roottype == CF_ROOTTYPE_PATH )       {
765                         cf_search_root_path(i);
766                 } else if ( root->roottype == CF_ROOTTYPE_PACK )        {
767                         cf_search_root_pack(i);
768                 }
769         }
770
771 }
772
773
774 void cf_build_secondary_filelist(const char *extras_dir)
775 {
776         int i;
777
778         // Assume no files
779         Num_roots = 0;
780         Num_files = 0;
781
782         // Init the path types
783         for (i=0; i<CF_MAX_PATH_TYPES; i++ )    {
784                 SDL_assert( Pathtypes[i].index == i );
785 #if 0 /* they are already lowercased -- SBF */
786                 if ( Pathtypes[i].extensions )  {
787                         SDL_strlwr(Pathtypes[i].extensions);
788                 }
789 #endif          
790         }
791         
792         // Init the root blocks
793         for (i=0; i<CF_MAX_ROOT_BLOCKS; i++ )   {
794                 Root_blocks[i] = NULL;
795         }
796
797         // Init the file blocks 
798         for (i=0; i<CF_MAX_FILE_BLOCKS; i++ )   {
799                 File_blocks[i] = NULL;
800         }
801
802         mprintf(( "Building file index...\n" ));
803         
804         // build the list of searchable roots
805         cf_build_root_list(extras_dir);
806
807         // build the list of files themselves
808         cf_build_file_list();
809
810         mprintf(( "Found %d roots and %d files.\n", Num_roots, Num_files ));
811 }
812
813 void cf_free_secondary_filelist()
814 {
815         int i;
816
817         // Free the root blocks
818         for (i=0; i<CF_MAX_ROOT_BLOCKS; i++ )   {
819                 if ( Root_blocks[i] )   {
820                         free( Root_blocks[i] );
821                         Root_blocks[i] = NULL;
822                 }
823         }
824         Num_roots = 0;
825
826         // Init the file blocks 
827         for (i=0; i<CF_MAX_FILE_BLOCKS; i++ )   {
828                 if ( File_blocks[i] )   {
829                         free( File_blocks[i] );
830                         File_blocks[i] = NULL;
831                 }
832         }
833         Num_files = 0;
834 }
835
836 // Searches for a file.   Follows all rules and precedence and searches
837 // CD's and pack files.
838 // Input:  filespace   - Filename & extension
839 //         pathtype    - See CF_TYPE_ defines in CFILE.H
840 // Output: pack_filename - Absolute path and filename of this file.   Could be a packfile or the actual file.
841 //         size        - File size
842 //         offset      - Offset into pack file.  0 if not a packfile.
843 // Returns: If not found returns 0.
844 int cf_find_file_location( const char *filespec, int pathtype, char *pack_filename, int *size, int *offset, bool localize )
845 {
846         int i;
847
848         SDL_assert(filespec && strlen(filespec));
849
850         // see if we have something other than just a filename
851         // our current rules say that any file that specifies a direct
852         // path will try to be opened on that path.  If that open
853         // fails, then we will open the file based on the extension
854         // of the file
855
856 #ifdef PLAT_UNIX
857         const char *toks = "/";
858 #else
859         const char *toks = "/\\:";
860 #endif
861
862         // NOTE: full path should also include localization, if so desired
863         if ( strpbrk(filespec, toks) ) {                // do we have a full path already?
864                 FILE *fp = fopen(filespec, "rb" );
865                 if (fp) {
866                         if ( size ) *size = filelength(fileno(fp));
867                         if ( offset ) *offset = 0;
868                         if ( pack_filename ) {
869                                 SDL_strlcpy( pack_filename, filespec, MAX_PATH_LEN );
870                         }                               
871                         fclose(fp);
872                         return 1;               
873                 }
874
875                 return 0;               // If they give a full path, fail if not found.
876         }
877
878         // Search the hard drive for files first.
879         int num_search_dirs = 0;
880         int search_order[CF_MAX_PATH_TYPES];
881
882         if ( CF_TYPE_SPECIFIED(pathtype) )      {
883                 search_order[num_search_dirs++] = pathtype;
884         }
885
886         for (i=CF_TYPE_ROOT; i<CF_MAX_PATH_TYPES; i++)  {
887                 if ( i != pathtype )    {
888                         search_order[num_search_dirs++] = i;
889                 }
890         }
891
892         for (i=0; i<num_search_dirs; i++ )      {
893                 char longname[MAX_PATH_LEN];
894
895                 cf_create_default_path_string( longname, search_order[i], filespec, localize );
896
897                 FILE *fp = fopen(longname, "rb" );
898                 if (fp) {
899                         if ( size ) *size = filelength(fileno(fp));
900                         if ( offset ) *offset = 0;
901                         if ( pack_filename ) {
902                                 SDL_strlcpy( pack_filename, longname, MAX_PATH_LEN );
903                         }                               
904                         fclose(fp);
905                         return 1;               
906                 }
907         } 
908
909         // Search the pak files and CD-ROM.
910
911                 for (i=0; i<Num_files; i++ )    {
912                         cf_file * f = cf_get_file(i);
913
914                         // only search paths we're supposed to...
915                         if ( (pathtype != CF_TYPE_ANY) && (pathtype != f->pathtype_index)  )    {
916                                 continue;
917                         }
918
919                         if (localize) {
920                                 // create localized filespec
921                                 char loc_filespec[MAX_PATH_LEN];
922                                 SDL_strlcpy(loc_filespec, filespec, SDL_arraysize(loc_filespec));
923                                 lcl_add_dir_to_path_with_filename(loc_filespec, sizeof(loc_filespec));
924                         
925                                 if ( !SDL_strcasecmp(loc_filespec, f->name_ext) )       {
926                                         if ( size ) *size = f->size;
927                                         if ( offset ) *offset = f->pack_offset;
928                                         if ( pack_filename ) {
929                                                 cf_root * r = cf_get_root(f->root_index);
930
931                                                 SDL_strlcpy( pack_filename, r->path, MAX_PATH_LEN );
932                                                 if ( f->pack_offset < 1 )       {
933                                                         SDL_strlcat( pack_filename, Pathtypes[f->pathtype_index].path, MAX_PATH_LEN );
934                                                         SDL_strlcat( pack_filename, DIR_SEPARATOR_STR, MAX_PATH_LEN );
935                                                         SDL_strlcat( pack_filename, f->name_ext, MAX_PATH_LEN );
936                                                 }
937                                         }                               
938                                         return 1;               
939                                 }
940                         }
941
942                         // file either not localized or localized version not found
943                         if ( !SDL_strcasecmp(filespec, f->name_ext) )   {
944                                 if ( size ) *size = f->size;
945                                 if ( offset ) *offset = f->pack_offset;
946                                 if ( pack_filename ) {
947                                         cf_root * r = cf_get_root(f->root_index);
948
949                                         SDL_strlcpy( pack_filename, r->path, MAX_PATH_LEN );
950                                         if ( f->pack_offset < 1 )       {
951
952                                                 if(strlen(Pathtypes[f->pathtype_index].path)){
953                                                         SDL_strlcat( pack_filename, Pathtypes[f->pathtype_index].path, MAX_PATH_LEN );
954                                                         SDL_strlcat( pack_filename, DIR_SEPARATOR_STR, MAX_PATH_LEN );
955                                                 }
956
957                                                 SDL_strlcat( pack_filename, f->name_ext, MAX_PATH_LEN );
958                                         }
959                                 }                               
960                                 return 1;               
961                         }
962                 }
963         
964         return 0;
965 }
966
967
968 // Returns true if filename matches filespec, else zero if not
969 int cf_matches_spec(const char *filespec, const char *filename)
970 {
971         const char *src_ext, *dst_ext;
972
973         src_ext = SDL_strchr(filespec, '.');
974         if (!src_ext)
975                 return 1;
976         if (*src_ext == '*')
977                 return 1;
978
979         dst_ext = SDL_strchr(filename, '.');
980         if (!dst_ext)
981                 return 1;
982         
983         return !SDL_strcasecmp(dst_ext, src_ext);
984 }
985
986 int (*Get_file_list_filter)(const char *filename) = NULL;
987 int Skip_packfile_search = 0;
988
989 int cf_file_already_in_list( int num_files, char **list, char *filename )
990 {
991         int i;
992
993         char name_no_extension[MAX_PATH_LEN];
994
995         SDL_strlcpy(name_no_extension, filename, SDL_arraysize(name_no_extension));
996         char *p = SDL_strchr( name_no_extension, '.' );
997         if ( p ) *p = 0;
998
999         for (i=0; i<num_files; i++ )    {
1000                 if ( !SDL_strcasecmp(list[i], name_no_extension ) )     {
1001                         // Match found!
1002                         return 1;
1003                 }
1004         }
1005         // Not found
1006         return 0;
1007 }
1008
1009 // An alternative cf_get_file_list(), dynamic list version.
1010 // This one has a 'type', which is a CF_TYPE_* value.  Because this specifies the directory
1011 // location, 'filter' only needs to be the filter itself, with no path information.
1012 // See above descriptions of cf_get_file_list() for more information about how it all works.
1013 int cf_get_file_list( int max, char **list, int pathtype, const char *filter, int sort, file_list_info *info )
1014 {
1015         char *ptr;
1016         int i, l, num_files = 0, own_flag = 0;
1017
1018         if (max < 1) {
1019                 Get_file_list_filter = NULL;
1020                 return 0;
1021         }
1022
1023         SDL_assert(list);
1024
1025         if (!info && (sort == CF_SORT_TIME)) {
1026                 info = (file_list_info *) malloc(sizeof(file_list_info) * max);
1027                 own_flag = 1;
1028         }
1029
1030         char filespec[MAX_PATH_LEN];
1031
1032 #ifdef PLAT_UNIX
1033         cf_create_default_path_string( filespec, pathtype, NULL );
1034
1035         DIR *dirp;
1036         struct dirent *dir;
1037
1038         dirp = opendir (filespec);
1039         if ( dirp ) {
1040                 while ((dir = readdir (dirp)) != NULL)
1041                 {
1042                         if (num_files >= max)
1043                                 break;
1044
1045                         if (fnmatch(filter, dir->d_name, 0) != 0)
1046                                 continue;
1047
1048                         char fn[MAX_PATH_LEN];
1049                         SDL_snprintf(fn, MAX_PATH_LEN, "%s/%s", filespec, dir->d_name);
1050
1051                         struct stat buf;
1052                         if (stat(fn, &buf) == -1) {
1053                                 continue;
1054                         }
1055
1056                         if (!S_ISREG(buf.st_mode)) {
1057                                 continue;
1058                         }
1059
1060                         if ( !Get_file_list_filter || (*Get_file_list_filter)(dir->d_name) ) {
1061                                 ptr = strrchr(dir->d_name, '.');
1062                                 if (ptr)
1063                                         l = ptr - dir->d_name;
1064                                 else
1065                                         l = strlen(dir->d_name);
1066
1067                                 list[num_files] = (char *)malloc(l + 1);
1068                                 SDL_strlcpy(list[num_files], dir->d_name, l+1);
1069                                 if (info)
1070                                         info[num_files].write_time = buf.st_mtime;
1071
1072                                 num_files++;
1073                         }
1074                 }
1075
1076                 closedir(dirp);
1077         }
1078 #else
1079         cf_create_default_path_string( filespec, pathtype, filter );
1080
1081         int find_handle;
1082         _finddata_t find;
1083
1084         find_handle = _findfirst( filespec, &find );
1085         if (find_handle != -1) {
1086                 do {
1087                         if (num_files >= max)
1088                                 break;
1089
1090                         if (!(find.attrib & _A_SUBDIR)) {
1091                                 if ( !Get_file_list_filter || (*Get_file_list_filter)(find.name) ) {
1092                                         ptr = strrchr(find.name, '.');
1093                                         if (ptr)
1094                                                 l = ptr - find.name;
1095                                         else
1096                                                 l = strlen(find.name);
1097
1098                                         list[num_files] = (char *)malloc(l + 1);
1099                                         SDL_strlcpy(list[num_files], find.name, l+1);
1100                                         if (info)
1101                                                 info[num_files].write_time = find.time_write;
1102
1103                                         num_files++;
1104                                 }
1105                         }
1106
1107                 } while (!_findnext(find_handle, &find));
1108
1109                 _findclose( find_handle );
1110         }
1111 #endif
1112
1113         // Search all the packfiles and CD.
1114         if ( !Skip_packfile_search )    {
1115                 for (i=0; i<Num_files; i++ )    {
1116                         cf_file * f = cf_get_file(i);
1117
1118                         // only search paths we're supposed to...
1119                         if ( (pathtype != CF_TYPE_ANY) && (pathtype != f->pathtype_index)  )    {
1120                                 continue;
1121                         }
1122
1123                         if (num_files >= max)
1124                                 break;
1125
1126                         if ( !cf_matches_spec( filter,f->name_ext))     {
1127                                 continue;
1128                         }
1129
1130                         if ( cf_file_already_in_list(num_files,list,f->name_ext))       {
1131                                 continue;
1132                         }
1133
1134                         if ( !Get_file_list_filter || (*Get_file_list_filter)(f->name_ext) ) {
1135
1136                                 //mprintf(( "Found '%s' in root %d path %d\n", f->name_ext, f->root_index, f->pathtype_index ));
1137
1138                                         ptr = strrchr(f->name_ext, '.');
1139                                         if (ptr)
1140                                                 l = ptr - f->name_ext;
1141                                         else
1142                                                 l = strlen(f->name_ext);
1143
1144                                         list[num_files] = (char *)malloc(l + 1);
1145                                         SDL_strlcpy(list[num_files], f->name_ext, l+1);
1146
1147                                 if (info)       {
1148                                         info[num_files].write_time = f->write_time;
1149                                 }
1150
1151                                 num_files++;
1152                         }
1153
1154                 }
1155         }
1156
1157
1158         if (sort != CF_SORT_NONE)       {
1159                 cf_sort_filenames( num_files, list, sort, info );
1160         }
1161
1162         if (own_flag)   {
1163                 free(info);
1164         }
1165
1166         Get_file_list_filter = NULL;
1167         return num_files;
1168 }
1169
1170 int cf_file_already_in_list_preallocated( int num_files, char arr[][MAX_FILENAME_LEN], const char *filename )
1171 {
1172         int i;
1173
1174         char name_no_extension[MAX_PATH_LEN];
1175
1176         SDL_strlcpy(name_no_extension, filename, SDL_arraysize(name_no_extension));
1177         char *p = SDL_strchr( name_no_extension, '.' );
1178         if ( p ) *p = 0;
1179
1180         for (i=0; i<num_files; i++ )    {
1181                 if ( !SDL_strcasecmp(arr[i], name_no_extension ) )      {
1182                         // Match found!
1183                         return 1;
1184                 }
1185         }
1186         // Not found
1187         return 0;
1188 }
1189
1190 // An alternative cf_get_file_list(), fixed array version.
1191 // This one has a 'type', which is a CF_TYPE_* value.  Because this specifies the directory
1192 // location, 'filter' only needs to be the filter itself, with no path information.
1193 // See above descriptions of cf_get_file_list() for more information about how it all works.
1194 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 )
1195 {
1196         int i, num_files = 0, own_flag = 0;
1197
1198         if (max < 1) {
1199                 Get_file_list_filter = NULL;
1200                 return 0;
1201         }
1202
1203         if (list) {
1204                 for (i=0; i<max; i++)   {
1205                         list[i] = arr[i];
1206                 }
1207         } else {
1208                 sort = CF_SORT_NONE;  // sorting of array directly not supported.  Sorting done on list only
1209         }
1210
1211         if (!info && (sort == CF_SORT_TIME)) {
1212                 info = (file_list_info *) malloc(sizeof(file_list_info) * max);
1213                 own_flag = 1;
1214         }
1215
1216         char filespec[MAX_PATH_LEN];
1217
1218         // Search the default directories
1219 #ifdef PLAT_UNIX
1220         cf_create_default_path_string( filespec, pathtype, NULL );
1221         
1222         DIR *dirp;
1223         struct dirent *dir;
1224
1225         dirp = opendir (filespec);
1226         if ( dirp ) {
1227                 while ((dir = readdir (dirp)) != NULL)
1228                 {
1229                         if (num_files >= max)
1230                                 break;
1231
1232                         if (fnmatch(filter, dir->d_name, 0) != 0)
1233                                 continue;
1234
1235                         char fn[MAX_PATH_LEN];
1236                         SDL_snprintf(fn, MAX_PATH_LEN, "%s/%s", filespec, dir->d_name);
1237
1238                         struct stat buf;
1239                         if (stat(fn, &buf) == -1) {
1240                                 continue;
1241                         }
1242
1243                         if (!S_ISREG(buf.st_mode)) {
1244                                 continue;
1245                         }
1246
1247                         if ( !Get_file_list_filter || (*Get_file_list_filter)(dir->d_name) ) {
1248
1249                                 SDL_strlcpy(arr[num_files], dir->d_name, MAX_FILENAME_LEN);
1250                                 char *ptr = strrchr(arr[num_files], '.');
1251                                 if ( ptr ) {
1252                                         *ptr = 0;
1253                                 }
1254
1255                                 if (info)       {
1256                                         info[num_files].write_time = buf.st_mtime;
1257                                 }
1258
1259                                 num_files++;
1260                         }
1261                 }
1262                 closedir(dirp);
1263         }
1264 #else
1265         cf_create_default_path_string( filespec, pathtype, filter );
1266         
1267         int find_handle;
1268         _finddata_t find;
1269         
1270         find_handle = _findfirst( filespec, &find );
1271         if (find_handle != -1) {
1272                 do {
1273                         if (num_files >= max)
1274                                 break;
1275
1276                         if (!(find.attrib & _A_SUBDIR)) {
1277
1278                                 if ( !Get_file_list_filter || (*Get_file_list_filter)(find.name) ) {
1279
1280                                         SDL_strlcpy(arr[num_files], find.name, MAX_FILENAME_LEN);
1281                                         char *ptr = strrchr(arr[num_files], '.');
1282                                         if ( ptr ) {
1283                                                 *ptr = 0;
1284                                         }
1285
1286                                         if (info)       {
1287                                                 info[num_files].write_time = find.time_write;
1288                                         }
1289
1290                                         num_files++;
1291                                 }
1292                         }
1293
1294                 } while (!_findnext(find_handle, &find));
1295
1296                 _findclose( find_handle );
1297         }
1298 #endif
1299                 
1300
1301         // Search all the packfiles and CD.
1302         if ( !Skip_packfile_search )    {
1303                 for (i=0; i<Num_files; i++ )    {
1304                         cf_file * f = cf_get_file(i);
1305
1306                         // only search paths we're supposed to...
1307                         if ( (pathtype != CF_TYPE_ANY) && (pathtype != f->pathtype_index)  )    {
1308                                 continue;
1309                         }
1310
1311                         if (num_files >= max)
1312                                 break;
1313
1314                         if ( !cf_matches_spec( filter,f->name_ext))     {
1315                                 continue;
1316                         }
1317
1318                         if ( cf_file_already_in_list_preallocated( num_files, arr, f->name_ext ))       {
1319                                 continue;
1320                         }
1321
1322                         if ( !Get_file_list_filter || (*Get_file_list_filter)(f->name_ext) ) {
1323
1324                                 //mprintf(( "Found '%s' in root %d path %d\n", f->name_ext, f->root_index, f->pathtype_index ));
1325
1326                                 SDL_strlcpy(arr[num_files], f->name_ext, MAX_FILENAME_LEN);
1327                                 char *ptr = strrchr(arr[num_files], '.');
1328                                 if ( ptr ) {
1329                                         *ptr = 0;
1330                                 }
1331
1332                                 if (info)       {
1333                                         info[num_files].write_time = f->write_time;
1334                                 }
1335
1336                                 num_files++;
1337                         }
1338
1339                 }
1340         }
1341
1342         if (sort != CF_SORT_NONE) {
1343                 SDL_assert(list);
1344                 cf_sort_filenames( num_files, list, sort, info );
1345         }
1346
1347         if (own_flag)   {
1348                 free(info);
1349         }
1350
1351         Get_file_list_filter = NULL;
1352         return num_files;
1353 }
1354
1355 // Returns the default storage path for files given a 
1356 // particular pathtype.   In other words, the path to 
1357 // the unpacked, non-cd'd, stored on hard drive path.
1358 // If filename isn't null it will also tack the filename
1359 // on the end, creating a completely valid filename.
1360 // Input:   pathtype  - CF_TYPE_??
1361 //          filename  - optional, if set, tacks the filename onto end of path.
1362 // Output:  path      - Fully qualified pathname.
1363 void cf_create_default_path_string( char *path, int pathtype, const char *filename, bool localize )
1364 {
1365 #ifdef PLAT_UNIX
1366         const char *toks = "/";
1367 #else
1368         const char *toks = "/\\:";
1369 #endif
1370
1371         if ( filename && strpbrk(filename, toks) ) {
1372                 // Already has full path
1373                 SDL_strlcpy( path, filename, MAX_PATH_LEN );
1374         } else {
1375                 if ( cfile_init_paths() ) {
1376                         SDL_strlcpy(path, (filename) ? filename : "", MAX_PATH_LEN);
1377                         return;
1378                 }
1379
1380                 SDL_assert(CF_TYPE_SPECIFIED(pathtype));
1381
1382                 SDL_strlcpy(path, Cfile_user_dir, MAX_PATH_LEN);
1383                 SDL_strlcat(path, Pathtypes[pathtype].path, MAX_PATH_LEN);
1384
1385                 // Don't add slash for root directory
1386                 if (Pathtypes[pathtype].path[0] != '\0') {
1387                         SDL_strlcat(path, DIR_SEPARATOR_STR, MAX_PATH_LEN);
1388                 }
1389
1390                 // add filename
1391                 if (filename) {
1392                         SDL_strlcat(path, filename, MAX_PATH_LEN);
1393
1394                         // localize filename
1395                         if (localize) {
1396                                 // create copy of path
1397                                 char temp_path[MAX_PATH_LEN];
1398                                 SDL_strlcpy(temp_path, path, SDL_arraysize(temp_path));
1399
1400                                 // localize the path
1401                                 lcl_add_dir_to_path_with_filename(path, MAX_PATH_LEN);
1402
1403                                 // verify localized path
1404                                 FILE *fp = fopen(path, "rb");
1405                                 if (fp) {
1406                                         fclose(fp);
1407                                 } else {
1408                                         SDL_strlcpy(path, temp_path, SDL_arraysize(temp_path));
1409                                 }
1410                         }
1411                 }
1412         }
1413 }
1414
1415 // returns true if packfile has been indexed by CFILE (case-insensitive search)
1416 bool cf_has_packfile(const char *fn)
1417 {
1418         cf_root *root;
1419         char vp_name[CF_MAX_PATHNAME_LENGTH];
1420         char *p;
1421         int i;
1422
1423         if (fn == NULL) {
1424                 return false;
1425         }
1426
1427         if ( !SDL_strlen(fn) || (SDL_strlen(fn) < 4) ) {
1428                 return false;
1429         }
1430
1431         // add ".vp" if needed
1432         SDL_strlcpy(vp_name, fn, CF_MAX_PATHNAME_LENGTH);
1433
1434         p = &vp_name[SDL_strlen(vp_name)-3];
1435
1436         if ( SDL_strcasecmp(p, ".vp") ) {
1437                 SDL_strlcat(vp_name, ".vp", CF_MAX_PATHNAME_LENGTH);
1438         }
1439
1440         // now see if we have the packfile
1441         for (i = 0; i < Num_roots; i++) {
1442                 root = cf_get_root(i);
1443
1444                 if (root->roottype != CF_ROOTTYPE_PACK) {
1445                         continue;
1446                 }
1447
1448                 p = SDL_strrchr(root->path, DIR_SEPARATOR_CHAR);
1449
1450                 if (p) {
1451                         p++;
1452
1453                         if ( !SDL_strcasecmp(p, vp_name) ) {
1454                                 return true;
1455                         }
1456                 }
1457         }
1458
1459         return false;
1460 }