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 // assuming a '\0' terminated string if no length specified
30 int CalcBuffSizeForTextConversion( UINT code_page
, LPCSTR lpMultiByteString
, int nLen
= -1 )
32 return ( MultiByteToWideChar( code_page
,
37 0 ) * sizeof( sal_Unicode
) );
40 // assuming a '\0' terminated string if no length specified
42 int CalcBuffSizeForTextConversion( UINT code_page
, LPCWSTR lpWideCharString
, int nLen
= -1 )
44 return WideCharToMultiByte( code_page
,
54 // converts text in one code page into unicode text
55 // automatically calculates the necessary buffer size and allocates
58 int MultiByteToWideCharEx( UINT cp_src
,
59 LPCSTR lpMultiByteString
,
61 CStgTransferHelper
& refDTransHelper
,
62 BOOL bEnsureTrailingZero
)
64 OSL_ASSERT( IsValidCodePage( cp_src
) );
65 OSL_ASSERT( NULL
!= lpMultiByteString
);
67 // calculate the required buff size
68 int reqSize
= CalcBuffSizeForTextConversion( cp_src
, lpMultiByteString
, lenStr
);
70 if ( bEnsureTrailingZero
)
71 reqSize
+= sizeof( sal_Unicode
);
73 // initialize the data-transfer helper
74 refDTransHelper
.init( reqSize
);
76 // setup a global memory pointer
77 CRawHGlobalPtr
ptrHGlob( refDTransHelper
);
79 // do the converssion an return
80 return MultiByteToWideChar( cp_src
,
84 static_cast< LPWSTR
>( ptrHGlob
.GetMemPtr( ) ),
85 ptrHGlob
.MemSize( ) );
88 // converts unicode text into text of the specified code page
89 // automatically calculates the necessary buffer size and allocates
92 int WideCharToMultiByteEx( UINT cp_dest
,
93 LPCWSTR lpWideCharString
,
95 CStgTransferHelper
& refDTransHelper
,
96 BOOL bEnsureTrailingZero
)
98 OSL_ASSERT( IsValidCodePage( cp_dest
) );
99 OSL_ASSERT( NULL
!= lpWideCharString
);
101 // calculate the required buff size
102 int reqSize
= CalcBuffSizeForTextConversion( cp_dest
, lpWideCharString
, lenStr
);
104 if ( bEnsureTrailingZero
)
105 reqSize
+= sizeof( sal_Int8
);
107 // initialize the data-transfer helper
108 refDTransHelper
.init( reqSize
);
110 // setup a global memory pointer
111 CRawHGlobalPtr
ptrHGlob( refDTransHelper
);
113 // do the converssion an return
114 return WideCharToMultiByte( cp_dest
,
118 static_cast< LPSTR
>( ptrHGlob
.GetMemPtr( ) ),
124 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */