Update ooo320-m1
[ooovba.git] / sal / osl / w32 / path_helper.cxx
blob4a54a3ad35a1985272f368dd678564ae6397271e
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.6 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sal.hxx"
34 /*******************************************************************
35 Includes
36 ******************************************************************/
38 #include "path_helper.hxx"
39 #include <osl/diagnose.h>
40 #include <rtl/ustring.hxx>
42 #include <algorithm>
43 #include <wchar.h>
45 /*******************************************************************
46 Constants
47 ******************************************************************/
49 const rtl::OUString BACKSLASH = rtl::OUString::createFromAscii("\\");
50 const rtl::OUString SLASH = rtl::OUString::createFromAscii("/");
52 /*******************************************************************
53 osl_systemPathEnsureSeparator
54 ******************************************************************/
56 void osl_systemPathEnsureSeparator(/*inout*/ rtl_uString** ppustrPath)
58 OSL_PRECOND(ppustrPath && (NULL != *ppustrPath), \
59 "osl_systemPathEnsureSeparator: Invalid parameter");
61 rtl::OUString path(*ppustrPath);
62 sal_Int32 i = std::max<sal_Int32>(path.lastIndexOf(BACKSLASH), path.lastIndexOf(SLASH));
64 if (i < (path.getLength()-1))
66 path += BACKSLASH;
67 rtl_uString_assign(ppustrPath, path.pData);
70 OSL_POSTCOND(path.lastIndexOf(BACKSLASH) == (path.getLength() - 1), \
71 "osl_systemPathEnsureSeparator: Post condition failed");
74 /*******************************************************************
75 osl_systemPathRemoveSeparator
76 ******************************************************************/
78 void SAL_CALL osl_systemPathRemoveSeparator(/*inout*/ rtl_uString** ppustrPath)
80 rtl::OUString path(*ppustrPath);
82 if (!osl::systemPathIsLogicalDrivePattern(path))
84 sal_Int32 i = std::max<sal_Int32>(path.lastIndexOf(BACKSLASH), path.lastIndexOf(SLASH));
86 if (i > -1 && (i == (path.getLength() - 1)))
88 path = rtl::OUString(path.getStr(), path.getLength() - 1);
89 rtl_uString_assign(ppustrPath, path.pData);
94 /*******************************************************************
95 osl_is_logical_drive_pattern
96 ******************************************************************/
98 // is [A-Za-z]:[/|\]\0
99 const sal_Char* LDP = ":";
100 const sal_Char* LDP_WITH_BACKSLASH = ":\\";
101 const sal_Char* LDP_WITH_SLASH = ":/";
103 // degenerated case returned by the Windows FileOpen dialog
104 // when someone enters for instance "x:filename", the Win32
105 // API accepts this case
106 const sal_Char* LDP_WITH_DOT_BACKSLASH = ":.\\";
108 sal_Int32 osl_systemPathIsLogicalDrivePattern(/*in*/ const rtl_uString* pustrPath)
110 const sal_Unicode* p = rtl_uString_getStr(const_cast<rtl_uString*>(pustrPath));
111 if (iswalpha(*p++))
113 return ((0 == rtl_ustr_ascii_compare(p, LDP)) ||
114 (0 == rtl_ustr_ascii_compare(p, LDP_WITH_BACKSLASH)) ||
115 (0 == rtl_ustr_ascii_compare(p, LDP_WITH_SLASH)) ||
116 (0 == rtl_ustr_ascii_compare(p, LDP_WITH_DOT_BACKSLASH)));
118 return 0;