Use Core Foundation to discover standard locations on OS X
authorMikko Rasa <tdb@tdb.fi>
Sun, 31 Jan 2016 15:29:52 +0000 (17:29 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 31 Jan 2016 15:29:52 +0000 (17:29 +0200)
Build
source/fs/osx/cfdir.m [new file with mode: 0644]
source/fs/osx/dir_location.cpp [new file with mode: 0644]

diff --git a/Build b/Build
index d940f4a695ce976c886be6341c08d85ed9a5bade..12e3114909efbd16356a498602d166db710ddf1e 100644 (file)
--- 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 (file)
index 0000000..4dd4b4b
--- /dev/null
@@ -0,0 +1,23 @@
+#import <Foundation/NSPathUtilities.h>
+
+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 (file)
index 0000000..7ce89f7
--- /dev/null
@@ -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