From 8e613903920ee006c2c84e1af5620d92fbfb8957 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 31 Jan 2016 17:29:52 +0200 Subject: [PATCH] Use Core Foundation to discover standard locations on OS X --- Build | 8 ++++++++ source/fs/osx/cfdir.m | 23 ++++++++++++++++++++++ source/fs/osx/dir_location.cpp | 35 ++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 source/fs/osx/cfdir.m create mode 100644 source/fs/osx/dir_location.cpp 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 -- 2.43.0