common: win32utils - compile fix
[supercollider.git] / common / SC_StandAloneInfo_Darwin.cpp
blob78f5abced8ee96f9e4b02605ef6f1ef1103e1b62
1 #if defined(__APPLE__) || defined(SC_IPHONE)
3 #include <sys/param.h>
4 #include <stdexcept>
5 #include <cstring> // for strncpy
6 #include <mach-o/dyld.h> // for _NSGetExecutablePath
7 #include <libgen.h>
8 #include <stdlib.h>
10 #include <CoreFoundation/CFString.h>
11 #include <CoreFoundation/CFBundle.h>
12 //#include <CoreServices/CoreServices.h>
14 #include "SC_StandAloneInfo_Darwin.h"
16 bool SC_StandAloneInfo::haveCheckedBundleStatus;
17 char SC_StandAloneInfo::dirPath[PATH_MAX];
19 void SC_StandAloneInfo::SC_StandAloneInfoInit() {
20 char relDir[PATH_MAX];
21 CFStringEncoding encoding = kCFStringEncodingASCII;
22 if(!haveCheckedBundleStatus) {
23 haveCheckedBundleStatus = true;
24 CFURLRef enablerURL = CFBundleCopyResourceURL (
25 CFBundleGetMainBundle(),
26 CFSTR("SCClassLibrary"),
27 NULL,
28 NULL
30 if ( enablerURL ) {
31 // If sclang or SuperCollider binary is run within the .app bundle,
32 // this is how we find the Resources path.
33 CFStringRef rawPath = CFURLCopyFileSystemPath(enablerURL, kCFURLPOSIXPathStyle);
34 rawPath = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@/.."), rawPath);
35 CFStringGetCString(rawPath, relDir, PATH_MAX, encoding);
36 } else {
37 // when sclang is run from a symlink, the resource URL above will not be found,
38 // so we need to find the path of the executable.
39 uint32_t *bufsize;
40 *bufsize = PATH_MAX;
41 if(_NSGetExecutablePath(relDir, bufsize)==0) {
42 realpath(relDir, dirPath); // resolve symlink
43 char *dir = dirname(dirPath);
44 strcpy(dirPath, dir);
45 return;
46 } else {
47 // in case it failed, fall back to current directory
48 getcwd(dirPath, PATH_MAX);
51 realpath(relDir, dirPath);
55 bool SC_StandAloneInfo::IsStandAlone() {
56 #ifdef SC_STANDALONE
57 return true;
58 #else
59 return false;
60 #endif
63 void SC_StandAloneInfo::GetResourceDir(char* pathBuf, int length)
65 if ( !haveCheckedBundleStatus )
67 SC_StandAloneInfoInit();
69 strncpy(pathBuf, dirPath, length);
72 #endif