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