goat3dgfx

view src/datapath.cc @ 34:3eb6c8f89fe1

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