/*************************************************************************
 *
 *  $RCSfile: exlpar.cxx,v $
 *
 *  $Revision: 1.1.1.1 $
 *
 *  last change: $Author: hr $ $Date: 2000/09/18 17:14:55 $
 *
 *  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 _ZFORLIST_HXX //autogen
#include <svtools/zforlist.hxx>
#endif
#ifndef _SVSTOR_HXX //autogen
#include <so3/svstor.hxx>
#endif


#ifndef _NODE_HXX //autogen
#include <node.hxx>
#endif
#ifndef _DOC_HXX
#include <doc.hxx>
#endif
#ifndef _PAM_HXX
#include <pam.hxx>
#endif
#ifndef _SWSWERROR_H
#include <swerror.h>
#endif
#ifndef _FLTINI_HXX
#include <fltini.hxx>
#endif
#ifndef _EXLPAR_HXX
#include <exlpar.hxx>
#endif
#ifndef _FLTGLBLS_HXX
#include <fltglbls.hxx>
#endif
#ifndef _EXCXFBUF_HXX
#include <excxfbuf.hxx>
#endif
#ifndef _EXCVFBFF_HXX
#include <excvfbff.hxx>
#endif
#ifndef _EXCFNTBF_HXX
#include <excfntbf.hxx>
#endif

ExcGlob *pExcGlob = NULL;
//const double SwExcelParser::fExcToTwips = ( double ) TWIPS_PER_CHAR / 256;
const double SwExcelParser::fExcToTwips =
	( double ) ((20.0 * 72.27) / 13.6) / 256;




//Diese Methode hier weil sie natuerlich nicht inline sein darf.
void SwFilterBase::Read( String &rS )
	{// liest 0-terminierten C-String!
	DBG_ASSERT( nReadBuffSize > 242,
		"-SwFilterBase::Read( String& ): Read-Buffer zu klein!!!!!!!!!" );

	sal_Char *pC = pReadBuff;
	register USHORT nCount = 0;	// Einer wird mindestens gelesen

	// String wird komplett mit 0-Terminator eingelesen
	do	{
		*pIn >> *pC;
		nCount++;
		}
	while( *(pC++) != 0 );

	nBytesLeft -= nCount;
	rS = String(pReadBuff, eQuellChar);
	}



ExcGlob::ExcGlob( SwDoc& rDoc, const SwPaM& rPam )
	: FilterGlobals( rDoc, rPam )
{
	pExcGlob = this;			// die Buffer greifen schon auf den Pointer zu
	pXF_Buff = new XF_Buffer;
	pFontBuff = new FontBuffer();
	pColorBuff = new ColorBuffer();
	pNumFormatter = new SvNumberFormatter( LANGUAGE_SYSTEM );
}

ExcGlob::~ExcGlob()
{
	delete pXF_Buff;
	delete pFontBuff;
	delete pColorBuff;
	delete pNumFormatter;
}


SwExcelParser::SwExcelParser( SwDoc &rDoc, const SwPaM & rCrsr,
								SvStream& rInInit, int bReadNewDoc,
								CharSet eQ )
	: bNewDoc( bReadNewDoc )
{
	pIn = &rInInit;
	eQuellChar = eQ;
	pExcGlob = new ExcGlob( rDoc, rCrsr );

	pValueFormBuffer = new ValueFormBuffer;

	nReadBuffSize = 2048;
	pReadBuff = new sal_Char[ nReadBuffSize ];

	eDateiTyp = BiffX;
	nLastCol = nLastRow = 0;
	aColRowBuff.SetDefWidth( ( USHORT ) ( fExcToTwips * 1024 ) );	// 4 Zeichen Standardbreite

	bResultString = FALSE;
}

SwExcelParser::~SwExcelParser()
{
	delete pExcGlob;
	delete pValueFormBuffer;
	delete[] pReadBuff;
	pExcGlob = NULL;
}


ULONG SwExcelParser::CallParser()
{
	USHORT nMinRow = ( USHORT ) ReadFilterFlags( "EXCELMINROW" );
	USHORT nMaxRow = ( USHORT ) ReadFilterFlags( "EXCELMAXROW" );
	USHORT nMinCol = ( USHORT ) ReadFilterFlags( "EXCELMINCOL" );
	USHORT nMaxCol = ( USHORT ) ReadFilterFlags( "EXCELMAXCOL" );

	USHORT nAnzNodes = 65000U - pExcGlob->pD->GetNodes().Count();

	if( nMaxRow < nMinRow )
	{
		USHORT nTemp = nMinRow;
		nMinRow = nMaxRow;
		nMaxRow = nTemp;
	}

	if( nMaxCol < nMinCol )
	{
		USHORT nTemp = nMinCol;
		nMinCol = nMaxCol;
		nMaxCol = nTemp;
	}

	if( nMaxRow - nMinRow == 0 )
		nMaxRow = nMinRow + 30;		// Default bei 0 Rows

	if( nMaxCol - nMinCol == 0 )
		nMaxCol = nMinCol + 15;		// Default bei 0 Cols

	if( nAnzNodes < ( nMaxRow - nMinRow ) * ( nMaxCol - nMinCol ) * 3 )
		return ERR_EXCLOT_WRONG_RANGE;

	pExcGlob->SetRange( nMinCol, nMaxCol, nMinRow, nMaxRow ); // Default-Einstellung
	Parse();
	return 0;
}



ULONG ExcelReader::Read( SwDoc &rDoc, SwPaM &rPam,
					const String & /* FileName, falls benoetigt wird */ )
{
	if( rPam.GetNode()->FindTableNode() )
		return ERR_SWG_READ_ERROR;

	USHORT nOldBuffSize = 32768;

	ULONG nRet = 0;
	SvStorageStreamRef refStrm;			// damit uns keiner den Stream klaut
	SvStream* pIn = pStrm;
	if( pStg )
	{
		nRet = OpenMainStream( refStrm, nOldBuffSize );
		pIn = &refStrm;
	}
	else if( !pStrm )
	{
		ASSERT( FALSE, "ExcelReader-Read ohne Stream/Storage" );
		nRet = ERR_SWG_READ_ERROR;
	}

	if( !nRet )
	{
		SwExcelParser* pParser = new SwExcelParser( rDoc, rPam, *pIn, !bInsertMode, gsl_getSystemTextEncoding() );
		nRet = pParser->CallParser();	// 0 == kein Fehler aufgetreten

		delete pParser;

		if( refStrm.Is() )
			refStrm->SetBufferSize( nOldBuffSize );
	}

	return nRet;
}

int ExcelReader::GetReaderType()
{
	return SW_STORAGE_READER | SW_STREAM_READER;
}

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

	  Source Code Control System - Header

	  $Header: /cvs/oo/sw/sw/source/filter/excel/exlpar.cxx,v 1.1.1.1 2000/09/18 17:14:55 hr Exp $

	  Source Code Control System - Update

	  $Log: exlpar.cxx,v $
	  Revision 1.1.1.1  2000/09/18 17:14:55  hr
	  initial import
	
	  Revision 1.39  2000/09/18 16:04:42  willem.vandorp
	  OpenOffice header added.
	
	  Revision 1.38  2000/05/15 16:36:04  jp
	  Changes for Unicode
	
	  Revision 1.37  2000/05/15 09:19:39  os
	  UNICODE - ReadFilterFlags

	  Revision 1.36  2000/05/09 11:16:54  os
	  UNICODE

	  Revision 1.35  2000/02/11 14:36:44  hr
	  #70473# changes for unicode ( patched by automated patchtool )

	  Revision 1.34  1998/07/23 09:12:24  JP
	  Task #52654#: Einfuegen Doc nicht mit einer CrsrShell sondern mit einen PaM


	  Rev 1.33   23 Jul 1998 11:12:24   JP
   Task #52654#: Einfuegen Doc nicht mit einer CrsrShell sondern mit einen PaM

	  Rev 1.32   26 Nov 1997 14:18:40   MA
   headerfiles

	  Rev 1.31   03 Nov 1997 14:06:02   MA
   precomp entfernt

	  Rev 1.30   04 Sep 1997 12:36:40   JP
   Umstellungen fuer FilterDetection im SwModule und SwDLL

	  Rev 1.29   18 Aug 1997 11:27:52   OS
   includes

	  Rev 1.28   29 Oct 1996 12:14:40   JP
   am Doc ist das NodesArray nur noch ueber Get..() zugaenglich

	  Rev 1.27   28 Jun 1996 13:49:00   MA
   includes

	  Rev 1.26   14 Dec 1995 23:04:34   JP
   Filter Umstellung: ErrorHandling und Read/Write-Parameter

	  Rev 1.25   30 Nov 1995 21:21:48   JP
   ReadFilterFlags: String Parameter -> char*

	  Rev 1.24   24 Nov 1995 17:25:08   OM
   PCH->PRECOMPILED

	  Rev 1.23   23 Nov 1995 16:13:48   MA
   fix: unerlaubtes inline jetzt impl.
   fix: statics vor eofglobals.

	  Rev 1.22   08 Nov 1995 11:46:04   JP
   Umstellung auf SbxValue

	  Rev 1.21   10 Oct 1995 12:11:14   JP
   Umstellung Reader - FilterDetection

	  Rev 1.20   31 Jul 1995 11:19:26   AMA
   International-Umbau wg. Update 253

	  Rev 1.19   28 Jul 1995 14:10:12   GT
	*	SwExcelParser::CallParser(): Defaultwerte fuer 0-Anzahl:
		30 Rows, 15 Cols

	  Rev 1.18   21 Jul 1995 21:03:16   JP
   SHGetInitFlag -> ReadFilterFlags

	  Rev 1.17   21 Jul 1995 16:44:12   GT
	*	SwExcelParser::CallParser(): Import-Tabellen-Bereich aus swriter3.ini
		in [User] als EXCELMINROW, EXCELMAXROW, EXCELMINCOL, EXCELMAXCOL

	  Rev 1.16   17 Jul 1995 18:19:04   JP
   Funktionalitaet in die StgReader-Klasse ausgelagert

	  Rev 1.15   17 Jul 1995 11:13:46   JP
   CTOR ExcGlob: statischen Pointer gleich besetzen

	  Rev 1.14   14 Jul 1995 20:33:58   JP
   Optimiert, ueberfluessige Methoden entfernt / auskommentiert

	  Rev 1.13   08 Jun 1995 17:16:24   ER
   zweites Filtersegment

	  Rev 1.12   07 Apr 1995 10:23:18   GT
	*  SwExcelParser::CallParser(): Returnwert 0 ( = kein Fehler )

	  Rev 1.11   04 Apr 1995 17:49:20   JP
   Read ohne Stream ohne Absturz

	  Rev 1.10   07 Mar 1995 19:23:14   GT
   Aenderungen fuer Numberformatter

	  Rev 1.9   02 Mar 1995 23:31:16   GT
   Abbruch bei Stream-Nullpointer aus Storage

	  Rev 1.8   02 Mar 1995 19:45:20   GT
   static ExcGlob -> pExcGlob

	  Rev 1.7   21 Feb 1995 18:17:22   GT
	*  SwExcelParser::LoadDoc(): Versuch, auch (schreibgeschuetzte) Storages
								 vom Netz zu oeffnen

	  Rev 1.6   20 Feb 1995 13:05:26   GT
	*  SwExcelParser::CallParser(): Tabellenerzeugung -> ::CreateTable()

	  Rev 1.5   18 Feb 1995 17:19:56   GT
	*  ExcelParser::Init(): Initialisierung der letzten Col/Row

	  Rev 1.4   17 Feb 1995 20:30:12   GT
	+  SwExcelParser::Ctor mit Storage-Schnittstelle

	  Rev 1.3   16 Feb 1995 19:15:46   GT
	*  SwExcelParser::Ctor(), ::CallParser(): Anpassungen an ExcGlob

	  Rev 1.2   13 Feb 1995 17:56:00   GT
   Baustelle

	  Rev 1.1   12 Feb 1995 17:14:34   GT
   Baustelle

	  Rev 1.0   15 Jan 1995 21:28:10   JP
   Initial revision.

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



