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