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 6858 : SwASCWriter::SwASCWriter( const OUString& rFltNm )
35 : {
36 6858 : SwAsciiOptions aNewOpts;
37 :
38 6858 : 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 6858 : if( rFltNm.getLength() >= 4 && rFltNm.copy( 4 )=="_DLG" )
76 : {
77 : // use the options
78 6856 : aNewOpts = GetAsciiOptions();
79 : }
80 : }
81 6858 : SetAsciiOptions( aNewOpts );
82 6858 : }
83 :
84 13716 : SwASCWriter::~SwASCWriter() {}
85 :
86 6858 : sal_uLong SwASCWriter::WriteStream()
87 : {
88 6858 : if( bASCII_ParaAsCR ) // If predefined
89 1 : m_sLineEnd = "\015";
90 6857 : else if( bASCII_ParaAsBlanc )
91 0 : m_sLineEnd = " ";
92 : else
93 6857 : switch( GetAsciiOptions().GetParaFlags() )
94 : {
95 0 : case LINEEND_CR: m_sLineEnd = "\015"; break;
96 6857 : case LINEEND_LF: m_sLineEnd = "\012"; break;
97 0 : case LINEEND_CRLF: m_sLineEnd = "\015\012"; break;
98 : }
99 :
100 6858 : long nMaxNode = pDoc->GetNodes().Count();
101 :
102 6858 : if( bShowProgress )
103 1 : ::StartProgress( STR_STATSTR_W4WWRITE, 0, nMaxNode, pDoc->GetDocShell() );
104 :
105 6858 : SwPaM* pPam = pOrigPam;
106 :
107 6860 : bool bWriteSttTag = bUCS2_WithStartChar &&
108 2 : (RTL_TEXTENCODING_UCS2 == GetAsciiOptions().GetCharSet() ||
109 6859 : RTL_TEXTENCODING_UTF8 == GetAsciiOptions().GetCharSet());
110 :
111 6858 : rtl_TextEncoding eOld = Strm().GetStreamCharSet();
112 6858 : Strm().SetStreamCharSet( GetAsciiOptions().GetCharSet() );
113 :
114 : // Output all areas of the pam into the ASC file
115 6858 : do {
116 6858 : bool bTstFly = true;
117 40037 : while( pCurPam->GetPoint()->nNode.GetIndex() < pCurPam->GetMark()->nNode.GetIndex() ||
118 18366 : (pCurPam->GetPoint()->nNode.GetIndex() == pCurPam->GetMark()->nNode.GetIndex() &&
119 6857 : pCurPam->GetPoint()->nContent.GetIndex() <= pCurPam->GetMark()->nContent.GetIndex()) )
120 : {
121 8509 : SwTxtNode* pNd = pCurPam->GetPoint()->nNode.GetNode().GetTxtNode();
122 8509 : if( pNd )
123 : {
124 : // Should we have frames only?
125 : // That's possible, if we put a frame selection into the clipboard
126 15368 : if( bTstFly && bWriteAll &&
127 2 : pNd->GetTxt().isEmpty() &&
128 : // Frame exists
129 1 : pDoc->GetSpzFrmFmts()->size() &&
130 : // Only one node in the array
131 0 : pDoc->GetNodes().GetEndOfExtras().GetIndex() + 3 ==
132 8509 : 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 : SwFrmFmt* pFmt = (*pDoc->GetSpzFrmFmts())[ 0 ];
140 0 : const SwNodeIndex* pIdx = pFmt->GetCntnt().GetCntntIdx();
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 8509 : if (bWriteSttTag)
153 : {
154 1 : switch(GetAsciiOptions().GetCharSet())
155 : {
156 : case RTL_TEXTENCODING_UTF8:
157 1 : Strm().WriteUChar( sal_uInt8(0xEF) ).WriteUChar( sal_uInt8(0xBB) ). WriteUChar( sal_uInt8(0xBF) );
158 1 : break;
159 : case RTL_TEXTENCODING_UCS2:
160 0 : Strm().SetEndianSwap(false);
161 : #ifdef OSL_LITENDIAN
162 0 : Strm().WriteUChar( sal_uInt8(0xFF) ).WriteUChar( sal_uInt8(0xFE) );
163 : #else
164 : Strm().WriteUChar( sal_uInt8(0xFE) ).WriteUChar( sal_uInt8(0xFF) );
165 : #endif
166 0 : break;
167 :
168 : }
169 1 : bWriteSttTag = false;
170 : }
171 8509 : Out( aASCNodeFnTab, *pNd, *this );
172 : }
173 8509 : bTstFly = false; // Testing once is enough
174 : }
175 :
176 8509 : if( !pCurPam->Move( fnMoveForward, fnGoNode ) )
177 2206 : break;
178 :
179 6303 : if( bShowProgress )
180 0 : ::SetProgressState( pCurPam->GetPoint()->nNode.GetIndex(),
181 0 : pDoc->GetDocShell() ); // How far?
182 :
183 : }
184 6858 : } while( CopyNextPam( &pPam ) ); // Until all pams are processed
185 :
186 6858 : Strm().SetStreamCharSet( eOld );
187 :
188 6858 : if( bShowProgress )
189 1 : ::EndProgress( pDoc->GetDocShell() );
190 :
191 6858 : return 0;
192 : }
193 :
194 6858 : void GetASCWriter( const OUString& rFltNm, const OUString& /*rBaseURL*/, WriterRef& xRet )
195 : {
196 6858 : xRet = new SwASCWriter( rFltNm );
197 6858 : }
198 :
199 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|