#include "CFBundle.h"
#include "CFString.h"
CFBundle::CFBundle(const char *path) :
CFReleaser<CFBundleRef>()
{
if (path && path[0])
SetPath(path);
}
CFBundle::CFBundle(const CFBundle& rhs) :
CFReleaser<CFBundleRef>(rhs)
{
}
CFBundle::CFBundle(CFURLRef url) :
CFReleaser<CFBundleRef>(url ? CFBundleCreate(NULL, url) : NULL)
{
}
CFBundle&
CFBundle::operator=(const CFBundle& rhs)
{
if (this != &rhs)
*this = rhs;
return *this;
}
CFBundle::~CFBundle()
{
}
bool
CFBundle::SetPath (const char *in_path)
{
reset();
if (in_path && in_path[0])
{
char resolved_path[PATH_MAX];
const char *path = ::realpath(in_path, resolved_path);
if (path == NULL)
path = in_path;
CFAllocatorRef alloc = kCFAllocatorDefault;
CFReleaser<CFURLRef> bundle_url(::CFURLCreateFromFileSystemRepresentation (alloc, (const UInt8 *)path, strlen (path), false));
if (bundle_url.get())
{
CFIndex last_length = LONG_MAX;
while (bundle_url.get() != NULL)
{
CFRange rangeIncludingSeparators;
CFRange range = ::CFURLGetByteRangeForComponent(bundle_url.get(), kCFURLComponentPath, &rangeIncludingSeparators);
if (range.length > last_length)
break;
reset (::CFBundleCreate (alloc, bundle_url.get()));
if (get() != NULL)
{
if (GetIdentifier() != NULL)
break;
reset();
}
bundle_url.reset (::CFURLCreateCopyDeletingLastPathComponent(alloc, bundle_url.get()));
last_length = range.length;
}
}
}
return get() != NULL;
}
CFStringRef
CFBundle::GetIdentifier () const
{
CFBundleRef bundle = get();
if (bundle != NULL)
return ::CFBundleGetIdentifier (bundle);
return NULL;
}
CFTypeRef
CFBundle::GetValueForInfoDictionaryKey(CFStringRef key) const
{
CFBundleRef bundle = get();
if (bundle != NULL)
return ::CFBundleGetValueForInfoDictionaryKey(bundle, key);
return NULL;
}