1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 // MARKER(update_precomp.py): autogen include statement, do not remove 25 #include "precompiled_sal.hxx" 26 27 /******************************************************************* 28 Includes 29 ******************************************************************/ 30 31 #include "path_helper.hxx" 32 #include <osl/diagnose.h> 33 #include <rtl/ustring.hxx> 34 35 #include <algorithm> 36 #include <wchar.h> 37 38 /******************************************************************* 39 Constants 40 ******************************************************************/ 41 42 const rtl::OUString BACKSLASH = rtl::OUString::createFromAscii("\\"); 43 const rtl::OUString SLASH = rtl::OUString::createFromAscii("/"); 44 45 /******************************************************************* 46 osl_systemPathEnsureSeparator 47 ******************************************************************/ 48 49 void osl_systemPathEnsureSeparator(/*inout*/ rtl_uString** ppustrPath) 50 { 51 OSL_PRECOND(ppustrPath && (NULL != *ppustrPath), \ 52 "osl_systemPathEnsureSeparator: Invalid parameter"); 53 54 rtl::OUString path(*ppustrPath); 55 sal_Int32 i = std::max<sal_Int32>(path.lastIndexOf(BACKSLASH), path.lastIndexOf(SLASH)); 56 57 if (i < (path.getLength()-1)) 58 { 59 path += BACKSLASH; 60 rtl_uString_assign(ppustrPath, path.pData); 61 } 62 63 OSL_POSTCOND(path.lastIndexOf(BACKSLASH) == (path.getLength() - 1), \ 64 "osl_systemPathEnsureSeparator: Post condition failed"); 65 } 66 67 /******************************************************************* 68 osl_systemPathRemoveSeparator 69 ******************************************************************/ 70 71 void SAL_CALL osl_systemPathRemoveSeparator(/*inout*/ rtl_uString** ppustrPath) 72 { 73 rtl::OUString path(*ppustrPath); 74 75 if (!osl::systemPathIsLogicalDrivePattern(path)) 76 { 77 sal_Int32 i = std::max<sal_Int32>(path.lastIndexOf(BACKSLASH), path.lastIndexOf(SLASH)); 78 79 if (i > -1 && (i == (path.getLength() - 1))) 80 { 81 path = rtl::OUString(path.getStr(), path.getLength() - 1); 82 rtl_uString_assign(ppustrPath, path.pData); 83 } 84 } 85 } 86 87 /******************************************************************* 88 osl_is_logical_drive_pattern 89 ******************************************************************/ 90 91 // is [A-Za-z]:[/|\]\0 92 const sal_Char* LDP = ":"; 93 const sal_Char* LDP_WITH_BACKSLASH = ":\\"; 94 const sal_Char* LDP_WITH_SLASH = ":/"; 95 96 // degenerated case returned by the Windows FileOpen dialog 97 // when someone enters for instance "x:filename", the Win32 98 // API accepts this case 99 const sal_Char* LDP_WITH_DOT_BACKSLASH = ":.\\"; 100 101 sal_Int32 osl_systemPathIsLogicalDrivePattern(/*in*/ const rtl_uString* pustrPath) 102 { 103 const sal_Unicode* p = rtl_uString_getStr(const_cast<rtl_uString*>(pustrPath)); 104 if (iswalpha(*p++)) 105 { 106 return ((0 == rtl_ustr_ascii_compare(p, LDP)) || 107 (0 == rtl_ustr_ascii_compare(p, LDP_WITH_BACKSLASH)) || 108 (0 == rtl_ustr_ascii_compare(p, LDP_WITH_SLASH)) || 109 (0 == rtl_ustr_ascii_compare(p, LDP_WITH_DOT_BACKSLASH))); 110 } 111 return 0; 112 } 113 114 115