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