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