vrshoot
annotate src/datapath.cc @ 2:334d17aed7de
visual studio project files
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 02 Feb 2014 18:36:38 +0200 |
parents | |
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@0 | 7 static std::set<std::string> paths; |
nuclear@0 | 8 |
nuclear@0 | 9 void add_data_path(const char *path) |
nuclear@0 | 10 { |
nuclear@0 | 11 paths.insert(path); |
nuclear@0 | 12 } |
nuclear@0 | 13 |
nuclear@0 | 14 #ifndef TARGET_IPHONE |
nuclear@0 | 15 std::string datafile_path(const char *fname) |
nuclear@0 | 16 { |
nuclear@0 | 17 std::string res; |
nuclear@0 | 18 if(!fname) { |
nuclear@0 | 19 return res; |
nuclear@0 | 20 } |
nuclear@0 | 21 |
nuclear@0 | 22 std::set<std::string>::const_iterator it = paths.begin(); |
nuclear@0 | 23 while(it != paths.end()) { |
nuclear@0 | 24 const std::string &path = *it++; |
nuclear@0 | 25 res = path + "/" + std::string(fname); |
nuclear@0 | 26 FILE *fp = fopen(res.c_str(), "r"); |
nuclear@0 | 27 if(fp) { |
nuclear@0 | 28 fclose(fp); |
nuclear@0 | 29 return res; |
nuclear@0 | 30 } |
nuclear@0 | 31 } |
nuclear@0 | 32 |
nuclear@0 | 33 // It's not found. Return the name itself just in case it's right here |
nuclear@0 | 34 return std::string(fname); |
nuclear@0 | 35 } |
nuclear@0 | 36 #else |
nuclear@0 | 37 #include <CoreFoundation/CoreFoundation.h> |
nuclear@0 | 38 |
nuclear@0 | 39 std::string datafile_path(const char *fname) |
nuclear@0 | 40 { |
nuclear@0 | 41 std::string res; |
nuclear@0 | 42 if(!fname) { |
nuclear@0 | 43 return res; |
nuclear@0 | 44 } |
nuclear@0 | 45 |
nuclear@0 | 46 CFBundleRef bundle; |
nuclear@0 | 47 CFURLRef url; |
nuclear@0 | 48 CFStringRef cfname; |
nuclear@0 | 49 |
nuclear@0 | 50 cfname = CFStringCreateWithCString(0, fname, kCFStringEncodingASCII); |
nuclear@0 | 51 |
nuclear@0 | 52 bundle = CFBundleGetMainBundle(); |
nuclear@0 | 53 if(!(url = CFBundleCopyResourceURL(bundle, cfname, 0, 0))) { |
nuclear@0 | 54 CFRelease(cfname); |
nuclear@0 | 55 return fname; |
nuclear@0 | 56 } |
nuclear@0 | 57 CFRelease(cfname); |
nuclear@0 | 58 |
nuclear@0 | 59 char path[1024]; |
nuclear@0 | 60 if(!CFURLGetFileSystemRepresentation(url, 1, (unsigned char*)path, sizeof path)) { |
nuclear@0 | 61 CFRelease(url); |
nuclear@0 | 62 return fname; |
nuclear@0 | 63 } |
nuclear@0 | 64 CFRelease(url); |
nuclear@0 | 65 return std::string(path); |
nuclear@0 | 66 } |
nuclear@0 | 67 #endif |