1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 /*******************************************************************
23 ******************************************************************/
25 #include "path_helper.hxx"
26 #include <osl/diagnose.h>
27 #include <rtl/ustring.hxx>
32 /*******************************************************************
34 ******************************************************************/
36 const rtl::OUString
BACKSLASH ("\\");
37 const rtl::OUString
SLASH ("/");
39 /*******************************************************************
40 osl_systemPathEnsureSeparator
41 ******************************************************************/
43 void osl_systemPathEnsureSeparator(/*inout*/ rtl_uString
** ppustrPath
)
45 OSL_PRECOND(ppustrPath
&& (NULL
!= *ppustrPath
), \
46 "osl_systemPathEnsureSeparator: Invalid parameter");
48 rtl::OUString
path(*ppustrPath
);
49 sal_Int32 i
= std::max
<sal_Int32
>(path
.lastIndexOf(BACKSLASH
), path
.lastIndexOf(SLASH
));
51 if (i
< (path
.getLength()-1))
54 rtl_uString_assign(ppustrPath
, path
.pData
);
57 OSL_POSTCOND(path
.endsWith(BACKSLASH
), \
58 "osl_systemPathEnsureSeparator: Post condition failed");
61 /*******************************************************************
62 osl_systemPathRemoveSeparator
63 ******************************************************************/
65 void SAL_CALL
osl_systemPathRemoveSeparator(/*inout*/ rtl_uString
** ppustrPath
)
67 rtl::OUString
path(*ppustrPath
);
69 if (!osl::systemPathIsLogicalDrivePattern(path
))
71 sal_Int32 i
= std::max
<sal_Int32
>(path
.lastIndexOf(BACKSLASH
), path
.lastIndexOf(SLASH
));
73 if (i
> -1 && (i
== (path
.getLength() - 1)))
75 path
= rtl::OUString(path
.getStr(), path
.getLength() - 1);
76 rtl_uString_assign(ppustrPath
, path
.pData
);
81 /*******************************************************************
82 osl_is_logical_drive_pattern
83 ******************************************************************/
85 // is [A-Za-z]:[/|\]\0
86 const sal_Char
* LDP
= ":";
87 const sal_Char
* LDP_WITH_BACKSLASH
= ":\\";
88 const sal_Char
* LDP_WITH_SLASH
= ":/";
90 // degenerated case returned by the Windows FileOpen dialog
91 // when someone enters for instance "x:filename", the Win32
92 // API accepts this case
93 const sal_Char
* LDP_WITH_DOT_BACKSLASH
= ":.\\";
95 sal_Int32
osl_systemPathIsLogicalDrivePattern(/*in*/ const rtl_uString
* pustrPath
)
97 const sal_Unicode
* p
= rtl_uString_getStr(const_cast<rtl_uString
*>(pustrPath
));
100 return ((0 == rtl_ustr_ascii_compare(p
, LDP
)) ||
101 (0 == rtl_ustr_ascii_compare(p
, LDP_WITH_BACKSLASH
)) ||
102 (0 == rtl_ustr_ascii_compare(p
, LDP_WITH_SLASH
)) ||
103 (0 == rtl_ustr_ascii_compare(p
, LDP_WITH_DOT_BACKSLASH
)));
109 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */