xref: /AOO41X/main/fpicker/source/aqua/CFStringUtilities.cxx (revision d192718aaac0a0c9475b45bba5eb4bfda1fc0446)
1b557fc96SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3b557fc96SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4b557fc96SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5b557fc96SAndrew Rist  * distributed with this work for additional information
6b557fc96SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7b557fc96SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8b557fc96SAndrew Rist  * "License"); you may not use this file except in compliance
9b557fc96SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11b557fc96SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13b557fc96SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14b557fc96SAndrew Rist  * software distributed under the License is distributed on an
15b557fc96SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16b557fc96SAndrew Rist  * KIND, either express or implied.  See the License for the
17b557fc96SAndrew Rist  * specific language governing permissions and limitations
18b557fc96SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20b557fc96SAndrew Rist  *************************************************************/
21b557fc96SAndrew Rist 
22b557fc96SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <osl/diagnose.h>
25cdf0e10cSrcweir #include "CFStringUtilities.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir rtl::OUString CFStringToOUString(const CFStringRef sOrig) {
28cdf0e10cSrcweir     //DBG_PRINT_ENTRY("CFStringUtilities", __func__, "sOrig", sOrig);
29cdf0e10cSrcweir 
30cdf0e10cSrcweir     if (NULL == sOrig) {
31cdf0e10cSrcweir         return rtl::OUString();
32cdf0e10cSrcweir     }
33cdf0e10cSrcweir 
34cdf0e10cSrcweir     CFRetain(sOrig);
35cdf0e10cSrcweir     CFIndex nFileNameLength = CFStringGetLength(sOrig);
36cdf0e10cSrcweir     //OSL_TRACE("FH: string length: %d", (int)(nFileNameLength));
37cdf0e10cSrcweir     UniChar unichars[nFileNameLength+1];
38cdf0e10cSrcweir     //'close' the string buffer correctly
39cdf0e10cSrcweir     unichars[nFileNameLength] = '\0';
40cdf0e10cSrcweir 
41cdf0e10cSrcweir     CFStringGetCharacters (sOrig, CFRangeMake(0,nFileNameLength), unichars);
42cdf0e10cSrcweir 
43cdf0e10cSrcweir     //we no longer need the original string
44cdf0e10cSrcweir     CFRelease(sOrig);
45cdf0e10cSrcweir 
46cdf0e10cSrcweir     //DBG_PRINT_EXIT("CFStringUtilities", __func__, unichars);
47cdf0e10cSrcweir 
48cdf0e10cSrcweir     return rtl::OUString(unichars);
49cdf0e10cSrcweir }
50cdf0e10cSrcweir 
51cdf0e10cSrcweir CFStringRef CFStringCreateWithOUString(const rtl::OUString& aString) {
52cdf0e10cSrcweir     //DBG_PRINT_ENTRY("CFStringUtilities", __func__);
53cdf0e10cSrcweir 
54cdf0e10cSrcweir     CFStringRef ref = CFStringCreateWithCharacters(kCFAllocatorDefault, aString.getStr(), aString.getLength());
55cdf0e10cSrcweir 
56cdf0e10cSrcweir     //DBG_PRINT_EXIT("CFStringUtilities", __func__, ref);
57cdf0e10cSrcweir 
58cdf0e10cSrcweir     return ref;
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
61cdf0e10cSrcweir rtl::OUString FSRefToOUString(FSRef fsRef, InfoType info)
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     //DBG_PRINT_ENTRY("CFStringUtilities", __func__);
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     CFURLRef aUrlRef = CFURLCreateFromFSRef(NULL, &fsRef);
66cdf0e10cSrcweir 
67cdf0e10cSrcweir     rtl::OUString sResult = CFURLRefToOUString(aUrlRef, info);
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     //we no longer need the CFURLRef
70cdf0e10cSrcweir     CFRelease(aUrlRef);
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     //DBG_PRINT_EXIT("CFStringUtilities", __func__, OUStringToOString(sResult, RTL_TEXTENCODING_UTF8).getStr());
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     return sResult;
75cdf0e10cSrcweir }
76cdf0e10cSrcweir 
77cdf0e10cSrcweir rtl::OUString CFURLRefToOUString(CFURLRef aUrlRef, InfoType info)
78cdf0e10cSrcweir {
79cdf0e10cSrcweir     //DBG_PRINT_ENTRY("CFStringUtilities", __func__);
80cdf0e10cSrcweir 
81cdf0e10cSrcweir     CFStringRef sURLString = NULL;
82cdf0e10cSrcweir 
83cdf0e10cSrcweir     switch(info) {
84cdf0e10cSrcweir         case FULLPATH:
85cdf0e10cSrcweir             OSL_TRACE("Extracting the full path of an item");
86cdf0e10cSrcweir             sURLString = CFURLGetString(aUrlRef);
87cdf0e10cSrcweir             CFRetain(sURLString);
88cdf0e10cSrcweir             break;
89*d192718aSHerbert Dürr         case FILENAME: {
90cdf0e10cSrcweir             OSL_TRACE("Extracting the file name of an item");
91cdf0e10cSrcweir             CFStringRef fullString = CFURLGetString(aUrlRef);
92cdf0e10cSrcweir             CFURLRef dirRef = CFURLCreateCopyDeletingLastPathComponent(NULL,aUrlRef);
93cdf0e10cSrcweir             CFIndex dirLength = CFStringGetLength(CFURLGetString(dirRef));
94cdf0e10cSrcweir             CFRelease(dirRef);
95cdf0e10cSrcweir             CFIndex fullLength = CFStringGetLength(fullString);
96cdf0e10cSrcweir             CFRange substringRange = CFRangeMake(dirLength, fullLength - dirLength);
97cdf0e10cSrcweir             sURLString = CFStringCreateWithSubstring(NULL, fullString, substringRange);
98*d192718aSHerbert Dürr             } break;
99*d192718aSHerbert Dürr         case PATHWITHOUTLASTCOMPONENT: {
100cdf0e10cSrcweir             OSL_TRACE("Extracting the last but one component of an item's path");
101cdf0e10cSrcweir             CFURLRef directoryRef = CFURLCreateCopyDeletingLastPathComponent(NULL,aUrlRef);
102cdf0e10cSrcweir             sURLString = CFURLGetString(directoryRef);
103cdf0e10cSrcweir             CFRetain(sURLString);
104cdf0e10cSrcweir             CFRelease(directoryRef);
105*d192718aSHerbert Dürr             } break;
106cdf0e10cSrcweir         default:
107cdf0e10cSrcweir             break;
108cdf0e10cSrcweir     }
109cdf0e10cSrcweir 
110cdf0e10cSrcweir     rtl::OUString sResult = CFStringToOUString(sURLString);
111cdf0e10cSrcweir 
112cdf0e10cSrcweir     CFRelease(sURLString);
113cdf0e10cSrcweir 
114cdf0e10cSrcweir     //DBG_PRINT_EXIT("CFStringUtilities", __func__, OUStringToOString(sResult, RTL_TEXTENCODING_UTF8).getStr());
115cdf0e10cSrcweir 
116cdf0e10cSrcweir     return sResult;
117cdf0e10cSrcweir }
118