CFComparisonResult CFStringCompareWithOptionsAndLocale (CFStringRef str1, CFStringRef str2, CFRange rangeToCompare, CFStringCompareFlags compareOptions, CFLocaleRef locale) { CFComparisonResult ret; UniChar *string1; UniChar *string2; CFIndex length1; CFIndex length2; CFAllocatorRef alloc; UCollator *ucol; alloc = CFAllocatorGetDefault (); length1 = rangeToCompare.length; string1 = CFAllocatorAllocate (alloc, (length1) * sizeof(UniChar), 0); CFStringGetCharacters (str1, rangeToCompare, string1); length2 = CFStringGetLength (str2); string2 = CFAllocatorAllocate (alloc, (length2) * sizeof(UniChar), 0); CFStringGetCharacters (str2, CFRangeMake(0, length2), string2); ucol = CFStringICUCollatorOpen (compareOptions, locale); ret = ucol_strcoll (ucol, string2, length2, string1, length1); CFStringICUCollatorClose (ucol); CFAllocatorDeallocate (alloc, string1); CFAllocatorDeallocate (alloc, string2); return ret; }
CF_PRIVATE UCalendar *__CFCalendarCreateUCalendar(CFStringRef calendarID, CFStringRef localeID, CFTimeZoneRef tz) { if (calendarID) { CFDictionaryRef components = CFLocaleCreateComponentsFromLocaleIdentifier(kCFAllocatorSystemDefault, localeID); CFMutableDictionaryRef mcomponents = CFDictionaryCreateMutableCopy(kCFAllocatorSystemDefault, 0, components); CFDictionarySetValue(mcomponents, kCFLocaleCalendarIdentifier, calendarID); localeID = CFLocaleCreateLocaleIdentifierFromComponents(kCFAllocatorSystemDefault, mcomponents); CFRelease(mcomponents); CFRelease(components); } char buffer[BUFFER_SIZE]; const char *cstr = CFStringGetCStringPtr(localeID, kCFStringEncodingASCII); if (NULL == cstr) { if (CFStringGetCString(localeID, buffer, BUFFER_SIZE, kCFStringEncodingASCII)) cstr = buffer; } if (NULL == cstr) { if (calendarID) CFRelease(localeID); return NULL; } UChar ubuffer[BUFFER_SIZE]; CFStringRef tznam = CFTimeZoneGetName(tz); CFIndex cnt = CFStringGetLength(tznam); if (BUFFER_SIZE < cnt) cnt = BUFFER_SIZE; CFStringGetCharacters(tznam, CFRangeMake(0, cnt), (UniChar *)ubuffer); UErrorCode status = U_ZERO_ERROR; UCalendar *cal = ucal_open(ubuffer, cnt, cstr, UCAL_DEFAULT, &status); if (calendarID) CFRelease(localeID); return cal; }
/************************************************************************** * debugstr_cf */ const char* debugstr_cf(CFTypeRef t) { CFStringRef s; const char* ret; if (!t) return "(null)"; if (CFGetTypeID(t) == CFStringGetTypeID()) s = t; else s = CFCopyDescription(t); ret = CFStringGetCStringPtr(s, kCFStringEncodingUTF8); if (ret) ret = debugstr_a(ret); if (!ret) { const UniChar* u = CFStringGetCharactersPtr(s); if (u) ret = debugstr_wn((const WCHAR*)u, CFStringGetLength(s)); } if (!ret) { UniChar buf[200]; int len = min(CFStringGetLength(s), sizeof(buf)/sizeof(buf[0])); CFStringGetCharacters(s, CFRangeMake(0, len), buf); ret = debugstr_wn(buf, len); } if (s != t) CFRelease(s); return ret; }
static jstring getJString(JNIEnv *env, CFStringRef cs) { if (cs == NULL) return NULL; CFRange range = CFRangeMake(0, CFStringGetLength(cs)); UniChar *chars = alloca(range.length); CFStringGetCharacters(cs, range, chars); return (*env)->NewString(env, (jchar *) chars, (jsize) range.length); }
void ARRAY_TEXT::convertFromUTF8(const CUTF8String* fromString, CUTF16String* toString) { #ifdef _WIN32 int len = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)fromString->c_str(), fromString->length(), NULL, 0); if(len){ std::vector<uint8_t> buf((len + 1) * sizeof(PA_Unichar)); if(MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)fromString->c_str(), fromString->length(), (LPWSTR)&buf[0], len)){ *toString = CUTF16String((const PA_Unichar *)&buf[0]); } }else{ *toString = CUTF16String((const PA_Unichar *)L""); } #else CFStringRef str = CFStringCreateWithBytes(kCFAllocatorDefault, fromString->c_str(), fromString->length(), kCFStringEncodingUTF8, true); if(str){ int len = CFStringGetLength(str)+1; std::vector<uint8_t> buf(len * sizeof(PA_Unichar)); CFStringGetCharacters(str, CFRangeMake(0, len), (UniChar *)&buf[0]); *toString = CUTF16String((const PA_Unichar *)&buf[0]); CFRelease(str); } #endif }
static void callback( ConstFSEventStreamRef streamRef, void *clientCallBackInfo, size_t numEvents, void *eventPathsVoid, const FSEventStreamEventFlags eventFlags[], const FSEventStreamEventId eventIds[]) { qDebug() << "FolderWatcherPrivate::callback by OS X"; QStringList paths; CFArrayRef eventPaths = (CFArrayRef)eventPathsVoid; for (int i = 0; i < numEvents; ++i) { CFStringRef path = reinterpret_cast<CFStringRef>(CFArrayGetValueAtIndex(eventPaths, i)); QString qstring; CFIndex pathLength = CFStringGetLength(path); qstring.resize(pathLength); CFStringGetCharacters(path, CFRangeMake(0, pathLength), reinterpret_cast<UniChar *>(qstring.data())); paths.append(qstring); } reinterpret_cast<FolderWatcherPrivate*>(clientCallBackInfo)->doNotifyParent(paths); }
static OSStatus FindSNESFolder (FSRef *folderRef, char *folderPath, const char *folderName) { OSStatus err; CFURLRef burl, purl; CFStringRef fstr; FSRef pref; UniChar buffer[PATH_MAX + 1]; Boolean r; fstr = CFStringCreateWithCString(kCFAllocatorDefault, folderName, CFStringGetSystemEncoding()); CFStringGetCharacters(fstr, CFRangeMake(0, CFStringGetLength(fstr)), buffer); burl = CFBundleCopyBundleURL(CFBundleGetMainBundle()); purl = CFURLCreateCopyDeletingLastPathComponent(kCFAllocatorDefault, burl); r = CFURLGetFSRef(purl, &pref); err = FSMakeFSRefUnicode(&pref, CFStringGetLength(fstr), buffer, kTextEncodingUnicodeDefault, folderRef); if (err == dirNFErr || err == fnfErr) { err = FSCreateDirectoryUnicode(&pref, CFStringGetLength(fstr), buffer, kFSCatInfoNone, NULL, folderRef, NULL, NULL); if (err == noErr) AddFolderIcon(folderRef, folderName); } if (err == noErr) err = FSRefMakePath(folderRef, (unsigned char *) folderPath, PATH_MAX); CFRelease(purl); CFRelease(burl); CFRelease(fstr); return (err); }
jstring createJavaString(JNIEnv *env, CFStringRef stringRef) { CFIndex length = CFStringGetLength(stringRef); UniChar buffer[length]; CFStringGetCharacters(stringRef, CFRangeMake(0, length), buffer); return (*env)->NewString(env, (jchar *)buffer, length); }
static const wxUint16* GetEncTable(wxFontEncoding enc) { #ifdef __WXMAC__ if( enc >= wxFONTENCODING_MACMIN && enc <= wxFONTENCODING_MACMAX ) { int i = enc-wxFONTENCODING_MACMIN ; if ( gMacEncodingsInited[i] == false ) { // create CFStringEncoding cfencoding = wxMacGetSystemEncFromFontEnc( enc ) ; if( !CFStringIsEncodingAvailable( cfencoding ) ) return NULL; memset( gMacEncodings[i] , 0 , 128 * 2 ); char s[2] = { 0 , 0 }; CFRange firstchar = CFRangeMake( 0, 1 ); for( unsigned char c = 255 ; c >= 128 ; --c ) { s[0] = c ; wxCFStringRef cfref( CFStringCreateWithCStringNoCopy( NULL, s, cfencoding , kCFAllocatorNull ) ); CFStringGetCharacters( cfref, firstchar, (UniChar*) &gMacEncodings[i][c-128] ); } gMacEncodingsInited[i]=true; } return gMacEncodings[i] ; } #endif for (int i = 0; encodings_list[i].table != NULL; i++) { if (encodings_list[i].encoding == enc) return encodings_list[i].table; } return NULL; }
static bool __CFLocaleICUCurrencyName(const char *locale, const char *value, UCurrNameStyle style, CFStringRef *out) { size_t valLen = strlen(value); if (valLen != 3) // not a valid ISO code return false; UChar curr[4]; UBool isChoice = FALSE; int32_t size = 0; UErrorCode icuStatus = U_ZERO_ERROR; u_charsToUChars(value, curr, (int32_t)valLen); curr[valLen] = '\0'; const UChar *name; name = ucurr_getName(curr, locale, style, &isChoice, &size, &icuStatus); if (U_FAILURE(icuStatus) || icuStatus == U_USING_DEFAULT_WARNING) return false; UChar result[kMaxICUNameSize]; if (isChoice) { UChar pattern[kMaxICUNameSize]; CFStringRef patternRef = CFStringCreateWithFormat(kCFAllocatorSystemDefault, NULL, CFSTR("{0,choice,%S}"), name); CFIndex pattlen = CFStringGetLength(patternRef); CFStringGetCharacters(patternRef, CFRangeMake(0, pattlen), (UniChar *)pattern); CFRelease(patternRef); pattern[pattlen] = '\0'; // null terminate the pattern // Format the message assuming a large amount of the currency size = u_formatMessage("en_US", pattern, pattlen, result, kMaxICUNameSize, &icuStatus, 10.0); if (U_FAILURE(icuStatus)) return false; name = result; } *out = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, (UniChar *)name, size); return (*out != NULL); }
//------------------------------------------------------------------------------------------------- // GetVisualName //------------------------------------------------------------------------------------------------- // void GetVisualName( ITUniStr255 name ) { CFIndex length = CFStringGetLength( kTVisualPluginName ); name[0] = (UniChar)length; CFStringGetCharacters( kTVisualPluginName, CFRangeMake( 0, length ), &name[1] ); }
// Sets up the text based on the specified CFString // void UpdateATSUIStuffString(CFStringRef string) { free(gText); gLength = CFStringGetLength(string); gText = (UniChar *)malloc(gLength * sizeof(UniChar)); CFStringGetCharacters(string, CFRangeMake(0, gLength), gText); }
static void assertEqualsAsCharactersPtr(JSValueRef value, const char* expectedValue) { JSStringRef valueAsString = JSValueToStringCopy(context, value, NULL); size_t jsLength = JSStringGetLength(valueAsString); const JSChar* jsBuffer = JSStringGetCharactersPtr(valueAsString); CFStringRef expectedValueAsCFString = CFStringCreateWithCString(kCFAllocatorDefault, expectedValue, kCFStringEncodingUTF8); CFIndex cfLength = CFStringGetLength(expectedValueAsCFString); UniChar* cfBuffer = (UniChar*)malloc(cfLength * sizeof(UniChar)); CFStringGetCharacters(expectedValueAsCFString, CFRangeMake(0, cfLength), cfBuffer); CFRelease(expectedValueAsCFString); if (memcmp(jsBuffer, cfBuffer, cfLength * sizeof(UniChar)) != 0) { fprintf(stderr, "assertEqualsAsCharactersPtr failed: jsBuffer != cfBuffer\n"); failed = 1; } if (jsLength != (size_t)cfLength) { fprintf(stderr, "assertEqualsAsCharactersPtr failed: jsLength(%ld) != cfLength(%ld)\n", jsLength, cfLength); failed = 1; } free(cfBuffer); JSStringRelease(valueAsString); }
/******************************************************************** * MoveFile * lpFromFileName: old file path * lpToFileName: new file path ********************************************************************/ BOOL MoveFile(const char * lpFromFileName, const char * lpToFileName) { OSErr theErr; FSRef fromFileRef; FSRef toFileRef; FSRef parentFolderRef; // Get the path to the old file theErr = FSPathMakeRef((const UInt8 *)lpFromFileName, &fromFileRef, NULL); if (theErr != noErr) { SetLastError(theErr); return false; } // Get the path to the new folder for the file char folderName[strlen(lpToFileName)]; CFStringRef folderPathCFString = CFStringCreateWithCString(NULL, lpToFileName, kCFStringEncodingUTF8); CFURLRef fileURL = CFURLCreateWithFileSystemPath(NULL, folderPathCFString, kCFURLPOSIXPathStyle, FALSE); CFURLRef folderURL = CFURLCreateCopyDeletingLastPathComponent(NULL, fileURL); CFURLGetFileSystemRepresentation(folderURL, TRUE, (UInt8 *)folderName, strlen(lpToFileName)); theErr = FSPathMakeRef((UInt8 *)folderName, &parentFolderRef, NULL); CFRelease(fileURL); CFRelease(folderURL); CFRelease(folderPathCFString); // Move the old file theErr = FSMoveObject(&fromFileRef, &parentFolderRef, &toFileRef); if (theErr != noErr) { SetLastError(theErr); return false; } // Get a CFString for the new file name CFStringRef newFileNameCFString = CFStringCreateWithCString(NULL, lpToFileName, kCFStringEncodingUTF8); fileURL = CFURLCreateWithFileSystemPath(NULL, newFileNameCFString, kCFURLPOSIXPathStyle, FALSE); CFRelease(newFileNameCFString); newFileNameCFString = CFURLCopyLastPathComponent(fileURL); CFRelease(fileURL); // Convert CFString to Unicode and rename the file UniChar unicodeFileName[256]; CFStringGetCharacters(newFileNameCFString, CFRangeMake(0, CFStringGetLength(newFileNameCFString)), unicodeFileName); theErr = FSRenameUnicode(&toFileRef, CFStringGetLength(newFileNameCFString), unicodeFileName, kTextEncodingUnknown, NULL); if (theErr != noErr) { SetLastError(theErr); CFRelease(newFileNameCFString); return false; } CFRelease(newFileNameCFString); SetLastError(theErr); return true; }
static QString q_toString(const CFStringRef &str) { CFIndex length = CFStringGetLength(str); QVarLengthArray<UniChar> buffer(length); CFRange range = { 0, length }; CFStringGetCharacters(str, range, buffer.data()); return QString(reinterpret_cast<const QChar *>(buffer.data()), length); }
QString convert_CFString_to_QString(CFStringRef str) { CFIndex length = CFStringGetLength(str); const UniChar *chars = CFStringGetCharactersPtr(str); if (chars) return QString(reinterpret_cast<const QChar *>(chars), length); QVarLengthArray<UniChar> buffer(length); CFStringGetCharacters(str, CFRangeMake(0, length), buffer.data()); return QString(reinterpret_cast<const QChar *>(buffer.constData()), length); }
// Given an application bundle reference and the name of an icon file // which is a resource in that bundle, look up the full (posix style) // path to that icon. Returns the path, or an empty wxString on failure wxString GetPathForIconFile( CFBundleRef bundle, CFStringRef iconFile ) { // If either parameter is NULL there is no hope of success if( !bundle || !iconFile ) return wxEmptyString; // Create a range object representing the whole string CFRange wholeString; wholeString.location = 0; wholeString.length = CFStringGetLength( iconFile ); // Index of the period in the file name for iconFile UniCharCount periodIndex; // In order to locate the period delimiting the extension, // iconFile must be represented as UniChar[] { // Allocate a buffer and copy in the iconFile string UniChar* buffer = new UniChar[ wholeString.length ]; CFStringGetCharacters( iconFile, wholeString, buffer ); // Locate the period character OSStatus status = LSGetExtensionInfo( wholeString.length, buffer, &periodIndex ); // Deallocate the buffer delete [] buffer; // If the period could not be located it will not be possible to get the URL if( status != noErr || periodIndex == kLSInvalidExtensionIndex ) return wxEmptyString; } // Range representing the name part of iconFile CFRange iconNameRange; iconNameRange.location = 0; iconNameRange.length = periodIndex - 1; // Range representing the extension part of iconFile CFRange iconExtRange; iconExtRange.location = periodIndex; iconExtRange.length = wholeString.length - periodIndex; // Get the name and extension strings wxCFStringRef iconName = CFStringCreateWithSubstring( kCFAllocatorDefault, iconFile, iconNameRange ); wxCFStringRef iconExt = CFStringCreateWithSubstring( kCFAllocatorDefault, iconFile, iconExtRange ); // Now it is possible to query the URL for the icon as a resource wxCFRef< CFURLRef > iconUrl = wxCFRef< CFURLRef >( CFBundleCopyResourceURL( bundle, iconName, iconExt, NULL ) ); if( !iconUrl.get() ) return wxEmptyString; // All being well, return the icon path return wxCFStringRef( CFURLCopyFileSystemPath( iconUrl, kCFURLPOSIXPathStyle ) ).AsString(); }
wxString wxMacCFStringHolder::AsString(wxFontEncoding encoding) { if ( m_cfs == NULL ) return wxEmptyString ; Size cflen = CFStringGetLength( m_cfs ) ; size_t noChars ; wxChar* buf = NULL ; #if wxUSE_UNICODE #if SIZEOF_WCHAR_T == 2 buf = new wxChar[ cflen + 1 ] ; CFStringGetCharacters( m_cfs , CFRangeMake( 0 , cflen ) , (UniChar*) buf ) ; noChars = cflen ; #else UniChar* unibuf = new UniChar[ cflen + 1 ] ; CFStringGetCharacters( m_cfs , CFRangeMake( 0 , cflen ) , (UniChar*) unibuf ) ; unibuf[cflen] = 0 ; wxMBConvUTF16 converter ; noChars = converter.MB2WC( NULL , (const char*)unibuf , 0 ) ; wxASSERT_MSG( noChars != wxCONV_FAILED, _T("Unable to count the number of characters in this string!") ); buf = new wxChar[ noChars + 1 ] ; noChars = converter.MB2WC( buf , (const char*)unibuf , noChars + 1 ) ; wxASSERT_MSG( noChars != wxCONV_FAILED, _T("Conversion of string failed!") ); delete[] unibuf ; #endif #else CFIndex cStrLen ; CFStringGetBytes( m_cfs , CFRangeMake(0, cflen) , wxMacGetSystemEncFromFontEnc( encoding ) , '?' , false , NULL , 0 , &cStrLen ) ; buf = new wxChar[ cStrLen + 1 ] ; CFStringGetBytes( m_cfs , CFRangeMake(0, cflen) , wxMacGetSystemEncFromFontEnc( encoding ) , '?' , false , (unsigned char*) buf , cStrLen , &cStrLen) ; noChars = cStrLen ; #endif buf[noChars] = 0 ; wxMacConvertNewlines10To13( buf ) ; wxString result(buf) ; delete[] buf ; return result ; }
// Creates the ATSUI data // void SetUpATSUIStuff(void) { CFStringRef string = CFSTR("Hello World!"); verify_noerr( ATSUCreateStyle(&gStyle) ); UpdateATSUIStyle(); gLength = CFStringGetLength(string); gText = (UniChar *)malloc(gLength * sizeof(UniChar)); CFStringGetCharacters(string, CFRangeMake(0, gLength), gText); }
/***************************************************** * * MyValidationProc(theControl) * * Purpose: called when a key filter can't be: after cut, paste, etc * * Inputs: theControl - the control to validate * * Returns: none */ static pascal void MyValidationProc(ControlRef theControl) { OSStatus status; CFStringRef theCFString = NULL; UniChar *buffer = NULL; // Getting the text content of the control status = GetControlData(theControl, kControlEntireControl, kControlEditTextCFStringTag, sizeof(theCFString), &theCFString, NULL); require_noerr(status, ExitValidation); require(theCFString != NULL, ExitValidation); CFIndex i, j, len = CFStringGetLength(theCFString); if (len == 0) goto ExitValidation; // there's nothing to validate // Grabbing the characters as Unicode chars buffer = (UniChar *)malloc(len * sizeof(UniChar)); require(buffer != NULL, ExitValidation); // Checking to see if we have only digits CFStringGetCharacters(theCFString, CFRangeMake(0, len), buffer); Boolean ok = true; for (i = 0; (i < len) && ok; i++) ok = (buffer[i] >= '0') && (buffer[i] <= '9'); if (!ok) { // if not, we remove the offending characters // we also make sure that we restore the insertion point to the correct location. ControlEditTextSelectionRec textSelection; status = GetControlData(theControl, kControlEntireControl, kControlEditTextSelectionTag, sizeof(textSelection), &textSelection, NULL); require_noerr(status, ExitValidation); for (i = 0, j = 0; i < len; i++) if ((buffer[i] >= '0') && (buffer[i] <= '9')) buffer[j++] = buffer[i]; CFRelease(theCFString); theCFString = CFStringCreateWithCharacters(NULL, buffer, j); require(theCFString != NULL, ExitValidation); status = SetControlData(theControl, kControlEntireControl, kControlEditTextCFStringTag, sizeof(theCFString), &theCFString); require_noerr(status, ExitValidation); textSelection.selEnd -= (len - j); textSelection.selStart = textSelection.selEnd; status = SetControlData(theControl, kControlEntireControl, kControlEditTextSelectionTag, sizeof(textSelection), &textSelection); require_noerr(status, ExitValidation); } ExitValidation: if (buffer != NULL) free(buffer); if (theCFString != NULL) CFRelease(theCFString); return; } // MyValidationProc
void ConsoleMessage::execute() { if (theSession) { jsize len = ::CFStringGetLength(mMessage); jchar* buffer = new jchar[len]; CFRange range = { 0, len }; CFStringGetCharacters(mMessage, range, buffer); System_out_print(theSession->getCurrentEnv(), buffer, len); delete[] buffer; } }
static void CopyUTF8toUTF16NFC(const nsACString& aSrc, nsAString& aResult) { static PRBool sChecked = PR_FALSE; static UnicodeNormalizer sUnicodeNormalizer = NULL; // CFStringNormalize was not introduced until Mac OS 10.2 if (!sChecked) { CFBundleRef carbonBundle = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.Carbon")); if (carbonBundle) sUnicodeNormalizer = (UnicodeNormalizer) ::CFBundleGetFunctionPointerForName(carbonBundle, CFSTR("CFStringNormalize")); sChecked = PR_TRUE; } if (!sUnicodeNormalizer) { // OS X 10.1 or earlier CopyUTF8toUTF16(aSrc, aResult); return; } const nsAFlatCString &inFlatSrc = PromiseFlatCString(aSrc); // The number of 16bit code units in a UTF-16 string will never be // larger than the number of bytes in the corresponding UTF-8 string. CFMutableStringRef inStr = ::CFStringCreateMutable(NULL, inFlatSrc.Length()); if (!inStr) { CopyUTF8toUTF16(aSrc, aResult); return; } ::CFStringAppendCString(inStr, inFlatSrc.get(), kCFStringEncodingUTF8); sUnicodeNormalizer(inStr, kCFStringNormalizationFormC); CFIndex length = CFStringGetLength(inStr); const UniChar* chars = CFStringGetCharactersPtr(inStr); if (chars) aResult.Assign(chars, length); else { nsAutoTArray<UniChar, 512> buffer; if (buffer.SetLength(length)) { CFStringGetCharacters(inStr, CFRangeMake(0, length), buffer.Elements()); aResult.Assign(buffer.Elements(), length); } else CopyUTF8toUTF16(aSrc, aResult); } CFRelease(inStr); }
int dir_Create(char *pathString, int pathStringLength) { /* Create a new directory with the given path. By default, this directory is created in the current directory. Use a full path name such as "MyDisk:Working:New Folder" to create folders elsewhere. */ char cFileName[1001]; if (pathStringLength >= 1000) { return false; } /* copy the file name into a null-terminated C string */ sqFilenameFromString((char *) cFileName, (int) pathString, pathStringLength); #if defined(__MWERKS__) { CFStringRef filePath,lastFilePath; CFURLRef sillyThing,sillyThing2; FSRef parentFSRef; UniChar buffer[1024]; long tokenLength; int err; filePath = CFStringCreateWithBytes(kCFAllocatorDefault,(UInt8 *)cFileName,strlen(cFileName),gCurrentVMEncoding,false); if (filePath == nil) return false; sillyThing = CFURLCreateWithFileSystemPath (kCFAllocatorDefault,filePath,kCFURLHFSPathStyle,true); CFRelease(filePath); lastFilePath = CFURLCopyLastPathComponent(sillyThing); tokenLength = CFStringGetLength(lastFilePath); if (tokenLength > 1024) return false; CFStringGetCharacters(lastFilePath,CFRangeMake(0,tokenLength),buffer); CFRelease(lastFilePath); sillyThing2 = CFURLCreateCopyDeletingLastPathComponent(kCFAllocatorDefault,sillyThing); err = CFURLGetFSRef(sillyThing2,&parentFSRef); CFRelease(sillyThing); CFRelease(sillyThing2); if (err == 0) { return false; } err = FSCreateDirectoryUnicode(&parentFSRef,tokenLength,buffer,kFSCatInfoNone,NULL,NULL,NULL,NULL); return (err == noErr ? 1 : 0); } #else return mkdir(cFileName, 0777) == 0; #endif }
rktio_char16_t *rktio_recase_utf16(rktio_t *rktio, rktio_bool_t to_up, rktio_char16_t *s1, intptr_t l1, intptr_t *olen) { #ifdef MACOS_UNICODE_SUPPORT CFMutableStringRef mstr; CFStringRef str; CFRange rng; rktio_char16_t *result; intptr_t len; str = CFStringCreateWithBytes(NULL, (unsigned char *)s1, (l1 * sizeof(rktio_char16_t)), kCFStringEncodingUnicode, FALSE); mstr = CFStringCreateMutableCopy(NULL, 0, str); CFRelease(str); if (to_up) CFStringUppercase(mstr, NULL); else CFStringLowercase(mstr, NULL); len = CFStringGetLength(mstr); result = malloc((len + 1) * sizeof(rktio_char16_t)); rng = CFRangeMake(0, len); CFStringGetCharacters(mstr, rng, (UniChar *)result); CFRelease(mstr); result[len] = 0; if (olen) *olen = len; return result; #elif defined(RKTIO_SYSTEM_WINDOWS) rktio_char16_t *result; result = malloc((l1 + 1) * sizeof(rktio_char16_t)); memcpy(result, s1, l1 * sizeof(rktio_char16_t)); result[l1] = 0; if (to_up) CharUpperBuffW((wchar_t *)result, l1); else CharLowerBuffW((wchar_t *)result, l1); if (olen) *olen = l1; return result; #else return NULL; #endif }
extern int utf8_decodestr(const u_int8_t * utf8p, size_t utf8len, u_int16_t *ucsp, size_t *ucslen, size_t buflen, u_int16_t altslash, int flags) { int err; CFStringRef str; CFIndex utf16Count; assert(utf8p != NULL); assert(ucsp != NULL); assert(ucslen != NULL); assert(altslash == 0); assert(flags == UTF_PRECOMPOSED); err = 0; str = CFStringCreateWithBytes(NULL, utf8p, utf8len, kCFStringEncodingUTF8, false); if (str == NULL) { err = EINVAL; } if (err == 0) { CFMutableStringRef tmpStr; tmpStr = CFStringCreateMutableCopy(NULL, 0, str); if (tmpStr != NULL) { CFStringNormalize(tmpStr, kCFStringNormalizationFormC); // Swap tmpStr into str. CFRelease(str); str = tmpStr; } } if (err == 0) { utf16Count = CFStringGetLength(str); if (utf16Count > (buflen / 2)) { utf16Count = (buflen / 2); err = ENAMETOOLONG; } CFStringGetCharacters(str, CFRangeMake(0, utf16Count), ucsp); *ucslen = utf16Count * sizeof(uint16_t); } if (str != NULL) { CFRelease(str); } return err; }
QString CFStringToQString(CFStringRef str) { if (!str) return QString(); CFIndex length = CFStringGetLength(str); if (length == 0) return QString(); QString string(length, Qt::Uninitialized); CFStringGetCharacters(str, CFRangeMake(0, length), reinterpret_cast<UniChar *> (const_cast<QChar *>(string.unicode()))); return string; }
QString QCFStringToQString(CFStringRef str) { if(!str) return QString(); CFIndex length = CFStringGetLength(str); const UniChar *chars = CFStringGetCharactersPtr(str); if (chars) return QString(reinterpret_cast<const QChar *>(chars), length); QVector<UniChar> buffer(length); CFStringGetCharacters(str, CFRangeMake(0, length), buffer.data()); return QString(reinterpret_cast<const QChar *>(buffer.constData()), length); }
// this is a Qt implementation I found QString cfstring2qstring(CFStringRef str) { if(!str) return QString(); CFIndex length = CFStringGetLength(str); if(const UniChar *chars = CFStringGetCharactersPtr(str)) return QString((QChar *)chars, length); UniChar *buffer = (UniChar*)malloc(length * sizeof(UniChar)); CFStringGetCharacters(str, CFRangeMake(0, length), buffer); QString ret((QChar *)buffer, length); free(buffer); return ret; }
void stringGettingAtCharactersExample(void) { CFStringRef str; const UniChar *chars; show(CFSTR("------------------Character Manipulations---------------")); // Create some test CFString str = CFStringCreateWithCString(NULL, "Hello World", kCFStringEncodingASCII); show(CFSTR("Original String : %@"), str); // The fastest way to get the contents; this might return NULL though // depending on the system, the release, etc, so don't depend on it // (unless you used CFStringCreateMutableWithExternalCharactersNoCopy()) chars = CFStringGetCharactersPtr(str); // If that fails, you can try copying the UniChars out // either into some stack buffer or some allocated piece of memory... // Using the former is fine, but you need to know the size; the latter // always works but requires allocating some memory; not too efficient. if (chars == NULL) { CFIndex length = CFStringGetLength(str); UniChar *buffer = (UniChar*)malloc(length * sizeof(UniChar)); CFStringGetCharacters(str, CFRangeMake(0, length), buffer); // Process the chars... free(buffer); } else show(CFSTR("Characters : %@"), str); // You can use CFStringGetCharacterAtIndex() to get at the characters one at a time, // but doing a lot of characters this way might get slow... // An option is to use "inline buffer" functionality which mixes the convenience of // one-at-a-time char access with efficiency of bulk access { CFStringInlineBuffer inlineBuffer; CFIndex length = CFStringGetLength(str); CFIndex cnt; CFStringInitInlineBuffer(str, &inlineBuffer, CFRangeMake(0, length)); for (cnt = 0; cnt < length; cnt++) { UniChar ch = CFStringGetCharacterFromInlineBuffer(&inlineBuffer, cnt); // Process character... (void)ch; // Dummy processing to prevent compiler warning... } } }
QT_BEGIN_NAMESPACE // XXX: remove at some future date static inline QString cfStringToQString(CFStringRef str) { CFIndex length = CFStringGetLength(str); const UniChar *chars = CFStringGetCharactersPtr(str); if (chars) return QString(reinterpret_cast<const QChar *>(chars), length); UniChar buffer[length]; CFStringGetCharacters(str, CFRangeMake(0, length), buffer); return QString(reinterpret_cast<const QChar *>(buffer), length); }