istereo

view src/respath.c @ 27:fd39c0198935

normal mapped tunnel
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 08 Sep 2011 08:30:00 +0300
parents 32503603eb7d
children ff055bff6a15
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 }
55 fprintf(stderr, "can't find resource: %s\n", fname);
56 return 0;
57 }
59 #else /* IPHONE */
61 void add_resource_path(const char *path)
62 {
63 }
66 char *find_resource(const char *fname, char *path, size_t sz)
67 {
68 static char buffer[1024];
69 CFBundleRef bundle;
70 CFURLRef url;
71 CFStringRef cfname;
73 cfname = CFStringCreateWithCString(0, fname, kCFStringEncodingASCII);
75 bundle = CFBundleGetMainBundle();
76 if(!(url = CFBundleCopyResourceURL(bundle, cfname, 0, 0))) {
77 return 0;
78 }
80 if(!path) {
81 path = buffer;
82 sz = sizeof buffer;
83 }
85 if(!CFURLGetFileSystemRepresentation(url, 1, (unsigned char*)path, sz)) {
86 return 0;
87 }
88 return path;
89 }
90 #endif