1*48123e16SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*48123e16SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*48123e16SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*48123e16SAndrew Rist  * distributed with this work for additional information
6*48123e16SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*48123e16SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*48123e16SAndrew Rist  * "License"); you may not use this file except in compliance
9*48123e16SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*48123e16SAndrew Rist  *
11*48123e16SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*48123e16SAndrew Rist  *
13*48123e16SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*48123e16SAndrew Rist  * software distributed under the License is distributed on an
15*48123e16SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*48123e16SAndrew Rist  * KIND, either express or implied.  See the License for the
17*48123e16SAndrew Rist  * specific language governing permissions and limitations
18*48123e16SAndrew Rist  * under the License.
19*48123e16SAndrew Rist  *
20*48123e16SAndrew Rist  *************************************************************/
21*48123e16SAndrew Rist 
22*48123e16SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_dtrans.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir 
28cdf0e10cSrcweir //------------------------------------------------------------------------
29cdf0e10cSrcweir // includes
30cdf0e10cSrcweir //------------------------------------------------------------------------
31cdf0e10cSrcweir #include <osl/diagnose.h>
32cdf0e10cSrcweir #include "ImplHelper.hxx"
33cdf0e10cSrcweir #include <rtl/tencinfo.h>
34cdf0e10cSrcweir #include <rtl/memory.h>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <memory>
37cdf0e10cSrcweir #if defined _MSC_VER
38cdf0e10cSrcweir #pragma warning(push,1)
39cdf0e10cSrcweir #endif
40cdf0e10cSrcweir #include <windows.h>
41cdf0e10cSrcweir #if defined _MSC_VER
42cdf0e10cSrcweir #pragma warning(pop)
43cdf0e10cSrcweir #endif
44cdf0e10cSrcweir #ifdef __MINGW32__
45cdf0e10cSrcweir #include <excpt.h>
46cdf0e10cSrcweir #endif
47cdf0e10cSrcweir 
48cdf0e10cSrcweir //------------------------------------------------------------------------
49cdf0e10cSrcweir // defines
50cdf0e10cSrcweir //------------------------------------------------------------------------
51cdf0e10cSrcweir 
52cdf0e10cSrcweir #define FORMATETC_EXACT_MATCH    1
53cdf0e10cSrcweir #define FORMATETC_PARTIAL_MATCH -1
54cdf0e10cSrcweir #define FORMATETC_NO_MATCH       0
55cdf0e10cSrcweir 
56cdf0e10cSrcweir //------------------------------------------------------------------------
57cdf0e10cSrcweir // namespace directives
58cdf0e10cSrcweir //------------------------------------------------------------------------
59cdf0e10cSrcweir 
60cdf0e10cSrcweir using ::rtl::OUString;
61cdf0e10cSrcweir using ::rtl::OString;
62cdf0e10cSrcweir 
63cdf0e10cSrcweir //------------------------------------------------------------------------
64cdf0e10cSrcweir // returns a windows codepage appropriate to the
65cdf0e10cSrcweir // given mime charset parameter value
66cdf0e10cSrcweir //------------------------------------------------------------------------
67cdf0e10cSrcweir 
getWinCPFromMimeCharset(const OUString & charset)68cdf0e10cSrcweir sal_uInt32 SAL_CALL getWinCPFromMimeCharset( const OUString& charset )
69cdf0e10cSrcweir {
70cdf0e10cSrcweir 	sal_uInt32 winCP = GetACP( );
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 	if ( charset.getLength( ) )
73cdf0e10cSrcweir 	{
74cdf0e10cSrcweir 		OString osCharset(
75cdf0e10cSrcweir 			charset.getStr( ), charset.getLength( ), RTL_TEXTENCODING_ASCII_US );
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 		rtl_TextEncoding txtEnc =
78cdf0e10cSrcweir 			rtl_getTextEncodingFromMimeCharset( osCharset.getStr( ) );
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 		sal_uInt32 winChrs = rtl_getBestWindowsCharsetFromTextEncoding( txtEnc );
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 		CHARSETINFO chrsInf;
83cdf0e10cSrcweir 		sal_Bool bRet = TranslateCharsetInfo( (DWORD*)winChrs, &chrsInf, TCI_SRCCHARSET ) ?
84cdf0e10cSrcweir                         sal_True : sal_False;
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 		// if one of the above functions fails
87cdf0e10cSrcweir 		// we will return the current ANSI codepage
88cdf0e10cSrcweir 		// of this thread
89cdf0e10cSrcweir 		if ( bRet )
90cdf0e10cSrcweir 			winCP = chrsInf.ciACP;
91cdf0e10cSrcweir 	}
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 	return winCP;
94cdf0e10cSrcweir }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir //--------------------------------------------------
97cdf0e10cSrcweir // returns a windows codepage appropriate to the
98cdf0e10cSrcweir // given locale and locale type
99cdf0e10cSrcweir //--------------------------------------------------
100cdf0e10cSrcweir 
getWinCPFromLocaleId(LCID lcid,LCTYPE lctype)101cdf0e10cSrcweir OUString SAL_CALL getWinCPFromLocaleId( LCID lcid, LCTYPE lctype )
102cdf0e10cSrcweir {
103cdf0e10cSrcweir 	OSL_ASSERT( IsValidLocale( lcid, LCID_SUPPORTED ) );
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 	// we set an default value
106cdf0e10cSrcweir 	OUString winCP;
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 	// set an default value
109cdf0e10cSrcweir 	if ( LOCALE_IDEFAULTCODEPAGE == lctype )
110cdf0e10cSrcweir 	{
111cdf0e10cSrcweir 		winCP = OUString::valueOf( static_cast<sal_Int32>(GetOEMCP( )), 10 );
112cdf0e10cSrcweir 	}
113cdf0e10cSrcweir 	else if ( LOCALE_IDEFAULTANSICODEPAGE == lctype )
114cdf0e10cSrcweir 	{
115cdf0e10cSrcweir 		winCP = OUString::valueOf( static_cast<sal_Int32>(GetACP( )), 10 );
116cdf0e10cSrcweir 	}
117cdf0e10cSrcweir 	else
118cdf0e10cSrcweir 		OSL_ASSERT( sal_False );
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 	// we use the GetLocaleInfoA because don't want to provide
121cdf0e10cSrcweir 	// a unicode wrapper function for Win9x in sal/systools
122cdf0e10cSrcweir 	char buff[6];
123cdf0e10cSrcweir 	sal_Int32 nResult = GetLocaleInfoA(
124cdf0e10cSrcweir 		lcid, lctype | LOCALE_USE_CP_ACP, buff, sizeof( buff ) );
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 	OSL_ASSERT( nResult );
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 	if ( nResult )
129cdf0e10cSrcweir 	{
130cdf0e10cSrcweir 		sal_Int32 len = MultiByteToWideChar(
131cdf0e10cSrcweir 			CP_ACP, 0, buff, -1, NULL, 0 );
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 		OSL_ASSERT( len > 0 );
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 		std::auto_ptr< sal_Unicode > lpwchBuff( new sal_Unicode[len] );
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 		if ( NULL != lpwchBuff.get( ) )
138cdf0e10cSrcweir 		{
139cdf0e10cSrcweir 			len = MultiByteToWideChar(
140cdf0e10cSrcweir 				CP_ACP, 0, buff, -1, reinterpret_cast<LPWSTR>(lpwchBuff.get( )), len );
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 			winCP = OUString( lpwchBuff.get( ), (len - 1) );
143cdf0e10cSrcweir 		}
144cdf0e10cSrcweir 	}
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 	return winCP;
147cdf0e10cSrcweir }
148cdf0e10cSrcweir 
149cdf0e10cSrcweir //--------------------------------------------------
150cdf0e10cSrcweir // returns a mime charset parameter value appropriate
151cdf0e10cSrcweir // to the given codepage, optional a prefix can be
152cdf0e10cSrcweir // given, e.g. "windows-" or "cp"
153cdf0e10cSrcweir //--------------------------------------------------
154cdf0e10cSrcweir 
getMimeCharsetFromWinCP(sal_uInt32 cp,const OUString & aPrefix)155cdf0e10cSrcweir OUString SAL_CALL getMimeCharsetFromWinCP( sal_uInt32 cp, const OUString& aPrefix )
156cdf0e10cSrcweir {
157cdf0e10cSrcweir 	return aPrefix + cptostr( cp );
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir //--------------------------------------------------
161cdf0e10cSrcweir // returns a mime charset parameter value appropriate
162cdf0e10cSrcweir // to the given locale id and locale type, optional a
163cdf0e10cSrcweir // prefix can be given, e.g. "windows-" or "cp"
164cdf0e10cSrcweir //--------------------------------------------------
165cdf0e10cSrcweir 
getMimeCharsetFromLocaleId(LCID lcid,LCTYPE lctype,const OUString & aPrefix)166cdf0e10cSrcweir OUString SAL_CALL getMimeCharsetFromLocaleId( LCID lcid, LCTYPE lctype, const OUString& aPrefix  )
167cdf0e10cSrcweir {
168cdf0e10cSrcweir 	OUString charset = getWinCPFromLocaleId( lcid, lctype );
169cdf0e10cSrcweir 	return aPrefix + charset;
170cdf0e10cSrcweir }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir //------------------------------------------------------------------------
173cdf0e10cSrcweir // IsOEMCP
174cdf0e10cSrcweir //------------------------------------------------------------------------
175cdf0e10cSrcweir 
IsOEMCP(sal_uInt32 codepage)176cdf0e10cSrcweir sal_Bool SAL_CALL IsOEMCP( sal_uInt32 codepage )
177cdf0e10cSrcweir {
178cdf0e10cSrcweir 	OSL_ASSERT( IsValidCodePage( codepage ) );
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 	sal_uInt32 arrOEMCP[] = { 437, 708, 709, 710, 720, 737,
181cdf0e10cSrcweir 							  775, 850, 852, 855, 857, 860,
182cdf0e10cSrcweir 						      861, 862, 863, 864, 865, 866,
183cdf0e10cSrcweir 						      869, 874, 932, 936, 949, 950, 1361 };
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 	for ( sal_Int8 i = 0; i < ( sizeof( arrOEMCP )/sizeof( sal_uInt32 ) ); ++i )
186cdf0e10cSrcweir 		if ( arrOEMCP[i] == codepage )
187cdf0e10cSrcweir 			return sal_True;
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 	return sal_False;
190cdf0e10cSrcweir }
191cdf0e10cSrcweir 
192cdf0e10cSrcweir //------------------------------------------------------------------------
193cdf0e10cSrcweir // converts a codepage into its string representation
194cdf0e10cSrcweir //------------------------------------------------------------------------
195cdf0e10cSrcweir 
cptostr(sal_uInt32 codepage)196cdf0e10cSrcweir OUString SAL_CALL cptostr( sal_uInt32 codepage )
197cdf0e10cSrcweir {
198cdf0e10cSrcweir 	OSL_ASSERT( IsValidCodePage( codepage ) );
199cdf0e10cSrcweir 
200cdf0e10cSrcweir 	return OUString::valueOf( static_cast<sal_Int64>( codepage ), 10 );
201cdf0e10cSrcweir }
202cdf0e10cSrcweir 
203cdf0e10cSrcweir //-------------------------------------------------------------------------
204cdf0e10cSrcweir // OleStdDeleteTargetDevice()
205cdf0e10cSrcweir //
206cdf0e10cSrcweir // Purpose:
207cdf0e10cSrcweir //
208cdf0e10cSrcweir // Parameters:
209cdf0e10cSrcweir //
210cdf0e10cSrcweir // Return Value:
211cdf0e10cSrcweir //    SCODE  -  S_OK if successful
212cdf0e10cSrcweir //-------------------------------------------------------------------------
213cdf0e10cSrcweir 
DeleteTargetDevice(DVTARGETDEVICE * ptd)214cdf0e10cSrcweir void SAL_CALL DeleteTargetDevice( DVTARGETDEVICE* ptd )
215cdf0e10cSrcweir {
216cdf0e10cSrcweir #ifdef __MINGW32__
217cdf0e10cSrcweir 	jmp_buf jmpbuf;
218cdf0e10cSrcweir 	__SEHandler han;
219cdf0e10cSrcweir 	if (__builtin_setjmp(jmpbuf) == 0)
220cdf0e10cSrcweir 	{
221cdf0e10cSrcweir 		han.Set(jmpbuf, NULL, (__SEHandler::PF)EXCEPTION_EXECUTE_HANDLER);
222cdf0e10cSrcweir #else
223cdf0e10cSrcweir 	__try
224cdf0e10cSrcweir 	{
225cdf0e10cSrcweir #endif
226cdf0e10cSrcweir 		CoTaskMemFree( ptd );
227cdf0e10cSrcweir 	}
228cdf0e10cSrcweir #ifdef __MINGW32__
229cdf0e10cSrcweir 	else
230cdf0e10cSrcweir #else
231cdf0e10cSrcweir 	__except( EXCEPTION_EXECUTE_HANDLER )
232cdf0e10cSrcweir #endif
233cdf0e10cSrcweir 	{
234cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "Error DeleteTargetDevice" );
235cdf0e10cSrcweir 	}
236cdf0e10cSrcweir #ifdef __MINGW32__
237cdf0e10cSrcweir 	han.Reset();
238cdf0e10cSrcweir #endif
239cdf0e10cSrcweir }
240cdf0e10cSrcweir 
241cdf0e10cSrcweir 
242cdf0e10cSrcweir 
243cdf0e10cSrcweir //-------------------------------------------------------------------------
244cdf0e10cSrcweir // OleStdCopyTargetDevice()
245cdf0e10cSrcweir //
246cdf0e10cSrcweir // Purpose:
247cdf0e10cSrcweir //  duplicate a TARGETDEVICE struct. this function allocates memory for
248cdf0e10cSrcweir //  the copy. the caller MUST free the allocated copy when done with it
249cdf0e10cSrcweir //  using the standard allocator returned from CoGetMalloc.
250cdf0e10cSrcweir //  (OleStdFree can be used to free the copy).
251cdf0e10cSrcweir //
252cdf0e10cSrcweir // Parameters:
253cdf0e10cSrcweir //  ptdSrc      pointer to source TARGETDEVICE
254cdf0e10cSrcweir //
255cdf0e10cSrcweir // Return Value:
256cdf0e10cSrcweir //    pointer to allocated copy of ptdSrc
257cdf0e10cSrcweir //    if ptdSrc==NULL then retuns NULL is returned.
258cdf0e10cSrcweir //    if ptdSrc!=NULL and memory allocation fails, then NULL is returned
259cdf0e10cSrcweir //-------------------------------------------------------------------------
260cdf0e10cSrcweir 
261cdf0e10cSrcweir DVTARGETDEVICE* SAL_CALL CopyTargetDevice( DVTARGETDEVICE* ptdSrc )
262cdf0e10cSrcweir {
263cdf0e10cSrcweir 	DVTARGETDEVICE* ptdDest = NULL;
264cdf0e10cSrcweir 
265cdf0e10cSrcweir #ifdef __MINGW32__
266cdf0e10cSrcweir 	jmp_buf jmpbuf;
267cdf0e10cSrcweir 	__SEHandler han;
268cdf0e10cSrcweir 	if (__builtin_setjmp(jmpbuf) == 0)
269cdf0e10cSrcweir 	{
270cdf0e10cSrcweir 		han.Set(jmpbuf, NULL, (__SEHandler::PF)EXCEPTION_EXECUTE_HANDLER);
271cdf0e10cSrcweir #else
272cdf0e10cSrcweir 	__try
273cdf0e10cSrcweir 	{
274cdf0e10cSrcweir #endif
275cdf0e10cSrcweir 		if ( NULL != ptdSrc )
276cdf0e10cSrcweir 		{
277cdf0e10cSrcweir 			ptdDest = static_cast< DVTARGETDEVICE* >( CoTaskMemAlloc( ptdSrc->tdSize ) );
278cdf0e10cSrcweir 			rtl_copyMemory( ptdDest, ptdSrc, static_cast< size_t >( ptdSrc->tdSize ) );
279cdf0e10cSrcweir 		}
280cdf0e10cSrcweir 	}
281cdf0e10cSrcweir #ifdef __MINGW32__
282cdf0e10cSrcweir 	han.Reset();
283cdf0e10cSrcweir #else
284cdf0e10cSrcweir 	__except( EXCEPTION_EXECUTE_HANDLER )
285cdf0e10cSrcweir 	{
286cdf0e10cSrcweir 	}
287cdf0e10cSrcweir #endif
288cdf0e10cSrcweir 
289cdf0e10cSrcweir 	return ptdDest;
290cdf0e10cSrcweir }
291cdf0e10cSrcweir 
292cdf0e10cSrcweir 
293cdf0e10cSrcweir //-------------------------------------------------------------------------
294cdf0e10cSrcweir // OleStdCopyFormatEtc()
295cdf0e10cSrcweir //
296cdf0e10cSrcweir // Purpose:
297cdf0e10cSrcweir //  Copies the contents of a FORMATETC structure. this function takes
298cdf0e10cSrcweir //  special care to copy correctly copying the pointer to the TARGETDEVICE
299cdf0e10cSrcweir //  contained within the source FORMATETC structure.
300cdf0e10cSrcweir //  if the source FORMATETC has a non-NULL TARGETDEVICE, then a copy
301cdf0e10cSrcweir //  of the TARGETDEVICE will be allocated for the destination of the
302cdf0e10cSrcweir //  FORMATETC (petcDest).
303cdf0e10cSrcweir //
304cdf0e10cSrcweir //  NOTE: the caller MUST free the allocated copy of the TARGETDEVICE
305cdf0e10cSrcweir //  within the destination FORMATETC when done with it
306cdf0e10cSrcweir //  using the standard allocator returned from CoGetMalloc.
307cdf0e10cSrcweir //  (OleStdFree can be used to free the copy).
308cdf0e10cSrcweir //
309cdf0e10cSrcweir // Parameters:
310cdf0e10cSrcweir //  petcDest      pointer to destination FORMATETC
311cdf0e10cSrcweir //  petcSrc       pointer to source FORMATETC
312cdf0e10cSrcweir //
313cdf0e10cSrcweir // Return Value:
314cdf0e10cSrcweir //  returns TRUE if copy was successful;
315cdf0e10cSrcweir //	retuns FALSE if not successful, e.g. one or both of the pointers
316cdf0e10cSrcweir //	were invalid or the pointers were equal
317cdf0e10cSrcweir //-------------------------------------------------------------------------
318cdf0e10cSrcweir 
319cdf0e10cSrcweir sal_Bool SAL_CALL CopyFormatEtc( LPFORMATETC petcDest, LPFORMATETC petcSrc )
320cdf0e10cSrcweir {
321cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
322cdf0e10cSrcweir 
323cdf0e10cSrcweir #ifdef __MINGW32__
324cdf0e10cSrcweir 	jmp_buf jmpbuf;
325cdf0e10cSrcweir 	__SEHandler han;
326cdf0e10cSrcweir 	if (__builtin_setjmp(jmpbuf) == 0)
327cdf0e10cSrcweir 	{
328cdf0e10cSrcweir 		han.Set(jmpbuf, NULL, (__SEHandler::PF)EXCEPTION_EXECUTE_HANDLER);
329cdf0e10cSrcweir #else
330cdf0e10cSrcweir 	__try
331cdf0e10cSrcweir 	{
332cdf0e10cSrcweir #endif
333cdf0e10cSrcweir 		if ( petcDest != petcSrc )
334cdf0e10cSrcweir 		{
335cdf0e10cSrcweir 
336cdf0e10cSrcweir 		petcDest->cfFormat = petcSrc->cfFormat;
337cdf0e10cSrcweir 
338cdf0e10cSrcweir 		petcDest->ptd      = NULL;
339cdf0e10cSrcweir 		if ( NULL != petcSrc->ptd )
340cdf0e10cSrcweir 			petcDest->ptd  = CopyTargetDevice(petcSrc->ptd);
341cdf0e10cSrcweir 
342cdf0e10cSrcweir 		petcDest->dwAspect = petcSrc->dwAspect;
343cdf0e10cSrcweir 		petcDest->lindex   = petcSrc->lindex;
344cdf0e10cSrcweir 		petcDest->tymed    = petcSrc->tymed;
345cdf0e10cSrcweir 
346cdf0e10cSrcweir 		bRet = sal_True;
347cdf0e10cSrcweir 		}
348cdf0e10cSrcweir 	}
349cdf0e10cSrcweir #ifdef __MINGW32__
350cdf0e10cSrcweir 	else
351cdf0e10cSrcweir #else
352cdf0e10cSrcweir 	__except( EXCEPTION_EXECUTE_HANDLER )
353cdf0e10cSrcweir #endif
354cdf0e10cSrcweir 	{
355cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "Error CopyFormatEtc" );
356cdf0e10cSrcweir 	}
357cdf0e10cSrcweir #ifdef __MINGW32__
358cdf0e10cSrcweir 	han.Reset();
359cdf0e10cSrcweir #endif
360cdf0e10cSrcweir 
361cdf0e10cSrcweir 	return bRet;
362cdf0e10cSrcweir }
363cdf0e10cSrcweir 
364cdf0e10cSrcweir //-------------------------------------------------------------------------
365cdf0e10cSrcweir // returns:
366cdf0e10cSrcweir //  1 for exact match,
367cdf0e10cSrcweir //  0 for no match,
368cdf0e10cSrcweir // -1 for partial match (which is defined to mean the left is a subset
369cdf0e10cSrcweir //    of the right: fewer aspects, null target device, fewer medium).
370cdf0e10cSrcweir //-------------------------------------------------------------------------
371cdf0e10cSrcweir 
372cdf0e10cSrcweir sal_Int32 SAL_CALL CompareFormatEtc( const FORMATETC* pFetcLhs, const FORMATETC* pFetcRhs )
373cdf0e10cSrcweir {
374cdf0e10cSrcweir 	sal_Int32 nMatch = FORMATETC_EXACT_MATCH;
375cdf0e10cSrcweir 
376cdf0e10cSrcweir #ifdef __MINGW32__
377cdf0e10cSrcweir 	jmp_buf jmpbuf;
378cdf0e10cSrcweir 	__SEHandler han;
379cdf0e10cSrcweir 	if (__builtin_setjmp(jmpbuf) == 0)
380cdf0e10cSrcweir 	{
381cdf0e10cSrcweir 		han.Set(jmpbuf, NULL, (__SEHandler::PF)EXCEPTION_EXECUTE_HANDLER);
382cdf0e10cSrcweir #else
383cdf0e10cSrcweir 	__try
384cdf0e10cSrcweir 	{
385cdf0e10cSrcweir #endif
386cdf0e10cSrcweir 		if ( pFetcLhs != pFetcRhs )
387cdf0e10cSrcweir 
388cdf0e10cSrcweir 		if ( ( pFetcLhs->cfFormat != pFetcRhs->cfFormat ) ||
389cdf0e10cSrcweir 			 ( pFetcLhs->lindex   != pFetcRhs->lindex ) ||
390cdf0e10cSrcweir 			 !CompareTargetDevice( pFetcLhs->ptd, pFetcRhs->ptd ) )
391cdf0e10cSrcweir 		{
392cdf0e10cSrcweir 			nMatch = FORMATETC_NO_MATCH;
393cdf0e10cSrcweir 		}
394cdf0e10cSrcweir 
395cdf0e10cSrcweir 		else if ( pFetcLhs->dwAspect == pFetcRhs->dwAspect )
396cdf0e10cSrcweir 			// same aspects; equal
397cdf0e10cSrcweir 			;
398cdf0e10cSrcweir 		else if ( ( pFetcLhs->dwAspect & ~pFetcRhs->dwAspect ) != 0 )
399cdf0e10cSrcweir 		{
400cdf0e10cSrcweir 			// left not subset of aspects of right; not equal
401cdf0e10cSrcweir 			nMatch = FORMATETC_NO_MATCH;
402cdf0e10cSrcweir 		}
403cdf0e10cSrcweir 		else
404cdf0e10cSrcweir 			// left subset of right
405cdf0e10cSrcweir 			nMatch = FORMATETC_PARTIAL_MATCH;
406cdf0e10cSrcweir 
407cdf0e10cSrcweir 		if ( nMatch == FORMATETC_EXACT_MATCH || nMatch == FORMATETC_PARTIAL_MATCH )
408cdf0e10cSrcweir 		{
409cdf0e10cSrcweir 		if ( pFetcLhs->tymed == pFetcRhs->tymed )
410cdf0e10cSrcweir 			// same medium flags; equal
411cdf0e10cSrcweir 			;
412cdf0e10cSrcweir 		else if ( ( pFetcLhs->tymed & ~pFetcRhs->tymed ) != 0 )
413cdf0e10cSrcweir 		{
414cdf0e10cSrcweir 			// left not subset of medium flags of right; not equal
415cdf0e10cSrcweir 			nMatch = FORMATETC_NO_MATCH;
416cdf0e10cSrcweir 		}
417cdf0e10cSrcweir 		else
418cdf0e10cSrcweir 			// left subset of right
419cdf0e10cSrcweir 			nMatch = FORMATETC_PARTIAL_MATCH;
420cdf0e10cSrcweir 		}
421cdf0e10cSrcweir 	}
422cdf0e10cSrcweir #ifdef __MINGW32__
423cdf0e10cSrcweir 	else
424cdf0e10cSrcweir #else
425cdf0e10cSrcweir 	__except( EXCEPTION_EXECUTE_HANDLER )
426cdf0e10cSrcweir #endif
427cdf0e10cSrcweir 	{
428cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "Error CompareFormatEtc" );
429cdf0e10cSrcweir 		nMatch = FORMATETC_NO_MATCH;
430cdf0e10cSrcweir 	}
431cdf0e10cSrcweir #ifdef __MINGW32__
432cdf0e10cSrcweir 	han.Reset();
433cdf0e10cSrcweir #endif
434cdf0e10cSrcweir 
435cdf0e10cSrcweir     return nMatch;
436cdf0e10cSrcweir }
437cdf0e10cSrcweir 
438cdf0e10cSrcweir 
439cdf0e10cSrcweir //-------------------------------------------------------------------------
440cdf0e10cSrcweir //
441cdf0e10cSrcweir //-------------------------------------------------------------------------
442cdf0e10cSrcweir 
443cdf0e10cSrcweir sal_Bool SAL_CALL CompareTargetDevice( DVTARGETDEVICE* ptdLeft, DVTARGETDEVICE* ptdRight )
444cdf0e10cSrcweir {
445cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
446cdf0e10cSrcweir 
447cdf0e10cSrcweir #ifdef __MINGW32__
448cdf0e10cSrcweir 	jmp_buf jmpbuf;
449cdf0e10cSrcweir 	__SEHandler han;
450cdf0e10cSrcweir 	if (__builtin_setjmp(jmpbuf) == 0)
451cdf0e10cSrcweir 	{
452cdf0e10cSrcweir 		han.Set(jmpbuf, NULL, (__SEHandler::PF)EXCEPTION_EXECUTE_HANDLER);
453cdf0e10cSrcweir #else
454cdf0e10cSrcweir 	__try
455cdf0e10cSrcweir 	{
456cdf0e10cSrcweir #endif
457cdf0e10cSrcweir 		if ( ptdLeft == ptdRight )
458cdf0e10cSrcweir 		{
459cdf0e10cSrcweir 			// same address of td; must be same (handles NULL case)
460cdf0e10cSrcweir 			bRet = sal_True;
461cdf0e10cSrcweir 		}
462cdf0e10cSrcweir 
463cdf0e10cSrcweir 		// one ot the two is NULL
464cdf0e10cSrcweir 		else if ( ( NULL != ptdRight ) && ( NULL != ptdLeft ) )
465cdf0e10cSrcweir 
466cdf0e10cSrcweir 		if ( ptdLeft->tdSize == ptdRight->tdSize )
467cdf0e10cSrcweir 
468cdf0e10cSrcweir 		if ( rtl_compareMemory( ptdLeft, ptdRight, ptdLeft->tdSize ) == 0 )
469cdf0e10cSrcweir 			bRet = sal_True;
470cdf0e10cSrcweir 	}
471cdf0e10cSrcweir #ifdef __MINGW32__
472cdf0e10cSrcweir 	else
473cdf0e10cSrcweir #else
474cdf0e10cSrcweir 	__except( EXCEPTION_EXECUTE_HANDLER )
475cdf0e10cSrcweir #endif
476cdf0e10cSrcweir 	{
477cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "Error CompareTargetDevice" );
478cdf0e10cSrcweir 		bRet = sal_False;
479cdf0e10cSrcweir 	}
480cdf0e10cSrcweir #ifdef __MINGW32__
481cdf0e10cSrcweir 	han.Reset();
482cdf0e10cSrcweir #endif
483cdf0e10cSrcweir 
484cdf0e10cSrcweir     return bRet;
485cdf0e10cSrcweir }
486