xref: /AOO41X/main/dtrans/source/win32/dtobj/TxtCnvtHlp.cxx (revision 48123e16153c92857455f9e7a0d17cc19307983f)
1*48123e16SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*48123e16SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*48123e16SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*48123e16SAndrew Rist  * distributed with this work for additional information
6*48123e16SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*48123e16SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*48123e16SAndrew Rist  * "License"); you may not use this file except in compliance
9*48123e16SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*48123e16SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*48123e16SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*48123e16SAndrew Rist  * software distributed under the License is distributed on an
15*48123e16SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*48123e16SAndrew Rist  * KIND, either express or implied.  See the License for the
17*48123e16SAndrew Rist  * specific language governing permissions and limitations
18*48123e16SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*48123e16SAndrew Rist  *************************************************************/
21*48123e16SAndrew Rist 
22*48123e16SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_dtrans.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir //------------------------------------------------------------------------
28cdf0e10cSrcweir // includes
29cdf0e10cSrcweir //------------------------------------------------------------------------
30cdf0e10cSrcweir #include <osl/diagnose.h>
31cdf0e10cSrcweir #include "TxtCnvtHlp.hxx"
32cdf0e10cSrcweir #include "DTransHelper.hxx"
33cdf0e10cSrcweir #include "..\misc\ImplHelper.hxx"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir using namespace ::com::sun::star::datatransfer;
36cdf0e10cSrcweir using namespace ::com::sun::star::uno;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir //------------------------------------------------------------------
39cdf0e10cSrcweir // assuming a '\0' terminated string if no length specified
40cdf0e10cSrcweir //------------------------------------------------------------------
41cdf0e10cSrcweir 
CalcBuffSizeForTextConversion(UINT code_page,LPCSTR lpMultiByteString,int nLen=-1)42cdf0e10cSrcweir int CalcBuffSizeForTextConversion( UINT code_page, LPCSTR lpMultiByteString, int nLen = -1 )
43cdf0e10cSrcweir {
44cdf0e10cSrcweir 	return ( MultiByteToWideChar( code_page,
45cdf0e10cSrcweir 								0,
46cdf0e10cSrcweir 								lpMultiByteString,
47cdf0e10cSrcweir 								nLen,
48cdf0e10cSrcweir 								NULL,
49cdf0e10cSrcweir 								0 ) * sizeof( sal_Unicode ) );
50cdf0e10cSrcweir }
51cdf0e10cSrcweir 
52cdf0e10cSrcweir //------------------------------------------------------------------
53cdf0e10cSrcweir // assuming a '\0' terminated string if no length specified
54cdf0e10cSrcweir //------------------------------------------------------------------
55cdf0e10cSrcweir 
CalcBuffSizeForTextConversion(UINT code_page,LPCWSTR lpWideCharString,int nLen=-1)56cdf0e10cSrcweir int CalcBuffSizeForTextConversion( UINT code_page, LPCWSTR lpWideCharString, int nLen = -1 )
57cdf0e10cSrcweir {
58cdf0e10cSrcweir 	return WideCharToMultiByte( code_page,
59cdf0e10cSrcweir 								0,
60cdf0e10cSrcweir 								lpWideCharString,
61cdf0e10cSrcweir 								nLen,
62cdf0e10cSrcweir 								NULL,
63cdf0e10cSrcweir 								0,
64cdf0e10cSrcweir 								NULL,
65cdf0e10cSrcweir 								NULL );
66cdf0e10cSrcweir }
67cdf0e10cSrcweir 
68cdf0e10cSrcweir //------------------------------------------------------------------
69cdf0e10cSrcweir // converts text in one code page into unicode text
70cdf0e10cSrcweir // automatically calculates the necessary buffer size and allocates
71cdf0e10cSrcweir // the buffer
72cdf0e10cSrcweir //------------------------------------------------------------------
73cdf0e10cSrcweir 
MultiByteToWideCharEx(UINT cp_src,LPCSTR lpMultiByteString,sal_uInt32 lenStr,CStgTransferHelper & refDTransHelper,BOOL bEnsureTrailingZero)74cdf0e10cSrcweir int MultiByteToWideCharEx( UINT cp_src,
75cdf0e10cSrcweir 						   LPCSTR lpMultiByteString,
76cdf0e10cSrcweir 						   sal_uInt32 lenStr,
77cdf0e10cSrcweir 						   CStgTransferHelper& refDTransHelper,
78cdf0e10cSrcweir 						   BOOL bEnsureTrailingZero )
79cdf0e10cSrcweir {
80cdf0e10cSrcweir 	OSL_ASSERT( IsValidCodePage( cp_src ) );
81cdf0e10cSrcweir 	OSL_ASSERT( NULL != lpMultiByteString );
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 	// calculate the required buff size
84cdf0e10cSrcweir 	int reqSize = CalcBuffSizeForTextConversion( cp_src, lpMultiByteString, lenStr );
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 	if ( bEnsureTrailingZero )
87cdf0e10cSrcweir 		reqSize += sizeof( sal_Unicode );
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 	// initialize the data-transfer helper
90cdf0e10cSrcweir 	refDTransHelper.init( reqSize );
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 	// setup a global memory pointer
93cdf0e10cSrcweir 	CRawHGlobalPtr ptrHGlob( refDTransHelper );
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 	// do the converssion an return
96cdf0e10cSrcweir 	return MultiByteToWideChar( cp_src,
97cdf0e10cSrcweir 								0,
98cdf0e10cSrcweir 								lpMultiByteString,
99cdf0e10cSrcweir 								lenStr,
100cdf0e10cSrcweir 								static_cast< LPWSTR >( ptrHGlob.GetMemPtr( ) ),
101cdf0e10cSrcweir 								ptrHGlob.MemSize( ) );
102cdf0e10cSrcweir }
103cdf0e10cSrcweir 
104cdf0e10cSrcweir //------------------------------------------------------------------
105cdf0e10cSrcweir // converts unicode text into text of the specified code page
106cdf0e10cSrcweir // automatically calculates the necessary buffer size and allocates
107cdf0e10cSrcweir // the buffer
108cdf0e10cSrcweir //------------------------------------------------------------------
109cdf0e10cSrcweir 
WideCharToMultiByteEx(UINT cp_dest,LPCWSTR lpWideCharString,sal_uInt32 lenStr,CStgTransferHelper & refDTransHelper,BOOL bEnsureTrailingZero)110cdf0e10cSrcweir int WideCharToMultiByteEx( UINT cp_dest,
111cdf0e10cSrcweir 						   LPCWSTR lpWideCharString,
112cdf0e10cSrcweir 						   sal_uInt32 lenStr,
113cdf0e10cSrcweir 						   CStgTransferHelper& refDTransHelper,
114cdf0e10cSrcweir 						   BOOL bEnsureTrailingZero )
115cdf0e10cSrcweir {
116cdf0e10cSrcweir 	OSL_ASSERT( IsValidCodePage( cp_dest ) );
117cdf0e10cSrcweir 	OSL_ASSERT( NULL != lpWideCharString );
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 	// calculate the required buff size
120cdf0e10cSrcweir 	int reqSize = CalcBuffSizeForTextConversion( cp_dest, lpWideCharString, lenStr );
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 	if ( bEnsureTrailingZero )
123cdf0e10cSrcweir 		reqSize += sizeof( sal_Int8 );
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 	// initialize the data-transfer helper
126cdf0e10cSrcweir 	refDTransHelper.init( reqSize );
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 	// setup a global memory pointer
129cdf0e10cSrcweir 	CRawHGlobalPtr ptrHGlob( refDTransHelper );
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 	// do the converssion an return
132cdf0e10cSrcweir 	return WideCharToMultiByte( cp_dest,
133cdf0e10cSrcweir 								0,
134cdf0e10cSrcweir 								lpWideCharString,
135cdf0e10cSrcweir 								lenStr,
136cdf0e10cSrcweir 								static_cast< LPSTR >( ptrHGlob.GetMemPtr( ) ),
137cdf0e10cSrcweir 								ptrHGlob.MemSize( ),
138cdf0e10cSrcweir 								NULL,
139cdf0e10cSrcweir 								NULL );
140cdf0e10cSrcweir }
141cdf0e10cSrcweir 
142