xref: /trunk/main/sal/osl/w32/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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sal.hxx"
30 
31 /*******************************************************************
32  Includes
33  ******************************************************************/
34 
35 #include "path_helper.hxx"
36 #include <osl/diagnose.h>
37 #include <rtl/ustring.hxx>
38 
39 #include <algorithm>
40 #include <wchar.h>
41 
42 /*******************************************************************
43  Constants
44  ******************************************************************/
45 
46 const rtl::OUString BACKSLASH = rtl::OUString::createFromAscii("\\");
47 const rtl::OUString SLASH     = rtl::OUString::createFromAscii("/");
48 
49 /*******************************************************************
50  osl_systemPathEnsureSeparator
51  ******************************************************************/
52 
53 void osl_systemPathEnsureSeparator(/*inout*/ rtl_uString** ppustrPath)
54 {
55     OSL_PRECOND(ppustrPath && (NULL != *ppustrPath), \
56 				"osl_systemPathEnsureSeparator: Invalid parameter");
57 
58  	rtl::OUString path(*ppustrPath);
59 	sal_Int32     i = std::max<sal_Int32>(path.lastIndexOf(BACKSLASH), path.lastIndexOf(SLASH));
60 
61 	if (i < (path.getLength()-1))
62 	{
63 		path += BACKSLASH;
64 		rtl_uString_assign(ppustrPath, path.pData);
65 	}
66 
67 	OSL_POSTCOND(path.lastIndexOf(BACKSLASH) == (path.getLength() - 1), \
68 				 "osl_systemPathEnsureSeparator: Post condition failed");
69 }
70 
71 /*******************************************************************
72  osl_systemPathRemoveSeparator
73  ******************************************************************/
74 
75 void SAL_CALL osl_systemPathRemoveSeparator(/*inout*/ rtl_uString** ppustrPath)
76 {
77     rtl::OUString path(*ppustrPath);
78 
79     if (!osl::systemPathIsLogicalDrivePattern(path))
80     {
81         sal_Int32 i = std::max<sal_Int32>(path.lastIndexOf(BACKSLASH), path.lastIndexOf(SLASH));
82 
83         if (i > -1 && (i == (path.getLength() - 1)))
84         {
85             path = rtl::OUString(path.getStr(), path.getLength() - 1);
86             rtl_uString_assign(ppustrPath, path.pData);
87         }
88     }
89 }
90 
91 /*******************************************************************
92  osl_is_logical_drive_pattern
93  ******************************************************************/
94 
95 // is [A-Za-z]:[/|\]\0
96 const sal_Char* LDP                    = ":";
97 const sal_Char* LDP_WITH_BACKSLASH     = ":\\";
98 const sal_Char* LDP_WITH_SLASH         = ":/";
99 
100 // degenerated case returned by the Windows FileOpen dialog
101 // when someone enters for instance "x:filename", the Win32
102 // API accepts this case
103 const sal_Char* LDP_WITH_DOT_BACKSLASH = ":.\\";
104 
105 sal_Int32 osl_systemPathIsLogicalDrivePattern(/*in*/ const rtl_uString* pustrPath)
106 {
107     const sal_Unicode* p = rtl_uString_getStr(const_cast<rtl_uString*>(pustrPath));
108     if (iswalpha(*p++))
109     {
110         return ((0 == rtl_ustr_ascii_compare(p, LDP)) ||
111                 (0 == rtl_ustr_ascii_compare(p, LDP_WITH_BACKSLASH)) ||
112                 (0 == rtl_ustr_ascii_compare(p, LDP_WITH_SLASH)) ||
113                 (0 == rtl_ustr_ascii_compare(p, LDP_WITH_DOT_BACKSLASH)));
114     }
115     return 0;
116 }
117 
118 
119