1//===-- CFCBundle.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#include "CFCBundle.h"
11#include "CFCString.h"
12
13//----------------------------------------------------------------------
14// CFCBundle constructor
15//----------------------------------------------------------------------
16CFCBundle::CFCBundle(const char *path) :
17    CFCReleaser<CFBundleRef>()
18{
19    if (path && path[0])
20        SetPath(path);
21}
22
23CFCBundle::CFCBundle(CFURLRef url) :
24    CFCReleaser<CFBundleRef>(url ? CFBundleCreate(NULL, url) : NULL)
25{
26}
27
28//----------------------------------------------------------------------
29// Destructor
30//----------------------------------------------------------------------
31CFCBundle::~CFCBundle()
32{
33}
34
35//----------------------------------------------------------------------
36// Set the path for a bundle by supplying a
37//----------------------------------------------------------------------
38bool
39CFCBundle::SetPath (const char *path)
40{
41    CFAllocatorRef alloc = kCFAllocatorDefault;
42    // Release our old bundle and URL
43    reset();
44
45    // Make a CFStringRef from the supplied path
46    CFCString cf_path;
47    cf_path.SetFileSystemRepresentation(path);
48    if (cf_path.get())
49    {
50        // Make our Bundle URL
51        CFCReleaser<CFURLRef> bundle_url (::CFURLCreateWithFileSystemPath (alloc, cf_path.get(), kCFURLPOSIXPathStyle, true));
52        if (bundle_url.get())
53            reset (::CFBundleCreate (alloc, bundle_url.get()));
54    }
55    return get() != NULL;
56}
57
58bool
59CFCBundle::GetPath (char *dst, size_t dst_len)
60{
61    CFBundleRef bundle = get();
62    if (bundle)
63    {
64        CFCReleaser<CFURLRef> bundle_url (CFBundleCopyBundleURL (bundle));
65        if (bundle_url.get())
66        {
67            Boolean resolveAgainstBase = 0;
68            return ::CFURLGetFileSystemRepresentation (bundle_url.get(), resolveAgainstBase, (UInt8 *)dst, dst_len) != 0;
69        }
70    }
71    return false;
72}
73
74CFStringRef
75CFCBundle::GetIdentifier () const
76{
77    CFBundleRef bundle = get();
78    if (bundle != NULL)
79        return ::CFBundleGetIdentifier (bundle);
80    return NULL;
81}
82
83CFTypeRef
84CFCBundle::GetValueForInfoDictionaryKey(CFStringRef key) const
85{
86    CFBundleRef bundle = get();
87    if (bundle != NULL)
88        return ::CFBundleGetValueForInfoDictionaryKey(bundle, key);
89    return NULL;
90}
91
92CFURLRef
93CFCBundle::CopyExecutableURL () const
94{
95    CFBundleRef bundle = get();
96    if (bundle != NULL)
97        return CFBundleCopyExecutableURL(bundle);
98    return NULL;
99}
100