rev |
line source |
nuclear@4
|
1 #include <stdio.h>
|
nuclear@4
|
2 #include <stdlib.h>
|
nuclear@4
|
3 #include <string.h>
|
nuclear@4
|
4 #include <errno.h>
|
nuclear@4
|
5 #include <unistd.h>
|
nuclear@4
|
6 #include "respath.h"
|
nuclear@15
|
7 #include "config.h"
|
nuclear@4
|
8
|
nuclear@15
|
9 #ifdef IPHONE
|
nuclear@4
|
10 #include <CoreFoundation/CoreFoundation.h>
|
nuclear@4
|
11 #endif
|
nuclear@4
|
12
|
nuclear@4
|
13
|
nuclear@4
|
14 #ifndef IPHONE
|
nuclear@4
|
15 struct path_node {
|
nuclear@4
|
16 char *path;
|
nuclear@4
|
17 struct path_node *next;
|
nuclear@4
|
18 };
|
nuclear@4
|
19
|
nuclear@4
|
20 static struct path_node *pathlist;
|
nuclear@4
|
21
|
nuclear@4
|
22 void add_resource_path(const char *path)
|
nuclear@4
|
23 {
|
nuclear@4
|
24 struct path_node *node = 0;
|
nuclear@4
|
25
|
nuclear@4
|
26 if(!(node = malloc(sizeof *node)) || !(node->path = malloc(strlen(path) + 1))) {
|
nuclear@4
|
27 free(node);
|
nuclear@4
|
28 fprintf(stderr, "failed to add path: %s: %s\n", path, strerror(errno));
|
nuclear@4
|
29 return;
|
nuclear@4
|
30 }
|
nuclear@4
|
31 strcpy(node->path, path);
|
nuclear@4
|
32 node->next = pathlist;
|
nuclear@4
|
33 pathlist = node;
|
nuclear@4
|
34 }
|
nuclear@4
|
35
|
nuclear@4
|
36 char *find_resource(const char *fname, char *path, size_t sz)
|
nuclear@4
|
37 {
|
nuclear@4
|
38 static char buffer[1024];
|
nuclear@4
|
39 struct path_node *node;
|
nuclear@4
|
40
|
nuclear@4
|
41 if(!path) {
|
nuclear@4
|
42 path = buffer;
|
nuclear@4
|
43 sz = sizeof buffer;
|
nuclear@4
|
44 }
|
nuclear@4
|
45
|
nuclear@4
|
46 node = pathlist;
|
nuclear@4
|
47 while(node) {
|
nuclear@4
|
48 snprintf(path, sz, "%s/%s", node->path, fname);
|
nuclear@4
|
49 if(access(path, F_OK) != -1) {
|
nuclear@4
|
50 return path;
|
nuclear@4
|
51 }
|
nuclear@4
|
52 node = node->next;
|
nuclear@4
|
53 }
|
nuclear@27
|
54
|
nuclear@27
|
55 fprintf(stderr, "can't find resource: %s\n", fname);
|
nuclear@4
|
56 return 0;
|
nuclear@4
|
57 }
|
nuclear@4
|
58
|
nuclear@4
|
59 #else /* IPHONE */
|
nuclear@4
|
60
|
nuclear@4
|
61 void add_resource_path(const char *path)
|
nuclear@4
|
62 {
|
nuclear@4
|
63 }
|
nuclear@4
|
64
|
nuclear@4
|
65
|
nuclear@4
|
66 char *find_resource(const char *fname, char *path, size_t sz)
|
nuclear@4
|
67 {
|
nuclear@4
|
68 static char buffer[1024];
|
nuclear@4
|
69 CFBundleRef bundle;
|
nuclear@4
|
70 CFURLRef url;
|
nuclear@4
|
71 CFStringRef cfname;
|
nuclear@4
|
72
|
nuclear@4
|
73 cfname = CFStringCreateWithCString(0, fname, kCFStringEncodingASCII);
|
nuclear@4
|
74
|
nuclear@4
|
75 bundle = CFBundleGetMainBundle();
|
nuclear@4
|
76 if(!(url = CFBundleCopyResourceURL(bundle, cfname, 0, 0))) {
|
nuclear@4
|
77 return 0;
|
nuclear@4
|
78 }
|
nuclear@4
|
79
|
nuclear@4
|
80 if(!path) {
|
nuclear@4
|
81 path = buffer;
|
nuclear@4
|
82 sz = sizeof buffer;
|
nuclear@4
|
83 }
|
nuclear@4
|
84
|
nuclear@4
|
85 if(!CFURLGetFileSystemRepresentation(url, 1, (unsigned char*)path, sz)) {
|
nuclear@4
|
86 return 0;
|
nuclear@4
|
87 }
|
nuclear@4
|
88 return path;
|
nuclear@4
|
89 }
|
nuclear@4
|
90 #endif
|