boolean directorytopath ( const ptrfilespec fs, bigstring path ) { // // 2006-09-09 creedon: FSRef-ized // another way might be to use FSRefMakePath and then push insert the volume name on it // OSErr err; OSStatus status; FSRef volumesfsref; status = FSPathMakeRef ((UInt8 *)"/Volumes", &volumesfsref, NULL); if ((status == noErr) && (FSCompareFSRefs (&fs->ref, &volumesfsref) == noErr)) { /* bail if fs is Volumes directory */ setemptystring (path); return (true); } CFMutableStringRef ioPath = CFStringCreateMutable (NULL, 0); FSCatalogInfo catalogInfo; FSRef fsrefnomad = fs->ref; HFSUniStr255 names[100]; int i, n; UniChar inSepChar = ':'; err = noErr; clearbytes (&catalogInfo, longsizeof (catalogInfo)); for (n = 0; err == noErr && catalogInfo.nodeID != fsRtDirID && n < 100; n++) { err = FSGetCatalogInfo (&fsrefnomad, kFSCatInfoNodeID, &catalogInfo, &names[n], NULL, &fsrefnomad); } if (err != noErr) return (false); for (i = n - 1; i >= 0; --i) { CFStringAppendCharacters (ioPath, names[i].unicode, names[i].length); CFStringAppendCharacters (ioPath, &inSepChar, 1); } boolean success = CFStringGetPascalString (ioPath, path, 256, kCFStringEncodingMacRoman); CFRelease(ioPath); //PBS 2/28/11: fixed memory leak return success; } // directorytopath
CF_PRIVATE void _CFAppendConditionalTrailingPathSlash2(CFMutableStringRef path) { static const UniChar slash[1] = { CFPreferredSlash }; UniChar character = CFStringGetCharacterAtIndex((CFStringRef)path, CFStringGetLength(path) - 1); if (!IS_SLASH(character)) { CFStringAppendCharacters(path, slash, 1); } }
CF_PRIVATE void _CFAppendTrailingPathSlash2(CFMutableStringRef path) { static const UniChar slash[1] = { CFPreferredSlash }; CFIndex len = CFStringGetLength(path); if (len == 0) { // Do nothing for this case } else if (len == 1) { UniChar character = CFStringGetCharacterAtIndex((CFStringRef)path, 0); if (!IS_SLASH(character)) { CFStringAppendCharacters(path, slash, 1); } } else if (len == 2) { if (!_hasDrive(path) && !_hasNet(path)) { CFStringAppendCharacters(path, slash, 1); } } else { CFStringAppendCharacters(path, slash, 1); } }
void diff_mungeHelper(CFStringRef token, CFMutableArrayRef tokenArray, CFMutableDictionaryRef tokenHash, CFMutableStringRef chars) { #define diff_UniCharMax (~(UniChar)0x00) CFIndex hash; if (CFDictionaryGetValueIfPresent(tokenHash, token, (const void **)&hash)) { const UniChar hashChar = (UniChar)hash; CFStringAppendCharacters(chars, &hashChar, 1); } else { CFArrayAppendValue(tokenArray, token); hash = CFArrayGetCount(tokenArray) - 1; check_string(hash <= diff_UniCharMax, "Hash value has exceeded UniCharMax!"); CFDictionaryAddValue(tokenHash, token, (void *)hash); const UniChar hashChar = (UniChar)hash; CFStringAppendCharacters(chars, &hashChar, 1); } #undef diff_UniCharMax }
CF_PRIVATE Boolean _CFAppendPathExtension2(CFMutableStringRef path, CFStringRef extension) { if (!path) { return false; } if (0 < CFStringGetLength(extension) && IS_SLASH(CFStringGetCharacterAtIndex(extension, 0))) { return false; } if (1 < CFStringGetLength(extension)) { if (_hasDrive(extension)) return false; } Boolean destHasDrive = (1 < CFStringGetLength(path)) && _hasDrive(path); while (((destHasDrive && 3 < CFStringGetLength(path)) || (!destHasDrive && 1 < CFStringGetLength(path))) && IS_SLASH(CFStringGetCharacterAtIndex(path, CFStringGetLength(path) - 1))) { CFStringDelete(path, CFRangeMake(CFStringGetLength(path) - 1, 1)); } if (CFStringGetLength(path) == 0) { return false; } UniChar firstChar = CFStringGetCharacterAtIndex(path, 0); CFIndex newLength = CFStringGetLength(path); switch (newLength) { case 0: return false; case 1: if (IS_SLASH(firstChar) || firstChar == '~') { return false; } break; case 2: if (_hasDrive(path) || _hasNet(path)) { return false; } break; case 3: if (IS_SLASH(CFStringGetCharacterAtIndex(path, 2)) && _hasDrive(path)) { return false; } break; } if (0 < newLength && firstChar == '~') { // Make sure we have a slash in the string if (!CFStringFindWithOptions(path, CFPreferredSlashStr, CFRangeMake(1, newLength - 1), 0, NULL)) { return false; } } static const UniChar dotChar = '.'; CFStringAppendCharacters(path, &dotChar, 1); CFStringAppend(path, extension); return true; }
/*++ Function: PAL_towupper See MSDN --*/ wchar_16 __cdecl PAL_towupper( wchar_16 c ) { #if HAVE_COREFOUNDATION PERF_ENTRY(towupper); ENTRY("towupper (c=%d)\n", c); if (!PAL_iswupper(c)) { CFMutableStringRef cfString = CFStringCreateMutable( kCFAllocatorDefault, 1); if (cfString != NULL) { CFStringAppendCharacters(cfString, (const UniChar*)&c, 1); CFStringUppercase(cfString, NULL); c = CFStringGetCharacterAtIndex(cfString, 0); CFRelease(cfString); } } LOGEXIT("towupper returns int %d\n", c ); PERF_EXIT(towupper); return c; #else /* HAVE_COREFOUNDATION */ UnicodeDataRec dataRec; PERF_ENTRY(towupper); ENTRY("towupper (c=%d)\n", c); if (!GetUnicodeData(c, &dataRec)) { TRACE( "Unable to retrieve unicode data for the character %c.\n", c ); LOGEXIT("towupper returns int %d\n", c ); PERF_EXIT(towupper); return c; } if ( (dataRec.C1_TYPE_FLAGS & C1_UPPER) || (dataRec.nOpposingCase == 0 )) { LOGEXIT("towupper returns int %d\n", c ); PERF_EXIT(towupper); return c; } else { LOGEXIT("towupper returns int %d\n", dataRec.nOpposingCase ); PERF_EXIT(towupper); return dataRec.nOpposingCase; } #endif /* HAVE_COREFOUNDATION */ }
wchar_t towlower (wchar_t wc) { CFMutableStringRef strRef = CFStringCreateMutable (NULL, 0); UniChar c = (UniChar) wc; wchar_t wcs; CFStringAppendCharacters (strRef, &c, 1); CFStringLowercase (strRef, NULL); wcs = CFStringGetCharacterAtIndex (strRef, 0); CFRelease (strRef); return wcs; }
CFStringRef convert_wchar_to_CFString(wchar_t *str) { CFMutableStringRef prov = CFStringCreateMutable(NULL, 0); CFIndex i; UniChar c; if(prov) { for(i = 0 ; str[i] != L'\0' ; i++) { c = (UniChar)str[i]; CFStringAppendCharacters(prov, &c, 1); } } return prov; }
/*++ Function: PAL_towlower See MSDN --*/ wchar_16 __cdecl PAL_towlower( wchar_16 c ) { #if HAVE_CFSTRING ENTRY("towlower (c=%d)\n", c); if (!PAL_iswlower(c)) { CFMutableStringRef cfString = CFStringCreateMutable( kCFAllocatorDefault, 1); if (cfString != NULL) { CFStringAppendCharacters(cfString, &c, 1); CFStringLowercase(cfString, NULL); c = CFStringGetCharacterAtIndex(cfString, 0); CFRelease(cfString); } } ENTRY("towlower returns int %d\n", c ); return c; #else /* HAVE_CFSTRING */ UnicodeDataRec dataRec; ENTRY("towlower (c=%d)\n", c); if (!GetUnicodeData(c, &dataRec)) { TRACE( "Unable to retrive unicode data for the character %c.\n", c ); LOGEXIT("towlower returns int %d\n", c ); return c; } if ( (dataRec.C1_TYPE_FLAGS & C1_LOWER) || (dataRec.nOpposingCase == 0 )) { LOGEXIT("towlower returns int %d\n", c ); return c; } else { LOGEXIT("towlower returns int %d\n", dataRec.nOpposingCase ); return dataRec.nOpposingCase; } #endif /* HAVE_CFSTRING */ }
CF_INLINE void _fillStringWithCharacters(CFMutableStringRef string, UniChar *characters, CFIndex numChars) { CFStringDelete(string, CFRangeMake(0, CFStringGetLength(string))); if (numChars) { CFStringAppendCharacters(string, characters, numChars); } }
static CFURLRef _CFCreateHomeDirectoryURLForUser(CFStringRef uName) { #if defined(__MACH__) || defined(__svr4__) || defined(__hpux__) || defined(__LINUX__) || defined(__FREEBSD__) if (!uName) { if (geteuid() != __CFEUID || getuid() != __CFUID || !__CFHomeDirectory) _CFUpdateUserInfo(); if (__CFHomeDirectory) CFRetain(__CFHomeDirectory); return __CFHomeDirectory; } else { struct passwd *upwd = NULL; char buf[128], *user; SInt32 len = CFStringGetLength(uName), size = CFStringGetMaximumSizeForEncoding(len, kCFPlatformInterfaceStringEncoding); CFIndex usedSize; if (size < 127) { user = buf; } else { user = CFAllocatorAllocate(kCFAllocatorDefault, size+1, 0); if (__CFOASafe) __CFSetLastAllocationEventName(user, "CFUtilities (temp)"); } if (CFStringGetBytes(uName, CFRangeMake(0, len), kCFPlatformInterfaceStringEncoding, 0, true, user, size, &usedSize) == len) { user[usedSize] = '\0'; upwd = getpwnam(user); } if (buf != user) { CFAllocatorDeallocate(kCFAllocatorDefault, user); } return _CFCopyHomeDirURLForUser(upwd); } #elif defined(__WIN32__) #warning CF: Windows home directory goop disabled return NULL; #if 0 CFString *user = !uName ? CFUserName() : uName; if (!uName || CFEqual(user, CFUserName())) { const char *cpath = getenv("HOMEPATH"); const char *cdrive = getenv("HOMEDRIVE"); if (cdrive && cpath) { char fullPath[CFMaxPathSize]; CFStringRef str; strcpy(fullPath, cdrive); strncat(fullPath, cpath, CFMaxPathSize-strlen(cdrive)-1); str = CFStringCreateWithCString(NULL, fullPath, kCFPlatformInterfaceStringEncoding); home = CFURLCreateWithFileSystemPath(NULL, str, kCFURLWindowsPathStyle, true); CFRelease(str); } } if (!home) { struct _USER_INFO_2 *userInfo; HINSTANCE hinstDll = GetModuleHandleA("NETAPI32"); if (!hinstDll) hinstDll = LoadLibraryEx("NETAPI32", NULL, 0); if (hinstDll) { FARPROC lpfn = GetProcAddress(hinstDll, "NetUserGetInfo"); if (lpfn) { unsigned namelen = CFStringGetLength(user); UniChar *username; username = CFAllocatorAllocate(kCFAllocatorDefault, sizeof(UniChar) * (namelen + 1), 0); if (__CFOASafe) __CFSetLastAllocationEventName(username, "CFUtilities (temp)"); CFStringGetCharacters(user, CFRangeMake(0, namelen), username); if (!(*lpfn)(NULL, (LPWSTR)username, 2, (LPBYTE *)&userInfo)) { UInt32 len = 0; CFMutableStringRef str; while (userInfo->usri2_home_dir[len] != 0) len ++; str = CFStringCreateMutable(NULL, len+1); CFStringAppendCharacters(str, userInfo->usri2_home_dir, len); home = CFURLCreateWithFileSystemPath(NULL, str, kCFURLWindowsPathStyle, true); CFRelease(str); } CFAllocatorDeallocate(kCFAllocatorDefault, username); } } else { } } // We could do more here (as in KB Article Q101507). If that article is to // be believed, we should only run into this case on Win95, or through // user error. if (CFStringGetLength(CFURLGetPath(home)) == 0) { CFRelease(home); home=NULL; } #endif #else #error Dont know how to compute users home directories on this platform #endif }