update dev300-m58
[ooovba.git] / fpicker / source / aqua / CFStringUtilities.cxx
blobeec4530f87a5cc43c008c11b1f217b8014b98eeb
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: CFStringUtilities.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <osl/diagnose.h>
32 #include "CFStringUtilities.hxx"
34 rtl::OUString CFStringToOUString(const CFStringRef sOrig) {
35 //DBG_PRINT_ENTRY("CFStringUtilities", __func__, "sOrig", sOrig);
37 if (NULL == sOrig) {
38 return rtl::OUString();
41 CFRetain(sOrig);
42 CFIndex nFileNameLength = CFStringGetLength(sOrig);
43 //OSL_TRACE("FH: string length: %d", (int)(nFileNameLength));
44 UniChar unichars[nFileNameLength+1];
45 //'close' the string buffer correctly
46 unichars[nFileNameLength] = '\0';
48 CFStringGetCharacters (sOrig, CFRangeMake(0,nFileNameLength), unichars);
50 //we no longer need the original string
51 CFRelease(sOrig);
53 //DBG_PRINT_EXIT("CFStringUtilities", __func__, unichars);
55 return rtl::OUString(unichars);
58 CFStringRef CFStringCreateWithOUString(const rtl::OUString& aString) {
59 //DBG_PRINT_ENTRY("CFStringUtilities", __func__);
61 CFStringRef ref = CFStringCreateWithCharacters(kCFAllocatorDefault, aString.getStr(), aString.getLength());
63 //DBG_PRINT_EXIT("CFStringUtilities", __func__, ref);
65 return ref;
68 rtl::OUString FSRefToOUString(FSRef fsRef, InfoType info)
70 //DBG_PRINT_ENTRY("CFStringUtilities", __func__);
72 CFURLRef aUrlRef = CFURLCreateFromFSRef(NULL, &fsRef);
74 rtl::OUString sResult = CFURLRefToOUString(aUrlRef, info);
76 //we no longer need the CFURLRef
77 CFRelease(aUrlRef);
79 //DBG_PRINT_EXIT("CFStringUtilities", __func__, OUStringToOString(sResult, RTL_TEXTENCODING_UTF8).getStr());
81 return sResult;
84 rtl::OUString CFURLRefToOUString(CFURLRef aUrlRef, InfoType info)
86 //DBG_PRINT_ENTRY("CFStringUtilities", __func__);
88 CFStringRef sURLString = NULL;
90 switch(info) {
91 case FULLPATH:
92 OSL_TRACE("Extracting the full path of an item");
93 sURLString = CFURLGetString(aUrlRef);
94 CFRetain(sURLString);
95 break;
96 case FILENAME:
97 OSL_TRACE("Extracting the file name of an item");
98 CFStringRef fullString = CFURLGetString(aUrlRef);
99 CFURLRef dirRef = CFURLCreateCopyDeletingLastPathComponent(NULL,aUrlRef);
100 CFIndex dirLength = CFStringGetLength(CFURLGetString(dirRef));
101 CFRelease(dirRef);
102 CFIndex fullLength = CFStringGetLength(fullString);
103 CFRange substringRange = CFRangeMake(dirLength, fullLength - dirLength);
104 sURLString = CFStringCreateWithSubstring(NULL, fullString, substringRange);
105 break;
106 case PATHWITHOUTLASTCOMPONENT:
107 OSL_TRACE("Extracting the last but one component of an item's path");
108 CFURLRef directoryRef = CFURLCreateCopyDeletingLastPathComponent(NULL,aUrlRef);
109 sURLString = CFURLGetString(directoryRef);
110 CFRetain(sURLString);
111 CFRelease(directoryRef);
112 break;
113 default:
114 break;
117 rtl::OUString sResult = CFStringToOUString(sURLString);
119 CFRelease(sURLString);
121 //DBG_PRINT_EXIT("CFStringUtilities", __func__, OUStringToOString(sResult, RTL_TEXTENCODING_UTF8).getStr());
123 return sResult;