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"
24 //------------------------------------------------------------------------
26 //------------------------------------------------------------------------
28 CStgTransferHelper::CStgTransferHelper( sal_Bool bAutoInit
,
30 sal_Bool bDelStgOnRelease
) :
32 m_bDelStgOnRelease( bDelStgOnRelease
)
35 init( hGlob
, m_bDelStgOnRelease
);
38 //------------------------------------------------------------------------
40 //------------------------------------------------------------------------
43 CStgTransferHelper::~CStgTransferHelper( )
46 m_lpStream
->Release( );
49 //------------------------------------------------------------------------
50 // TransferData into the
51 //------------------------------------------------------------------------
53 void SAL_CALL
CStgTransferHelper::write( const void* lpData
, ULONG cb
, ULONG
* cbWritten
)
58 hr
= m_lpStream
->Write( lpData
, cb
, cbWritten
);
61 throw CStgTransferException( hr
);
63 #if OSL_DEBUG_LEVEL > 0
65 hr
= GetHGlobalFromStream( m_lpStream
, &hGlob
);
66 OSL_ASSERT( SUCCEEDED( hr
) );
68 /*DWORD dwSize =*/ GlobalSize( hGlob
);
69 /*LPVOID lpdbgData =*/ GlobalLock( hGlob
);
70 GlobalUnlock( hGlob
);
74 //------------------------------------------------------------------------
76 //------------------------------------------------------------------------
78 void SAL_CALL
CStgTransferHelper::read( LPVOID pv
, ULONG cb
, ULONG
* pcbRead
)
83 hr
= m_lpStream
->Read( pv
, cb
, pcbRead
);
86 throw CStgTransferException( hr
);
89 //------------------------------------------------------------------------
91 //------------------------------------------------------------------------
93 HGLOBAL SAL_CALL
CStgTransferHelper::getHGlobal( ) const
95 OSL_ASSERT( m_lpStream
);
101 HRESULT hr
= GetHGlobalFromStream( m_lpStream
, &hGlob
);
109 //------------------------------------------------------------------------
111 //------------------------------------------------------------------------
113 void SAL_CALL
CStgTransferHelper::getIStream( LPSTREAM
* ppStream
)
115 OSL_ASSERT( ppStream
);
116 *ppStream
= m_lpStream
;
118 static_cast< LPUNKNOWN
>( *ppStream
)->AddRef( );
121 //------------------------------------------------------------------------
123 //------------------------------------------------------------------------
125 void SAL_CALL
CStgTransferHelper::init( SIZE_T newSize
,
127 sal_Bool bDelStgOnRelease
)
131 m_bDelStgOnRelease
= bDelStgOnRelease
;
133 HGLOBAL hGlob
= GlobalAlloc( uiFlags
, newSize
);
135 throw CStgTransferException( STG_E_MEDIUMFULL
);
137 HRESULT hr
= CreateStreamOnHGlobal( hGlob
, m_bDelStgOnRelease
, &m_lpStream
);
142 throw CStgTransferException( hr
);
145 #if OSL_DEBUG_LEVEL > 0
147 hr
= m_lpStream
->Stat( &statstg
, STATFLAG_DEFAULT
);
148 OSL_ASSERT( SUCCEEDED( hr
) );
152 //------------------------------------------------------------------------
154 //------------------------------------------------------------------------
156 void SAL_CALL
CStgTransferHelper::init( HGLOBAL hGlob
,
157 sal_Bool bDelStgOnRelease
)
161 m_bDelStgOnRelease
= bDelStgOnRelease
;
163 HRESULT hr
= CreateStreamOnHGlobal( hGlob
, m_bDelStgOnRelease
, &m_lpStream
);
165 throw CStgTransferException( hr
);
168 //------------------------------------------------------------------------
169 // free the global memory and invalidate the stream pointer
170 //------------------------------------------------------------------------
172 void SAL_CALL
CStgTransferHelper::cleanup( )
174 if ( m_lpStream
&& !m_bDelStgOnRelease
)
177 GetHGlobalFromStream( m_lpStream
, &hGlob
);
183 m_lpStream
->Release( );
188 //------------------------------------------------------------------------
189 // return the size of memory we point to
190 //------------------------------------------------------------------------
192 sal_uInt32 SAL_CALL
CStgTransferHelper::memSize( CLIPFORMAT cf
) const
196 if ( NULL
!= m_lpStream
)
199 GetHGlobalFromStream( m_lpStream
, &hGlob
);
201 if ( CF_TEXT
== cf
|| RegisterClipboardFormat( "HTML Format" ) == cf
)
203 sal_Char
* pText
= static_cast< sal_Char
* >( GlobalLock( hGlob
) );
206 dwSize
= strlen(pText
) + 1; // strlen + trailing '\0'
207 GlobalUnlock( hGlob
);
210 else if ( CF_UNICODETEXT
== cf
)
212 sal_Unicode
* pText
= static_cast< sal_Unicode
* >( GlobalLock( hGlob
) );
215 dwSize
= rtl_ustr_getLength( pText
) * sizeof( sal_Unicode
);
216 GlobalUnlock( hGlob
);
220 dwSize
= GlobalSize( hGlob
);
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */