From: Mikko Rasa Date: Sun, 31 Jan 2016 15:29:52 +0000 (+0200) Subject: Use Core Foundation to discover standard locations on OS X X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=8e613903920ee006c2c84e1af5620d92fbfb8957;p=libs%2Fcore.git Use Core Foundation to discover standard locations on OS X --- diff --git a/Build b/Build index d940f4a..12e3114 100644 --- a/Build +++ b/Build @@ -24,6 +24,14 @@ package "mspcore" }; }; + if_arch "darwin" + { + build_info + { + library "Foundation.framework"; + }; + }; + feature "zlib" "Support compression with zlib" { default "yes"; diff --git a/source/fs/osx/cfdir.m b/source/fs/osx/cfdir.m new file mode 100644 index 0000000..4dd4b4b --- /dev/null +++ b/source/fs/osx/cfdir.m @@ -0,0 +1,23 @@ +#import + +unsigned get_home_dir(char *buf, unsigned size) +{ + NSString *path = NSHomeDirectory(); + if(![path getCString:buf maxLength:size encoding:NSUTF8StringEncoding]) + return 0; + + return [path lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; +} + +unsigned get_application_support_dir(char *buf, unsigned size) +{ + NSArray *dirs = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES); + if(![dirs count]) + return 0; + + NSString *path = [dirs objectAtIndex:0]; + if(![path getCString:buf maxLength:size encoding:NSUTF8StringEncoding]) + return 0; + + return [path lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; +} diff --git a/source/fs/osx/dir_location.cpp b/source/fs/osx/dir_location.cpp new file mode 100644 index 0000000..7ce89f7 --- /dev/null +++ b/source/fs/osx/dir_location.cpp @@ -0,0 +1,35 @@ +#include "dir.h" + +using namespace std; + +extern "C" unsigned get_home_dir(char *, unsigned); +extern "C" unsigned get_application_support_dir(char *, unsigned); + +namespace Msp { +namespace FS { + +Path get_home_dir() +{ + char buf[1024]; + unsigned len = ::get_home_dir(buf, sizeof(buf)); + if(len) + return string(buf, len); + + const char *home = getenv("HOME"); + if(home) + return home; + + return "."; +} + +Path get_user_data_dir(const string &appname) +{ + char buf[1024]; + unsigned len = get_application_support_dir(buf, sizeof(buf)); + if(len) + return Path(string(buf, len))/appname; + return get_home_dir()/"Library"/"Application Support"/appname; +} + +} // namespace FS +} // namespace Msp