istereo
view src/respath.c @ 21:75a63f9ab7cc
ha!
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Wed, 07 Sep 2011 10:49:11 +0300 |
parents | 14bbdfcb9030 |
children | fd39c0198935 |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <unistd.h>
6 #include "respath.h"
7 #include "config.h"
9 #ifdef IPHONE
10 #include <CoreFoundation/CoreFoundation.h>
11 #endif
14 #ifndef IPHONE
15 struct path_node {
16 char *path;
17 struct path_node *next;
18 };
20 static struct path_node *pathlist;
22 void add_resource_path(const char *path)
23 {
24 struct path_node *node = 0;
26 if(!(node = malloc(sizeof *node)) || !(node->path = malloc(strlen(path) + 1))) {
27 free(node);
28 fprintf(stderr, "failed to add path: %s: %s\n", path, strerror(errno));
29 return;
30 }
31 strcpy(node->path, path);
32 node->next = pathlist;
33 pathlist = node;
34 }
36 char *find_resource(const char *fname, char *path, size_t sz)
37 {
38 static char buffer[1024];
39 struct path_node *node;
41 if(!path) {
42 path = buffer;
43 sz = sizeof buffer;
44 }
46 node = pathlist;
47 while(node) {
48 snprintf(path, sz, "%s/%s", node->path, fname);
49 if(access(path, F_OK) != -1) {
50 return path;
51 }
52 node = node->next;
53 }
54 return 0;
55 }
57 #else /* IPHONE */
59 void add_resource_path(const char *path)
60 {
61 }
64 char *find_resource(const char *fname, char *path, size_t sz)
65 {
66 static char buffer[1024];
67 CFBundleRef bundle;
68 CFURLRef url;
69 CFStringRef cfname;
71 cfname = CFStringCreateWithCString(0, fname, kCFStringEncodingASCII);
73 bundle = CFBundleGetMainBundle();
74 if(!(url = CFBundleCopyResourceURL(bundle, cfname, 0, 0))) {
75 return 0;
76 }
78 if(!path) {
79 path = buffer;
80 sz = sizeof buffer;
81 }
83 if(!CFURLGetFileSystemRepresentation(url, 1, (unsigned char*)path, sz)) {
84 return 0;
85 }
86 return path;
87 }
88 #endif