1dda28197Spatrick //===-- CFCBundle.cpp -----------------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick
9061da546Spatrick #include "CFCBundle.h"
10061da546Spatrick #include "CFCString.h"
11061da546Spatrick
12061da546Spatrick // CFCBundle constructor
CFCBundle(const char * path)13061da546Spatrick CFCBundle::CFCBundle(const char *path) : CFCReleaser<CFBundleRef>() {
14061da546Spatrick if (path && path[0])
15061da546Spatrick SetPath(path);
16061da546Spatrick }
17061da546Spatrick
CFCBundle(CFURLRef url)18061da546Spatrick CFCBundle::CFCBundle(CFURLRef url)
19061da546Spatrick : CFCReleaser<CFBundleRef>(url ? CFBundleCreate(NULL, url) : NULL) {}
20061da546Spatrick
21061da546Spatrick // Destructor
22*be691f3bSpatrick CFCBundle::~CFCBundle() = default;
23061da546Spatrick
24061da546Spatrick // Set the path for a bundle by supplying a
SetPath(const char * path)25061da546Spatrick bool CFCBundle::SetPath(const char *path) {
26061da546Spatrick CFAllocatorRef alloc = kCFAllocatorDefault;
27061da546Spatrick // Release our old bundle and URL
28061da546Spatrick reset();
29061da546Spatrick
30061da546Spatrick // Make a CFStringRef from the supplied path
31061da546Spatrick CFCString cf_path;
32061da546Spatrick cf_path.SetFileSystemRepresentation(path);
33061da546Spatrick if (cf_path.get()) {
34061da546Spatrick // Make our Bundle URL
35061da546Spatrick CFCReleaser<CFURLRef> bundle_url(::CFURLCreateWithFileSystemPath(
36061da546Spatrick alloc, cf_path.get(), kCFURLPOSIXPathStyle, true));
37061da546Spatrick if (bundle_url.get())
38061da546Spatrick reset(::CFBundleCreate(alloc, bundle_url.get()));
39061da546Spatrick }
40061da546Spatrick return get() != NULL;
41061da546Spatrick }
42061da546Spatrick
GetPath(char * dst,size_t dst_len)43061da546Spatrick bool CFCBundle::GetPath(char *dst, size_t dst_len) {
44061da546Spatrick CFBundleRef bundle = get();
45061da546Spatrick if (bundle) {
46061da546Spatrick CFCReleaser<CFURLRef> bundle_url(CFBundleCopyBundleURL(bundle));
47061da546Spatrick if (bundle_url.get()) {
48061da546Spatrick Boolean resolveAgainstBase = 0;
49061da546Spatrick return ::CFURLGetFileSystemRepresentation(bundle_url.get(),
50061da546Spatrick resolveAgainstBase,
51061da546Spatrick (UInt8 *)dst, dst_len) != 0;
52061da546Spatrick }
53061da546Spatrick }
54061da546Spatrick return false;
55061da546Spatrick }
56061da546Spatrick
GetIdentifier() const57061da546Spatrick CFStringRef CFCBundle::GetIdentifier() const {
58061da546Spatrick CFBundleRef bundle = get();
59061da546Spatrick if (bundle != NULL)
60061da546Spatrick return ::CFBundleGetIdentifier(bundle);
61061da546Spatrick return NULL;
62061da546Spatrick }
63061da546Spatrick
GetValueForInfoDictionaryKey(CFStringRef key) const64061da546Spatrick CFTypeRef CFCBundle::GetValueForInfoDictionaryKey(CFStringRef key) const {
65061da546Spatrick CFBundleRef bundle = get();
66061da546Spatrick if (bundle != NULL)
67061da546Spatrick return ::CFBundleGetValueForInfoDictionaryKey(bundle, key);
68061da546Spatrick return NULL;
69061da546Spatrick }
70061da546Spatrick
CopyExecutableURL() const71061da546Spatrick CFURLRef CFCBundle::CopyExecutableURL() const {
72061da546Spatrick CFBundleRef bundle = get();
73061da546Spatrick if (bundle != NULL)
74061da546Spatrick return CFBundleCopyExecutableURL(bundle);
75061da546Spatrick return NULL;
76061da546Spatrick }
77