1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
26 #include <mdiexp.hxx> // ...Percent()
28 #include <fmtcntnt.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 )
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;
59 aNewOpts
.SetCharSet( RTL_TEXTENCODING_MS_1252
);
60 aNewOpts
.SetParaFlags( LINEEND_CRLF
);
65 aNewOpts
.SetCharSet( RTL_TEXTENCODING_APPLE_ROMAN
);
66 aNewOpts
.SetParaFlags( LINEEND_CR
);
71 aNewOpts
.SetCharSet( RTL_TEXTENCODING_MS_1252
);
72 aNewOpts
.SetParaFlags( LINEEND_LF
);
77 if( rFltNm
.Copy( 4 ).EqualsAscii( "_DLG" ))
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
94 else if( bASCII_ParaAsBlanc
)
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;
105 sLineEnd
.AssignAscii( cLineEnd
);
107 long nMaxNode
= pDoc
->GetNodes().Count();
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
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();
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() &&
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();
151 pCurPam
= NewSwPaM( *pDoc
, pIdx
->GetIndex(),
152 pIdx
->GetNode().EndOfSectionIndex() );
154 continue; // reset while loop!
161 switch(GetAsciiOptions().GetCharSet())
163 case RTL_TEXTENCODING_UTF8
:
164 Strm() << sal_uInt8(0xEF) << sal_uInt8(0xBB) <<
167 case RTL_TEXTENCODING_UCS2
:
168 Strm().SetEndianSwap(sal_False
);
170 Strm() << sal_uInt8(0xFF) << sal_uInt8(0xFE);
172 Strm() << sal_uInt8(0xFE) << sal_uInt8(0xFF);
177 bWriteSttTag
= false;
179 Out( aASCNodeFnTab
, *pNd
, *this );
181 bTstFly
= false; // Testing once is enough
184 if( !pCurPam
->Move( fnMoveForward
, fnGoNode
) )
188 ::SetProgressState( pCurPam
->GetPoint()->nNode
.GetIndex(),
189 pDoc
->GetDocShell() ); // How far?
192 } while( CopyNextPam( &pPam
) ); // Until all pams are processed
194 Strm().SetStreamCharSet( eOld
);
197 ::EndProgress( pDoc
->GetDocShell() );
203 void GetASCWriter( const String
& rFltNm
, const String
& /*rBaseURL*/, WriterRef
& xRet
)
205 xRet
= new SwASCWriter( rFltNm
);
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */