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