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 <rtl/ustring.h>
21 #include <osl/diagnose.h>
22 #include "DTransHelper.hxx"
26 CStgTransferHelper::CStgTransferHelper( sal_Bool bAutoInit
,
28 sal_Bool bDelStgOnRelease
) :
30 m_bDelStgOnRelease( bDelStgOnRelease
)
33 init( hGlob
, m_bDelStgOnRelease
);
38 CStgTransferHelper::~CStgTransferHelper( )
41 m_lpStream
->Release( );
44 // TransferData into the
46 void SAL_CALL
CStgTransferHelper::write( const void* lpData
, ULONG cb
, ULONG
* cbWritten
)
51 hr
= m_lpStream
->Write( lpData
, cb
, cbWritten
);
54 throw CStgTransferException( hr
);
56 #if OSL_DEBUG_LEVEL > 0
58 hr
= GetHGlobalFromStream( m_lpStream
, &hGlob
);
59 OSL_ASSERT( SUCCEEDED( hr
) );
61 /*DWORD dwSize =*/ GlobalSize( hGlob
);
62 /*LPVOID lpdbgData =*/ GlobalLock( hGlob
);
63 GlobalUnlock( hGlob
);
69 void SAL_CALL
CStgTransferHelper::read( LPVOID pv
, ULONG cb
, ULONG
* pcbRead
)
74 hr
= m_lpStream
->Read( pv
, cb
, pcbRead
);
77 throw CStgTransferException( hr
);
82 HGLOBAL SAL_CALL
CStgTransferHelper::getHGlobal( ) const
84 OSL_ASSERT( m_lpStream
);
90 HRESULT hr
= GetHGlobalFromStream( m_lpStream
, &hGlob
);
100 void SAL_CALL
CStgTransferHelper::getIStream( LPSTREAM
* ppStream
)
102 OSL_ASSERT( ppStream
);
103 *ppStream
= m_lpStream
;
105 static_cast< LPUNKNOWN
>( *ppStream
)->AddRef( );
110 void SAL_CALL
CStgTransferHelper::init( SIZE_T newSize
,
112 sal_Bool bDelStgOnRelease
)
116 m_bDelStgOnRelease
= bDelStgOnRelease
;
118 HGLOBAL hGlob
= GlobalAlloc( uiFlags
, newSize
);
120 throw CStgTransferException( STG_E_MEDIUMFULL
);
122 HRESULT hr
= CreateStreamOnHGlobal( hGlob
, m_bDelStgOnRelease
, &m_lpStream
);
127 throw CStgTransferException( hr
);
130 #if OSL_DEBUG_LEVEL > 0
132 hr
= m_lpStream
->Stat( &statstg
, STATFLAG_DEFAULT
);
133 OSL_ASSERT( SUCCEEDED( hr
) );
139 void SAL_CALL
CStgTransferHelper::init( HGLOBAL hGlob
,
140 sal_Bool bDelStgOnRelease
)
144 m_bDelStgOnRelease
= bDelStgOnRelease
;
146 HRESULT hr
= CreateStreamOnHGlobal( hGlob
, m_bDelStgOnRelease
, &m_lpStream
);
148 throw CStgTransferException( hr
);
151 // free the global memory and invalidate the stream pointer
153 void SAL_CALL
CStgTransferHelper::cleanup( )
155 if ( m_lpStream
&& !m_bDelStgOnRelease
)
158 GetHGlobalFromStream( m_lpStream
, &hGlob
);
164 m_lpStream
->Release( );
169 // return the size of memory we point to
171 sal_uInt32 SAL_CALL
CStgTransferHelper::memSize( CLIPFORMAT cf
) const
175 if ( NULL
!= m_lpStream
)
178 GetHGlobalFromStream( m_lpStream
, &hGlob
);
180 if ( CF_TEXT
== cf
|| RegisterClipboardFormat( "HTML Format" ) == cf
)
182 sal_Char
* pText
= static_cast< sal_Char
* >( GlobalLock( hGlob
) );
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
) );
194 dwSize
= rtl_ustr_getLength( pText
) * sizeof( sal_Unicode
);
195 GlobalUnlock( hGlob
);
199 dwSize
= GlobalSize( hGlob
);
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */