Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / dtrans / source / win32 / dtobj / TxtCnvtHlp.cxx
blob1ea9f4c9f549607e0cabf666fe4365ee227c3a71
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 static int CalcBuffSizeForTextConversion( UINT code_page, LPCSTR lpMultiByteString, int nLen = -1 )
32 return ( MultiByteToWideChar( code_page,
34 lpMultiByteString,
35 nLen,
36 nullptr,
37 0 ) * sizeof( sal_Unicode ) );
40 // assuming a '\0' terminated string if no length specified
42 static int CalcBuffSizeForTextConversion( UINT code_page, LPCWSTR lpWideCharString, int nLen = -1 )
44 return WideCharToMultiByte( code_page,
46 lpWideCharString,
47 nLen,
48 nullptr,
50 nullptr,
51 nullptr );
54 // converts text in one code page into unicode text
55 // automatically calculates the necessary buffer size and allocates
56 // the buffer
58 int MultiByteToWideCharEx( UINT cp_src,
59 LPCSTR lpMultiByteString,
60 sal_uInt32 lenStr,
61 CStgTransferHelper& refDTransHelper,
62 BOOL bEnsureTrailingZero )
64 OSL_ASSERT( IsValidCodePage( cp_src ) );
65 OSL_ASSERT( nullptr != 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 conversion and return
80 return MultiByteToWideChar( cp_src,
82 lpMultiByteString,
83 lenStr,
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
90 // the buffer
92 int WideCharToMultiByteEx( UINT cp_dest,
93 LPCWSTR lpWideCharString,
94 sal_uInt32 lenStr,
95 CStgTransferHelper& refDTransHelper,
96 BOOL bEnsureTrailingZero )
98 OSL_ASSERT( IsValidCodePage( cp_dest ) );
99 OSL_ASSERT( nullptr != 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 conversion and return
114 return WideCharToMultiByte( cp_dest,
116 lpWideCharString,
117 lenStr,
118 static_cast< LPSTR >( ptrHGlob.GetMemPtr( ) ),
119 ptrHGlob.MemSize( ),
120 nullptr,
121 nullptr );
124 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */