/*************************************************************************
 *
 *  $RCSfile: wrt_db.cxx,v $
 *
 *  $Revision: 1.1.1.1 $
 *
 *  last change: $Author: hr $ $Date: 2000/09/18 17:14:54 $
 *
 *  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

#include <stdlib.h>
#include "pam.hxx"
#include "doc.hxx"
#include "segmentc.hxx"
#ifndef _DOCARY_HXX
#include <docary.hxx>
#endif

#ifndef _STREAM_HXX //autogen
#include <tools/stream.hxx>
#endif
#ifndef _FRMFMT_HXX //autogen
#include <frmfmt.hxx>
#endif
#ifndef _NODE_HXX //autogen
#include <node.hxx>
#endif
#include "wrt_db.hxx"

#if defined(MAC)
const sal_Char SwDBWriter::sNewLine = '\015';
#elif defined(UNX)
const sal_Char SwDBWriter::sNewLine = '\012';
#else
const sal_Char __FAR_DATA SwDBWriter::sNewLine[] = "\015\012";
#endif


static void OutNodeNm( const SwNodes& rNds, SwNode* pNode, Writer& rWrt )
{
	const sal_Char* aSttNdArr[] = { "", "Box", "Fly", "Footnote",
								"Header", "Footer" };

	switch( pNode->GetNodeType() )
	{
	case ND_STARTNODE:      rWrt.Strm() << aSttNdArr[ ((SwStartNode*)pNode)->GetStartNodeType() ]
										<< "StartNode: Ende= ";
							rWrt.OutULong( pNode->EndOfSectionIndex() );
							break;
	case ND_TABLENODE:      rWrt.Strm() << "TableNode: Ende= ";
							rWrt.OutULong( pNode->EndOfSectionIndex() );
							break;
	case ND_SECTIONNODE:    rWrt.Strm() << "SectionNode: Ende= ";
							rWrt.OutULong( pNode->EndOfSectionIndex() );
							break;
	case ND_ENDNODE:        rWrt.Strm()
								<< aSttNdArr[ pNode->FindStartNode()->GetStartNodeType() ]
								<< "EndNode:";
							break;
   case ND_TEXTNODE:        rWrt.Strm() << "TxtNode:";    break;
	case ND_GRFNODE:        rWrt.Strm() << "GrfNode:";    break;
	case ND_OLENODE:        rWrt.Strm() << "OleNode:";    break;
	default:
		ASSERT( FALSE, "was ist das fuer ein Node??");
	}
	rWrt.Strm() << " Start= ";
	rWrt.OutULong( pNode->StartOfSectionIndex() ) << " Lvl= ";
	rWrt.OutULong( pNode->GetSectionLevel() );
}


SwDBWriter::SwDBWriter()
{
	bOutTable = TRUE;
	bTxtAttr = FALSE;
	pNdCondColl = pNdFmt = 0;
}


SEXPORT SwDBWriter::~SwDBWriter() {}


ULONG SwDBWriter::WriteStream()
{
	//$ long nFlags = rStrm().setf( ios::fixed, ios::floatfield );
	//$ rStrm().precision( 3 );

// gebe das gesamte Dokument aus - mit allen Sections
pCurPam->GetPoint()->nNode = 0;
pCurPam->GetMark()->nNode = pDoc->GetNodes().Count()-1;

	Out_SwDoc( pOrigPam, pDoc->GetNodes() );

	Strm() << SwDBWriter::sNewLine << SwDBWriter::sNewLine
			<< "========================================"
			<< SwDBWriter::sNewLine << "Die PageDescriptoren: "
			<< SwDBWriter::sNewLine;
	OutDB_SwPageDescs( *this, *pDoc );

	Strm() << SwDBWriter::sNewLine << SwDBWriter::sNewLine
			<< "========================================"
			<< SwDBWriter::sNewLine << "Die Fly-Formate: "
			<< SwDBWriter::sNewLine;
	for( USHORT n = 0; n < pDoc->GetSpzFrmFmts()->Count(); ++n )
	{
		Strm() << "Fly (";
		OutULong( n ) << "):";
		OutDB_SwFmt( *this, *(*pDoc->GetSpzFrmFmts())[n] );
		Strm() << SwDBWriter::sNewLine;
	}
	Strm() << SwDBWriter::sNewLine << SwDBWriter::sNewLine
			<< "========================================"
			<< SwDBWriter::sNewLine << "Die NumRules: "
			<< SwDBWriter::sNewLine;
	for( n = 0; n < pDoc->GetNumRuleTbl().Count(); ++n )
	{
		Strm() << "Rule ";
		OutULong( n ) << ':';
		OutDB_SwNumRule( *this, *pDoc->GetNumRuleTbl()[ n ] );
		Strm() << SwDBWriter::sNewLine;
	}

	Strm() << SwDBWriter::sNewLine << SwDBWriter::sNewLine
			<< "========================================"
			<< SwDBWriter::sNewLine << "Es folgt das UndoNodesArray: "
			<< SwDBWriter::sNewLine;
	SwPaM * pDebug = NewSwPaM( *pDoc, 0,
						pDoc->GetUndoNds()->Count()-1, FALSE );
	pDebug->Exchange();
	SwPaM * pOldPam = pCurPam, *pOldEnd = pOrigPam;
	pOrigPam = pCurPam = pDebug;
	Out_SwDoc( pDebug, *pDoc->GetUndoNds() );

	pCurPam = pOldPam;
	pOrigPam = pOldEnd;

	delete pDebug; 		// Pam wieder loeschen

	Strm() << '\0';

	return 0;
}



void SwDBWriter::Out_SwDoc( SwPaM * pPam, const SwNodes& rNds  )
{
	// gebe alle Bereiche des Pams in das RTF-File aus.
	do {
		while( pCurPam->GetPoint()->nNode < pCurPam->GetMark()->nNode ||
			  (pCurPam->GetPoint()->nNode == pCurPam->GetMark()->nNode &&
			   pCurPam->GetPoint()->nContent.GetIndex() <= pCurPam->GetMark()->nContent.GetIndex()) )
		{
			SwNode* pNode = &pCurPam->GetPoint()->nNode.GetNode();
			SwCntntNode * pNd = pNode->GetCntntNode();

			Strm() << SwDBWriter::sNewLine << "No: ";
			OutULong( pCurPam->GetPoint()->nNode.GetIndex() ) << ' ';

			OutNodeNm( rNds, pNode, *this );
			if( pNode->GetTableNode() )
			{
				// Verkettung der Tabellen-Nodes ausgeben
				SwNodeIndex aIdx( pCurPam->GetPoint()->nNode, 1 );
				while( aIdx.GetIndex() <= pNode->EndOfSectionIndex() )
				{
					Strm() << SwDBWriter::sNewLine << "No: ";
					OutULong( aIdx.GetIndex() ) << ' ';
					OutNodeNm( rNds, rNds[ aIdx ], *this );
					aIdx++;
				}
			}
			else if( pNode->IsSectionNode() )
				OutDB_SwSectionNode( *this, *pNode->GetSectionNode() );

			if( bOutTable && pNd )
			{
				SwTableNode* pTblNd = pNd->StartOfSectionNode()->
							StartOfSectionNode()->GetTableNode();
				if( pTblNd )
					OutDB_SwTableNode( *this, *pTblNd );
				else
					Out( aDBNodeFnTab, *pNd, *this );
			}
			else if( pNd )
				Out( aDBNodeFnTab, *pNd, *this );

			if( pCurPam->GetPoint()->nNode.GetIndex() == rNds.Count() - 1 )
				break;
			pCurPam->GetPoint()->nNode++;
		}
	} while( CopyNextPam( &pPam ) );		// bis alle Pam bearbeitet
}


#ifndef PRODUCT

void GetDebugWriter( const String&, WriterRef& xRet )
{
	xRet = new SwDBWriter;
}

#endif

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

	  Source Code Control System - Header

	  $Header: /cvs/oo/sw/sw/source/filter/debug/wrt_db.cxx,v 1.1.1.1 2000/09/18 17:14:54 hr Exp $

	  Source Code Control System - Update

	  $Log: wrt_db.cxx,v $
	  Revision 1.1.1.1  2000/09/18 17:14:54  hr
	  initial import
	
	  Revision 1.51  2000/09/18 16:04:41  willem.vandorp
	  OpenOffice header added.
	
	  Revision 1.50  2000/05/09 12:44:40  os
	  UNICODE
	
	  Revision 1.49  2000/03/03 16:10:20  pl
	  #73771# workaround for c50 intel compiler

	  Revision 1.48  2000/01/27 13:10:47  jp
	  for Task #72343#: dump section values

	  Revision 1.47  1999/06/16 17:51:18  JP
	  Change interface of base class Writer


	  Rev 1.46   16 Jun 1999 19:51:18   JP
   Change interface of base class Writer

	  Rev 1.45   04 May 1999 15:00:34   JP
   FilterExportklasse Writer von SvRef abgeleitet, damit sie immer zerstoert wird

	  Rev 1.44   15 Jan 1998 18:46:28   JP
   NumRules und Formate dumpen

	  Rev 1.43   09 Oct 1997 14:26:58   JP
   Umstellung NodeIndex/-Array/BigPtrArray

	  Rev 1.42   02 Sep 1997 20:43:46   OS
   Header

	  Rev 1.41   18 Aug 1997 11:25:06   OS
   includes

	  Rev 1.40   14 Jan 1997 08:47:02   MA
   includes

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

	  Rev 1.38   05 Sep 1996 14:54:20   JP
   CondColls ausgeben

	  Rev 1.37   29 Aug 1996 10:10:30   JP
   StartNodetype mit rausschreiben

	  Rev 1.36   28 Jun 1996 14:32:26   MA
   includes

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

	  Rev 1.34   24 Nov 1995 17:24:20   OM
   PCH->PRECOMPILED

	  Rev 1.33   24 Feb 1995 00:10:18   ER
   sexport

	  Rev 1.32   20 Jan 1995 19:35:10   JP
   Debug-Writer um SectionNode erweitert

	  Rev 1.31   13 Jan 1995 10:51:40   JP
   Initialisierung und Erkennung der spz. Reader/Writer umgestellt

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

	  Rev 1.29   25 Oct 1994 15:25:14   MA
   PreHdr.

	  Rev 1.28   30 Sep 1994 18:35:56   JP
   Debug-Ausgabe optimiert/verbessert

	  Rev 1.27   28 Sep 1994 19:23:50   JP
   Umstellung auf Sfx-Items, FS_Stream durch SvStream ersetzt

	  Rev 1.26   21 Sep 1994 18:00:04   PK
   einbau sv-streams

	  Rev 1.25   04 Aug 1994 13:43:46   SWG
   swg32: SED Size to SSize, LSize to Size etc.

	  Rev 1.24   24 Mar 1994 19:04:44   JP
   Writer::Write - zus. Parameter

	  Rev 1.23   28 Jan 1994 11:29:14   MI
   TCOV() entfernt, SW_... nach SEG_... umbenannt

	  Rev 1.22   13 Jan 1994 08:29:00   MI
   Segmentierung per #define ermoeglicht

	  Rev 1.21   10 Jan 1994 08:34:44   MI
   Segmentierungs-Problem

	  Rev 1.20   09 Jan 1994 20:07:50   MI
   Provisorische Segmentierung

	  Rev 1.19   13 Dec 1993 17:46:30   JP
   Dumpe auch die FlyFormate.

	  Rev 1.18   10 Sep 1993 14:49:58   ER
   DEBUG_SEG Segment

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


