xref: /openbsd-src/gnu/llvm/lldb/tools/debugserver/source/MacOSX/CFBundle.cpp (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1061da546Spatrick //===-- CFBundle.cpp --------------------------------------------*- C++ -*-===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick //
9061da546Spatrick //  Created by Greg Clayton on 1/16/08.
10061da546Spatrick //
11061da546Spatrick //===----------------------------------------------------------------------===//
12061da546Spatrick 
13061da546Spatrick #include "CFBundle.h"
14061da546Spatrick #include "CFString.h"
15061da546Spatrick 
16061da546Spatrick // CFBundle constructor
CFBundle(const char * path)17061da546Spatrick CFBundle::CFBundle(const char *path)
18061da546Spatrick     : CFReleaser<CFBundleRef>(), m_bundle_url() {
19061da546Spatrick   if (path && path[0])
20061da546Spatrick     SetPath(path);
21061da546Spatrick }
22061da546Spatrick 
23061da546Spatrick // CFBundle copy constructor
24*f6aab3d8Srobert CFBundle::CFBundle(const CFBundle &rhs) = default;
25061da546Spatrick 
26061da546Spatrick // CFBundle copy constructor
operator =(const CFBundle & rhs)27061da546Spatrick CFBundle &CFBundle::operator=(const CFBundle &rhs) {
28061da546Spatrick   if (this != &rhs)
29061da546Spatrick     *this = rhs;
30061da546Spatrick   return *this;
31061da546Spatrick }
32061da546Spatrick 
33061da546Spatrick // Destructor
34*f6aab3d8Srobert CFBundle::~CFBundle() = default;
35061da546Spatrick 
36061da546Spatrick // Set the path for a bundle by supplying a
SetPath(const char * path)37061da546Spatrick bool CFBundle::SetPath(const char *path) {
38061da546Spatrick   CFAllocatorRef alloc = kCFAllocatorDefault;
39061da546Spatrick   // Release our old bundle and ULR
40061da546Spatrick   reset(); // This class is a CFReleaser<CFBundleRef>
41061da546Spatrick   m_bundle_url.reset();
42061da546Spatrick   // Make a CFStringRef from the supplied path
43061da546Spatrick   CFString cf_path;
44061da546Spatrick   cf_path.SetFileSystemRepresentation(path);
45061da546Spatrick   if (cf_path.get()) {
46061da546Spatrick     // Make our Bundle URL
47061da546Spatrick     m_bundle_url.reset(::CFURLCreateWithFileSystemPath(
48061da546Spatrick         alloc, cf_path.get(), kCFURLPOSIXPathStyle, true));
49061da546Spatrick     if (m_bundle_url.get()) {
50061da546Spatrick       reset(::CFBundleCreate(alloc, m_bundle_url.get()));
51061da546Spatrick     }
52061da546Spatrick   }
53061da546Spatrick   return get() != NULL;
54061da546Spatrick }
55061da546Spatrick 
GetIdentifier() const56061da546Spatrick CFStringRef CFBundle::GetIdentifier() const {
57061da546Spatrick   CFBundleRef bundle = get();
58061da546Spatrick   if (bundle != NULL)
59061da546Spatrick     return ::CFBundleGetIdentifier(bundle);
60061da546Spatrick   return NULL;
61061da546Spatrick }
62061da546Spatrick 
CopyExecutableURL() const63061da546Spatrick CFURLRef CFBundle::CopyExecutableURL() const {
64061da546Spatrick   CFBundleRef bundle = get();
65061da546Spatrick   if (bundle != NULL)
66061da546Spatrick     return CFBundleCopyExecutableURL(bundle);
67061da546Spatrick   return NULL;
68061da546Spatrick }
69