1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include "path_helper.hxx"
21 #include <osl/diagnose.h>
22 #include <rtl/ustring.hxx>
23 #include <sal/log.hxx>
24 
25 #include <algorithm>
26 #include <wchar.h>
27 
28 const OUString BACKSLASH ("\\");
29 const OUString SLASH     ("/");
30 
osl_systemPathEnsureSeparator(rtl_uString ** ppustrPath)31 void osl_systemPathEnsureSeparator(/*inout*/ rtl_uString** ppustrPath)
32 {
33     OSL_PRECOND(ppustrPath && (nullptr != *ppustrPath),
34                 "osl_systemPathEnsureSeparator: Invalid parameter");
35 
36     OUString path(*ppustrPath);
37     sal_Int32     i = std::max<sal_Int32>(path.lastIndexOf(BACKSLASH), path.lastIndexOf(SLASH));
38 
39     if (i < (path.getLength()-1))
40     {
41         path += BACKSLASH;
42         rtl_uString_assign(ppustrPath, path.pData);
43     }
44 
45     SAL_WARN_IF( !path.endsWith(BACKSLASH),
46                  "sal.osl",
47                  "osl_systemPathEnsureSeparator: Post condition failed");
48 }
49 
osl_systemPathRemoveSeparator(rtl_uString ** ppustrPath)50 void osl_systemPathRemoveSeparator(/*inout*/ rtl_uString** ppustrPath)
51 {
52     OUString path(*ppustrPath);
53 
54     if (!osl::systemPathIsLogicalDrivePattern(path))
55     {
56         sal_Int32 i = std::max<sal_Int32>(path.lastIndexOf(BACKSLASH), path.lastIndexOf(SLASH));
57 
58         if (i > -1 && (i == (path.getLength() - 1)))
59         {
60             path = OUString(path.getStr(), path.getLength() - 1);
61             rtl_uString_assign(ppustrPath, path.pData);
62         }
63     }
64 }
65 
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     = ":/";
70 
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 = ":.\\";
75 
osl_systemPathIsLogicalDrivePattern(const rtl_uString * pustrPath)76 bool osl_systemPathIsLogicalDrivePattern(/*in*/ const rtl_uString* pustrPath)
77 {
78     const sal_Unicode* p = rtl_uString_getStr(const_cast<rtl_uString*>(pustrPath));
79     if (iswalpha(*p++))
80     {
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)));
85     }
86     return false;
87 }
88 
89 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
90