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