]> git.saurik.com Git - apple/security.git/blame - Security/libsecurity_transform/lib/c++utils.cpp
Security-57031.1.35.tar.gz
[apple/security.git] / Security / libsecurity_transform / lib / c++utils.cpp
CommitLineData
b1ab9ed8
A
1#include "c++utils.h"
2
3using namespace std;
4
5std::string StringFromCFString(CFStringRef theString)
6{
7 CFIndex maxLength = CFStringGetMaximumSizeForEncoding(CFStringGetLength(theString), 0);
8
9 if (maxLength <= 0) // roll over? just plain bad?
10 {
11 return "";
12 }
13
14 // leave room for NULL termination
15 maxLength += 1;
16
17 char* buffer = new char[maxLength];
18
19 if (buffer == NULL) // out of memory? Naughty, naughty...
20 {
21 return "";
22 }
23
24 CFStringGetCString(theString, buffer, maxLength, 0);
25
26 string result(buffer);
27 delete buffer;
28 return result;
29}
30
31
32
33CFStringRef CFStringFromString(std::string theString)
34{
35 return CFStringCreateWithCString(NULL, theString.c_str(), 0);
36}
37
38
39
40CFTypeRefHolder::~CFTypeRefHolder()
41{
42 if (mTypeRef != NULL)
43 {
44 CFRelease(mTypeRef);
45 }
46}
47
48
49
50void CFTypeRefHolder::Set(CFTypeRef typeRef)
51{
52 if (mTypeRef != NULL)
53 {
54 CFRelease(mTypeRef);
55 }
56
57 mTypeRef = typeRef;
58}
59