xref: /aoo4110/main/sal/osl/os2/path_helper.cxx (revision b1cdbd2c)
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 /*******************************************************************
25  Includes
26  ******************************************************************/
27 
28 #include "path_helper.hxx"
29 #include <osl/diagnose.h>
30 #include <rtl/ustring.hxx>
31 
32 #include <algorithm>
33 #include <wchar.h>
34 #include <wctype.h>
35 
36 /*******************************************************************
37  Constants
38  ******************************************************************/
39 
40 const rtl::OUString BACKSLASH = rtl::OUString::createFromAscii("\\");
41 const rtl::OUString SLASH     = rtl::OUString::createFromAscii("/");
42 
43 /*******************************************************************
44  osl_systemPathEnsureSeparator
45  ******************************************************************/
46 
osl_systemPathEnsureSeparator(rtl_uString ** ppustrPath)47 void osl_systemPathEnsureSeparator(/*inout*/ rtl_uString** ppustrPath)
48 {
49     OSL_PRECOND(ppustrPath && (NULL != *ppustrPath), \
50 				"osl_systemPathEnsureSeparator: Invalid parameter");
51 
52  	rtl::OUString path(*ppustrPath);
53 	sal_Int32     i = std::max<sal_Int32>(path.lastIndexOf(BACKSLASH), path.lastIndexOf(SLASH));
54 
55 	if (i < (path.getLength()-1))
56 	{
57 		path += BACKSLASH;
58 		rtl_uString_assign(ppustrPath, path.pData);
59 	}
60 
61 	OSL_POSTCOND(path.lastIndexOf(BACKSLASH) == (path.getLength() - 1), \
62 				 "osl_systemPathEnsureSeparator: Post condition failed");
63 }
64 
65 /*******************************************************************
66  osl_systemPathRemoveSeparator
67  ******************************************************************/
68 
osl_systemPathRemoveSeparator(rtl_uString ** ppustrPath)69 void SAL_CALL osl_systemPathRemoveSeparator(/*inout*/ rtl_uString** ppustrPath)
70 {
71     rtl::OUString path(*ppustrPath);
72 
73     if (!osl::systemPathIsLogicalDrivePattern(path))
74     {
75         sal_Int32 i = std::max<sal_Int32>(path.lastIndexOf(BACKSLASH), path.lastIndexOf(SLASH));
76 
77         if (i > -1 && (i == (path.getLength() - 1)))
78         {
79             path = rtl::OUString(path.getStr(), path.getLength() - 1);
80             rtl_uString_assign(ppustrPath, path.pData);
81         }
82     }
83 }
84 
85 /*******************************************************************
86  osl_is_logical_drive_pattern
87  ******************************************************************/
88 
89 // is [A-Za-z]:[/|\]\0
90 const sal_Unicode* LDP                    = L":";
91 const sal_Unicode* LDP_WITH_BACKSLASH     = L":\\";
92 const sal_Unicode* LDP_WITH_SLASH         = L":/";
93 
94 // degenerated case returned by the Windows FileOpen dialog
95 // when someone enters for instance "x:filename", the Win32
96 // API accepts this case
97 const sal_Unicode* LDP_WITH_DOT_BACKSLASH = L":.\\";
98 
osl_systemPathIsLogicalDrivePattern(const rtl_uString * pustrPath)99 sal_Int32 osl_systemPathIsLogicalDrivePattern(/*in*/ const rtl_uString* pustrPath)
100 {
101     const sal_Unicode* p = rtl_uString_getStr(const_cast<rtl_uString*>(pustrPath));
102     if (iswalpha(*p++))
103     {
104         return ((0 == wcscmp(p, LDP)) ||
105                 (0 == wcscmp(p, LDP_WITH_BACKSLASH)) ||
106                 (0 == wcscmp(p, LDP_WITH_SLASH)) ||
107                 (0 == wcscmp(p, LDP_WITH_DOT_BACKSLASH)));
108     }
109     return 0;
110 }
111 
112 
113