CFBundle.cpp revision 311128
1193354Srmacklem//===-- CFBundle.cpp --------------------------------------------*- C++ -*-===//
2193354Srmacklem//
3193354Srmacklem//                     The LLVM Compiler Infrastructure
4193354Srmacklem//
5193354Srmacklem// This file is distributed under the University of Illinois Open Source
6193354Srmacklem// License. See LICENSE.TXT for details.
7193354Srmacklem//
8193354Srmacklem//===----------------------------------------------------------------------===//
9193354Srmacklem//
10193354Srmacklem//  Created by Greg Clayton on 1/16/08.
11193354Srmacklem//
12193354Srmacklem//===----------------------------------------------------------------------===//
13231653Sdougb
14193354Srmacklem#include "CFBundle.h"
15193354Srmacklem#include "CFString.h"
16193354Srmacklem
17193354Srmacklem//----------------------------------------------------------------------
18193354Srmacklem// CFBundle constructor
19193354Srmacklem//----------------------------------------------------------------------
20CFBundle::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//----------------------------------------------------------------------
29CFBundle::CFBundle(const CFBundle &rhs)
30    : CFReleaser<CFBundleRef>(rhs), m_bundle_url(rhs.m_bundle_url) {}
31
32//----------------------------------------------------------------------
33// CFBundle copy constructor
34//----------------------------------------------------------------------
35CFBundle &CFBundle::operator=(const CFBundle &rhs) {
36  *this = rhs;
37  return *this;
38}
39
40//----------------------------------------------------------------------
41// Destructor
42//----------------------------------------------------------------------
43CFBundle::~CFBundle() {}
44
45//----------------------------------------------------------------------
46// Set the path for a bundle by supplying a
47//----------------------------------------------------------------------
48bool 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
67CFStringRef CFBundle::GetIdentifier() const {
68  CFBundleRef bundle = get();
69  if (bundle != NULL)
70    return ::CFBundleGetIdentifier(bundle);
71  return NULL;
72}
73
74CFURLRef CFBundle::CopyExecutableURL() const {
75  CFBundleRef bundle = get();
76  if (bundle != NULL)
77    return CFBundleCopyExecutableURL(bundle);
78  return NULL;
79}
80