1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: TxtCnvtHlp.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dtrans.hxx"
34 //------------------------------------------------------------------------
36 //------------------------------------------------------------------------
37 #include <osl/diagnose.h>
38 #include "TxtCnvtHlp.hxx"
39 #include "DTransHelper.hxx"
40 #include "..\misc\ImplHelper.hxx"
42 using namespace ::com::sun::star::datatransfer
;
43 using namespace ::com::sun::star::uno
;
45 //------------------------------------------------------------------
46 // assuming a '\0' terminated string if no length specified
47 //------------------------------------------------------------------
49 int CalcBuffSizeForTextConversion( UINT code_page
, LPCSTR lpMultiByteString
, int nLen
= -1 )
51 return ( MultiByteToWideChar( code_page
,
56 0 ) * sizeof( sal_Unicode
) );
59 //------------------------------------------------------------------
60 // assuming a '\0' terminated string if no length specified
61 //------------------------------------------------------------------
63 int CalcBuffSizeForTextConversion( UINT code_page
, LPCWSTR lpWideCharString
, int nLen
= -1 )
65 return WideCharToMultiByte( code_page
,
75 //------------------------------------------------------------------
76 // converts text in one code page into unicode text
77 // automatically calculates the necessary buffer size and allocates
79 //------------------------------------------------------------------
81 int MultiByteToWideCharEx( UINT cp_src
,
82 LPCSTR lpMultiByteString
,
84 CStgTransferHelper
& refDTransHelper
,
85 BOOL bEnsureTrailingZero
)
87 OSL_ASSERT( IsValidCodePage( cp_src
) );
88 OSL_ASSERT( NULL
!= lpMultiByteString
);
90 // calculate the required buff size
91 int reqSize
= CalcBuffSizeForTextConversion( cp_src
, lpMultiByteString
, lenStr
);
93 if ( bEnsureTrailingZero
)
94 reqSize
+= sizeof( sal_Unicode
);
96 // initialize the data-transfer helper
97 refDTransHelper
.init( reqSize
);
99 // setup a global memory pointer
100 CRawHGlobalPtr
ptrHGlob( refDTransHelper
);
102 // do the converssion an return
103 return MultiByteToWideChar( cp_src
,
107 static_cast< LPWSTR
>( ptrHGlob
.GetMemPtr( ) ),
108 ptrHGlob
.MemSize( ) );
111 //------------------------------------------------------------------
112 // converts unicode text into text of the specified code page
113 // automatically calculates the necessary buffer size and allocates
115 //------------------------------------------------------------------
117 int WideCharToMultiByteEx( UINT cp_dest
,
118 LPCWSTR lpWideCharString
,
120 CStgTransferHelper
& refDTransHelper
,
121 BOOL bEnsureTrailingZero
)
123 OSL_ASSERT( IsValidCodePage( cp_dest
) );
124 OSL_ASSERT( NULL
!= lpWideCharString
);
126 // calculate the required buff size
127 int reqSize
= CalcBuffSizeForTextConversion( cp_dest
, lpWideCharString
, lenStr
);
129 if ( bEnsureTrailingZero
)
130 reqSize
+= sizeof( sal_Int8
);
132 // initialize the data-transfer helper
133 refDTransHelper
.init( reqSize
);
135 // setup a global memory pointer
136 CRawHGlobalPtr
ptrHGlob( refDTransHelper
);
138 // do the converssion an return
139 return WideCharToMultiByte( cp_dest
,
143 static_cast< LPSTR
>( ptrHGlob
.GetMemPtr( ) ),