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