istereo

annotate src/respath.c @ 15:32503603eb7d

ah!
author John Tsiombikas <nuclear@mutantstargoat.com>
date Wed, 07 Sep 2011 09:17:15 +0300
parents 14bbdfcb9030
children fd39c0198935
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@4 54 return 0;
nuclear@4 55 }
nuclear@4 56
nuclear@4 57 #else /* IPHONE */
nuclear@4 58
nuclear@4 59 void add_resource_path(const char *path)
nuclear@4 60 {
nuclear@4 61 }
nuclear@4 62
nuclear@4 63
nuclear@4 64 char *find_resource(const char *fname, char *path, size_t sz)
nuclear@4 65 {
nuclear@4 66 static char buffer[1024];
nuclear@4 67 CFBundleRef bundle;
nuclear@4 68 CFURLRef url;
nuclear@4 69 CFStringRef cfname;
nuclear@4 70
nuclear@4 71 cfname = CFStringCreateWithCString(0, fname, kCFStringEncodingASCII);
nuclear@4 72
nuclear@4 73 bundle = CFBundleGetMainBundle();
nuclear@4 74 if(!(url = CFBundleCopyResourceURL(bundle, cfname, 0, 0))) {
nuclear@4 75 return 0;
nuclear@4 76 }
nuclear@4 77
nuclear@4 78 if(!path) {
nuclear@4 79 path = buffer;
nuclear@4 80 sz = sizeof buffer;
nuclear@4 81 }
nuclear@4 82
nuclear@4 83 if(!CFURLGetFileSystemRepresentation(url, 1, (unsigned char*)path, sz)) {
nuclear@4 84 return 0;
nuclear@4 85 }
nuclear@4 86 return path;
nuclear@4 87 }
nuclear@4 88 #endif