goat3dgfx

diff src/datapath.cc @ 0:1873dfd13f2d

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 14 Nov 2013 05:27:09 +0200
parents
children 7d6b667821cf
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/datapath.cc	Thu Nov 14 05:27:09 2013 +0200
     1.3 @@ -0,0 +1,67 @@
     1.4 +#include <stdio.h>
     1.5 +#include <set>
     1.6 +#include <string>
     1.7 +#include "logger.h"
     1.8 +#include "datapath.h"
     1.9 +
    1.10 +static std::set<std::string> paths;
    1.11 +
    1.12 +void add_data_path(const char *path)
    1.13 +{
    1.14 +	paths.insert(path);
    1.15 +}
    1.16 +
    1.17 +#ifndef TARGET_IPHONE
    1.18 +std::string datafile_path(const char *fname)
    1.19 +{
    1.20 +	std::string res;
    1.21 +	if(!fname) {
    1.22 +		return res;
    1.23 +	}
    1.24 +
    1.25 +	std::set<std::string>::const_iterator it = paths.begin();
    1.26 +	while(it != paths.end()) {
    1.27 +		const std::string &path = *it++;
    1.28 +		res = path + "/" + std::string(fname);
    1.29 +		FILE *fp = fopen(res.c_str(), "r");
    1.30 +		if(fp) {
    1.31 +			fclose(fp);
    1.32 +			return res;
    1.33 +		}
    1.34 +	}
    1.35 +
    1.36 +	// It's not found. Return the name itself just in case it's right here
    1.37 +	return std::string(fname);
    1.38 +}
    1.39 +#else
    1.40 +#include <CoreFoundation/CoreFoundation.h>
    1.41 +
    1.42 +std::string datafile_path(const char *fname)
    1.43 +{
    1.44 +	std::string res;
    1.45 +	if(!fname) {
    1.46 +		return res;
    1.47 +	}
    1.48 +
    1.49 +	CFBundleRef bundle;
    1.50 +	CFURLRef url;
    1.51 +	CFStringRef cfname;
    1.52 +
    1.53 +	cfname = CFStringCreateWithCString(0, fname, kCFStringEncodingASCII);
    1.54 +
    1.55 +	bundle = CFBundleGetMainBundle();
    1.56 +	if(!(url = CFBundleCopyResourceURL(bundle, cfname, 0, 0))) {
    1.57 +		CFRelease(cfname);
    1.58 +		return fname;
    1.59 +	}
    1.60 +	CFRelease(cfname);
    1.61 +
    1.62 +	char path[1024];
    1.63 +	if(!CFURLGetFileSystemRepresentation(url, 1, (unsigned char*)path, sizeof path)) {
    1.64 +		CFRelease(url);
    1.65 +		return fname;
    1.66 +	}
    1.67 +	CFRelease(url);
    1.68 +	return std::string(path);
    1.69 +}
    1.70 +#endif