update dev300-m58
[ooovba.git] / sw / source / filter / ascii / wrtasc.cxx
blobc7f29d105b9ac497c53d62485c2f570fd4d66c0f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: wrtasc.cxx,v $
10 * $Revision: 1.13 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
33 #include <hintids.hxx>
34 #include <osl/endian.h>
35 #include <tools/stream.hxx>
36 #include <pam.hxx>
37 #include <doc.hxx>
38 #include <ndtxt.hxx>
39 #include <mdiexp.hxx> // ...Percent()
40 #include <docary.hxx>
41 #include <fmtcntnt.hxx>
42 #include <frmfmt.hxx>
43 #include <wrtasc.hxx>
45 #ifndef _STATSTR_HRC
46 #include <statstr.hrc> // ResId fuer Statusleiste
47 #endif
49 //-----------------------------------------------------------------
51 SwASCWriter::SwASCWriter( const String& rFltNm )
53 SwAsciiOptions aNewOpts;
55 switch( 5 <= rFltNm.Len() ? rFltNm.GetChar( 4 ) : 0 )
57 case 'D':
58 #if !defined(PM2)
59 aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_850 );
60 aNewOpts.SetParaFlags( LINEEND_CRLF );
61 #endif
62 if( 5 < rFltNm.Len() )
63 switch( rFltNm.Copy( 5 ).ToInt32() )
65 case 437: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_437 ); break;
66 case 850: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_850 ); break;
67 case 860: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_860 ); break;
68 case 861: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_861 ); break;
69 case 863: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_863 ); break;
70 case 865: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_865 ); break;
72 break;
74 case 'A':
75 #if !defined(WIN) && !defined(WNT)
76 aNewOpts.SetCharSet( RTL_TEXTENCODING_MS_1252 );
77 aNewOpts.SetParaFlags( LINEEND_CRLF );
78 #endif
79 break;
81 case 'M':
82 aNewOpts.SetCharSet( RTL_TEXTENCODING_APPLE_ROMAN );
83 aNewOpts.SetParaFlags( LINEEND_CR );
84 break;
86 case 'X':
87 #if !defined(UNX)
88 aNewOpts.SetCharSet( RTL_TEXTENCODING_MS_1252 );
89 aNewOpts.SetParaFlags( LINEEND_LF );
90 #endif
91 break;
93 default:
94 if( rFltNm.Copy( 4 ).EqualsAscii( "_DLG" ))
96 // use the options
97 aNewOpts = GetAsciiOptions();
100 SetAsciiOptions( aNewOpts );
103 SwASCWriter::~SwASCWriter() {}
105 ULONG SwASCWriter::WriteStream()
107 sal_Char cLineEnd[ 3 ];
108 sal_Char* pCEnd = cLineEnd;
109 if( bASCII_ParaAsCR ) // falls vorgegeben ist.
110 *pCEnd++ = '\015';
111 else if( bASCII_ParaAsBlanc )
112 *pCEnd++ = ' ';
113 else
114 switch( GetAsciiOptions().GetParaFlags() )
116 case LINEEND_CR: *pCEnd++ = '\015'; break;
117 case LINEEND_LF: *pCEnd++ = '\012'; break;
118 case LINEEND_CRLF: *pCEnd++ = '\015', *pCEnd++ = '\012'; break;
120 *pCEnd = 0;
122 sLineEnd.AssignAscii( cLineEnd );
124 long nMaxNode = pDoc->GetNodes().Count();
126 if( bShowProgress )
127 ::StartProgress( STR_STATSTR_W4WWRITE, 0, nMaxNode, pDoc->GetDocShell() );
129 SwPaM* pPam = pOrigPam;
131 BOOL bWriteSttTag = bUCS2_WithStartChar &&
132 (RTL_TEXTENCODING_UCS2 == GetAsciiOptions().GetCharSet() ||
133 RTL_TEXTENCODING_UTF8 == GetAsciiOptions().GetCharSet());
135 rtl_TextEncoding eOld = Strm().GetStreamCharSet();
136 Strm().SetStreamCharSet( GetAsciiOptions().GetCharSet() );
138 // gebe alle Bereich des Pams in das ASC-File aus.
139 do {
140 BOOL bTstFly = TRUE;
141 while( pCurPam->GetPoint()->nNode.GetIndex() < pCurPam->GetMark()->nNode.GetIndex() ||
142 (pCurPam->GetPoint()->nNode.GetIndex() == pCurPam->GetMark()->nNode.GetIndex() &&
143 pCurPam->GetPoint()->nContent.GetIndex() <= pCurPam->GetMark()->nContent.GetIndex()) )
145 SwTxtNode* pNd = pCurPam->GetPoint()->nNode.GetNode().GetTxtNode();
146 if( pNd )
148 // sollten nur Rahmen vorhanden sein?
149 // (Moeglich, wenn Rahmen-Selektion ins Clipboard
150 // gestellt wurde)
151 if( bTstFly && bWriteAll &&
152 // keine Laenge
153 !pNd->GetTxt().Len() &&
154 // Rahmen vorhanden
155 pDoc->GetSpzFrmFmts()->Count() &&
156 // nur ein Node im Array
157 pDoc->GetNodes().GetEndOfExtras().GetIndex() + 3 ==
158 pDoc->GetNodes().GetEndOfContent().GetIndex() &&
159 // und genau der ist selektiert
160 pDoc->GetNodes().GetEndOfContent().GetIndex() - 1 ==
161 pCurPam->GetPoint()->nNode.GetIndex() )
163 // dann den Inhalt vom Rahmen ausgeben.
164 // dieser steht immer an Position 0 !!
165 SwFrmFmt* pFmt = (*pDoc->GetSpzFrmFmts())[ 0 ];
166 const SwNodeIndex* pIdx = pFmt->GetCntnt().GetCntntIdx();
167 if( pIdx )
169 delete pCurPam;
170 pCurPam = NewSwPaM( *pDoc, pIdx->GetIndex(),
171 pIdx->GetNode().EndOfSectionIndex() );
172 pCurPam->Exchange();
173 continue; // while-Schleife neu aufsetzen !!
176 else
178 if (bWriteSttTag)
180 switch(GetAsciiOptions().GetCharSet())
182 case RTL_TEXTENCODING_UTF8:
183 Strm() << BYTE(0xEF) << BYTE(0xBB) <<
184 BYTE(0xBF);
185 break;
186 case RTL_TEXTENCODING_UCS2:
187 //Strm().StartWritingUnicodeText();
188 Strm().SetEndianSwap(FALSE);
189 #ifdef OSL_LITENDIAN
190 Strm() << BYTE(0xFF) << BYTE(0xFE);
191 #else
192 Strm() << BYTE(0xFE) << BYTE(0xFF);
193 #endif
194 break;
197 bWriteSttTag = FALSE;
199 Out( aASCNodeFnTab, *pNd, *this );
201 bTstFly = FALSE; // eimal Testen reicht
204 if( !pCurPam->Move( fnMoveForward, fnGoNode ) )
205 break;
207 if( bShowProgress )
208 ::SetProgressState( pCurPam->GetPoint()->nNode.GetIndex(),
209 pDoc->GetDocShell() ); // Wie weit ?
212 } while( CopyNextPam( &pPam ) ); // bis alle Pam bearbeitet
214 Strm().SetStreamCharSet( eOld );
216 if( bShowProgress )
217 ::EndProgress( pDoc->GetDocShell() );
219 return 0;
223 void GetASCWriter( const String& rFltNm, const String& /*rBaseURL*/, WriterRef& xRet )
225 xRet = new SwASCWriter( rFltNm );