#include <security_utilities/cfutilities.h>
#include <security_utilities/errors.h>
#include <security_utilities/debugging.h>
namespace Security {
CFURLRef makeCFURL(const char *s, bool isDirectory)
{
return CFURLCreateWithFileSystemPath(NULL,
CFRef<CFStringRef>(makeCFString(s)),
kCFURLPOSIXPathStyle, isDirectory);
}
string cfString(CFStringRef inStr, bool release)
{
if (!inStr)
CFError::throwMe();
CFRef<CFStringRef> str(inStr); if (!release)
CFRetain(inStr);
if (!str)
return "";
if (const char *s = CFStringGetCStringPtr(str, kCFStringEncodingUTF8)) {
return s;
}
string ret;
CFIndex length = CFStringGetLength(str); char *buffer = new char[6 * length + 1]; if (CFStringGetCString(str, buffer, 6 * length + 1, kCFStringEncodingUTF8))
ret = buffer;
delete[] buffer;
return ret;
}
string cfString(CFURLRef inUrl, bool release)
{
if (!inUrl)
CFError::throwMe();
CFRef<CFURLRef> url(inUrl); if (!release)
CFRetain(inUrl);
UInt8 buffer[PATH_MAX+1];
if (CFURLGetFileSystemRepresentation(url, true, buffer, sizeof(buffer)))
return string(reinterpret_cast<char *>(buffer));
else
CFError::throwMe();
}
string cfString(CFBundleRef inBundle, bool release)
{
if (!inBundle)
CFError::throwMe();
CFRef<CFBundleRef> bundle(inBundle);
if (!release)
CFRetain(inBundle);
return cfString(CFBundleCopyBundleURL(bundle), true);
}
uint32_t cfNumber(CFNumberRef number)
{
uint32_t value;
if (CFNumberGetValue(number, kCFNumberSInt32Type, &value))
return value;
else
CFError::throwMe();
}
uint32_t cfNumber(CFNumberRef number, uint32_t defaultValue)
{
uint32_t value;
if (CFNumberGetValue(number, kCFNumberSInt32Type, &value))
return value;
else
return defaultValue;
}
}