Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / fpicker / source / win32 / WinImplHelper.cxx
blobab1dcbe007a91bebcce21574b8083ea490f8038d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "WinImplHelper.hxx"
22 #include <vector>
23 #include <osl/diagnose.h>
24 #include <rtl/ustrbuf.hxx>
25 #include <com/sun/star/uno/Sequence.hxx>
27 using ::com::sun::star::lang::IllegalArgumentException;
28 using ::com::sun::star::uno::Reference;
29 using ::com::sun::star::uno::XInterface;
30 using ::com::sun::star::uno::Any;
31 using ::com::sun::star::uno::Sequence;
33 const OUString TILDE( "~" );
34 const sal_Unicode TILDE_SIGN = L'~';
35 const OUString AMPERSAND( "&" );
36 const sal_Unicode AMPERSAND_SIGN = L'&';
38 // OS NAME Platform Major Minor
40 // Windows NT 3.51 VER_PLATFORM_WIN32_NT 3 51
41 // Windows NT 4.0 VER_PLATFORM_WIN32_NT 4 0
42 // Windows 2000 VER_PLATFORM_WIN32_NT 5 0
43 // Windows XP VER_PLATFORM_WIN32_NT 5 1
44 // Windows Vista VER_PLATFORM_WIN32_NT 6 0
45 // Windows 7 VER_PLATFORM_WIN32_NT 6 1
46 // Windows 95 VER_PLATFORM_WIN32_WINDOWS 4 0
47 // Windows 98 VER_PLATFORM_WIN32_WINDOWS 4 10
48 // Windows ME VER_PLATFORM_WIN32_WINDOWS 4 90
51 static void Replace( const OUString& aLabel, sal_Unicode OldChar, sal_Unicode NewChar, OUStringBuffer& aBuffer )
53 OSL_ASSERT( aLabel.getLength( ) );
54 OSL_ASSERT( aBuffer.getCapacity( ) >= (aLabel.getLength( )) );
56 sal_Int32 i = 0;
57 const sal_Unicode* pCurrent = aLabel.getStr( );
58 const sal_Unicode* pNext = aLabel.getStr( ) + 1;
59 const sal_Unicode* pEnd = aLabel.getStr( ) + aLabel.getLength( );
61 while( pCurrent < pEnd )
63 OSL_ASSERT( pNext <= pEnd );
64 OSL_ASSERT( (i >= 0) && (i < aBuffer.getCapacity( )) );
66 if ( OldChar == *pCurrent )
68 if ( OldChar == *pNext )
70 // two OldChars in line will
71 // be replaced by one
72 // e.g. ~~ -> ~
73 aBuffer.insert( i, *pCurrent );
75 // skip the next one
76 pCurrent++;
77 pNext++;
79 else
81 // one OldChar will be replace
82 // by NexChar
83 aBuffer.insert( i, NewChar );
86 else if ( *pCurrent == NewChar )
88 // a NewChar will be replaced by
89 // two NewChars
90 // e.g. & -> &&
91 aBuffer.insert( i++, *pCurrent );
92 aBuffer.insert( i, *pCurrent );
94 else
96 aBuffer.insert( i, *pCurrent );
99 pCurrent++;
100 pNext++;
101 i++;
105 // converts a soffice label to a windows label
106 // the following rules for character replacements
107 // will be done:
108 // '~' -> '&'
109 // '~~' -> '~'
110 // '&' -> '&&'
112 OUString SOfficeToWindowsLabel( const OUString& aSOLabel )
114 OUString aWinLabel = aSOLabel;
116 if ( (aWinLabel.indexOf( TILDE ) > -1) || (aWinLabel.indexOf( AMPERSAND ) > -1) )
118 sal_Int32 nStrLen = aWinLabel.getLength( );
120 // in the worst case the new string is
121 // doubled in length, maybe some waste
122 // of memory but how long is a label
123 // normally(?)
124 OUStringBuffer aBuffer( nStrLen * 2 );
126 Replace( aWinLabel, TILDE_SIGN, AMPERSAND_SIGN, aBuffer );
128 aWinLabel = aBuffer.makeStringAndClear( );
131 return aWinLabel;
134 // converts a windows label to a soffice label
135 // the following rules for character replacements
136 // will be done:
137 // '&' -> '~'
138 // '&&' -> '&'
139 // '~' -> '~~'
141 OUString WindowsToSOfficeLabel( const OUString& aWinLabel )
143 OUString aSOLabel = aWinLabel;
145 if ( (aSOLabel.indexOf( TILDE ) > -1) || (aSOLabel.indexOf( AMPERSAND ) > -1) )
147 sal_Int32 nStrLen = aSOLabel.getLength( );
149 // in the worst case the new string is
150 // doubled in length, maybe some waste
151 // of memory but how long is a label
152 // normally(?)
153 OUStringBuffer aBuffer( nStrLen * 2 );
155 Replace( aSOLabel, AMPERSAND_SIGN, TILDE_SIGN, aBuffer );
157 aSOLabel = aBuffer.makeStringAndClear( );
160 return aSOLabel;
163 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */