merge the formfield patch from ooo-build
[ooovba.git] / sal / osl / os2 / path_helper.cxx
blobdad0dc91053322a5af3a66838b2317a47b260b3d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: path_helper.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 /*******************************************************************
32 Includes
33 ******************************************************************/
35 #include "path_helper.hxx"
36 #include <osl/diagnose.h>
37 #include <rtl/ustring.hxx>
39 #include <algorithm>
40 #include <wchar.h>
41 #include <wctype.h>
43 /*******************************************************************
44 Constants
45 ******************************************************************/
47 const rtl::OUString BACKSLASH = rtl::OUString::createFromAscii("\\");
48 const rtl::OUString SLASH = rtl::OUString::createFromAscii("/");
50 /*******************************************************************
51 osl_systemPathEnsureSeparator
52 ******************************************************************/
54 void osl_systemPathEnsureSeparator(/*inout*/ rtl_uString** ppustrPath)
56 OSL_PRECOND(ppustrPath && (NULL != *ppustrPath), \
57 "osl_systemPathEnsureSeparator: Invalid parameter");
59 rtl::OUString path(*ppustrPath);
60 sal_Int32 i = std::max<sal_Int32>(path.lastIndexOf(BACKSLASH), path.lastIndexOf(SLASH));
62 if (i < (path.getLength()-1))
64 path += BACKSLASH;
65 rtl_uString_assign(ppustrPath, path.pData);
68 OSL_POSTCOND(path.lastIndexOf(BACKSLASH) == (path.getLength() - 1), \
69 "osl_systemPathEnsureSeparator: Post condition failed");
72 /*******************************************************************
73 osl_systemPathRemoveSeparator
74 ******************************************************************/
76 void SAL_CALL osl_systemPathRemoveSeparator(/*inout*/ rtl_uString** ppustrPath)
78 rtl::OUString path(*ppustrPath);
80 if (!osl::systemPathIsLogicalDrivePattern(path))
82 sal_Int32 i = std::max<sal_Int32>(path.lastIndexOf(BACKSLASH), path.lastIndexOf(SLASH));
84 if (i > -1 && (i == (path.getLength() - 1)))
86 path = rtl::OUString(path.getStr(), path.getLength() - 1);
87 rtl_uString_assign(ppustrPath, path.pData);
92 /*******************************************************************
93 osl_is_logical_drive_pattern
94 ******************************************************************/
96 // is [A-Za-z]:[/|\]\0
97 const sal_Unicode* LDP = L":";
98 const sal_Unicode* LDP_WITH_BACKSLASH = L":\\";
99 const sal_Unicode* LDP_WITH_SLASH = L":/";
101 // degenerated case returned by the Windows FileOpen dialog
102 // when someone enters for instance "x:filename", the Win32
103 // API accepts this case
104 const sal_Unicode* LDP_WITH_DOT_BACKSLASH = L":.\\";
106 sal_Int32 osl_systemPathIsLogicalDrivePattern(/*in*/ const rtl_uString* pustrPath)
108 const sal_Unicode* p = rtl_uString_getStr(const_cast<rtl_uString*>(pustrPath));
109 if (iswalpha(*p++))
111 return ((0 == wcscmp(p, LDP)) ||
112 (0 == wcscmp(p, LDP_WITH_BACKSLASH)) ||
113 (0 == wcscmp(p, LDP_WITH_SLASH)) ||
114 (0 == wcscmp(p, LDP_WITH_DOT_BACKSLASH)));
116 return 0;