1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <osl/diagnose.h>
21 #include "TxtCnvtHlp.hxx"
22 #include "DTransHelper.hxx"
23 #include "../misc/ImplHelper.hxx"
25 using namespace ::com::sun::star::datatransfer
;
26 using namespace ::com::sun::star::uno
;
28 //------------------------------------------------------------------
29 // assuming a '\0' terminated string if no length specified
30 //------------------------------------------------------------------
32 int CalcBuffSizeForTextConversion( UINT code_page
, LPCSTR lpMultiByteString
, int nLen
= -1 )
34 return ( MultiByteToWideChar( code_page
,
39 0 ) * sizeof( sal_Unicode
) );
42 //------------------------------------------------------------------
43 // assuming a '\0' terminated string if no length specified
44 //------------------------------------------------------------------
46 int CalcBuffSizeForTextConversion( UINT code_page
, LPCWSTR lpWideCharString
, int nLen
= -1 )
48 return WideCharToMultiByte( code_page
,
58 //------------------------------------------------------------------
59 // converts text in one code page into unicode text
60 // automatically calculates the necessary buffer size and allocates
62 //------------------------------------------------------------------
64 int MultiByteToWideCharEx( UINT cp_src
,
65 LPCSTR lpMultiByteString
,
67 CStgTransferHelper
& refDTransHelper
,
68 BOOL bEnsureTrailingZero
)
70 OSL_ASSERT( IsValidCodePage( cp_src
) );
71 OSL_ASSERT( NULL
!= lpMultiByteString
);
73 // calculate the required buff size
74 int reqSize
= CalcBuffSizeForTextConversion( cp_src
, lpMultiByteString
, lenStr
);
76 if ( bEnsureTrailingZero
)
77 reqSize
+= sizeof( sal_Unicode
);
79 // initialize the data-transfer helper
80 refDTransHelper
.init( reqSize
);
82 // setup a global memory pointer
83 CRawHGlobalPtr
ptrHGlob( refDTransHelper
);
85 // do the converssion an return
86 return MultiByteToWideChar( cp_src
,
90 static_cast< LPWSTR
>( ptrHGlob
.GetMemPtr( ) ),
91 ptrHGlob
.MemSize( ) );
94 //------------------------------------------------------------------
95 // converts unicode text into text of the specified code page
96 // automatically calculates the necessary buffer size and allocates
98 //------------------------------------------------------------------
100 int WideCharToMultiByteEx( UINT cp_dest
,
101 LPCWSTR lpWideCharString
,
103 CStgTransferHelper
& refDTransHelper
,
104 BOOL bEnsureTrailingZero
)
106 OSL_ASSERT( IsValidCodePage( cp_dest
) );
107 OSL_ASSERT( NULL
!= lpWideCharString
);
109 // calculate the required buff size
110 int reqSize
= CalcBuffSizeForTextConversion( cp_dest
, lpWideCharString
, lenStr
);
112 if ( bEnsureTrailingZero
)
113 reqSize
+= sizeof( sal_Int8
);
115 // initialize the data-transfer helper
116 refDTransHelper
.init( reqSize
);
118 // setup a global memory pointer
119 CRawHGlobalPtr
ptrHGlob( refDTransHelper
);
121 // do the converssion an return
122 return WideCharToMultiByte( cp_dest
,
126 static_cast< LPSTR
>( ptrHGlob
.GetMemPtr( ) ),
132 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */