istereo2

annotate src/respath.c @ 15:7bd4264bf74a

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