]> icculus.org git repositories - btb/d2x.git/blob - arch/carbon/findfile.c
make Mac OS 9 Voodoo display textures
[btb/d2x.git] / arch / carbon / findfile.c
1 /*
2  *  findfile.c
3  *  D2X (Descent2)
4  *
5  *  Created by Chris Taylor on Tue Jun 22 2004.
6  *
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include "conf.h"
11 #endif
12
13 #include "pstypes.h"
14 #include "findfile.h"
15 #include "u_mem.h"
16 #include "strutil.h"
17
18 #ifdef macintosh
19 #include <Files.h>
20 extern void macify_posix_path(char *posix_path, char *mac_path);
21 #else
22 #include <CoreServices/CoreServices.h>
23 #endif
24
25 //      Global Variables
26
27 FSIterator              iterator;
28 int                             path_len;
29 FSRef                   *refs;
30 int                             i;
31 char                    type[5];
32 ItemCount               num_new_files;
33
34
35 //      Functions
36
37 int FileFindFirst(char *search_str, FILEFINDSTRUCT *ffstruct)
38 {
39     FSRef                       parent;
40     char                        path[_MAX_PATH];
41     OSStatus            myErr;
42     
43     for (path_len = 0; *search_str && *search_str != '*'; path_len ++)
44         path[path_len] = *search_str++;
45     path[path_len] = 0;
46     if (*search_str == '*')
47         search_str++;
48     strcpy(type, search_str);
49
50 #ifdef macintosh
51     // Convert the search directory to an FSRef in a way that OS 9 can handle
52     {
53         FSSpec          parentSpec;
54         Str255          pascalPath;
55
56         macify_posix_path(path, path);
57         CopyCStringToPascal(path, pascalPath);
58         if (FSMakeFSSpec(0, 0, pascalPath, &parentSpec) != noErr)
59             return 1;
60         if (FSpMakeFSRef(&parentSpec, &parent) != noErr)
61             return 1;
62     }
63 #else
64 // "This function, though available through Carbon on Mac OS 8 and 9, is only implemented on Mac OS X."
65     if ((myErr = FSPathMakeRef((unsigned char const *) (path_len? path : "."), &parent, NULL)) != noErr)
66         return 1;
67 #endif
68     if (FSRefMakePath(&parent, (unsigned char *) path, _MAX_PATH) != noErr)     // Get the full path, to get the length
69         return 1;
70     path_len = strlen(path)
71 #ifndef macintosh
72         + 1 // For the '/'
73 #endif
74         ;
75     
76     if (FSOpenIterator(&parent, kFSIterateFlat, &iterator) != noErr)
77         return 1;
78     
79     MALLOC(refs, FSRef, 256);
80     
81     myErr = FSGetCatalogInfoBulk(iterator, 256, &num_new_files, NULL, kFSCatInfoNone, NULL, refs, NULL, NULL);
82     
83     i = 0;
84     do {
85         char *p = path + path_len;
86         char *matchingType;
87
88         if (i >= num_new_files) {
89             if (!myErr) {
90                 myErr = FSGetCatalogInfoBulk(iterator, 256, &num_new_files, NULL, kFSCatInfoNone, NULL, refs, NULL, NULL);
91                 i = 0;
92             } else
93                 return 1;       // The last file
94         }
95         
96         FSRefMakePath (refs + i, (unsigned char *) path, 255);
97         for (matchingType = p; *matchingType && strnicmp(matchingType, search_str, strlen(search_str)); matchingType++) {}
98
99         i++;
100         if (*matchingType) {
101             strncpy(ffstruct->name, p, 12);
102             return 0;   // Found one
103         }
104     } while (1);
105 }
106
107 int     FileFindNext(FILEFINDSTRUCT *ffstruct)
108 {
109     OSErr myErr;
110     
111     do {
112         char path[_MAX_PATH];
113         char *p = path + path_len;
114         char *matchingType;
115     
116         if (i >= num_new_files) {
117             if (!myErr) {
118                 myErr = FSGetCatalogInfoBulk(iterator, 256, &num_new_files, NULL, kFSCatInfoNone, NULL, refs, NULL, NULL);
119                 i = 0;
120             } else
121                 return 1;       // The last file
122         }
123     
124         FSRefMakePath (refs + i, (unsigned char *) path, 255);
125         for (matchingType = p; *matchingType && strnicmp(matchingType, type, strlen(type)); matchingType++) {}
126     
127         i++;
128         if (*matchingType) {
129             strncpy(ffstruct->name, p, 12);
130             return 0;   // Found one
131         }
132     } while (1);
133 }
134
135 int     FileFindClose(void)
136 {
137     d_free(refs);
138     if (FSCloseIterator(iterator) != noErr)
139         return 1;
140     
141     return 0;
142 }