forked from supercollider/supercollider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSC_StandAloneInfo_Darwin.cpp
81 lines (72 loc) · 2.09 KB
/
SC_StandAloneInfo_Darwin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#if defined(__APPLE__) || defined(SC_IPHONE)
#include <sys/param.h>
#include <stdexcept>
#include <cstring> // for strncpy
#include <mach-o/dyld.h> // for _NSGetExecutablePath
#include <libgen.h>
#include <stdlib.h>
#include <unistd.h>
#include <CoreFoundation/CFString.h>
#include <CoreFoundation/CFBundle.h>
//#include <CoreServices/CoreServices.h>
#include "SC_StandAloneInfo_Darwin.h"
bool SC_StandAloneInfo::haveCheckedBundleStatus;
char SC_StandAloneInfo::dirPath[PATH_MAX];
void SC_StandAloneInfo::SC_StandAloneInfoInit() {
char relDir[PATH_MAX];
CFStringEncoding encoding = kCFStringEncodingASCII;
if(!haveCheckedBundleStatus) {
haveCheckedBundleStatus = true;
CFURLRef enablerURL = CFBundleCopyResourceURL (
CFBundleGetMainBundle(),
CFSTR("SCClassLibrary"),
NULL,
NULL
);
if( !enablerURL ) {
enablerURL = CFBundleCopyResourceURL (
CFBundleGetMainBundle(),
CFSTR("sclang.app"),
NULL,
NULL
);
}
if ( enablerURL ) {
// If sclang or SuperCollider binary is run within the .app bundle,
// this is how we find the Resources path.
CFStringRef rawPath = CFURLCopyFileSystemPath(enablerURL, kCFURLPOSIXPathStyle);
rawPath = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@/.."), rawPath);
CFStringGetCString(rawPath, relDir, PATH_MAX, encoding);
} else {
// when sclang is run from a symlink, the resource URL above will not be found,
// so we need to find the path of the executable.
uint32_t bufsize = PATH_MAX, *bufsizep = &bufsize;
if(_NSGetExecutablePath(relDir, bufsizep)==0) {
realpath(relDir, dirPath); // resolve symlink
char *dir = dirname(dirPath);
strcpy(dirPath, dir);
return;
} else {
// in case it failed, fall back to current directory
getcwd(dirPath, PATH_MAX);
}
}
realpath(relDir, dirPath);
}
}
bool SC_StandAloneInfo::IsStandAlone() {
#ifdef SC_STANDALONE
return true;
#else
return false;
#endif
}
void SC_StandAloneInfo::GetResourceDir(char* pathBuf, int length)
{
if ( !haveCheckedBundleStatus )
{
SC_StandAloneInfoInit();
}
strncpy(pathBuf, dirPath, length);
}
#endif