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