istereo

annotate src/respath.c @ 39:ff055bff6a15

copyright statements and stuff
author John Tsiombikas <nuclear@mutantstargoat.com>
date Sun, 11 Sep 2011 09:03:18 +0300
parents fd39c0198935
children
rev   line source
nuclear@39 1 /*
nuclear@39 2 Stereoscopic tunnel for iOS.
nuclear@39 3 Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org>
nuclear@39 4
nuclear@39 5 This program is free software: you can redistribute it and/or modify
nuclear@39 6 it under the terms of the GNU General Public License as published by
nuclear@39 7 the Free Software Foundation, either version 3 of the License, or
nuclear@39 8 (at your option) any later version.
nuclear@39 9
nuclear@39 10 This program is distributed in the hope that it will be useful,
nuclear@39 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@39 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@39 13 GNU General Public License for more details.
nuclear@39 14
nuclear@39 15 You should have received a copy of the GNU General Public License
nuclear@39 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
nuclear@39 17 */
nuclear@39 18
nuclear@39 19
nuclear@4 20 #include <stdio.h>
nuclear@4 21 #include <stdlib.h>
nuclear@4 22 #include <string.h>
nuclear@4 23 #include <errno.h>
nuclear@4 24 #include <unistd.h>
nuclear@4 25 #include "respath.h"
nuclear@15 26 #include "config.h"
nuclear@4 27
nuclear@15 28 #ifdef IPHONE
nuclear@4 29 #include <CoreFoundation/CoreFoundation.h>
nuclear@4 30 #endif
nuclear@4 31
nuclear@4 32
nuclear@4 33 #ifndef IPHONE
nuclear@4 34 struct path_node {
nuclear@4 35 char *path;
nuclear@4 36 struct path_node *next;
nuclear@4 37 };
nuclear@4 38
nuclear@4 39 static struct path_node *pathlist;
nuclear@4 40
nuclear@4 41 void add_resource_path(const char *path)
nuclear@4 42 {
nuclear@4 43 struct path_node *node = 0;
nuclear@4 44
nuclear@4 45 if(!(node = malloc(sizeof *node)) || !(node->path = malloc(strlen(path) + 1))) {
nuclear@4 46 free(node);
nuclear@4 47 fprintf(stderr, "failed to add path: %s: %s\n", path, strerror(errno));
nuclear@4 48 return;
nuclear@4 49 }
nuclear@4 50 strcpy(node->path, path);
nuclear@4 51 node->next = pathlist;
nuclear@4 52 pathlist = node;
nuclear@4 53 }
nuclear@4 54
nuclear@4 55 char *find_resource(const char *fname, char *path, size_t sz)
nuclear@4 56 {
nuclear@4 57 static char buffer[1024];
nuclear@4 58 struct path_node *node;
nuclear@4 59
nuclear@4 60 if(!path) {
nuclear@4 61 path = buffer;
nuclear@4 62 sz = sizeof buffer;
nuclear@4 63 }
nuclear@4 64
nuclear@4 65 node = pathlist;
nuclear@4 66 while(node) {
nuclear@4 67 snprintf(path, sz, "%s/%s", node->path, fname);
nuclear@4 68 if(access(path, F_OK) != -1) {
nuclear@4 69 return path;
nuclear@4 70 }
nuclear@4 71 node = node->next;
nuclear@4 72 }
nuclear@27 73
nuclear@27 74 fprintf(stderr, "can't find resource: %s\n", fname);
nuclear@4 75 return 0;
nuclear@4 76 }
nuclear@4 77
nuclear@4 78 #else /* IPHONE */
nuclear@4 79
nuclear@4 80 void add_resource_path(const char *path)
nuclear@4 81 {
nuclear@4 82 }
nuclear@4 83
nuclear@4 84
nuclear@4 85 char *find_resource(const char *fname, char *path, size_t sz)
nuclear@4 86 {
nuclear@4 87 static char buffer[1024];
nuclear@4 88 CFBundleRef bundle;
nuclear@4 89 CFURLRef url;
nuclear@4 90 CFStringRef cfname;
nuclear@4 91
nuclear@4 92 cfname = CFStringCreateWithCString(0, fname, kCFStringEncodingASCII);
nuclear@4 93
nuclear@4 94 bundle = CFBundleGetMainBundle();
nuclear@4 95 if(!(url = CFBundleCopyResourceURL(bundle, cfname, 0, 0))) {
nuclear@4 96 return 0;
nuclear@4 97 }
nuclear@4 98
nuclear@4 99 if(!path) {
nuclear@4 100 path = buffer;
nuclear@4 101 sz = sizeof buffer;
nuclear@4 102 }
nuclear@4 103
nuclear@4 104 if(!CFURLGetFileSystemRepresentation(url, 1, (unsigned char*)path, sz)) {
nuclear@4 105 return 0;
nuclear@4 106 }
nuclear@4 107 return path;
nuclear@4 108 }
nuclear@4 109 #endif