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 static constexpr OUStringLiteral STR_CR = u"\015";
94 static constexpr OUStringLiteral STR_LF = u"\012";
95 static constexpr OUStringLiteral STR_CRLF = u"\015\012";
96 static constexpr OUStringLiteral STR_BLANK = u" ";
97 bool bIncludeBOM = GetAsciiOptions().GetIncludeBOM();
98 bool bIncludeHidden = GetAsciiOptions().GetIncludeHidden();
99
100 if( m_bASCII_ParaAsCR ) // If predefined
101 m_sLineEnd = STR_CR;
102 else if( m_bASCII_ParaAsBlank )
103 m_sLineEnd = STR_BLANK;
104 else
105 switch( GetAsciiOptions().GetParaFlags() )
106 {
107 case LINEEND_CR: m_sLineEnd = STR_CR; break;
108 case LINEEND_LF: m_sLineEnd = STR_LF; break;
109 case LINEEND_CRLF: m_sLineEnd = STR_CRLF; break;
110 }
111
112 SwNodeOffset nMaxNode = m_pDoc->GetNodes().Count();
113
114 if( m_bShowProgress )
115 ::StartProgress( STR_STATSTR_W4WWRITE, 0, sal_Int32(nMaxNode), m_pDoc->GetDocShell() );
116
117 SwPaM* pPam = m_pOrigPam;
118
119 bool bWriteSttTag = m_bUCS2_WithStartChar &&
120 (RTL_TEXTENCODING_UCS2 == GetAsciiOptions().GetCharSet() ||
121 RTL_TEXTENCODING_UTF8 == GetAsciiOptions().GetCharSet());
122
123 rtl_TextEncoding eOld = Strm().GetStreamCharSet();
124 Strm().SetStreamCharSet( GetAsciiOptions().GetCharSet() );
125
126 // Output all areas of the pam into the ASC file
127 do {
128 bool bTstFly = true;
129 while( m_pCurrentPam->GetPoint()->GetNodeIndex() < m_pCurrentPam->GetMark()->GetNodeIndex() ||
130 (m_pCurrentPam->GetPoint()->GetNodeIndex() == m_pCurrentPam->GetMark()->GetNodeIndex() &&
131 m_pCurrentPam->GetPoint()->GetContentIndex() <= m_pCurrentPam->GetMark()->GetContentIndex()) )
132 {
133 SwTextNode* pNd = m_pCurrentPam->GetPoint()->GetNode().GetTextNode();
134 if( pNd )
135 {
136 // Should we have frames only?
137 // That's possible, if we put a frame selection into the clipboard
138 if( bTstFly && m_bWriteAll &&
139 pNd->GetText().isEmpty() &&
140 // Frame exists
141 !m_pDoc->GetSpzFrameFormats()->empty() &&
142 // Only one node in the array
145 // And exactly this one is selected
147 m_pCurrentPam->GetPoint()->GetNodeIndex() )
148 {
149 // Print the frame's content.
150 // It is always at position 0!
151 const SwFrameFormat* pFormat = (*m_pDoc->GetSpzFrameFormats())[ 0 ];
152 const SwNodeIndex* pIdx = pFormat->GetContent().GetContentIdx();
153 if( pIdx )
154 {
156 pIdx->GetNode().EndOfSectionIndex() );
157 m_pCurrentPam->Exchange();
158 continue; // reset while loop!
159 }
160 }
161 else if (!pNd->IsHidden() || bIncludeHidden)
162 {
163 if (bWriteSttTag)
164 {
165 switch(GetAsciiOptions().GetCharSet())
166 {
167 case RTL_TEXTENCODING_UTF8:
168 if( bIncludeBOM )
169 {
170 Strm().WriteUChar( 0xEF ).WriteUChar( 0xBB ).WriteUChar( 0xBF );
171 }
172
173 break;
174 case RTL_TEXTENCODING_UCS2:
175#ifdef OSL_LITENDIAN
176 Strm().SetEndian(SvStreamEndian::LITTLE);
177#else
178 Strm().SetEndian(SvStreamEndian::BIG);
179#endif
180 if( bIncludeBOM )
181 {
183 }
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 if( const SfxStringItem* pItem = rMedium.GetItemSet().GetItemIfSet( SID_FILE_FILTEROPTIONS ) )
215 {
216 SwAsciiOptions aOpt;
217 OUString sItemOpt;
218 sItemOpt = pItem->GetValue();
219 aOpt.ReadUserData(sItemOpt);
220 SetAsciiOptions(aOpt);
221 }
222}
223
225 std::u16string_view rFltNm, [[maybe_unused]] const OUString& /*rBaseURL*/, WriterRef& xRet )
226{
227 xRet = new SwASCWriter( rFltNm );
228}
229
230/* 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 StartWritingUnicodeText()
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:533
SwNodes & GetNodes()
Definition: doc.hxx:422
const sw::FrameFormats< sw::SpzFrameFormat * > * GetSpzFrameFormats() const
Definition: doc.hxx:759
SwDocShell * GetDocShell()
Definition: doc.hxx:1370
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:72
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:691
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:188
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:112
bool IsHidden() const
Definition: ndtxt.cxx:4749
const OUString & GetText() const
Definition: ndtxt.hxx:244
bool m_bASCII_ParaAsCR
Definition: shellio.hxx:414
bool CopyNextPam(SwPaM **)
Definition: writer.cxx:141
SwPaM * m_pOrigPam
Definition: shellio.hxx:408
bool m_bShowProgress
Definition: shellio.hxx:411
SvStream & Strm()
Definition: writer.cxx:193
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:170
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:1194
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:224