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