Bump for 3.6-28
[LibreOffice.git] / sal / osl / w32 / path_helper.cxx
blobddbb1a43512e17e9b67511662768f437a95b27c8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 /*******************************************************************
31 Includes
32 ******************************************************************/
34 #include "path_helper.hxx"
35 #include <osl/diagnose.h>
36 #include <rtl/ustring.hxx>
38 #include <algorithm>
39 #include <wchar.h>
41 /*******************************************************************
42 Constants
43 ******************************************************************/
45 const rtl::OUString BACKSLASH ("\\");
46 const rtl::OUString SLASH ("/");
48 /*******************************************************************
49 osl_systemPathEnsureSeparator
50 ******************************************************************/
52 void osl_systemPathEnsureSeparator(/*inout*/ rtl_uString** ppustrPath)
54 OSL_PRECOND(ppustrPath && (NULL != *ppustrPath), \
55 "osl_systemPathEnsureSeparator: Invalid parameter");
57 rtl::OUString path(*ppustrPath);
58 sal_Int32 i = std::max<sal_Int32>(path.lastIndexOf(BACKSLASH), path.lastIndexOf(SLASH));
60 if (i < (path.getLength()-1))
62 path += BACKSLASH;
63 rtl_uString_assign(ppustrPath, path.pData);
66 OSL_POSTCOND(path.lastIndexOf(BACKSLASH) == (path.getLength() - 1), \
67 "osl_systemPathEnsureSeparator: Post condition failed");
70 /*******************************************************************
71 osl_systemPathRemoveSeparator
72 ******************************************************************/
74 void SAL_CALL osl_systemPathRemoveSeparator(/*inout*/ rtl_uString** ppustrPath)
76 rtl::OUString path(*ppustrPath);
78 if (!osl::systemPathIsLogicalDrivePattern(path))
80 sal_Int32 i = std::max<sal_Int32>(path.lastIndexOf(BACKSLASH), path.lastIndexOf(SLASH));
82 if (i > -1 && (i == (path.getLength() - 1)))
84 path = rtl::OUString(path.getStr(), path.getLength() - 1);
85 rtl_uString_assign(ppustrPath, path.pData);
90 /*******************************************************************
91 osl_is_logical_drive_pattern
92 ******************************************************************/
94 // is [A-Za-z]:[/|\]\0
95 const sal_Char* LDP = ":";
96 const sal_Char* LDP_WITH_BACKSLASH = ":\\";
97 const sal_Char* LDP_WITH_SLASH = ":/";
99 // degenerated case returned by the Windows FileOpen dialog
100 // when someone enters for instance "x:filename", the Win32
101 // API accepts this case
102 const sal_Char* LDP_WITH_DOT_BACKSLASH = ":.\\";
104 sal_Int32 osl_systemPathIsLogicalDrivePattern(/*in*/ const rtl_uString* pustrPath)
106 const sal_Unicode* p = rtl_uString_getStr(const_cast<rtl_uString*>(pustrPath));
107 if (iswalpha(*p++))
109 return ((0 == rtl_ustr_ascii_compare(p, LDP)) ||
110 (0 == rtl_ustr_ascii_compare(p, LDP_WITH_BACKSLASH)) ||
111 (0 == rtl_ustr_ascii_compare(p, LDP_WITH_SLASH)) ||
112 (0 == rtl_ustr_ascii_compare(p, LDP_WITH_DOT_BACKSLASH)));
114 return 0;
118 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */