1 //===-- CFBundle.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 //  Created by Greg Clayton on 1/16/08.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "CFBundle.h"
14 #include "CFString.h"
15 
16 //----------------------------------------------------------------------
17 // CFBundle constructor
18 //----------------------------------------------------------------------
19 CFBundle::CFBundle(const char *path)
20     : CFReleaser<CFBundleRef>(), m_bundle_url() {
21   if (path && path[0])
22     SetPath(path);
23 }
24 
25 //----------------------------------------------------------------------
26 // CFBundle copy constructor
27 //----------------------------------------------------------------------
28 CFBundle::CFBundle(const CFBundle &rhs)
29     : CFReleaser<CFBundleRef>(rhs), m_bundle_url(rhs.m_bundle_url) {}
30 
31 //----------------------------------------------------------------------
32 // CFBundle copy constructor
33 //----------------------------------------------------------------------
34 CFBundle &CFBundle::operator=(const CFBundle &rhs) {
35   if (this != &rhs)
36     *this = rhs;
37   return *this;
38 }
39 
40 //----------------------------------------------------------------------
41 // Destructor
42 //----------------------------------------------------------------------
43 CFBundle::~CFBundle() {}
44 
45 //----------------------------------------------------------------------
46 // Set the path for a bundle by supplying a
47 //----------------------------------------------------------------------
48 bool CFBundle::SetPath(const char *path) {
49   CFAllocatorRef alloc = kCFAllocatorDefault;
50   // Release our old bundle and ULR
51   reset(); // This class is a CFReleaser<CFBundleRef>
52   m_bundle_url.reset();
53   // Make a CFStringRef from the supplied path
54   CFString cf_path;
55   cf_path.SetFileSystemRepresentation(path);
56   if (cf_path.get()) {
57     // Make our Bundle URL
58     m_bundle_url.reset(::CFURLCreateWithFileSystemPath(
59         alloc, cf_path.get(), kCFURLPOSIXPathStyle, true));
60     if (m_bundle_url.get()) {
61       reset(::CFBundleCreate(alloc, m_bundle_url.get()));
62     }
63   }
64   return get() != NULL;
65 }
66 
67 CFStringRef CFBundle::GetIdentifier() const {
68   CFBundleRef bundle = get();
69   if (bundle != NULL)
70     return ::CFBundleGetIdentifier(bundle);
71   return NULL;
72 }
73 
74 CFURLRef CFBundle::CopyExecutableURL() const {
75   CFBundleRef bundle = get();
76   if (bundle != NULL)
77     return CFBundleCopyExecutableURL(bundle);
78   return NULL;
79 }
80