]> icculus.org git repositories - btb/d2x.git/blob - arch/carbon/findfile.c
don't allow MPW build to run in OS X (it won't work and doesn't need to)
[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
87         if (i >= num_new_files) {
88             if (!myErr) {
89                 myErr = FSGetCatalogInfoBulk(iterator, 256, &num_new_files, NULL, kFSCatInfoNone, NULL, refs, NULL, NULL);
90                 i = 0;
91             } else
92                 return 1;       // The last file
93         }
94         
95         FSRefMakePath (refs + i, (unsigned char *) path, 255);
96
97         i++;
98         if (!stricmp(p + strlen(p) - strlen(search_str), search_str)) {
99             strncpy(ffstruct->name, p, 256);
100             return 0;   // Found one
101         }
102     } while (1);
103 }
104
105 int     FileFindNext(FILEFINDSTRUCT *ffstruct)
106 {
107     OSErr myErr;
108     
109     do {
110         char path[_MAX_PATH];
111         char *p = path + path_len;
112     
113         if (i >= num_new_files) {
114             if (!myErr) {
115                 myErr = FSGetCatalogInfoBulk(iterator, 256, &num_new_files, NULL, kFSCatInfoNone, NULL, refs, NULL, NULL);
116                 i = 0;
117             } else
118                 return 1;       // The last file
119         }
120     
121         FSRefMakePath (refs + i, (unsigned char *) path, 255);
122     
123         i++;
124         if (!stricmp(p + strlen(p) - strlen(type), type)) {
125             strncpy(ffstruct->name, p, 256);
126             return 0;   // Found one
127         }
128     } while (1);
129 }
130
131 int     FileFindClose(void)
132 {
133     d_free(refs);
134     if (FSCloseIterator(iterator) != noErr)
135         return 1;
136     
137     return 0;
138 }