bump product version to 4.1.6.2
[LibreOffice.git] / sw / source / filter / ascii / wrtasc.cxx
blob42a0b21f86203a308d76263c6e3b4a176990791c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <hintids.hxx>
21 #include <osl/endian.h>
22 #include <tools/stream.hxx>
23 #include <pam.hxx>
24 #include <doc.hxx>
25 #include <ndtxt.hxx>
26 #include <mdiexp.hxx> // ...Percent()
27 #include <docary.hxx>
28 #include <fmtcntnt.hxx>
29 #include <frmfmt.hxx>
30 #include <wrtasc.hxx>
32 #include <statstr.hrc> // ResId for status bar
34 //-----------------------------------------------------------------
36 SwASCWriter::SwASCWriter( const String& rFltNm )
38 SwAsciiOptions aNewOpts;
40 switch( 5 <= rFltNm.Len() ? rFltNm.GetChar( 4 ) : 0 )
42 case 'D':
43 aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_850 );
44 aNewOpts.SetParaFlags( LINEEND_CRLF );
45 if( 5 < rFltNm.Len() )
46 switch( rFltNm.Copy( 5 ).ToInt32() )
48 case 437: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_437 ); break;
49 case 850: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_850 ); break;
50 case 860: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_860 ); break;
51 case 861: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_861 ); break;
52 case 863: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_863 ); break;
53 case 865: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_865 ); break;
55 break;
57 case 'A':
58 #if !defined(WNT)
59 aNewOpts.SetCharSet( RTL_TEXTENCODING_MS_1252 );
60 aNewOpts.SetParaFlags( LINEEND_CRLF );
61 #endif
62 break;
64 case 'M':
65 aNewOpts.SetCharSet( RTL_TEXTENCODING_APPLE_ROMAN );
66 aNewOpts.SetParaFlags( LINEEND_CR );
67 break;
69 case 'X':
70 #if !defined(UNX)
71 aNewOpts.SetCharSet( RTL_TEXTENCODING_MS_1252 );
72 aNewOpts.SetParaFlags( LINEEND_LF );
73 #endif
74 break;
76 default:
77 if( rFltNm.Copy( 4 ).EqualsAscii( "_DLG" ))
79 // use the options
80 aNewOpts = GetAsciiOptions();
83 SetAsciiOptions( aNewOpts );
86 SwASCWriter::~SwASCWriter() {}
88 sal_uLong SwASCWriter::WriteStream()
90 sal_Char cLineEnd[ 3 ];
91 sal_Char* pCEnd = cLineEnd;
92 if( bASCII_ParaAsCR ) // If predefined
93 *pCEnd++ = '\015';
94 else if( bASCII_ParaAsBlanc )
95 *pCEnd++ = ' ';
96 else
97 switch( GetAsciiOptions().GetParaFlags() )
99 case LINEEND_CR: *pCEnd++ = '\015'; break;
100 case LINEEND_LF: *pCEnd++ = '\012'; break;
101 case LINEEND_CRLF: *pCEnd++ = '\015', *pCEnd++ = '\012'; break;
103 *pCEnd = 0;
105 sLineEnd.AssignAscii( cLineEnd );
107 long nMaxNode = pDoc->GetNodes().Count();
109 if( bShowProgress )
110 ::StartProgress( STR_STATSTR_W4WWRITE, 0, nMaxNode, pDoc->GetDocShell() );
112 SwPaM* pPam = pOrigPam;
114 bool bWriteSttTag = bUCS2_WithStartChar &&
115 (RTL_TEXTENCODING_UCS2 == GetAsciiOptions().GetCharSet() ||
116 RTL_TEXTENCODING_UTF8 == GetAsciiOptions().GetCharSet());
118 rtl_TextEncoding eOld = Strm().GetStreamCharSet();
119 Strm().SetStreamCharSet( GetAsciiOptions().GetCharSet() );
121 // Output all areas of the pam into the ASC file
122 do {
123 bool bTstFly = true;
124 while( pCurPam->GetPoint()->nNode.GetIndex() < pCurPam->GetMark()->nNode.GetIndex() ||
125 (pCurPam->GetPoint()->nNode.GetIndex() == pCurPam->GetMark()->nNode.GetIndex() &&
126 pCurPam->GetPoint()->nContent.GetIndex() <= pCurPam->GetMark()->nContent.GetIndex()) )
128 SwTxtNode* pNd = pCurPam->GetPoint()->nNode.GetNode().GetTxtNode();
129 if( pNd )
131 // Should we have frames only?
132 // That's possible, if we put a frame selection into the clipboard
133 if( bTstFly && bWriteAll &&
134 pNd->GetTxt().isEmpty() &&
135 // Frame exists
136 pDoc->GetSpzFrmFmts()->size() &&
137 // Only one node in the array
138 pDoc->GetNodes().GetEndOfExtras().GetIndex() + 3 ==
139 pDoc->GetNodes().GetEndOfContent().GetIndex() &&
140 // And exactly this one is selected
141 pDoc->GetNodes().GetEndOfContent().GetIndex() - 1 ==
142 pCurPam->GetPoint()->nNode.GetIndex() )
144 // Print the frame's content.
145 // It is always at position 0!
146 SwFrmFmt* pFmt = (*pDoc->GetSpzFrmFmts())[ 0 ];
147 const SwNodeIndex* pIdx = pFmt->GetCntnt().GetCntntIdx();
148 if( pIdx )
150 delete pCurPam;
151 pCurPam = NewSwPaM( *pDoc, pIdx->GetIndex(),
152 pIdx->GetNode().EndOfSectionIndex() );
153 pCurPam->Exchange();
154 continue; // reset while loop!
157 else
159 if (bWriteSttTag)
161 switch(GetAsciiOptions().GetCharSet())
163 case RTL_TEXTENCODING_UTF8:
164 Strm() << sal_uInt8(0xEF) << sal_uInt8(0xBB) <<
165 sal_uInt8(0xBF);
166 break;
167 case RTL_TEXTENCODING_UCS2:
168 Strm().SetEndianSwap(sal_False);
169 #ifdef OSL_LITENDIAN
170 Strm() << sal_uInt8(0xFF) << sal_uInt8(0xFE);
171 #else
172 Strm() << sal_uInt8(0xFE) << sal_uInt8(0xFF);
173 #endif
174 break;
177 bWriteSttTag = false;
179 Out( aASCNodeFnTab, *pNd, *this );
181 bTstFly = false; // Testing once is enough
184 if( !pCurPam->Move( fnMoveForward, fnGoNode ) )
185 break;
187 if( bShowProgress )
188 ::SetProgressState( pCurPam->GetPoint()->nNode.GetIndex(),
189 pDoc->GetDocShell() ); // How far?
192 } while( CopyNextPam( &pPam ) ); // Until all pams are processed
194 Strm().SetStreamCharSet( eOld );
196 if( bShowProgress )
197 ::EndProgress( pDoc->GetDocShell() );
199 return 0;
203 void GetASCWriter( const String& rFltNm, const String& /*rBaseURL*/, WriterRef& xRet )
205 xRet = new SwASCWriter( rFltNm );
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */