Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / dtrans / source / win32 / dtobj / DTransHelper.cxx
blob5ca12d0fe26b186c127a74076da5159c101e60ef
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 <rtl/ustring.h>
21 #include <osl/diagnose.h>
22 #include "DTransHelper.hxx"
24 // implementation
26 CStgTransferHelper::CStgTransferHelper( bool bAutoInit,
27 HGLOBAL hGlob,
28 bool bDelStgOnRelease ) :
29 m_lpStream( nullptr ),
30 m_bDelStgOnRelease( bDelStgOnRelease )
32 if ( bAutoInit )
33 init( hGlob, m_bDelStgOnRelease );
36 // dtor
38 CStgTransferHelper::~CStgTransferHelper( )
40 if ( m_lpStream )
41 m_lpStream->Release( );
44 // TransferData into the
46 void CStgTransferHelper::write( const void* lpData, ULONG cb, ULONG* cbWritten )
48 HRESULT hr = E_FAIL;
50 if ( m_lpStream )
51 hr = m_lpStream->Write( lpData, cb, cbWritten );
53 if ( FAILED( hr ) )
54 throw CStgTransferException( hr );
56 #if OSL_DEBUG_LEVEL > 0
57 HGLOBAL hGlob;
58 hr = GetHGlobalFromStream( m_lpStream, &hGlob );
59 OSL_ASSERT( SUCCEEDED( hr ) );
61 /*DWORD dwSize =*/ GlobalSize( hGlob );
62 /*LPVOID lpdbgData =*/ GlobalLock( hGlob );
63 GlobalUnlock( hGlob );
64 #endif
67 // read
69 void CStgTransferHelper::read( LPVOID pv, ULONG cb, ULONG* pcbRead )
71 HRESULT hr = E_FAIL;
73 if ( m_lpStream )
74 hr = m_lpStream->Read( pv, cb , pcbRead );
76 if ( FAILED( hr ) )
77 throw CStgTransferException( hr );
80 // GetHGlobal
82 HGLOBAL CStgTransferHelper::getHGlobal( ) const
84 OSL_ASSERT( m_lpStream );
86 HGLOBAL hGlob = nullptr;
88 if ( m_lpStream )
90 HRESULT hr = GetHGlobalFromStream( m_lpStream, &hGlob );
91 if ( FAILED( hr ) )
92 hGlob = nullptr;
95 return hGlob;
98 // getIStream
100 void CStgTransferHelper::getIStream( LPSTREAM* ppStream )
102 OSL_ASSERT( ppStream );
103 *ppStream = m_lpStream;
104 if ( *ppStream )
105 static_cast< LPUNKNOWN >( *ppStream )->AddRef( );
108 // Init
110 void CStgTransferHelper::init( SIZE_T newSize,
111 sal_uInt32 uiFlags,
112 bool bDelStgOnRelease )
114 cleanup( );
116 m_bDelStgOnRelease = bDelStgOnRelease;
118 HGLOBAL hGlob = GlobalAlloc( uiFlags, newSize );
119 if ( nullptr == hGlob )
120 throw CStgTransferException( STG_E_MEDIUMFULL );
122 HRESULT hr = CreateStreamOnHGlobal( hGlob, m_bDelStgOnRelease, &m_lpStream );
123 if ( FAILED( hr ) )
125 GlobalFree( hGlob );
126 m_lpStream = nullptr;
127 throw CStgTransferException( hr );
130 #if OSL_DEBUG_LEVEL > 0
131 STATSTG statstg;
132 hr = m_lpStream->Stat( &statstg, STATFLAG_DEFAULT );
133 OSL_ASSERT( SUCCEEDED( hr ) );
134 #endif
137 // Init
139 void CStgTransferHelper::init( HGLOBAL hGlob,
140 bool bDelStgOnRelease )
142 cleanup( );
144 m_bDelStgOnRelease = bDelStgOnRelease;
146 HRESULT hr = CreateStreamOnHGlobal( hGlob, m_bDelStgOnRelease, &m_lpStream );
147 if ( FAILED( hr ) )
148 throw CStgTransferException( hr );
151 // free the global memory and invalidate the stream pointer
153 void CStgTransferHelper::cleanup( )
155 if ( m_lpStream && !m_bDelStgOnRelease )
157 HGLOBAL hGlob;
158 GetHGlobalFromStream( m_lpStream, &hGlob );
159 GlobalFree( hGlob );
162 if ( m_lpStream )
164 m_lpStream->Release( );
165 m_lpStream = nullptr;
169 // return the size of memory we point to
171 sal_uInt32 CStgTransferHelper::memSize( CLIPFORMAT cf ) const
173 DWORD dwSize = 0;
175 if ( nullptr != m_lpStream )
177 HGLOBAL hGlob;
178 GetHGlobalFromStream( m_lpStream, &hGlob );
180 if ( CF_TEXT == cf || RegisterClipboardFormatW( L"HTML Format" ) == cf )
182 sal_Char* pText = static_cast< sal_Char* >( GlobalLock( hGlob ) );
183 if ( pText )
185 dwSize = strlen(pText) + 1; // strlen + trailing '\0'
186 GlobalUnlock( hGlob );
189 else if ( CF_UNICODETEXT == cf )
191 sal_Unicode* pText = static_cast< sal_Unicode* >( GlobalLock( hGlob ) );
192 if ( pText )
194 dwSize = rtl_ustr_getLength( pText ) * sizeof( sal_Unicode );
195 GlobalUnlock( hGlob );
198 else
199 dwSize = GlobalSize( hGlob );
202 return dwSize;
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */