xref: /llvm-project/lldb/source/Host/macosx/cfcpp/CFCBundle.cpp (revision 2946cd701067404b99c39fb29dc9c74bd7193eb3)
1 //===-- CFCBundle.cpp -------------------------------------------*- C++ -*-===//
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 //----------------------------------------------------------------------
13 // CFCBundle constructor
14 //----------------------------------------------------------------------
15 CFCBundle::CFCBundle(const char *path) : CFCReleaser<CFBundleRef>() {
16   if (path && path[0])
17     SetPath(path);
18 }
19 
20 CFCBundle::CFCBundle(CFURLRef url)
21     : CFCReleaser<CFBundleRef>(url ? CFBundleCreate(NULL, url) : NULL) {}
22 
23 //----------------------------------------------------------------------
24 // Destructor
25 //----------------------------------------------------------------------
26 CFCBundle::~CFCBundle() {}
27 
28 //----------------------------------------------------------------------
29 // Set the path for a bundle by supplying a
30 //----------------------------------------------------------------------
31 bool CFCBundle::SetPath(const char *path) {
32   CFAllocatorRef alloc = kCFAllocatorDefault;
33   // Release our old bundle and URL
34   reset();
35 
36   // Make a CFStringRef from the supplied path
37   CFCString cf_path;
38   cf_path.SetFileSystemRepresentation(path);
39   if (cf_path.get()) {
40     // Make our Bundle URL
41     CFCReleaser<CFURLRef> bundle_url(::CFURLCreateWithFileSystemPath(
42         alloc, cf_path.get(), kCFURLPOSIXPathStyle, true));
43     if (bundle_url.get())
44       reset(::CFBundleCreate(alloc, bundle_url.get()));
45   }
46   return get() != NULL;
47 }
48 
49 bool CFCBundle::GetPath(char *dst, size_t dst_len) {
50   CFBundleRef bundle = get();
51   if (bundle) {
52     CFCReleaser<CFURLRef> bundle_url(CFBundleCopyBundleURL(bundle));
53     if (bundle_url.get()) {
54       Boolean resolveAgainstBase = 0;
55       return ::CFURLGetFileSystemRepresentation(bundle_url.get(),
56                                                 resolveAgainstBase,
57                                                 (UInt8 *)dst, dst_len) != 0;
58     }
59   }
60   return false;
61 }
62 
63 CFStringRef CFCBundle::GetIdentifier() const {
64   CFBundleRef bundle = get();
65   if (bundle != NULL)
66     return ::CFBundleGetIdentifier(bundle);
67   return NULL;
68 }
69 
70 CFTypeRef CFCBundle::GetValueForInfoDictionaryKey(CFStringRef key) const {
71   CFBundleRef bundle = get();
72   if (bundle != NULL)
73     return ::CFBundleGetValueForInfoDictionaryKey(bundle, key);
74   return NULL;
75 }
76 
77 CFURLRef CFCBundle::CopyExecutableURL() const {
78   CFBundleRef bundle = get();
79   if (bundle != NULL)
80     return CFBundleCopyExecutableURL(bundle);
81   return NULL;
82 }
83