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"
27 CStgTransferHelper::CStgTransferHelper( bool bAutoInit
,
29 bool bDelStgOnRelease
) :
30 m_lpStream( nullptr ),
31 m_bDelStgOnRelease( bDelStgOnRelease
)
34 init( hGlob
, m_bDelStgOnRelease
);
39 CStgTransferHelper::~CStgTransferHelper( )
42 m_lpStream
->Release( );
45 // TransferData into the
47 void CStgTransferHelper::write( const void* lpData
, ULONG cb
, ULONG
* cbWritten
)
52 hr
= m_lpStream
->Write( lpData
, cb
, cbWritten
);
55 throw CStgTransferException( hr
);
57 #if OSL_DEBUG_LEVEL > 0
59 hr
= GetHGlobalFromStream( m_lpStream
, &hGlob
);
60 OSL_ASSERT( SUCCEEDED( hr
) );
62 /*DWORD dwSize =*/ GlobalSize( hGlob
);
63 /*LPVOID lpdbgData =*/ GlobalLock( hGlob
);
64 GlobalUnlock( hGlob
);
70 void CStgTransferHelper::read( LPVOID pv
, ULONG cb
, ULONG
* pcbRead
)
75 hr
= m_lpStream
->Read( pv
, cb
, pcbRead
);
78 throw CStgTransferException( hr
);
83 HGLOBAL
CStgTransferHelper::getHGlobal( ) const
85 OSL_ASSERT( m_lpStream
);
87 HGLOBAL hGlob
= nullptr;
91 HRESULT hr
= GetHGlobalFromStream( m_lpStream
, &hGlob
);
101 void CStgTransferHelper::getIStream( LPSTREAM
* ppStream
)
104 *ppStream
= m_lpStream
;
106 static_cast< LPUNKNOWN
>( *ppStream
)->AddRef( );
111 void CStgTransferHelper::init( SIZE_T newSize
,
113 bool bDelStgOnRelease
)
117 m_bDelStgOnRelease
= bDelStgOnRelease
;
119 HGLOBAL hGlob
= GlobalAlloc( uiFlags
, newSize
);
120 if ( nullptr == hGlob
)
121 throw CStgTransferException( STG_E_MEDIUMFULL
);
123 HRESULT hr
= CreateStreamOnHGlobal( hGlob
, m_bDelStgOnRelease
, &m_lpStream
);
127 m_lpStream
= nullptr;
128 throw CStgTransferException( hr
);
131 #if OSL_DEBUG_LEVEL > 0
133 hr
= m_lpStream
->Stat( &statstg
, STATFLAG_DEFAULT
);
134 OSL_ASSERT( SUCCEEDED( hr
) );
140 void CStgTransferHelper::init( HGLOBAL hGlob
,
141 bool bDelStgOnRelease
)
145 m_bDelStgOnRelease
= bDelStgOnRelease
;
147 HRESULT hr
= CreateStreamOnHGlobal( hGlob
, m_bDelStgOnRelease
, &m_lpStream
);
149 throw CStgTransferException( hr
);
152 // free the global memory and invalidate the stream pointer
154 void CStgTransferHelper::cleanup( )
156 if ( m_lpStream
&& !m_bDelStgOnRelease
)
159 GetHGlobalFromStream( m_lpStream
, &hGlob
);
165 m_lpStream
->Release( );
166 m_lpStream
= nullptr;
170 // return the size of memory we point to
172 sal_uInt32
CStgTransferHelper::memSize( CLIPFORMAT cf
) const
176 if ( nullptr != m_lpStream
)
179 GetHGlobalFromStream( m_lpStream
, &hGlob
);
181 if ( CF_TEXT
== cf
|| RegisterClipboardFormatW( L
"HTML Format" ) == cf
)
183 char* pText
= static_cast< char* >( GlobalLock( hGlob
) );
186 dwSize
= strlen(pText
) + 1; // strlen + trailing '\0'
187 GlobalUnlock( hGlob
);
190 else if ( CF_UNICODETEXT
== cf
)
192 sal_Unicode
* pText
= static_cast< sal_Unicode
* >( GlobalLock( hGlob
) );
195 dwSize
= rtl_ustr_getLength( pText
) * sizeof( sal_Unicode
);
196 GlobalUnlock( hGlob
);
200 dwSize
= GlobalSize( hGlob
);
206 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */