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>
22 #include "TxtCnvtHlp.hxx"
23 #include "DTransHelper.hxx"
24 #include "ImplHelper.hxx"
26 using namespace ::com::sun::star::datatransfer
;
27 using namespace ::com::sun::star::uno
;
29 // assuming a '\0' terminated string if no length specified
31 static int CalcBuffSizeForTextConversion( UINT code_page
, LPCSTR lpMultiByteString
, int nLen
= -1 )
33 return ( MultiByteToWideChar( code_page
,
38 0 ) * sizeof( sal_Unicode
) );
41 // assuming a '\0' terminated string if no length specified
43 static int CalcBuffSizeForTextConversion( UINT code_page
, LPCWSTR lpWideCharString
, int nLen
= -1 )
45 return WideCharToMultiByte( code_page
,
55 // converts text in one code page into unicode text
56 // automatically calculates the necessary buffer size and allocates
59 int MultiByteToWideCharEx( UINT cp_src
,
60 LPCSTR lpMultiByteString
,
62 CStgTransferHelper
& refDTransHelper
,
63 BOOL bEnsureTrailingZero
)
65 OSL_ASSERT( IsValidCodePage( cp_src
) );
66 OSL_ASSERT( nullptr != lpMultiByteString
);
68 // calculate the required buff size
69 int reqSize
= CalcBuffSizeForTextConversion( cp_src
, lpMultiByteString
, lenStr
);
71 if ( bEnsureTrailingZero
)
72 reqSize
+= sizeof( sal_Unicode
);
74 // initialize the data-transfer helper
75 refDTransHelper
.init( reqSize
);
77 // setup a global memory pointer
78 CRawHGlobalPtr
ptrHGlob( refDTransHelper
);
80 // do the conversion and return
81 return MultiByteToWideChar( cp_src
,
85 static_cast< LPWSTR
>( ptrHGlob
.GetMemPtr( ) ),
86 ptrHGlob
.MemSize( ) );
89 // converts unicode text into text of the specified code page
90 // automatically calculates the necessary buffer size and allocates
93 int WideCharToMultiByteEx( UINT cp_dest
,
94 LPCWSTR lpWideCharString
,
96 CStgTransferHelper
& refDTransHelper
,
97 BOOL bEnsureTrailingZero
)
99 OSL_ASSERT( IsValidCodePage( cp_dest
) );
100 OSL_ASSERT( nullptr != lpWideCharString
);
102 // calculate the required buff size
103 int reqSize
= CalcBuffSizeForTextConversion( cp_dest
, lpWideCharString
, lenStr
);
105 if ( bEnsureTrailingZero
)
106 reqSize
+= sizeof( sal_Int8
);
108 // initialize the data-transfer helper
109 refDTransHelper
.init( reqSize
);
111 // setup a global memory pointer
112 CRawHGlobalPtr
ptrHGlob( refDTransHelper
);
114 // do the conversion and return
115 return WideCharToMultiByte( cp_dest
,
119 static_cast< LPSTR
>( ptrHGlob
.GetMemPtr( ) ),
125 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */