LibreOffice Module dtrans (master)  1
TxtCnvtHlp.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <osl/diagnose.h>
21 #include "TxtCnvtHlp.hxx"
22 #include "DTransHelper.hxx"
23 #include "../misc/ImplHelper.hxx"
24 
25 using namespace ::com::sun::star::datatransfer;
26 using namespace ::com::sun::star::uno;
27 
28 // assuming a '\0' terminated string if no length specified
29 
30 static int CalcBuffSizeForTextConversion( UINT code_page, LPCSTR lpMultiByteString, int nLen = -1 )
31 {
32  return ( MultiByteToWideChar( code_page,
33  0,
34  lpMultiByteString,
35  nLen,
36  nullptr,
37  0 ) * sizeof( sal_Unicode ) );
38 }
39 
40 // assuming a '\0' terminated string if no length specified
41 
42 static int CalcBuffSizeForTextConversion( UINT code_page, LPCWSTR lpWideCharString, int nLen = -1 )
43 {
44  return WideCharToMultiByte( code_page,
45  0,
46  lpWideCharString,
47  nLen,
48  nullptr,
49  0,
50  nullptr,
51  nullptr );
52 }
53 
54 // converts text in one code page into unicode text
55 // automatically calculates the necessary buffer size and allocates
56 // the buffer
57 
58 int MultiByteToWideCharEx( UINT cp_src,
59  LPCSTR lpMultiByteString,
60  sal_uInt32 lenStr,
61  CStgTransferHelper& refDTransHelper,
62  BOOL bEnsureTrailingZero )
63 {
64  OSL_ASSERT( IsValidCodePage( cp_src ) );
65  OSL_ASSERT( nullptr != lpMultiByteString );
66 
67  // calculate the required buff size
68  int reqSize = CalcBuffSizeForTextConversion( cp_src, lpMultiByteString, lenStr );
69 
70  if ( bEnsureTrailingZero )
71  reqSize += sizeof( sal_Unicode );
72 
73  // initialize the data-transfer helper
74  refDTransHelper.init( reqSize );
75 
76  // setup a global memory pointer
77  CRawHGlobalPtr ptrHGlob( refDTransHelper );
78 
79  // do the conversion and return
80  return MultiByteToWideChar( cp_src,
81  0,
82  lpMultiByteString,
83  lenStr,
84  static_cast< LPWSTR >( ptrHGlob.GetMemPtr( ) ),
85  ptrHGlob.MemSize( ) );
86 }
87 
88 // converts unicode text into text of the specified code page
89 // automatically calculates the necessary buffer size and allocates
90 // the buffer
91 
92 int WideCharToMultiByteEx( UINT cp_dest,
93  LPCWSTR lpWideCharString,
94  sal_uInt32 lenStr,
95  CStgTransferHelper& refDTransHelper,
96  BOOL bEnsureTrailingZero )
97 {
98  OSL_ASSERT( IsValidCodePage( cp_dest ) );
99  OSL_ASSERT( nullptr != lpWideCharString );
100 
101  // calculate the required buff size
102  int reqSize = CalcBuffSizeForTextConversion( cp_dest, lpWideCharString, lenStr );
103 
104  if ( bEnsureTrailingZero )
105  reqSize += sizeof( sal_Int8 );
106 
107  // initialize the data-transfer helper
108  refDTransHelper.init( reqSize );
109 
110  // setup a global memory pointer
111  CRawHGlobalPtr ptrHGlob( refDTransHelper );
112 
113  // do the conversion and return
114  return WideCharToMultiByte( cp_dest,
115  0,
116  lpWideCharString,
117  lenStr,
118  static_cast< LPSTR >( ptrHGlob.GetMemPtr( ) ),
119  ptrHGlob.MemSize( ),
120  nullptr,
121  nullptr );
122 }
123 
124 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
int MultiByteToWideCharEx(UINT cp_src, LPCSTR lpMultiByteString, sal_uInt32 lenStr, CStgTransferHelper &refDTransHelper, BOOL bEnsureTrailingZero)
Definition: TxtCnvtHlp.cxx:58
signed char sal_Int8
LPVOID GetMemPtr()
int WideCharToMultiByteEx(UINT cp_dest, LPCWSTR lpWideCharString, sal_uInt32 lenStr, CStgTransferHelper &refDTransHelper, BOOL bEnsureTrailingZero)
Definition: TxtCnvtHlp.cxx:92
sal_uInt16 sal_Unicode
const wchar_t *typedef BOOL
int MemSize() const
static int CalcBuffSizeForTextConversion(UINT code_page, LPCSTR lpMultiByteString, int nLen=-1)
Definition: TxtCnvtHlp.cxx:30
void init(SIZE_T newSize, sal_uInt32 uiFlags=GHND, bool bDelStgOnRelease=false)