Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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>
27 : #include <docary.hxx>
28 : #include <fmtcntnt.hxx>
29 : #include <frmfmt.hxx>
30 : #include <wrtasc.hxx>
31 :
32 : #include <statstr.hrc>
33 :
34 8076 : SwASCWriter::SwASCWriter( const OUString& rFltNm )
35 : {
36 8076 : SwAsciiOptions aNewOpts;
37 :
38 8076 : switch( 5 <= rFltNm.getLength() ? rFltNm[4] : 0 )
39 : {
40 : case 'D':
41 0 : aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_850 );
42 0 : aNewOpts.SetParaFlags( LINEEND_CRLF );
43 0 : if( 5 < rFltNm.getLength() )
44 0 : switch( rFltNm.copy( 5 ).toInt32() )
45 : {
46 0 : case 437: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_437 ); break;
47 0 : case 850: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_850 ); break;
48 0 : case 860: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_860 ); break;
49 0 : case 861: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_861 ); break;
50 0 : case 863: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_863 ); break;
51 0 : case 865: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_865 ); break;
52 : }
53 0 : break;
54 :
55 : case 'A':
56 : #ifndef _WIN32
57 0 : aNewOpts.SetCharSet( RTL_TEXTENCODING_MS_1252 );
58 0 : aNewOpts.SetParaFlags( LINEEND_CRLF );
59 : #endif
60 0 : break;
61 :
62 : case 'M':
63 0 : aNewOpts.SetCharSet( RTL_TEXTENCODING_APPLE_ROMAN );
64 0 : aNewOpts.SetParaFlags( LINEEND_CR );
65 0 : break;
66 :
67 : case 'X':
68 : #ifdef _WIN32
69 : aNewOpts.SetCharSet( RTL_TEXTENCODING_MS_1252 );
70 : aNewOpts.SetParaFlags( LINEEND_LF );
71 : #endif
72 0 : break;
73 :
74 : default:
75 8076 : if( rFltNm.getLength() >= 4 && rFltNm.copy( 4 )=="_DLG" )
76 : {
77 : // use the options
78 8074 : aNewOpts = GetAsciiOptions();
79 : }
80 : }
81 8076 : SetAsciiOptions( aNewOpts );
82 8076 : }
83 :
84 16152 : SwASCWriter::~SwASCWriter() {}
85 :
86 8076 : sal_uLong SwASCWriter::WriteStream()
87 : {
88 8076 : if( bASCII_ParaAsCR ) // If predefined
89 1 : m_sLineEnd = "\015";
90 8075 : else if( bASCII_ParaAsBlanc )
91 0 : m_sLineEnd = " ";
92 : else
93 8075 : switch( GetAsciiOptions().GetParaFlags() )
94 : {
95 0 : case LINEEND_CR: m_sLineEnd = "\015"; break;
96 8075 : case LINEEND_LF: m_sLineEnd = "\012"; break;
97 0 : case LINEEND_CRLF: m_sLineEnd = "\015\012"; break;
98 : }
99 :
100 8076 : long nMaxNode = pDoc->GetNodes().Count();
101 :
102 8076 : if( bShowProgress )
103 1 : ::StartProgress( STR_STATSTR_W4WWRITE, 0, nMaxNode, pDoc->GetDocShell() );
104 :
105 8076 : SwPaM* pPam = pOrigPam;
106 :
107 8078 : bool bWriteSttTag = bUCS2_WithStartChar &&
108 2 : (RTL_TEXTENCODING_UCS2 == GetAsciiOptions().GetCharSet() ||
109 8077 : RTL_TEXTENCODING_UTF8 == GetAsciiOptions().GetCharSet());
110 :
111 8076 : rtl_TextEncoding eOld = Strm().GetStreamCharSet();
112 8076 : Strm().SetStreamCharSet( GetAsciiOptions().GetCharSet() );
113 :
114 : // Output all areas of the pam into the ASC file
115 8076 : do {
116 8076 : bool bTstFly = true;
117 47203 : while( pCurPam->GetPoint()->nNode.GetIndex() < pCurPam->GetMark()->nNode.GetIndex() ||
118 20915 : (pCurPam->GetPoint()->nNode.GetIndex() == pCurPam->GetMark()->nNode.GetIndex() &&
119 8075 : pCurPam->GetPoint()->nContent.GetIndex() <= pCurPam->GetMark()->nContent.GetIndex()) )
120 : {
121 10761 : SwTextNode* pNd = pCurPam->GetPoint()->nNode.GetNode().GetTextNode();
122 10761 : if( pNd )
123 : {
124 : // Should we have frames only?
125 : // That's possible, if we put a frame selection into the clipboard
126 18838 : if( bTstFly && bWriteAll &&
127 2 : pNd->GetText().isEmpty() &&
128 : // Frame exists
129 1 : pDoc->GetSpzFrameFormats()->size() &&
130 : // Only one node in the array
131 0 : pDoc->GetNodes().GetEndOfExtras().GetIndex() + 3 ==
132 10761 : pDoc->GetNodes().GetEndOfContent().GetIndex() &&
133 : // And exactly this one is selected
134 0 : pDoc->GetNodes().GetEndOfContent().GetIndex() - 1 ==
135 0 : pCurPam->GetPoint()->nNode.GetIndex() )
136 : {
137 : // Print the frame's content.
138 : // It is always at position 0!
139 0 : const SwFrameFormat* pFormat = (*pDoc->GetSpzFrameFormats())[ 0 ];
140 0 : const SwNodeIndex* pIdx = pFormat->GetContent().GetContentIdx();
141 0 : if( pIdx )
142 : {
143 0 : delete pCurPam;
144 : pCurPam = NewSwPaM( *pDoc, pIdx->GetIndex(),
145 0 : pIdx->GetNode().EndOfSectionIndex() );
146 0 : pCurPam->Exchange();
147 0 : continue; // reset while loop!
148 : }
149 : }
150 : else
151 : {
152 10761 : if (bWriteSttTag)
153 : {
154 1 : switch(GetAsciiOptions().GetCharSet())
155 : {
156 : case RTL_TEXTENCODING_UTF8:
157 1 : Strm().WriteUChar( 0xEF ).WriteUChar( 0xBB ).WriteUChar( 0xBF );
158 1 : break;
159 : case RTL_TEXTENCODING_UCS2:
160 : #ifdef OSL_LITENDIAN
161 0 : Strm().SetEndian(SvStreamEndian::LITTLE);
162 0 : Strm().WriteUChar( 0xFF ).WriteUChar( 0xFE );
163 : #else
164 : Strm().SetEndian(SvStreamEndian::BIG);
165 : Strm().WriteUChar( 0xFE ).WriteUChar( 0xFF );
166 : #endif
167 0 : break;
168 :
169 : }
170 1 : bWriteSttTag = false;
171 : }
172 10761 : Out( aASCNodeFnTab, *pNd, *this );
173 : }
174 10761 : bTstFly = false; // Testing once is enough
175 : }
176 :
177 10761 : if( !pCurPam->Move( fnMoveForward, fnGoNode ) )
178 3311 : break;
179 :
180 7450 : if( bShowProgress )
181 0 : ::SetProgressState( pCurPam->GetPoint()->nNode.GetIndex(),
182 0 : pDoc->GetDocShell() ); // How far?
183 :
184 : }
185 8076 : } while( CopyNextPam( &pPam ) ); // Until all pams are processed
186 :
187 8076 : Strm().SetStreamCharSet( eOld );
188 :
189 8076 : if( bShowProgress )
190 1 : ::EndProgress( pDoc->GetDocShell() );
191 :
192 8076 : return 0;
193 : }
194 :
195 8076 : void GetASCWriter( const OUString& rFltNm, const OUString& /*rBaseURL*/, WriterRef& xRet )
196 : {
197 8076 : xRet = new SwASCWriter( rFltNm );
198 8253 : }
199 :
200 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|