LibreOffice Module sw (master) 1
wrtasc.cxx
Go to the documentation of this file.
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 <osl/endian.h>
21#include <tools/stream.hxx>
22#include <pam.hxx>
23#include <doc.hxx>
24#include <ndtxt.hxx>
25#include <mdiexp.hxx>
26#include <fmtcntnt.hxx>
27#include <frmfmt.hxx>
28#include "wrtasc.hxx"
29#include <frameformats.hxx>
30#include <sfx2/docfile.hxx>
31#include <sfx2/sfxsids.hrc>
32#include <o3tl/string_view.hxx>
33
34#include <strings.hrc>
35
36SwASCWriter::SwASCWriter( std::u16string_view rFltNm )
37{
38 SwAsciiOptions aNewOpts;
39
40 switch( 5 <= rFltNm.size() ? rFltNm[4] : 0 )
41 {
42 case 'D':
43 aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_850 );
44 aNewOpts.SetParaFlags( LINEEND_CRLF );
45 if( 5 < rFltNm.size() )
46 {
47 std::u16string_view aFilterNum = rFltNm.substr( 5 );
48 switch( o3tl::toInt32(aFilterNum) )
49 {
50 case 437: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_437 ); break;
51 case 850: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_850 ); break;
52 case 860: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_860 ); break;
53 case 861: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_861 ); break;
54 case 863: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_863 ); break;
55 case 865: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_865 ); break;
56 }
57 }
58 break;
59
60 case 'A':
61#ifndef _WIN32
62 aNewOpts.SetCharSet( RTL_TEXTENCODING_MS_1252 );
63 aNewOpts.SetParaFlags( LINEEND_CRLF );
64#endif
65 break;
66
67 case 'M':
68 aNewOpts.SetCharSet( RTL_TEXTENCODING_APPLE_ROMAN );
69 aNewOpts.SetParaFlags( LINEEND_CR );
70 break;
71
72 case 'X':
73#ifdef _WIN32
74 aNewOpts.SetCharSet( RTL_TEXTENCODING_MS_1252 );
75 aNewOpts.SetParaFlags( LINEEND_LF );
76#endif
77 break;
78
79 default:
80 if( rFltNm.size() >= 4 && rFltNm.substr( 4 )==u"_DLG" )
81 {
82 // use the options
83 aNewOpts = GetAsciiOptions();
84 }
85 }
86 SetAsciiOptions( aNewOpts );
87}
88
90
92{
93 bool bIncludeBOM = GetAsciiOptions().GetIncludeBOM();
94 bool bIncludeHidden = GetAsciiOptions().GetIncludeHidden();
95
96 if( m_bASCII_ParaAsCR ) // If predefined
97 m_sLineEnd = "\015";
98 else if( m_bASCII_ParaAsBlank )
99 m_sLineEnd = " ";
100 else
101 switch( GetAsciiOptions().GetParaFlags() )
102 {
103 case LINEEND_CR: m_sLineEnd = "\015"; break;
104 case LINEEND_LF: m_sLineEnd = "\012"; break;
105 case LINEEND_CRLF: m_sLineEnd = "\015\012"; break;
106 }
107
108 SwNodeOffset nMaxNode = m_pDoc->GetNodes().Count();
109
110 if( m_bShowProgress )
111 ::StartProgress( STR_STATSTR_W4WWRITE, 0, sal_Int32(nMaxNode), m_pDoc->GetDocShell() );
112
113 SwPaM* pPam = m_pOrigPam;
114
115 bool bWriteSttTag = m_bUCS2_WithStartChar &&
116 (RTL_TEXTENCODING_UCS2 == GetAsciiOptions().GetCharSet() ||
117 RTL_TEXTENCODING_UTF8 == GetAsciiOptions().GetCharSet());
118
119 rtl_TextEncoding eOld = Strm().GetStreamCharSet();
120 Strm().SetStreamCharSet( GetAsciiOptions().GetCharSet() );
121
122 // Output all areas of the pam into the ASC file
123 do {
124 bool bTstFly = true;
125 while( m_pCurrentPam->GetPoint()->GetNodeIndex() < m_pCurrentPam->GetMark()->GetNodeIndex() ||
126 (m_pCurrentPam->GetPoint()->GetNodeIndex() == m_pCurrentPam->GetMark()->GetNodeIndex() &&
127 m_pCurrentPam->GetPoint()->GetContentIndex() <= m_pCurrentPam->GetMark()->GetContentIndex()) )
128 {
129 SwTextNode* pNd = m_pCurrentPam->GetPoint()->GetNode().GetTextNode();
130 if( pNd )
131 {
132 // Should we have frames only?
133 // That's possible, if we put a frame selection into the clipboard
134 if( bTstFly && m_bWriteAll &&
135 pNd->GetText().isEmpty() &&
136 // Frame exists
138 // Only one node in the array
141 // And exactly this one is selected
143 m_pCurrentPam->GetPoint()->GetNodeIndex() )
144 {
145 // Print the frame's content.
146 // It is always at position 0!
147 const SwFrameFormat* pFormat = (*m_pDoc->GetSpzFrameFormats())[ 0 ];
148 const SwNodeIndex* pIdx = pFormat->GetContent().GetContentIdx();
149 if( pIdx )
150 {
152 pIdx->GetNode().EndOfSectionIndex() );
153 m_pCurrentPam->Exchange();
154 continue; // reset while loop!
155 }
156 }
157 else if (!pNd->IsHidden() || bIncludeHidden)
158 {
159 if (bWriteSttTag)
160 {
161 switch(GetAsciiOptions().GetCharSet())
162 {
163 case RTL_TEXTENCODING_UTF8:
164 if( bIncludeBOM )
165 {
166 Strm().WriteUChar( 0xEF ).WriteUChar( 0xBB ).WriteUChar( 0xBF );
167 }
168
169 break;
170 case RTL_TEXTENCODING_UCS2:
171#ifdef OSL_LITENDIAN
172 Strm().SetEndian(SvStreamEndian::LITTLE);
173 if( bIncludeBOM )
174 {
175 Strm().WriteUChar( 0xFF ).WriteUChar( 0xFE );
176 }
177#else
178 Strm().SetEndian(SvStreamEndian::BIG);
179 if( bIncludeBOM )
180 {
181 Strm().WriteUChar( 0xFE ).WriteUChar( 0xFF );
182 }
183#endif
184 break;
185
186 }
187 bWriteSttTag = false;
188 }
189 Out( aASCNodeFnTab, *pNd, *this );
190 }
191 bTstFly = false; // Testing once is enough
192 }
193
194 if( !m_pCurrentPam->Move( fnMoveForward, GoInNode ) )
195 break;
196
197 if( m_bShowProgress )
198 ::SetProgressState( sal_Int32(m_pCurrentPam->GetPoint()->GetNodeIndex()),
199 m_pDoc->GetDocShell() ); // How far?
200
201 }
202 } while( CopyNextPam( &pPam ) ); // Until all pams are processed
203
204 Strm().SetStreamCharSet( eOld );
205
206 if( m_bShowProgress )
208
209 return ERRCODE_NONE;
210}
211
213{
214 const SfxItemSet* pSet = rMedium.GetItemSet();
215 if( nullptr != pSet )
216 {
217 if( const SfxStringItem* pItem = pSet->GetItemIfSet( SID_FILE_FILTEROPTIONS ) )
218 {
219 SwAsciiOptions aOpt;
220 OUString sItemOpt;
221 sItemOpt = pItem->GetValue();
222 aOpt.ReadUserData(sItemOpt);
223 SetAsciiOptions(aOpt);
224 }
225 }
226}
227
229 std::u16string_view rFltNm, [[maybe_unused]] const OUString& /*rBaseURL*/, WriterRef& xRet )
230{
231 xRet = new SwASCWriter( rFltNm );
232}
233
234/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SwNodeFnTab aASCNodeFnTab
Definition: ascatr.cxx:382
const T * GetItemIfSet(TypedWhichId< T > nWhich, bool bSrchInParent=true) const
SfxItemSet * GetItemSet() const
void SetEndian(SvStreamEndian SvStreamEndian)
SvStream & WriteUChar(unsigned char nChar)
void SetStreamCharSet(rtl_TextEncoding eCharSet)
rtl_TextEncoding GetStreamCharSet() const
void SetupFilterOptions(SfxMedium &rMedium) override
Definition: wrtasc.cxx:212
virtual ErrCode WriteStream() override
Definition: wrtasc.cxx:91
OUString m_sLineEnd
Definition: wrtasc.hxx:31
SwASCWriter(std::u16string_view rFilterName)
Definition: wrtasc.cxx:36
virtual ~SwASCWriter() override
Definition: wrtasc.cxx:89
rtl_TextEncoding GetCharSet() const
Definition: shellio.hxx:76
void SetParaFlags(LineEnd eVal)
Definition: shellio.hxx:83
bool GetIncludeHidden() const
Definition: shellio.hxx:88
void SetCharSet(rtl_TextEncoding nVal)
Definition: shellio.hxx:77
bool GetIncludeBOM() const
Definition: shellio.hxx:85
void ReadUserData(std::u16string_view)
Definition: fltini.cxx:547
SwNodes & GetNodes()
Definition: doc.hxx:420
const SwFrameFormats * GetSpzFrameFormats() const
Definition: doc.hxx:755
SwDocShell * GetDocShell()
Definition: doc.hxx:1364
const SwNodeIndex * GetContentIdx() const
Definition: fmtcntnt.hxx:46
const SwFormatContent & GetContent(bool=true) const
Definition: fmtcntnt.hxx:55
Style of a layout element.
Definition: frmfmt.hxx:62
bool empty() const
Marks a node in the document model.
Definition: ndindex.hxx:31
SwNode & GetNode() const
Definition: ndindex.hxx:123
SwNodeOffset GetIndex() const
Definition: ndindex.hxx:111
SwNodeOffset GetIndex() const
Definition: node.hxx:312
SwNodeOffset EndOfSectionIndex() const
Definition: node.hxx:728
SwNode & GetEndOfExtras() const
This is the last EndNode of a special section.
Definition: ndarr.hxx:163
SwNode & GetEndOfContent() const
Regular ContentSection (i.e. the BodyText).
Definition: ndarr.hxx:165
SwNodeOffset Count() const
Definition: ndarr.hxx:142
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:187
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:112
bool IsHidden() const
Definition: ndtxt.cxx:4774
const OUString & GetText() const
Definition: ndtxt.hxx:244
bool m_bASCII_ParaAsCR
Definition: shellio.hxx:414
bool CopyNextPam(SwPaM **)
Definition: writer.cxx:162
SwPaM * m_pOrigPam
Definition: shellio.hxx:408
bool m_bShowProgress
Definition: shellio.hxx:411
SvStream & Strm()
Definition: writer.cxx:214
void SetAsciiOptions(const SwAsciiOptions &rOpt)
Definition: shellio.hxx:441
bool m_bUCS2_WithStartChar
Definition: shellio.hxx:417
const SwAsciiOptions & GetAsciiOptions() const
Definition: shellio.hxx:440
static std::shared_ptr< SwUnoCursor > NewUnoCursor(SwDoc &rDoc, SwNodeOffset const nStartIdx, SwNodeOffset const nEndIdx)
Definition: writer.cxx:191
SwDoc * m_pDoc
Definition: shellio.hxx:407
std::shared_ptr< SwUnoCursor > m_pCurrentPam
Definition: shellio.hxx:409
bool m_bWriteAll
Definition: shellio.hxx:410
bool m_bASCII_ParaAsBlank
Definition: shellio.hxx:415
float u
#define ERRCODE_NONE
LINEEND_LF
LINEEND_CRLF
LINEEND_CR
void StartProgress(TranslateId pMessResId, tools::Long nStartValue, tools::Long nEndValue, SwDocShell *pDocShell)
Definition: mainwn.cxx:52
void EndProgress(SwDocShell const *pDocShell)
Definition: mainwn.cxx:92
void SetProgressState(tools::Long nPosition, SwDocShell const *pDocShell)
Definition: mainwn.cxx:82
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
bool GoInNode(SwPaM &rPam, SwMoveFnCollection const &fnMove)
Definition: pam.cxx:1171
SwMoveFnCollection const & fnMoveForward
SwPam::Move()/Find() default argument.
Definition: paminit.cxx:61
SwHTMLWriter & Out(const SwAttrFnTab, const SfxPoolItem &, SwHTMLWriter &)
Definition: wrt_fn.cxx:31
void GetASCWriter(std::u16string_view rFltNm, const OUString &, WriterRef &xRet)
Definition: wrtasc.cxx:228