goat3dgfx

annotate src/datapath.cc @ 15:7d6b667821cf

wrapped everything in the goatgfx namespace
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 30 Nov 2013 20:52:21 +0200
parents 1873dfd13f2d
children
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@0 2 #include <set>
nuclear@0 3 #include <string>
nuclear@0 4 #include "logger.h"
nuclear@0 5 #include "datapath.h"
nuclear@0 6
nuclear@15 7 using namespace goatgfx;
nuclear@15 8
nuclear@0 9 static std::set<std::string> paths;
nuclear@0 10
nuclear@15 11 namespace goatgfx {
nuclear@15 12
nuclear@0 13 void add_data_path(const char *path)
nuclear@0 14 {
nuclear@0 15 paths.insert(path);
nuclear@0 16 }
nuclear@0 17
nuclear@0 18 #ifndef TARGET_IPHONE
nuclear@0 19 std::string datafile_path(const char *fname)
nuclear@0 20 {
nuclear@0 21 std::string res;
nuclear@0 22 if(!fname) {
nuclear@0 23 return res;
nuclear@0 24 }
nuclear@0 25
nuclear@0 26 std::set<std::string>::const_iterator it = paths.begin();
nuclear@0 27 while(it != paths.end()) {
nuclear@0 28 const std::string &path = *it++;
nuclear@0 29 res = path + "/" + std::string(fname);
nuclear@0 30 FILE *fp = fopen(res.c_str(), "r");
nuclear@0 31 if(fp) {
nuclear@0 32 fclose(fp);
nuclear@0 33 return res;
nuclear@0 34 }
nuclear@0 35 }
nuclear@0 36
nuclear@0 37 // It's not found. Return the name itself just in case it's right here
nuclear@0 38 return std::string(fname);
nuclear@0 39 }
nuclear@0 40 #else
nuclear@0 41 #include <CoreFoundation/CoreFoundation.h>
nuclear@0 42
nuclear@0 43 std::string datafile_path(const char *fname)
nuclear@0 44 {
nuclear@0 45 std::string res;
nuclear@0 46 if(!fname) {
nuclear@0 47 return res;
nuclear@0 48 }
nuclear@0 49
nuclear@0 50 CFBundleRef bundle;
nuclear@0 51 CFURLRef url;
nuclear@0 52 CFStringRef cfname;
nuclear@0 53
nuclear@0 54 cfname = CFStringCreateWithCString(0, fname, kCFStringEncodingASCII);
nuclear@0 55
nuclear@0 56 bundle = CFBundleGetMainBundle();
nuclear@0 57 if(!(url = CFBundleCopyResourceURL(bundle, cfname, 0, 0))) {
nuclear@0 58 CFRelease(cfname);
nuclear@0 59 return fname;
nuclear@0 60 }
nuclear@0 61 CFRelease(cfname);
nuclear@0 62
nuclear@0 63 char path[1024];
nuclear@0 64 if(!CFURLGetFileSystemRepresentation(url, 1, (unsigned char*)path, sizeof path)) {
nuclear@0 65 CFRelease(url);
nuclear@0 66 return fname;
nuclear@0 67 }
nuclear@0 68 CFRelease(url);
nuclear@0 69 return std::string(path);
nuclear@0 70 }
nuclear@0 71 #endif
nuclear@15 72
nuclear@15 73 } // namespace goatgfx