xref: /aoo42x/main/sal/osl/w32/path_helper.cxx (revision 87d2adbc)
1*87d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*87d2adbcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*87d2adbcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*87d2adbcSAndrew Rist  * distributed with this work for additional information
6*87d2adbcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*87d2adbcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*87d2adbcSAndrew Rist  * "License"); you may not use this file except in compliance
9*87d2adbcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*87d2adbcSAndrew Rist  *
11*87d2adbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*87d2adbcSAndrew Rist  *
13*87d2adbcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*87d2adbcSAndrew Rist  * software distributed under the License is distributed on an
15*87d2adbcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*87d2adbcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*87d2adbcSAndrew Rist  * specific language governing permissions and limitations
18*87d2adbcSAndrew Rist  * under the License.
19*87d2adbcSAndrew Rist  *
20*87d2adbcSAndrew Rist  *************************************************************/
21*87d2adbcSAndrew Rist 
22*87d2adbcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sal.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir /*******************************************************************
28cdf0e10cSrcweir  Includes
29cdf0e10cSrcweir  ******************************************************************/
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include "path_helper.hxx"
32cdf0e10cSrcweir #include <osl/diagnose.h>
33cdf0e10cSrcweir #include <rtl/ustring.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <algorithm>
36cdf0e10cSrcweir #include <wchar.h>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir /*******************************************************************
39cdf0e10cSrcweir  Constants
40cdf0e10cSrcweir  ******************************************************************/
41cdf0e10cSrcweir 
42cdf0e10cSrcweir const rtl::OUString BACKSLASH = rtl::OUString::createFromAscii("\\");
43cdf0e10cSrcweir const rtl::OUString SLASH     = rtl::OUString::createFromAscii("/");
44cdf0e10cSrcweir 
45cdf0e10cSrcweir /*******************************************************************
46cdf0e10cSrcweir  osl_systemPathEnsureSeparator
47cdf0e10cSrcweir  ******************************************************************/
48cdf0e10cSrcweir 
osl_systemPathEnsureSeparator(rtl_uString ** ppustrPath)49cdf0e10cSrcweir void osl_systemPathEnsureSeparator(/*inout*/ rtl_uString** ppustrPath)
50cdf0e10cSrcweir {
51cdf0e10cSrcweir     OSL_PRECOND(ppustrPath && (NULL != *ppustrPath), \
52cdf0e10cSrcweir 				"osl_systemPathEnsureSeparator: Invalid parameter");
53cdf0e10cSrcweir 
54cdf0e10cSrcweir  	rtl::OUString path(*ppustrPath);
55cdf0e10cSrcweir 	sal_Int32     i = std::max<sal_Int32>(path.lastIndexOf(BACKSLASH), path.lastIndexOf(SLASH));
56cdf0e10cSrcweir 
57cdf0e10cSrcweir 	if (i < (path.getLength()-1))
58cdf0e10cSrcweir 	{
59cdf0e10cSrcweir 		path += BACKSLASH;
60cdf0e10cSrcweir 		rtl_uString_assign(ppustrPath, path.pData);
61cdf0e10cSrcweir 	}
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 	OSL_POSTCOND(path.lastIndexOf(BACKSLASH) == (path.getLength() - 1), \
64cdf0e10cSrcweir 				 "osl_systemPathEnsureSeparator: Post condition failed");
65cdf0e10cSrcweir }
66cdf0e10cSrcweir 
67cdf0e10cSrcweir /*******************************************************************
68cdf0e10cSrcweir  osl_systemPathRemoveSeparator
69cdf0e10cSrcweir  ******************************************************************/
70cdf0e10cSrcweir 
osl_systemPathRemoveSeparator(rtl_uString ** ppustrPath)71cdf0e10cSrcweir void SAL_CALL osl_systemPathRemoveSeparator(/*inout*/ rtl_uString** ppustrPath)
72cdf0e10cSrcweir {
73cdf0e10cSrcweir     rtl::OUString path(*ppustrPath);
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     if (!osl::systemPathIsLogicalDrivePattern(path))
76cdf0e10cSrcweir     {
77cdf0e10cSrcweir         sal_Int32 i = std::max<sal_Int32>(path.lastIndexOf(BACKSLASH), path.lastIndexOf(SLASH));
78cdf0e10cSrcweir 
79cdf0e10cSrcweir         if (i > -1 && (i == (path.getLength() - 1)))
80cdf0e10cSrcweir         {
81cdf0e10cSrcweir             path = rtl::OUString(path.getStr(), path.getLength() - 1);
82cdf0e10cSrcweir             rtl_uString_assign(ppustrPath, path.pData);
83cdf0e10cSrcweir         }
84cdf0e10cSrcweir     }
85cdf0e10cSrcweir }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir /*******************************************************************
88cdf0e10cSrcweir  osl_is_logical_drive_pattern
89cdf0e10cSrcweir  ******************************************************************/
90cdf0e10cSrcweir 
91cdf0e10cSrcweir // is [A-Za-z]:[/|\]\0
92cdf0e10cSrcweir const sal_Char* LDP                    = ":";
93cdf0e10cSrcweir const sal_Char* LDP_WITH_BACKSLASH     = ":\\";
94cdf0e10cSrcweir const sal_Char* LDP_WITH_SLASH         = ":/";
95cdf0e10cSrcweir 
96cdf0e10cSrcweir // degenerated case returned by the Windows FileOpen dialog
97cdf0e10cSrcweir // when someone enters for instance "x:filename", the Win32
98cdf0e10cSrcweir // API accepts this case
99cdf0e10cSrcweir const sal_Char* LDP_WITH_DOT_BACKSLASH = ":.\\";
100cdf0e10cSrcweir 
osl_systemPathIsLogicalDrivePattern(const rtl_uString * pustrPath)101cdf0e10cSrcweir sal_Int32 osl_systemPathIsLogicalDrivePattern(/*in*/ const rtl_uString* pustrPath)
102cdf0e10cSrcweir {
103cdf0e10cSrcweir     const sal_Unicode* p = rtl_uString_getStr(const_cast<rtl_uString*>(pustrPath));
104cdf0e10cSrcweir     if (iswalpha(*p++))
105cdf0e10cSrcweir     {
106cdf0e10cSrcweir         return ((0 == rtl_ustr_ascii_compare(p, LDP)) ||
107cdf0e10cSrcweir                 (0 == rtl_ustr_ascii_compare(p, LDP_WITH_BACKSLASH)) ||
108cdf0e10cSrcweir                 (0 == rtl_ustr_ascii_compare(p, LDP_WITH_SLASH)) ||
109cdf0e10cSrcweir                 (0 == rtl_ustr_ascii_compare(p, LDP_WITH_DOT_BACKSLASH)));
110cdf0e10cSrcweir     }
111cdf0e10cSrcweir     return 0;
112cdf0e10cSrcweir }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 
115