2 SuperColliderAU Copyright (c) 2006 Gerard Roma.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "Resources.h"
20 #include <sys/syslimits.h>
21 #include <CoreFoundation/CFURL.h>
22 #include <CoreFoundation/CFURLAccess.h>
23 #include <CoreFoundation/CFBundle.h>
24 #include <CoreFoundation/CFPropertyList.h>
27 Resources::Resources(){
28 SC_PLUGIN_PATH
= "/plugins";
29 SC_SYNTHDEF_PATH
= "/synthdefs";
30 PLUGIN_SPEC_FILE
= "/pluginSpec.plist";
31 SERVER_CONFIG_FILE
= "/serverConfig.plist";
34 CFStringRef
Resources::getResourcesPathFromDyldImage(){
35 const mach_header
* header
;
36 header
= (mach_header
*)&_mh_bundle_header
;
37 const char* image_name
= 0;
38 char buffer
[PATH_MAX
];
40 int cnt
= _dyld_image_count();
41 for (int i
= 1; i
< cnt
; i
++)
43 if (_dyld_get_image_header((unsigned long)i
) == header
)
45 image_name
= _dyld_get_image_name(i
);
50 CFURLRef executableURL
= CFURLCreateFromFileSystemRepresentation (kCFAllocatorDefault
, (const unsigned char*)image_name
, strlen (image_name
), false);
51 CFURLRef bundleContentsMacOSURL
= CFURLCreateCopyDeletingLastPathComponent (kCFAllocatorDefault
, executableURL
);
52 CFRelease (executableURL
);
53 CFURLRef bundleContentsURL
= CFURLCreateCopyDeletingLastPathComponent (kCFAllocatorDefault
, bundleContentsMacOSURL
);
54 CFRelease (bundleContentsMacOSURL
);
55 CFURLRef bundleURL
= CFURLCreateCopyDeletingLastPathComponent (kCFAllocatorDefault
, bundleContentsURL
);
56 CFRelease (bundleContentsURL
);
57 CFBundleRef bundle
= CFBundleCreate (kCFAllocatorDefault
, bundleURL
);
58 CFURLRef bundleResourcesURL
= CFBundleCopyResourcesDirectoryURL(bundle
);
59 CFURLGetFileSystemRepresentation(bundleResourcesURL
, TRUE
, (UInt8
*)buffer
,PATH_MAX
);
60 CFStringRef path
= CFStringCreateWithCString(NULL
,buffer
, kCFStringEncodingUTF8
);
65 CFStringRef
Resources::getResourcesPathFromBundleId() { //@@TODO
66 char buffer
[PATH_MAX
];
67 CFBundleRef bundle
= CFBundleGetBundleWithIdentifier(CFSTR("SuperColldierAU") );
68 if (bundle
== NULL
) return NULL
;
69 CFURLRef bundleURL
= CFBundleCopyBundleURL(bundle
);
70 CFURLGetFileSystemRepresentation(bundleURL
, TRUE
, (UInt8
*)buffer
,PATH_MAX
);
71 CFStringRef bundlePath
= CFStringCreateWithCString(NULL
,buffer
, kCFStringEncodingUTF8
);
72 CFURLRef bundleResourcesURL
= CFBundleCopyResourcesDirectoryURL(bundle
);
73 CFStringRef resourcesRelativePath
= CFURLGetString(bundleResourcesURL
);
74 CFMutableStringRef resourcesPath
= CFStringCreateMutable(NULL
,0);
75 CFStringAppend(resourcesPath
,bundlePath
);
76 CFStringAppend(resourcesPath
,resourcesRelativePath
);
81 CFStringRef
Resources::getResourcePath(const char* resource
){
82 CFMutableStringRef path
= CFStringCreateMutable(NULL
,0);
83 CFStringAppend(path
,getResourcesPathFromDyldImage());
84 CFStringAppend(path
, CFStringCreateWithCString(NULL
,resource
, kCFStringEncodingUTF8
));
90 CFPropertyListRef
Resources::getPropertyList(const char* filename
)
92 CFDataRef xmlCFDataRef
;
95 CFPropertyListRef myCFPropertyListRef
= NULL
;
97 char filePathBuf
[PATH_MAX
];
98 CFStringRef filePath
= this->getResourcePath(filename
);
99 CFStringGetCString(filePath
, filePathBuf
, sizeof(filePathBuf
), kCFStringEncodingUTF8
);
100 CFURLRef fileURL
= CFURLCreateFromFileSystemRepresentation (kCFAllocatorDefault
, (const unsigned char*)filePathBuf
, strlen (filePathBuf
), false);
102 readOK
= CFURLCreateDataAndPropertiesFromResource( kCFAllocatorDefault
, fileURL
, &xmlCFDataRef
, NULL
, NULL
, NULL
);
105 myCFPropertyListRef
= CFPropertyListCreateFromXMLData(kCFAllocatorDefault
, xmlCFDataRef
, kCFPropertyListImmutable
, &error
);
107 CFStringGetCString(error
, cerror
, sizeof(cerror
), kCFStringEncodingUTF8
);
108 scprintf("getPropertyList error: %s\n", cerror
);
110 CFRelease(xmlCFDataRef
);
113 scprintf("Couldn't read Plist File %s\n", filePathBuf
);
116 return myCFPropertyListRef
;