merged tag ooo/DEV300_m102
[LibreOffice.git] / sal / osl / os2 / path_helper.cxx
blob6425927a0021b4536e17d28064ccf4fd95ff8d54
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 /*******************************************************************
29 Includes
30 ******************************************************************/
32 #include "path_helper.hxx"
33 #include <osl/diagnose.h>
34 #include <rtl/ustring.hxx>
36 #include <algorithm>
37 #include <wchar.h>
38 #include <wctype.h>
40 /*******************************************************************
41 Constants
42 ******************************************************************/
44 const rtl::OUString BACKSLASH = rtl::OUString::createFromAscii("\\");
45 const rtl::OUString SLASH = rtl::OUString::createFromAscii("/");
47 /*******************************************************************
48 osl_systemPathEnsureSeparator
49 ******************************************************************/
51 void osl_systemPathEnsureSeparator(/*inout*/ rtl_uString** ppustrPath)
53 OSL_PRECOND(ppustrPath && (NULL != *ppustrPath), \
54 "osl_systemPathEnsureSeparator: Invalid parameter");
56 rtl::OUString path(*ppustrPath);
57 sal_Int32 i = std::max<sal_Int32>(path.lastIndexOf(BACKSLASH), path.lastIndexOf(SLASH));
59 if (i < (path.getLength()-1))
61 path += BACKSLASH;
62 rtl_uString_assign(ppustrPath, path.pData);
65 OSL_POSTCOND(path.lastIndexOf(BACKSLASH) == (path.getLength() - 1), \
66 "osl_systemPathEnsureSeparator: Post condition failed");
69 /*******************************************************************
70 osl_systemPathRemoveSeparator
71 ******************************************************************/
73 void SAL_CALL osl_systemPathRemoveSeparator(/*inout*/ rtl_uString** ppustrPath)
75 rtl::OUString path(*ppustrPath);
77 if (!osl::systemPathIsLogicalDrivePattern(path))
79 sal_Int32 i = std::max<sal_Int32>(path.lastIndexOf(BACKSLASH), path.lastIndexOf(SLASH));
81 if (i > -1 && (i == (path.getLength() - 1)))
83 path = rtl::OUString(path.getStr(), path.getLength() - 1);
84 rtl_uString_assign(ppustrPath, path.pData);
89 /*******************************************************************
90 osl_is_logical_drive_pattern
91 ******************************************************************/
93 // is [A-Za-z]:[/|\]\0
94 const sal_Unicode* LDP = L":";
95 const sal_Unicode* LDP_WITH_BACKSLASH = L":\\";
96 const sal_Unicode* LDP_WITH_SLASH = L":/";
98 // degenerated case returned by the Windows FileOpen dialog
99 // when someone enters for instance "x:filename", the Win32
100 // API accepts this case
101 const sal_Unicode* LDP_WITH_DOT_BACKSLASH = L":.\\";
103 sal_Int32 osl_systemPathIsLogicalDrivePattern(/*in*/ const rtl_uString* pustrPath)
105 const sal_Unicode* p = rtl_uString_getStr(const_cast<rtl_uString*>(pustrPath));
106 if (iswalpha(*p++))
108 return ((0 == wcscmp(p, LDP)) ||
109 (0 == wcscmp(p, LDP_WITH_BACKSLASH)) ||
110 (0 == wcscmp(p, LDP_WITH_SLASH)) ||
111 (0 == wcscmp(p, LDP_WITH_DOT_BACKSLASH)));
113 return 0;