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 .
20 #include "path_helper.hxx"
21 #include <osl/diagnose.h>
22 #include <rtl/ustring.hxx>
23 #include <sal/log.hxx>
28 const OUString
BACKSLASH ("\\");
29 const OUString
SLASH ("/");
31 void osl_systemPathEnsureSeparator(/*inout*/ rtl_uString
** ppustrPath
)
33 OSL_PRECOND(ppustrPath
&& (nullptr != *ppustrPath
),
34 "osl_systemPathEnsureSeparator: Invalid parameter");
36 OUString
path(*ppustrPath
);
37 sal_Int32 i
= std::max
<sal_Int32
>(path
.lastIndexOf(BACKSLASH
), path
.lastIndexOf(SLASH
));
39 if (i
< (path
.getLength()-1))
42 rtl_uString_assign(ppustrPath
, path
.pData
);
45 SAL_WARN_IF( !path
.endsWith(BACKSLASH
),
47 "osl_systemPathEnsureSeparator: Post condition failed");
50 void osl_systemPathRemoveSeparator(/*inout*/ rtl_uString
** ppustrPath
)
52 OUString
path(*ppustrPath
);
54 if (!osl::systemPathIsLogicalDrivePattern(path
))
56 sal_Int32 i
= std::max
<sal_Int32
>(path
.lastIndexOf(BACKSLASH
), path
.lastIndexOf(SLASH
));
58 if (i
> -1 && (i
== (path
.getLength() - 1)))
60 path
= OUString(path
.getStr(), path
.getLength() - 1);
61 rtl_uString_assign(ppustrPath
, path
.pData
);
66 // is [A-Za-z]:[/|\]\0
67 const sal_Char
* const LDP
= ":";
68 const sal_Char
* const LDP_WITH_BACKSLASH
= ":\\";
69 const sal_Char
* const LDP_WITH_SLASH
= ":/";
71 // degenerated case returned by the Windows FileOpen dialog
72 // when someone enters for instance "x:filename", the Win32
73 // API accepts this case
74 const sal_Char
* const LDP_WITH_DOT_BACKSLASH
= ":.\\";
76 bool osl_systemPathIsLogicalDrivePattern(/*in*/ const rtl_uString
* pustrPath
)
78 const sal_Unicode
* p
= rtl_uString_getStr(const_cast<rtl_uString
*>(pustrPath
));
81 return ((0 == rtl_ustr_ascii_compare(p
, LDP
)) ||
82 (0 == rtl_ustr_ascii_compare(p
, LDP_WITH_BACKSLASH
)) ||
83 (0 == rtl_ustr_ascii_compare(p
, LDP_WITH_SLASH
)) ||
84 (0 == rtl_ustr_ascii_compare(p
, LDP_WITH_DOT_BACKSLASH
)));
89 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */