istereo

diff src/respath.c @ 4:14bbdfcb9030

resource path find code
author John Tsiombikas <nuclear@mutantstargoat.com>
date Wed, 07 Sep 2011 06:25:05 +0300
parents
children 32503603eb7d
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/respath.c	Wed Sep 07 06:25:05 2011 +0300
     1.3 @@ -0,0 +1,88 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <string.h>
     1.7 +#include <errno.h>
     1.8 +#include <unistd.h>
     1.9 +#include "respath.h"
    1.10 +
    1.11 +#if defined(__IPHONE_3_0) || defined(__IPHONE_3_2) || defined(__IPHONE_4_0)
    1.12 +#define IPHONE
    1.13 +#include <CoreFoundation/CoreFoundation.h>
    1.14 +#endif
    1.15 +
    1.16 +
    1.17 +#ifndef IPHONE
    1.18 +struct path_node {
    1.19 +	char *path;
    1.20 +	struct path_node *next;
    1.21 +};
    1.22 +
    1.23 +static struct path_node *pathlist;
    1.24 +
    1.25 +void add_resource_path(const char *path)
    1.26 +{
    1.27 +	struct path_node *node = 0;
    1.28 +
    1.29 +	if(!(node = malloc(sizeof *node)) || !(node->path = malloc(strlen(path) + 1))) {
    1.30 +		free(node);
    1.31 +		fprintf(stderr, "failed to add path: %s: %s\n", path, strerror(errno));
    1.32 +		return;
    1.33 +	}
    1.34 +	strcpy(node->path, path);
    1.35 +	node->next = pathlist;
    1.36 +	pathlist = node;
    1.37 +}
    1.38 +
    1.39 +char *find_resource(const char *fname, char *path, size_t sz)
    1.40 +{
    1.41 +	static char buffer[1024];
    1.42 +	struct path_node *node;
    1.43 +
    1.44 +	if(!path) {
    1.45 +		path = buffer;
    1.46 +		sz = sizeof buffer;
    1.47 +	}
    1.48 +
    1.49 +	node = pathlist;
    1.50 +	while(node) {
    1.51 +		snprintf(path, sz, "%s/%s", node->path, fname);
    1.52 +		if(access(path, F_OK) != -1) {
    1.53 +			return path;
    1.54 +		}
    1.55 +		node = node->next;
    1.56 +	}
    1.57 +	return 0;
    1.58 +}
    1.59 +
    1.60 +#else	/* IPHONE */
    1.61 +
    1.62 +void add_resource_path(const char *path)
    1.63 +{
    1.64 +}
    1.65 +
    1.66 +
    1.67 +char *find_resource(const char *fname, char *path, size_t sz)
    1.68 +{
    1.69 +	static char buffer[1024];
    1.70 +	CFBundleRef bundle;
    1.71 +	CFURLRef url;
    1.72 +	CFStringRef cfname;
    1.73 +
    1.74 +	cfname = CFStringCreateWithCString(0, fname, kCFStringEncodingASCII);
    1.75 +
    1.76 +	bundle = CFBundleGetMainBundle();
    1.77 +	if(!(url = CFBundleCopyResourceURL(bundle, cfname, 0, 0))) {
    1.78 +		return 0;
    1.79 +	}
    1.80 +
    1.81 +	if(!path) {
    1.82 +		path = buffer;
    1.83 +		sz = sizeof buffer;
    1.84 +	}
    1.85 +
    1.86 +	if(!CFURLGetFileSystemRepresentation(url, 1, (unsigned char*)path, sz)) {
    1.87 +		return 0;
    1.88 +	}
    1.89 +	return path;
    1.90 +}
    1.91 +#endif