]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_transform/lib/c++utils.cpp
Security-57740.51.3.tar.gz
[apple/security.git] / OSX / libsecurity_transform / lib / c++utils.cpp
1 #include "c++utils.h"
2
3 using namespace std;
4
5 std::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
33 CFStringRef CFStringFromString(std::string theString)
34 {
35 return CFStringCreateWithCString(NULL, theString.c_str(), 0);
36 }
37
38
39
40 CFTypeRefHolder::~CFTypeRefHolder()
41 {
42 if (mTypeRef != NULL)
43 {
44 CFRelease(mTypeRef);
45 }
46 }
47
48
49
50 void CFTypeRefHolder::Set(CFTypeRef typeRef)
51 {
52 if (mTypeRef != NULL)
53 {
54 CFRelease(mTypeRef);
55 }
56
57 mTypeRef = typeRef;
58 }
59