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