/*************************************************************************
 *
 *  $RCSfile: w4wflt.cxx,v $
 *
 *  $Revision: 1.1.1.1 $
 *
 *  last change: $Author: hr $ $Date: 2000/09/19 10:59:15 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/
#ifdef PRECOMPILED
#include "filt_pch.hxx"
#endif

#pragma hdrstop


#ifndef _URLOBJ_HXX
#include <tools/urlobj.hxx>
#endif
#ifdef MAC
 #include <mac_start.h>
 #include <Resources.h>
 #include <Memory.h>
 #include <Signal.h>
 #include <Types.h>
 #include <Strings.h>
 #include <mac_end.h>

typedef int (*FNDisplay)(int);
# define FNDisp int
typedef int(**CHand)( void*, void*, void*, void*, void* );
#endif

#ifdef UNX
#include <stdlib.h>
#endif
#ifdef PM2
#define INCL_DOS                // fuer bsedos.h <- os2.h <- svpm.h <- sysdep.hxx
#endif

#ifndef _LIBCALL_HXX //autogen
#include <svtools/libcall.hxx>
#endif
#ifndef _SFX_INIMGR_HXX
#include <sfx2/inimgr.hxx>
#endif
#ifndef _SFXAPP_HXX //autogen
#include <sfx2/app.hxx>
#endif

#ifndef _SHELLIO_HXX
#include <shellio.hxx>                  // fuer GetTmpFileName()
#endif
#ifndef _MDIEXP_HXX
#include <mdiexp.hxx>
#endif
#ifndef _ERRHDL_HXX
#include <errhdl.hxx>
#endif
#ifndef _W4WFLT_HXX
#include <w4wflt.hxx>
#endif

#ifndef _SWERROR_H
#include <swerror.h>
#endif



#if defined( WIN ) || defined( WNT )
// Typen fuer die Funktion der W4W-DLL
//typedef _far16 pascal short (*FNDisplay)();   // alt
#include <tools/svwin.h>
typedef int ( FAR PASCAL *FNDisplay)( int );
#ifdef WIN
  #define FNDisp _loadds FAR PASCAL
#else
  #define FNDisp FAR PASCAL
#endif
typedef int (FAR PASCAL *FNautorec)( LPSTR, int, LPINT );
typedef int (FAR PASCAL *FNw4wt)( LPSTR, LPSTR, LPSTR, LPSTR, FNDisplay );
typedef int (FAR PASCAL *FNw4wf)( LPSTR, LPSTR, LPSTR, LPSTR, FNDisplay );
#endif

#ifdef PM2

// die InSo Filter wollen wieder alles anders haben:

#define INSO

#ifdef INSO
#include <os2.h>

//typedef int (PASCAL16 *FNautorec)( PCHAR16, short, PSHORT16 );
//typedef int (PASCAL16 *FNw4wt)( PCHAR16, PCHAR16, PCHAR16, PCHAR16, FNDisplay );
//typedef int (PASCAL16 *FNw4wf)( PCHAR16, PCHAR16, PCHAR16, PCHAR16, FNDisplay );

typedef int ( _System *FN32Display)( int );
typedef int ( _System *FN32autorec)( PSZ, int, int FAR* );
typedef int ( _System *FN32w4wt)( PSZ, PSZ, PSZ, PSZ, FN32Display );
typedef int ( _System *FN32w4wf)( PSZ, PSZ, PSZ, PSZ, FN32Display );

#else

#if defined( CSET ) || defined ( WTC )

#include <os2.h>

typedef short (PASCAL16 *FNDisplay)(short);
#define FNDisp short FAR _Pascal
#define PSHORT16 short * _Seg16


typedef short (PASCAL16 *FNautorec)( PCHAR16, short, PSHORT16 );
typedef short (PASCAL16 *FNw4wt)( PCHAR16, PCHAR16, PCHAR16, PCHAR16, FNDisplay );
typedef short (PASCAL16 *FNw4wf)( PCHAR16, PCHAR16, PCHAR16, PCHAR16, FNDisplay );

typedef short ( _cdecl *FN32Display)(short);
typedef short ( _cdecl *FN32autorec)( PSZ, short, PSHORT16 );
typedef short ( _cdecl *FN32w4wt)( PSZ, PSZ, PSZ, PSZ, FN32Display );
typedef short ( _cdecl *FN32w4wf)( PSZ, PSZ, PSZ, PSZ, FN32Display );

// SH: Wenn es _cdecl unter CSET nicht gibt, bitte entfernen und hinter jede
// Parameterliste ", ..." setzen. Sorry, konnte ich nicht ausprobieren.

#else

typedef char _far16* PCHAR16;
//typedef _far16 pascal short (*FNDisplay)();   //alt
typedef _far16 pascal short (*FNDisplay)(short);
typedef short ( _cdecl *FN32Display)(short);
//# define FNDisp _far16 pascal short
//#define FNDisp short _far16 _pascal
#define FNDisp short FAR _pascal
#define PSHORT16 short _far16*

typedef _far16 pascal short ( *FNautorec)( PCHAR16, short, PSHORT16 );
typedef _far16 pascal short ( *FNw4wt)( PCHAR16, PCHAR16, PCHAR16, PCHAR16, FNDisplay );
typedef _far16 pascal short ( *FNw4wf)( PCHAR16, PCHAR16, PCHAR16, PCHAR16, FNDisplay );

// folgende muessen unbedingt als _cdecl deklariert sein, da BLC sonst als
// Optimierung eine Pascal-Funktion draus macht, was zu einem geringfuegig
// zermarmelten Stack fuehrt ( Register-Vars der aufrufenden Methode werden
// zerstoert ).
typedef short ( _cdecl *FN32autorec)( PSZ, short, PSHORT );
typedef short ( _cdecl *FN32w4wt)( PSZ, PSZ, PSZ, PSZ, FN32Display );
typedef short ( _cdecl *FN32w4wf)( PSZ, PSZ, PSZ, PSZ, FN32Display );

#endif  //CSET

// Assembler-Funktion
extern "C" void DisplayFunc();

#endif
#endif // INSO

#if defined( SOLARIS ) && defined ( SPARC )
#ifdef  __cplusplus
extern "C"
#endif
	int autorec( const char* pFileName, int* pVersion );
#endif


#define ERR_DLL 9999

// suche fuer das File den enstsprechenden Filter Typ
// und die Version


USHORT AutoDetec( const String& rFileName, USHORT & rVersion )
{
	ByteString aFileNm( rFileName, ::gsl_getSystemTextEncoding() );
#if defined( SOLARIS ) && defined ( SPARC )

	// unter UNIX wird das o-File dazu gelinkt
	int nVersion;
	USHORT nFileType = (USHORT)autorec( aFileNm.GetBuffer(), &nVersion );
	if( nFileType )
		rVersion = (USHORT)nVersion;

#else

#ifdef MAC
	// bei den Demos gab es kein autorec - Programm
#else
	USHORT nFileType = 0;

	String aAutoRecDLL( String::CreateFromAscii(
					RTL_CONSTASCII_STRINGPARAM( "autorec.dll" )));
	if( !SFX_INIMANAGER()->SearchFile( aAutoRecDLL, SFX_KEY_FILTER_PATH ))
		return 0;

	ByteString sAutoRecDllNm( aAutoRecDLL, ::gsl_getSystemTextEncoding() );
#if defined( WIN ) || defined( WNT )
	HANDLE hDLL = LoadLibrary( (LPSTR)sAutoRecDllNm.GetBuffer() );
	if( hDLL >= (HANDLE)HINSTANCE_ERROR )
	{
		FNautorec fnAutorec = (FNautorec)GetProcAddress( (HINSTANCE)hDLL,
													(LPSTR)"autorec" );

		long nVersion = 0;
		if( fnAutorec )
			nFileType = (*fnAutorec)( (LPSTR)aFileNm.GetBuffer(),
										0,
										(LPINT)&nVersion );
		rVersion = (USHORT)nVersion;
		FreeLibrary( (HINSTANCE)hDLL );
	}
#endif  // WIN || WNT
#ifdef PM2
	HMODULE hDLL;
	CHAR sLoadError[ 100 ];
	APIRET rc = DosLoadModule( (PSZ)sLoadError, sizeof( sLoadError ),
								 (PSZ)sAutoRecDllNm.Buffer(), &hDLL );
	if( !rc )
	{
		PFN ppFN;
		ULONG nBitVers;

	if( 0 == ( rc = DosQueryProcType( hDLL, 0L, (PSZ)"autorec", &nBitVers )) &&
			1 == nBitVers &&            // 32 Bit DDLs
		0 == ( rc = DosQueryProcAddr( hDLL, 0L, (PSZ)"autorec", &ppFN )))
		{
			int nVers;
			FN32autorec fnAutorec = (FN32autorec)ppFN;
			nFileType = (*fnAutorec)( (PSZ)aFileNm.GetBuffer(),
									   0,
#ifdef INSO
									  &nVers );
			rVersion = nVers;
#else
									  (PSHORT)&rVersion );
#endif
		}
#ifndef INSO
		else if( 0 == ( rc = DosQueryProcType( hDLL, 0L, (PSZ)"AUTOREC", &nBitVers )) &&
		 0 == nBitVers &&                // 16 Bit DLLs
		0 == ( rc = DosQueryProcAddr( hDLL, 0L, (PSZ)"AUTOREC", &ppFN )))
		{
			FNautorec fnAutorec = (FNautorec)ppFN;
			nFileType = (*fnAutorec)( (PCHAR16)aFileNm.GetBuffer(),
										0,
									(PSHORT16)&rVersion );
		}
#endif
//Bug 33334: Modul wird von OS/2 nie aus dem Speicher entfernt,
// W4W stoplter darueber --> also nie entfernen!
//		DosFreeModule( hDLL );
	}
#endif  // PM2

#endif  // MAC

#endif  // UNX

	if( nFileType == 31 )	// Autorec-Bug umpopeln: Eine ASCII-Datei, die nur
		nFileType = 1;		// aus sehr vielen CRs besteht,
							// wird als VAX Mass-11 erkannt

	return nFileType == (USHORT)-1 ? 0 : nFileType;
}

#ifdef _DLL_

#ifdef MAC

static short Call_MacRes( const String& rFromFileName, USHORT nFilter, BOOL bFrom,
						  const String& rVersion, const String & rToFileName )
{
	short nRefNo = 0, nError = 0;
	USHORT nMacFilter = nFilter * 10 + 2;
	if ( bFrom ) nMacFilter--;
	// beim Mac ist alles anders:  Letzte Ziffer = 1 entspricht from,
	//                                                         Letzte Ziffer = 2 entspricht to

	String aFormat( "w4w" );
	if( nFilter < 10 )
		aFormat += "00";
	else if( nFilter < 100 )
		aFormat += '0';
	aFormat += nFilter;

	if( bFrom )
		aFormat += ".f";
	else
		aFormat += ".t";

	if( !SFX_INIMANAGER()->SearchFile( aFormat, SFX_KEY_FILTER_PATH ))
		return ERR_DLL;

	Str255  aMacStr;        // registriere den W4W-Filter im MAC-Resource System
	strcpy( (char*)aMacStr+1, aFormat );
	aMacStr[0] = aFormat.Len();
	nRefNo = OpenResFile( aMacStr );

	CHand zConv = (CHand) GetResource( 'CONV', nMacFilter );
	if( zConv )                             // Rufe Code-Resource (aehnlich DLL)
	{
		HLock((Handle) zConv);
#ifdef __powerc
		ProcInfoType theProcInfo = kCStackBased
								| RESULT_SIZE(SIZE_CODE(sizeof(int)))
								| STACK_ROUTINE_PARAMETER( 1, SIZE_CODE( sizeof( void* ) ) )
								| STACK_ROUTINE_PARAMETER( 2, SIZE_CODE( sizeof( void* ) ) )
								| STACK_ROUTINE_PARAMETER( 3, SIZE_CODE( sizeof( void* ) ) )
								| STACK_ROUTINE_PARAMETER( 4, SIZE_CODE( sizeof( void* ) ) )
								| STACK_ROUTINE_PARAMETER( 5, SIZE_CODE( sizeof( void* ) ) );
		UniversalProcPtr pMyProc = NewRoutineDescriptor( (long (*)())*zConv, theProcInfo, kM68kISA );
		nError = CallUniversalProc(     pMyProc,
									theProcInfo,
									(void*)rFromFileName.GetStr(),
									(void*)rToFileName.GetStr(),
									(void*)rVersion.GetStr(),
									aEmptyStr.GetStr(),
									0 );
//neu:                                                                  (void*)&W4WReadDisplayHandler );
		DisposeRoutineDescriptor( pMyProc )
#else
		nError = (**zConv)( (void*)rFromFileName.GetStr(),
							(void*)rToFileName.GetStr(),
							(void*)rVersion.GetStr(),
							aEmptyStr.GetStr(),
//neu:                                          (void*)&W4WReadDisplayHandler );
							0 );
#endif
		HUnlock((Handle) zConv);
		ReleaseResource((Handle) zConv);
	}else{
		nError = ERR_DLL;
	}
	if ( nRefNo > 0 ) CloseResFile( nRefNo );
	return nError;
}
#endif  // MAC


// Handler fuer die Prozentanzeige

#if defined( WIN ) || defined( WNT ) || defined( PM2 )

#ifdef INSO
int W4W32ReadDisplayHandler( int nPercent )
{
	::SetProgressState( nPercent  / 3, 0 );
	return 0;                                               // -1 fuehrt zu Abbruch
}

int W4W32WriteDisplayHandler( int nPercent )
{
	::SetProgressState( 67 + nPercent  / 3, 0 );
	return 0;                                               // -1 fuehrt zu Abbruch
}

#else

FNDisp W4WReadDisplayHandler( int nPercent )
{
	::SetProgressState( nPercent  / 3, 0 );
	return 0;                                               // -1 fuehrt zu Abbruch
}

FNDisp W4WWriteDisplayHandler( int nPercent )
{
	::SetProgressState( 67 + nPercent  / 3, 0 );
	return 0;                                               // -1 fuehrt zu Abbruch
}

#ifdef PM2

short W4W32ReadDisplayHandler( int nPercent )
{
	::SetProgressState( nPercent  / 3, 0 );
	return 0;                                               // -1 fuehrt zu Abbruch
}

short W4W32WriteDisplayHandler( int nPercent )
{
	::SetProgressState( 67 + nPercent  / 3, 0 );
	return 0;                                               // -1 fuehrt zu Abbruch
}

#endif
#endif
#endif



// W4W speichert beim Laden vom File rFileName die Ausgabe in aTmpFile.
// der Filter wird ueber nFilter bestimmt.

ULONG LoadFile( const String& rFileName, USHORT nFilter,
				const String& rVersion, String & rTmpFile )
{
	rTmpFile = GetTmpFileName();
	USHORT nError = 1;

	ByteString sFileName( rFileName, ::gsl_getSystemTextEncoding() ),
			   sVersion( rVersion, ::gsl_getSystemTextEncoding() ),
			   sTmpFile( INetURLObject(rTmpFile).PathToFileName(), ::gsl_getSystemTextEncoding() );

#ifdef MAC

	nError = Call_MacRes( rFileName, nFilter, TRUE, rVersion, rTmpFile );

#else //MAC

	String aFormat;

#if defined( WIN ) || defined( WNT )
	aFormat += 'w';
	if( nFilter < 10 )
		aFormat += '0';
	if( nFilter < 100 )
		aFormat += '0';
	aFormat += String::CreateFromInt32( nFilter );
	aFormat += 'f';
#if defined( WIN )
	aFormat.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "16W" ));
#else
	aFormat.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "32W" ));
#endif
	aFormat.AppendAscii( RTL_CONSTASCII_STRINGPARAM( ".DLL" ));
#endif

#ifdef PM2
	aFormat.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "w4w" ));
	if( nFilter < 10 )
		aFormat += '0';
	aFormat += String::CreateFromInt32( nFilter );
	aFormat += 'f';

	aFormat.AppendAscii( RTL_CONSTASCII_STRINGPARAM( ".dll" ));
#endif

#ifdef UNX
	aFormat.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "w4w" ));
	if( nFilter < 10 )
		aFormat += '0';
	aFormat += String::CreateFromInt32( nFilter );
	aFormat += 'f';
#endif

	if( !SFX_INIMANAGER()->SearchFile( aFormat, SFX_KEY_FILTER_PATH ))
		return ERR_W4W_DLL_ERROR | ERROR_SW_READ_BASE;

	ByteString sFormat( aFormat, ::gsl_getSystemTextEncoding() );
#if defined( WIN ) || defined( WNT )

	HANDLE hDLL = LoadLibrary( (LPSTR)sFormat.GetBuffer() );
	if( hDLL >= (HANDLE)HINSTANCE_ERROR )
	{
		FNw4wf fnRead = (FNw4wf)GetProcAddress( (HINSTANCE)hDLL, (LPSTR)"w4wf" );
		if( fnRead )
			nError = (*fnRead)( (LPSTR)sFileName.GetBuffer(),
								(LPSTR)sTmpFile.GetBuffer(),
								(LPSTR)sVersion.GetBuffer(),
								(LPSTR)aEmptyByteStr.GetBuffer(),
								(FNDisplay)&W4WReadDisplayHandler
								);

		FreeLibrary( (HINSTANCE)hDLL );
	}else{
#ifndef WIN     /* Bei Windows kommt schon eine System-Box hoch */
		nError = ERR_DLL;
#endif
	}
#endif //WIN || WNT

#ifdef PM2
	HMODULE hDLL;
	CHAR sLoadError[ 100 ];
	APIRET rc = DosLoadModule( (PSZ)sLoadError, sizeof( sLoadError ),
								 (PSZ)sFormat.GetBuffer(), &hDLL );
	if( !rc )
	{
		PFN ppFN;
		ULONG nBitVers;

		if( 0 == ( rc = DosQueryProcType( hDLL, 0L, (PSZ)"w4wf", &nBitVers )) &&
		1 == nBitVers &&                // 32 Bit DLLs
		0 == ( rc = DosQueryProcAddr( hDLL, 0L, (PSZ)"w4wf", &ppFN )))
		{
			// die neuen 32 Bit DLLs
			FN32w4wf fnRead = (FN32w4wf)ppFN;
			nError = (*fnRead)( (PSZ)sFileName.GetBuffer(),
							(PSZ)sTmpFile.GetBuffer(),
							(PSZ)sVersion.GetBuffer(),
							(PSZ)aEmptyByteStr.GetBuffer(),
//							(FNDisplay)&W4WReadDisplayHandler
							(FN32Display)&W4W32ReadDisplayHandler
							);
		}
#ifndef INSO
		else if( 0 == ( rc = DosQueryProcType( hDLL, 0L, (PSZ)"W4WF", &nBitVers )) &&
		 0 == nBitVers &&                // 16 Bit DLLs
		   0 == ( rc = DosQueryProcAddr( hDLL, 0L, (PSZ)"W4WF", &ppFN )))
		{
			// die alten 16 Bit DLLs
			FNw4wf fnRead = (FNw4wf)ppFN;
			nError = (*fnRead)( (PCHAR16)sFileName.GetBuffer(),
							(PCHAR16)sTmpFile.GetBuffer(),
							(PCHAR16)sVersion.GetBuffer(),
							(PCHAR16)aEmptyByteStr.GetBuffer(),
//                                                              (FNDisplay)&W4WReadDisplayHandler
							(FNDisplay)&DisplayFunc
							);
		}
#endif
//Bug 33334: Modul wird von OS/2 nie aus dem Speicher entfernt,
// W4W stoplter darueber --> also nie entfernen!
//JP 23.04.98: fuer den DCF-RFT-Filter unter OS/2 muss aber die DLL
//				freigegeben werden.
		if( 15 == nFilter )
			DosFreeModule( hDLL );
	}else{
		nError = ERR_DLL;
	}
#endif  // PM2

#ifdef UNX

	String aParam( aFormat );
	aParam += ' ';
	aParam += rFileName;
	aParam.AppendAscii( RTL_CONSTASCII_STRINGPARAM( " -N -t=" ));
	aParam += rTmpFile;
	aParam.AppendAscii( RTL_CONSTASCII_STRINGPARAM( " -v" ));
	aParam += rVersion;

	ByteString sParam( aParam, ::gsl_getSystemTextEncoding() );

	nError = system( sParam.GetBuffer() );
#endif  // UNX
#endif  // MAC

static const ULONG nErrors[18]={
	0, ERR_SWG_READ_ERROR, ERR_SWG_READ_ERROR, ERR_W4W_WRITE_TMP_ERROR,
	ERR_W4W_WRITE_TMP_ERROR, ERR_SWG_FILE_FORMAT_ERROR,
	ERR_W4W_WRITE_TMP_ERROR, ERR_W4W_WRITE_TMP_ERROR,
	ERR_W4W_MEM | ERROR_SW_READ_BASE,
	ERR_SWG_FILE_FORMAT_ERROR, ERR_W4W_WRITE_FULL | ERROR_SW_READ_BASE,
	0, 0, 0,
	0, ERR_SW6_PASSWD, ERR_W4W_INTERNAL_ERROR | ERROR_SW_READ_BASE,
	0 };

	if ( nError != 12 )			// 12 ist nur Warning
	{
		if ( nError == ERR_DLL )
			return ERR_W4W_DLL_ERROR | ERROR_SW_READ_BASE;
		if ( nError>0 && nError<=17 )
			return nErrors[ nError ];
	}
	return 0;
}

// W4W liest beim Speichern vom File pFileName die Eingabe aus pTmpFile.
// der Filter wird ueber nFilter bestimmt.

ULONG SaveFile( const String& rFileName, const String& rTmpFile,
				USHORT nFilter, const String& rVersion )
{
//      ASSERT( pAppDirName, "wo steht die Applikation?" );
	USHORT nError = 1;

	ByteString sFileName( rFileName, ::gsl_getSystemTextEncoding() ),
			   sVersion( rVersion, ::gsl_getSystemTextEncoding() ),
			   sTmpFile( INetURLObject(rTmpFile).PathToFileName(), ::gsl_getSystemTextEncoding() );

#ifdef MAC

	nError = Call_MacRes( rTmpFile, nFilter, FALSE, rVersion, rFileName );

#else //MAC

	String aFormat;

#if defined( WIN ) || defined( WNT )
	aFormat += 'w';
	if( nFilter < 10 )
		aFormat += '0';
	if( nFilter < 100 )
		aFormat += '0';
	aFormat += String::CreateFromInt32( nFilter );
	aFormat += 't';
#if defined( WIN )
	aFormat.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "16W" ));
#else
	aFormat.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "32W" ));
#endif
	aFormat.AppendAscii( RTL_CONSTASCII_STRINGPARAM( ".DLL" ));
#endif


#ifdef PM2
	aFormat.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "w4w" ));
	if( nFilter < 10 )
		aFormat += '0';
	aFormat += String::CreateFromInt32( nFilter );
	aFormat += 't';

	aFormat.AppendAscii( RTL_CONSTASCII_STRINGPARAM( ".dll" ));
#endif

#ifdef UNX
	aFormat.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "w4w" ));
	if( nFilter < 10 )
		aFormat += '0';
	aFormat += String::CreateFromInt32( nFilter );
	aFormat += 't';
#endif

	if( !SFX_INIMANAGER()->SearchFile( aFormat, SFX_KEY_FILTER_PATH ))
		return ERR_W4W_DLL_ERROR | ERROR_SW_WRITE_BASE;

	ByteString sFormat( aFormat, ::gsl_getSystemTextEncoding() );
#if defined( WIN ) || defined( WNT )

	HANDLE hDLL = LoadLibrary( (LPSTR)sFormat.GetBuffer() );
	if( hDLL >= (HANDLE)HINSTANCE_ERROR )
	{
		FNw4wt fnSave = (FNw4wt)GetProcAddress( (HINSTANCE)hDLL, (LPSTR)"w4wt" );
		if( fnSave )
			nError = (*fnSave)( (LPSTR)sTmpFile.GetBuffer(),
								(LPSTR)sFileName.GetBuffer(),
								(LPSTR)sVersion.GetBuffer(),
								(LPSTR)aEmptyByteStr.GetBuffer(),
								(FNDisplay)&W4WWriteDisplayHandler );
		FreeLibrary( (HINSTANCE)hDLL );
	}else{
#ifndef WIN             /* Bei Windows kommt schon eine System-Box hoch */
		nError = ERR_DLL;
#endif
	}
#endif //WIN || WNT

#ifdef PM2
	HMODULE hDLL;
	CHAR sLoadError[ 100 ];
	APIRET rc = DosLoadModule( (PSZ)sLoadError, sizeof( sLoadError ),
								 (PSZ)sFormat.GetStr(), &hDLL );
	if( !rc )
	{
		PFN ppFN;
		ULONG nBitVers;

		if( 0 == ( rc = DosQueryProcType( hDLL, 0L, (PSZ)"w4wt", &nBitVers )) &&
		1 == nBitVers &&                // 32 Bit DLLs
		0 == ( rc = DosQueryProcAddr( hDLL, 0L, (PSZ)"w4wt", &ppFN )))
		{
			// die neuen 32 Bit DLLs
			FN32w4wt fnSave = (FN32w4wt)ppFN;
			nError = (*fnSave)( (PSZ)sTmpFile.GetBuffer(),
								(PSZ)sFileName.GetBuffer(),
								(PSZ)sVersion.GetBuffer(),
								(PSZ)aEmptyByteStr.GetBuffer(),
//								(FNDisplay)&W4WWriteDisplayHandler
								(FN32Display)&W4W32WriteDisplayHandler
								);
		}
#ifndef INSO
		else if( 0 == ( rc = DosQueryProcType( hDLL, 0L, (PSZ)"W4WT", &nBitVers )) &&
		 0 == nBitVers &&                // 16 Bit DLLs
		   0 == ( rc = DosQueryProcAddr( hDLL, 0L, (PSZ)"W4WT", &ppFN )))
		{
			// die alten 16 Bit DLLs
			FNw4wt fnSave = (FNw4wt)ppFN;
			nError = (*fnSave)( (PCHAR16)sTmpFile.GetBuffer(),
								(PCHAR16)sFileName.GetBuffer(),
								(PCHAR16)sVersion.GetBuffer(),
								(PCHAR16)aEmptyByteStr.GetBuffer(),
//                                                                      (FNDisplay)&W4WWriteDisplayHandler
								(FNDisplay)&DisplayFunc
								);
		}
#endif
//Bug 33334: Modul wird von OS/2 nie aus dem Speicher entfernt,
// W4W stoplter darueber --> also nie entfernen!
//		DosFreeModule( hDLL );
	}else{
		nError = ERR_DLL;
	}
#endif  // PM2

#ifdef UNX

	String aParam( aFormat );
	aParam += ' ';
	aParam += rFileName;
	aParam.AppendAscii( RTL_CONSTASCII_STRINGPARAM( " -N -t=" ));
	aParam += rTmpFile;
	aParam.AppendAscii( RTL_CONSTASCII_STRINGPARAM( " -v" ));
	aParam += rVersion;

	ByteString sParam( aParam, ::gsl_getSystemTextEncoding() );

	nError = system( sParam.GetBuffer() );

#endif  // UNX
#endif  // MAC
static const ULONG nErrors[18]={ 0,ERR_SWG_WRITE_ERROR,
	ERR_W4W_READ_TMP_ERROR, ERR_W4W_READ_TMP_ERROR, ERR_SWG_WRITE_ERROR,
	0, ERR_W4W_WRITE_TMP_ERROR, ERR_W4W_WRITE_TMP_ERROR,
	ERR_W4W_MEM | ERROR_SW_WRITE_BASE, 0,
	ERR_W4W_WRITE_FULL | ERROR_SW_WRITE_BASE,
	0, 0, 0,
	0, ERR_SW6_PASSWD, ERR_W4W_INTERNAL_ERROR | ERROR_SW_WRITE_BASE,
	0 };

	if ( nError != 12 )			// 12 ist nur Warning
	{
		if ( nError == ERR_DLL )
			return ERR_W4W_DLL_ERROR | ERROR_SW_WRITE_BASE;
		if ( nError>0 && nError<=17 )
			return nErrors[ nError ];
	}
	return 0;
}

FASTBOOL W4WDLLExist( W4WDLL_TYPE eType, USHORT nFilter )
{
#ifdef MAC
	return TRUE;			// dann beim CallMacRes feststellen
#else

	String aFormat;
	switch( eType )
	{
	case W4WDLL_EXPORT:
	case W4WDLL_IMPORT:
#if defined( PM2 ) || defined( UNX )
		aFormat.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "w4w" ));
		if( nFilter < 10 )
			aFormat += '0';
		aFormat += String::CreateFromInt32( nFilter );
		aFormat += W4WDLL_IMPORT ? 'f' : 't';
#endif // PM2 || UNX

#if defined( WIN ) || defined( WNT )
		aFormat += 'w';
		if( nFilter < 10 )
			aFormat += '0';
		if( nFilter < 100 )
			aFormat += '0';
		aFormat += String::CreateFromInt32( nFilter );
		aFormat += W4WDLL_IMPORT ? 'f' : 't';
#if defined( WIN )
		aFormat.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "16W" ));
#else
		aFormat.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "32W" ));
#endif

#endif	// WIN || WNT
		break;

	case W4WDLL_AUTODETEC:
		aFormat.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "autorec" ));
		break;
	}

#ifndef UNX
	aFormat.AppendAscii( RTL_CONSTASCII_STRINGPARAM( ".dll" ));
#endif  // UNX

	// Fehlerbehandlung oder fuer Systeme die keine W4W-Filter kennen
	return SFX_INIMANAGER()->SearchFile( aFormat, SFX_KEY_FILTER_PATH );
#endif
}

#endif
	// _DLL_

/*************************************************************************

	  Source Code Control System - Header

	  $Header: /cvs/oo/sw/sw/source/filter/basflt/w4wflt.cxx,v 1.1.1.1 2000/09/19 10:59:15 hr Exp $

	  Source Code Control System - Update

	  $Log: w4wflt.cxx,v $
	  Revision 1.1.1.1  2000/09/19 10:59:15  hr
	  initial import
	
	  Revision 1.90  2000/09/18 16:04:40  willem.vandorp
	  OpenOffice header added.
	
	  Revision 1.89  2000/09/11 12:49:36  ama
	  Opt.: fsys removed
	
	  Revision 1.88  2000/06/22 17:52:54  os
	  #76313# w4w needs file names not URLs
	
	  Revision 1.87  2000/05/25 08:11:48  hr
	  Unicode

	  Revision 1.86  2000/05/08 16:42:45  jp
	  Changes for Unicode

	  Revision 1.85  2000/01/04 14:40:53  jp
	  Bug #71442#: UNX define in method W4WDLLExist

	  Revision 1.84  1999/12/06 13:56:34  jp
	  Bug #70523#: dont use access delimiter for SearchFile

	  Revision 1.83  1999/10/21 17:49:58  jp
	  have to change - SearchFile with SfxIniManager, dont use SwFinder for this

	  Revision 1.82  1999/09/22 16:32:04  mh
	  add: include

	  Revision 1.81  1999/09/20 13:00:27  hr
	  #65293#: removed obsolete header <sv/sysdep.hxx>

	  Revision 1.80  1999/07/22 12:52:12  JP
	  Bug #67727#: use of SFX_FILTER_PATH


	  Rev 1.79   22 Jul 1999 14:52:12   JP
   Bug #67727#: use of SFX_FILTER_PATH

	  Rev 1.78   11 Mar 1999 13:41:02   HR
   automatische Filtererkennung fuer Solaris (Sparc)

	  Rev 1.77   09 Jul 1998 09:25:12   JP
   EmptyStr benutzen

	  Rev 1.76   06 Jul 1998 13:47:44   JP
   InSo AutoRecDll fuer OS/2 hat anders definierte Funktionspointer

	  Rev 1.75   02 Jul 1998 15:48:34   JP
   Modules Path wurde ein MultiPath

	  Rev 1.74   23 Apr 1998 11:44:18   JP
   W4W-Filter von Inso haben unter WIN/NT andere Namen

	  Rev 1.73   23 Apr 1998 11:13:50   JP
   Bug #33334#: fuer UF - DCF-RFT-Filter unter OS/2 freigeben

	  Rev 1.72   04 Feb 1998 11:56:42   JP
   Fehlercodes mit den richtigen Import/Export-Flag

	  Rev 1.71   25 Nov 1997 19:07:32   MA
   includes

	  Rev 1.70   03 Nov 1997 14:04:34   MA
   precomp entfernt

	  Rev 1.69   04 Sep 1997 12:36:02   JP
   Umstellungen fuer FilterDetection im SwModule und SwDLL

	  Rev 1.68   21 Nov 1996 14:42:00   JP
   Bug #33334#: unter OS/2 keine DocFreeModul rufen - sonst GPF in W4W-DLls

	  Rev 1.67   09 Oct 1996 17:58:22   MA
   Progress

	  Rev 1.66   28 Jun 1996 14:18:48   MA
   includes

	  Rev 1.65   19 Jun 1996 11:34:20   MA
   headerfiles 323

	  Rev 1.64   28 May 1996 10:37:50   OS
   includes wieder īrepariertī

	  Rev 1.63   24 May 1996 13:41:42   OS
   mit BLC precompiled uebersetzen

	  Rev 1.62   30 Apr 1996 15:40:18   TRI
   WTC und CSET Anpassung

	  Rev 1.61   21 Mar 1996 19:01:18   MA
   svhxx entfernt

	  Rev 1.60   14 Dec 1995 23:35:44   JP
   Filter Umstellung: ErrorHandling und Read/Write-Parameter

	  Rev 1.59   24 Nov 1995 17:24:12   OM
   PCH->PRECOMPILED

	  Rev 1.58   26 Oct 1995 19:22:58   JP
   Umstellung auf SfxErrorHandler

	  Rev 1.57   18 Sep 1995 19:05:58   JP
   Bug 19351: .dll sollte schon noch dahinter

	  Rev 1.56   12 Sep 1995 21:00:02   JP
   neu: W4WDLLExist - prueft ob der vorgebene W4W-Filter vorhanden ist (DLL/..)

	  Rev 1.55   17 Aug 1995 18:41:16   JP
   ist keine autorec.dll vorhanden, dann auch kein LoadLibrary - gibt SystemFehlermeldungen

	  Rev 1.54   08 Aug 1995 13:10:52   SH
   Autorec-Bug umpopelt

	  Rev 1.53   14 Jul 1995 19:50:20   SH
   OS2BLCI: Keine Stackprobleme mehr beim W4W-Dll-Aufruf

	  Rev 1.52   14 Jun 1995 12:17:32   JP
   neu: 32-Bt unterstuetzung fuer OS/2

	  Rev 1.51   15 Dec 1994 20:50:32   SWG
   *ARR* Ersetzungen, svmem, __far_data etc.

	  Rev 1.50   14 Nov 1994 09:37:42   SWG
   system() braucht stdlib.h

	  Rev 1.49   04 Nov 1994 16:57:56   ER
   define INCL_DOS vor sysdep.hxx fuer bsedos.h, os2.h, svpm.h, sysdep.hxx

	  Rev 1.48   26 Oct 1994 07:59:12   SWG
   Include nachgeruestet.

	  Rev 1.47   25 Oct 1994 15:20:12   MA
   PreHdr.

	  Rev 1.46   21 Sep 1994 18:01:02   PK
   einbau sv-streams

	  Rev 1.45   19 May 1994 16:14:08   MA
   Anpassung fuer CSET.

*************************************************************************/



