xref: /aoo41x/main/sal/osl/os2/path_helper.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 /*******************************************************************
29  Includes
30  ******************************************************************/
31 
32 #include "path_helper.hxx"
33 #include <osl/diagnose.h>
34 #include <rtl/ustring.hxx>
35 
36 #include <algorithm>
37 #include <wchar.h>
38 #include <wctype.h>
39 
40 /*******************************************************************
41  Constants
42  ******************************************************************/
43 
44 const rtl::OUString BACKSLASH = rtl::OUString::createFromAscii("\\");
45 const rtl::OUString SLASH     = rtl::OUString::createFromAscii("/");
46 
47 /*******************************************************************
48  osl_systemPathEnsureSeparator
49  ******************************************************************/
50 
51 void osl_systemPathEnsureSeparator(/*inout*/ rtl_uString** ppustrPath)
52 {
53     OSL_PRECOND(ppustrPath && (NULL != *ppustrPath), \
54 				"osl_systemPathEnsureSeparator: Invalid parameter");
55 
56  	rtl::OUString path(*ppustrPath);
57 	sal_Int32     i = std::max<sal_Int32>(path.lastIndexOf(BACKSLASH), path.lastIndexOf(SLASH));
58 
59 	if (i < (path.getLength()-1))
60 	{
61 		path += BACKSLASH;
62 		rtl_uString_assign(ppustrPath, path.pData);
63 	}
64 
65 	OSL_POSTCOND(path.lastIndexOf(BACKSLASH) == (path.getLength() - 1), \
66 				 "osl_systemPathEnsureSeparator: Post condition failed");
67 }
68 
69 /*******************************************************************
70  osl_systemPathRemoveSeparator
71  ******************************************************************/
72 
73 void SAL_CALL osl_systemPathRemoveSeparator(/*inout*/ rtl_uString** ppustrPath)
74 {
75     rtl::OUString path(*ppustrPath);
76 
77     if (!osl::systemPathIsLogicalDrivePattern(path))
78     {
79         sal_Int32 i = std::max<sal_Int32>(path.lastIndexOf(BACKSLASH), path.lastIndexOf(SLASH));
80 
81         if (i > -1 && (i == (path.getLength() - 1)))
82         {
83             path = rtl::OUString(path.getStr(), path.getLength() - 1);
84             rtl_uString_assign(ppustrPath, path.pData);
85         }
86     }
87 }
88 
89 /*******************************************************************
90  osl_is_logical_drive_pattern
91  ******************************************************************/
92 
93 // is [A-Za-z]:[/|\]\0
94 const sal_Unicode* LDP                    = L":";
95 const sal_Unicode* LDP_WITH_BACKSLASH     = L":\\";
96 const sal_Unicode* LDP_WITH_SLASH         = L":/";
97 
98 // degenerated case returned by the Windows FileOpen dialog
99 // when someone enters for instance "x:filename", the Win32
100 // API accepts this case
101 const sal_Unicode* LDP_WITH_DOT_BACKSLASH = L":.\\";
102 
103 sal_Int32 osl_systemPathIsLogicalDrivePattern(/*in*/ const rtl_uString* pustrPath)
104 {
105     const sal_Unicode* p = rtl_uString_getStr(const_cast<rtl_uString*>(pustrPath));
106     if (iswalpha(*p++))
107     {
108         return ((0 == wcscmp(p, LDP)) ||
109                 (0 == wcscmp(p, LDP_WITH_BACKSLASH)) ||
110                 (0 == wcscmp(p, LDP_WITH_SLASH)) ||
111                 (0 == wcscmp(p, LDP_WITH_DOT_BACKSLASH)));
112     }
113     return 0;
114 }
115 
116 
117