如何将CFURLRef
转换为C++的std::string
?
我还可以通过以下方式从CFURLRef
转换为CFStringRef
:
CFStringRef CFURLGetString ( CFURLRef anURL );
但是现在我也遇到了同样的问题。我该如何将CFStringRef
转换为std::string
?
如何将CFURLRef
转换为C++的std::string
?
我还可以通过以下方式从CFURLRef
转换为CFStringRef
:
CFStringRef CFURLGetString ( CFURLRef anURL );
但是现在我也遇到了同样的问题。我该如何将CFStringRef
转换为std::string
?
CFStringRef 是与NSString对象无缝桥接的,因此如果您以任何方式使用Cocoa或Objective C,转换非常简单:
NSString *foo = (NSString *)yourOriginalCFStringRef;
std::string *bar = new std::string([foo UTF8String]);
更多细节请参见这里。
现在,既然您没有使用Cocoa或Objective-C标签,我猜您不想使用Objective-C解决方案。
在这种情况下,您需要从CFStringRef获取等效的C字符串:
const CFIndex kCStringSize = 128;
char temporaryCString[kCStringSize];
bzero(temporaryCString,kCStringSize);
CFStringGetCString(yourStringRef, temporaryCString, kCStringSize, kCFStringEncodingUTF8);
std::string *bar = new std::string(temporaryCString);
这段代码没有进行错误检查,你可能需要通过CFStringGetCString
获取的字符串进行空终止(我尝试通过bzero
来缓解这个问题)。
这个函数可能是最简单的解决方案:
const char * CFStringGetCStringPtr ( CFStringRef theString, CFStringEncoding encoding );
std::string str(CFStringGetCStringPtr(CFURLGetString(anUrl),kCFStringEncodingUTF8));
CFIndex bufferSize = CFStringGetLength(cfString) + 1; // The +1 is for having space for the string to be NUL terminated
char buffer[bufferSize];
// CFStringGetCString is documented to return a false if the buffer is too small
// (which shouldn't happen in this example) or if the conversion generally fails
if (CFStringGetCString(cfString, buffer, bufferSize, kCFStringEncodingUTF8))
{
std::string cppString (buffer);
}
CFStringGetCString
没有像CFStringGetCStringPtr
一样被记录返回NULL的情况。请确保您使用正确的CFStringEncoding
类型,我认为UTF8编码应该对大多数事情都是安全的。您可以查看苹果公司有关CFStringGetCString
的文档,网址为https://developer.apple.com/reference/corefoundation/1542721-cfstringgetcstring?language=objc。CFStringGetLength(cfString)
返回UTF-16
编码的长度。 - Marek R
std::string stdStringFromCF(CFStringRef s)
{
if (auto fastCString = CFStringGetCStringPtr(s, kCFStringEncodingUTF8))
{
return std::string(fastCString);
}
auto utf16length = CFStringGetLength(s);
auto maxUtf8len = CFStringGetMaximumSizeForEncoding(utf16length, kCFStringEncodingUTF8);
std::string converted(maxUtf8len, '\0');
CFStringGetCString(s, converted.data(), maxUtf8len, kCFStringEncodingUTF8);
converted.resize(std::strlen(converted.data()));
return converted;
}
还没有测试过。
CFStringGetCStringPtr
旁边的注释说:/* May return NULL at any time; be prepared for NULL */
。所以,如果你使用它,要小心。 - dgross