CFURLRef createCFURLFromBuffer(const CharBuffer& buffer) { // NOTE: We use UTF-8 here since this encoding is used when computing strings when returning URL components // (e.g calls to NSURL -path). However, this function is not tolerant of illegal UTF-8 sequences, which // could either be a malformed string or bytes in a different encoding, like Shift-JIS, so we fall back // onto using ISO Latin-1 in those cases. CFURLRef result = CFURLCreateAbsoluteURLWithBytes(0, reinterpret_cast<const UInt8*>(buffer.data()), buffer.size(), kCFStringEncodingUTF8, 0, true); if (!result) result = CFURLCreateAbsoluteURLWithBytes(0, reinterpret_cast<const UInt8*>(buffer.data()), buffer.size(), kCFStringEncodingISOLatin1, 0, true); return result; }
RetainPtr<CFURLRef> createCFURLFromBuffer(const char* data, size_t size, CFURLRef baseURL) { // NOTE: We use UTF-8 here since this encoding is used when computing strings when returning URL components // (e.g calls to NSURL -path). However, this function is not tolerant of illegal UTF-8 sequences, which // could either be a malformed string or bytes in a different encoding, like Shift-JIS, so we fall back // onto using ISO Latin-1 in those cases. RetainPtr<CFURLRef> result = adoptCF(CFURLCreateAbsoluteURLWithBytes(0, reinterpret_cast<const UInt8*>(data), size, kCFStringEncodingUTF8, baseURL, true)); if (!result) result = adoptCF(CFURLCreateAbsoluteURLWithBytes(0, reinterpret_cast<const UInt8*>(data), size, kCFStringEncodingISOLatin1, baseURL, true)); return result; }
CFURLRef KURL::createCFURL() const { const UInt8 *bytes = (const UInt8 *)urlString.latin1(); // NOTE: We use UTF-8 here since this encoding is used when computing strings when returning URL components // (e.g calls to NSURL -path). However, this function is not tolerant of illegal UTF-8 sequences, which // could either be a malformed string or bytes in a different encoding, like Shift-JIS, so we fall back // onto using ISO Latin-1 in those cases. CFURLRef result = CFURLCreateAbsoluteURLWithBytes(0, bytes, urlString.length(), kCFStringEncodingUTF8, 0, true); if (!result) result = CFURLCreateAbsoluteURLWithBytes(0, bytes, urlString.length(), kCFStringEncodingISOLatin1, 0, true); return result; }
CFURLRef CFURLCreateWithEvent(const AppleEvent *ev, AEKeyword theKey, OSErr *errPtr) { CFURLRef file_url = NULL; Size data_size; void *value_ptr = NULL; *errPtr = AEGetParamPtr(ev, theKey, typeFileURL, NULL, NULL, 0, &data_size); if (*errPtr != noErr) goto bail; value_ptr = malloc(data_size); if (value_ptr == NULL) { fputs("Faild to malloc in CFStringCreatePOSIXPathWithEvent", stderr); goto bail; } *errPtr = AEGetParamPtr(ev, theKey, typeFileURL, NULL, value_ptr, data_size, NULL); if (*errPtr != noErr) { free(value_ptr); goto bail; } file_url = CFURLCreateAbsoluteURLWithBytes(NULL, (const UInt8 *)value_ptr, data_size, kCFStringEncodingUTF8, NULL, false); free(value_ptr); bail: return file_url; }
CFURLRef KURL::createCFURL() const { // FIXME: What should this return for invalid URLs? // Currently it throws away the high bytes of the characters in the string in that case, // which is clearly wrong. Vector<char, 512> buffer; copyToBuffer(buffer); // NOTE: We use UTF-8 here since this encoding is used when computing strings when returning URL components // (e.g calls to NSURL -path). However, this function is not tolerant of illegal UTF-8 sequences, which // could either be a malformed string or bytes in a different encoding, like Shift-JIS, so we fall back // onto using ISO Latin-1 in those cases. CFURLRef result = CFURLCreateAbsoluteURLWithBytes(0, reinterpret_cast<const UInt8*>(buffer.data()), buffer.size(), kCFStringEncodingUTF8, 0, true); if (!result) result = CFURLCreateAbsoluteURLWithBytes(0, reinterpret_cast<const UInt8*>(buffer.data()), buffer.size(), kCFStringEncodingISOLatin1, 0, true); return result; }
CFMutableArrayRef CFMutableArrayCreatePOSIXPathsWithEvent( const AppleEvent *ev, AEKeyword theKey, OSErr *errPtr) { CFMutableArrayRef outArray = NULL; DescType typeCode; Size dataSize; AEDescList aeList = {typeNull, NULL}; *errPtr = AESizeOfParam(ev, theKey, &typeCode, &dataSize); if ((*errPtr != noErr) || (typeCode == typeNull)){ goto bail; } *errPtr = AEGetParamDesc(ev, theKey, typeAEList, &aeList); if (*errPtr != noErr) goto bail; long count = 0; *errPtr = AECountItems(&aeList, &count); if (*errPtr != noErr) goto bail; outArray = CFArrayCreateMutable(NULL, count, &kCFTypeArrayCallBacks); for(long index = 1; index <= count; index++) { void *value_ptr = NULL; Size data_size; *errPtr = AEGetNthPtr(&aeList, index, typeFileURL, NULL, NULL, value_ptr, 0, &data_size); if (*errPtr == noErr) { value_ptr = malloc(data_size); *errPtr = AEGetNthPtr(&aeList, index, typeFileURL, NULL, NULL, value_ptr, data_size, NULL); } if (*errPtr != noErr) { fputs("Fail to AEGetNthPtr in CFMutableArrayCreatePOSIXPathsWithEvent", stderr); goto bail; } CFURLRef file_url = CFURLCreateAbsoluteURLWithBytes( NULL, (const UInt8 *)value_ptr, data_size, kCFStringEncodingUTF8, NULL, false); CFStringRef path = CFURLCopyFileSystemPath(file_url, kCFURLPOSIXPathStyle); CFArrayAppendValue(outArray, path); CFRelease(file_url); CFRelease(path); free(value_ptr); } bail: AEDisposeDesc(&aeList); return outArray; }