xref: /AOO41X/main/dtrans/source/win32/dtobj/TxtCnvtHlp.cxx (revision 48123e16153c92857455f9e7a0d17cc19307983f)
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_dtrans.hxx"
26 
27 //------------------------------------------------------------------------
28 // includes
29 //------------------------------------------------------------------------
30 #include <osl/diagnose.h>
31 #include "TxtCnvtHlp.hxx"
32 #include "DTransHelper.hxx"
33 #include "..\misc\ImplHelper.hxx"
34 
35 using namespace ::com::sun::star::datatransfer;
36 using namespace ::com::sun::star::uno;
37 
38 //------------------------------------------------------------------
39 // assuming a '\0' terminated string if no length specified
40 //------------------------------------------------------------------
41 
CalcBuffSizeForTextConversion(UINT code_page,LPCSTR lpMultiByteString,int nLen=-1)42 int CalcBuffSizeForTextConversion( UINT code_page, LPCSTR lpMultiByteString, int nLen = -1 )
43 {
44     return ( MultiByteToWideChar( code_page,
45                                 0,
46                                 lpMultiByteString,
47                                 nLen,
48                                 NULL,
49                                 0 ) * sizeof( sal_Unicode ) );
50 }
51 
52 //------------------------------------------------------------------
53 // assuming a '\0' terminated string if no length specified
54 //------------------------------------------------------------------
55 
CalcBuffSizeForTextConversion(UINT code_page,LPCWSTR lpWideCharString,int nLen=-1)56 int CalcBuffSizeForTextConversion( UINT code_page, LPCWSTR lpWideCharString, int nLen = -1 )
57 {
58     return WideCharToMultiByte( code_page,
59                                 0,
60                                 lpWideCharString,
61                                 nLen,
62                                 NULL,
63                                 0,
64                                 NULL,
65                                 NULL );
66 }
67 
68 //------------------------------------------------------------------
69 // converts text in one code page into unicode text
70 // automatically calculates the necessary buffer size and allocates
71 // the buffer
72 //------------------------------------------------------------------
73 
MultiByteToWideCharEx(UINT cp_src,LPCSTR lpMultiByteString,sal_uInt32 lenStr,CStgTransferHelper & refDTransHelper,BOOL bEnsureTrailingZero)74 int MultiByteToWideCharEx( UINT cp_src,
75                            LPCSTR lpMultiByteString,
76                            sal_uInt32 lenStr,
77                            CStgTransferHelper& refDTransHelper,
78                            BOOL bEnsureTrailingZero )
79 {
80     OSL_ASSERT( IsValidCodePage( cp_src ) );
81     OSL_ASSERT( NULL != lpMultiByteString );
82 
83     // calculate the required buff size
84     int reqSize = CalcBuffSizeForTextConversion( cp_src, lpMultiByteString, lenStr );
85 
86     if ( bEnsureTrailingZero )
87         reqSize += sizeof( sal_Unicode );
88 
89     // initialize the data-transfer helper
90     refDTransHelper.init( reqSize );
91 
92     // setup a global memory pointer
93     CRawHGlobalPtr ptrHGlob( refDTransHelper );
94 
95     // do the converssion an return
96     return MultiByteToWideChar( cp_src,
97                                 0,
98                                 lpMultiByteString,
99                                 lenStr,
100                                 static_cast< LPWSTR >( ptrHGlob.GetMemPtr( ) ),
101                                 ptrHGlob.MemSize( ) );
102 }
103 
104 //------------------------------------------------------------------
105 // converts unicode text into text of the specified code page
106 // automatically calculates the necessary buffer size and allocates
107 // the buffer
108 //------------------------------------------------------------------
109 
WideCharToMultiByteEx(UINT cp_dest,LPCWSTR lpWideCharString,sal_uInt32 lenStr,CStgTransferHelper & refDTransHelper,BOOL bEnsureTrailingZero)110 int WideCharToMultiByteEx( UINT cp_dest,
111                            LPCWSTR lpWideCharString,
112                            sal_uInt32 lenStr,
113                            CStgTransferHelper& refDTransHelper,
114                            BOOL bEnsureTrailingZero )
115 {
116     OSL_ASSERT( IsValidCodePage( cp_dest ) );
117     OSL_ASSERT( NULL != lpWideCharString );
118 
119     // calculate the required buff size
120     int reqSize = CalcBuffSizeForTextConversion( cp_dest, lpWideCharString, lenStr );
121 
122     if ( bEnsureTrailingZero )
123         reqSize += sizeof( sal_Int8 );
124 
125     // initialize the data-transfer helper
126     refDTransHelper.init( reqSize );
127 
128     // setup a global memory pointer
129     CRawHGlobalPtr ptrHGlob( refDTransHelper );
130 
131     // do the converssion an return
132     return WideCharToMultiByte( cp_dest,
133                                 0,
134                                 lpWideCharString,
135                                 lenStr,
136                                 static_cast< LPSTR >( ptrHGlob.GetMemPtr( ) ),
137                                 ptrHGlob.MemSize( ),
138                                 NULL,
139                                 NULL );
140 }
141 
142