xref: /llvm-project/lldb/source/Host/macosx/cfcpp/CFCBundle.cpp (revision fd2433e139f78658e059cf911af8ae735fcda57c)
1 //===-- CFCBundle.cpp -----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "CFCBundle.h"
10 #include "CFCString.h"
11 
12 // CFCBundle constructor
CFCBundle(const char * path)13 CFCBundle::CFCBundle(const char *path) : CFCReleaser<CFBundleRef>() {
14   if (path && path[0])
15     SetPath(path);
16 }
17 
CFCBundle(CFURLRef url)18 CFCBundle::CFCBundle(CFURLRef url)
19     : CFCReleaser<CFBundleRef>(url ? CFBundleCreate(NULL, url) : NULL) {}
20 
21 // Destructor
22 CFCBundle::~CFCBundle() = default;
23 
24 // Set the path for a bundle by supplying a
SetPath(const char * path)25 bool CFCBundle::SetPath(const char *path) {
26   CFAllocatorRef alloc = kCFAllocatorDefault;
27   // Release our old bundle and URL
28   reset();
29 
30   // Make a CFStringRef from the supplied path
31   CFCString cf_path;
32   cf_path.SetFileSystemRepresentation(path);
33   if (cf_path.get()) {
34     // Make our Bundle URL
35     CFCReleaser<CFURLRef> bundle_url(::CFURLCreateWithFileSystemPath(
36         alloc, cf_path.get(), kCFURLPOSIXPathStyle, true));
37     if (bundle_url.get())
38       reset(::CFBundleCreate(alloc, bundle_url.get()));
39   }
40   return get() != NULL;
41 }
42 
GetPath(char * dst,size_t dst_len)43 bool CFCBundle::GetPath(char *dst, size_t dst_len) {
44   CFBundleRef bundle = get();
45   if (bundle) {
46     CFCReleaser<CFURLRef> bundle_url(CFBundleCopyBundleURL(bundle));
47     if (bundle_url.get()) {
48       Boolean resolveAgainstBase = 0;
49       return ::CFURLGetFileSystemRepresentation(bundle_url.get(),
50                                                 resolveAgainstBase,
51                                                 (UInt8 *)dst, dst_len) != 0;
52     }
53   }
54   return false;
55 }
56 
GetIdentifier() const57 CFStringRef CFCBundle::GetIdentifier() const {
58   CFBundleRef bundle = get();
59   if (bundle != NULL)
60     return ::CFBundleGetIdentifier(bundle);
61   return NULL;
62 }
63 
GetValueForInfoDictionaryKey(CFStringRef key) const64 CFTypeRef CFCBundle::GetValueForInfoDictionaryKey(CFStringRef key) const {
65   CFBundleRef bundle = get();
66   if (bundle != NULL)
67     return ::CFBundleGetValueForInfoDictionaryKey(bundle, key);
68   return NULL;
69 }
70 
CopyExecutableURL() const71 CFURLRef CFCBundle::CopyExecutableURL() const {
72   CFBundleRef bundle = get();
73   if (bundle != NULL)
74     return CFBundleCopyExecutableURL(bundle);
75   return NULL;
76 }
77