]> icculus.org git repositories - btb/d2x.git/blob - arch/carbon/findfile.c
New file for supporting compilation with MPW for carbon (Mac OS 9)
[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 // Gets the names of matching files and the number of matching files.
26 int FindFiles(char *search_str, int max_matches, char **names)
27 {
28     FSRef                       parent;
29     char                        path[_MAX_PATH];
30     int                         path_len;
31     FSRef                       *refs;
32     ItemCount           num_found = 0;
33     FSIterator          iterator;
34     int                         i;
35     OSStatus            myErr;
36     
37     for (path_len = 0; *search_str && *search_str != '*'; path_len ++)
38         path[path_len] = *search_str++;
39     path[path_len] = 0;
40     if (*search_str == '*')
41         search_str++;
42
43 #ifdef macintosh
44     // Convert the search directory to an FSRef in a way that OS 9 can handle
45     {
46         FSSpec          parentSpec;
47         Str255          pascalPath;
48
49         macify_posix_path(path, path);
50         CopyCStringToPascal(path, pascalPath);
51         if (FSMakeFSSpec(0, 0, pascalPath, &parentSpec) != noErr)
52             return 0;
53         if (FSpMakeFSRef(&parentSpec, &parent) != noErr)
54             return 0;
55     }
56 #else
57 // "This function, though available through Carbon on Mac OS 8 and 9, is only implemented on Mac OS X."
58     if ((myErr = FSPathMakeRef((unsigned char const *) (path_len? path : "."), &parent, NULL)) != noErr)
59         return 0;
60 #endif
61     if (FSRefMakePath(&parent, (unsigned char *) path, _MAX_PATH) != noErr)     // Get the full path, to get the length
62         return 0;
63     path_len = strlen(path)
64 #ifndef macintosh
65         + 1 // For the '/'
66 #endif
67         ;
68     
69     if (FSOpenIterator(&parent, kFSIterateFlat, &iterator) != noErr)
70         return 0;
71     
72     MALLOC(refs, FSRef, max_matches);
73     
74     do {
75         ItemCount num_new_files;
76         
77         myErr = FSGetCatalogInfoBulk(iterator, max_matches, &num_new_files, NULL, kFSCatInfoNone, NULL, refs, NULL, NULL);
78         
79         for (i = 0; i < num_new_files; i++) {
80             char *p = path + path_len;
81             char *filename;
82             
83             FSRefMakePath (refs + i, (unsigned char *) path, 255);
84             for (filename = p; *filename && strnicmp(filename, search_str, strlen(search_str)); filename++) {}
85             
86             if (*filename) {
87                 strncpy(*names, p, 12);
88                 num_found++;
89                 names++;
90             }
91         }
92     } while (myErr == noErr && num_found < max_matches);
93     
94     d_free(refs);
95     if (FSCloseIterator(iterator) != noErr)
96         return 0;
97     
98     return num_found;
99 }