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