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;
122  {
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
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  {
145  delete m_pCurrentPam;
146  m_pCurrentPam = NewSwPaM( *m_pDoc, pIdx->GetIndex(),
147  pIdx->GetNode().EndOfSectionIndex() );
149  continue; // reset while loop!
150  }
151  }
152  else
153  {
154  if (bWriteSttTag)
155  {
156  switch(GetAsciiOptions().GetCharSet())
157  {
158  case RTL_TEXTENCODING_UTF8:
159  if( bIncludeBOM )
160  {
161  Strm().WriteUChar( 0xEF ).WriteUChar( 0xBB ).WriteUChar( 0xBF );
162  }
163 
164  break;
165  case RTL_TEXTENCODING_UCS2:
166 #ifdef OSL_LITENDIAN
167  Strm().SetEndian(SvStreamEndian::LITTLE);
168  if( bIncludeBOM )
169  {
170  Strm().WriteUChar( 0xFF ).WriteUChar( 0xFE );
171  }
172 #else
173  Strm().SetEndian(SvStreamEndian::BIG);
174  if( bIncludeBOM )
175  {
176  Strm().WriteUChar( 0xFE ).WriteUChar( 0xFF );
177  }
178 #endif
179  break;
180 
181  }
182  bWriteSttTag = false;
183  }
184  Out( aASCNodeFnTab, *pNd, *this );
185  }
186  bTstFly = false; // Testing once is enough
187  }
188 
190  break;
191 
192  if( m_bShowProgress )
194  m_pDoc->GetDocShell() ); // How far?
195 
196  }
197  } while( CopyNextPam( &pPam ) ); // Until all pams are processed
198 
199  Strm().SetStreamCharSet( eOld );
200 
201  if( m_bShowProgress )
203 
204  return ERRCODE_NONE;
205 }
206 
207 void GetASCWriter( const OUString& rFltNm, const OUString& /*rBaseURL*/, WriterRef& xRet )
208 {
209  xRet = new SwASCWriter( rFltNm );
210 }
211 
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
rtl_TextEncoding GetCharSet() const
Definition: shellio.hxx:76
sal_uLong GetIndex() const
Definition: node.hxx:282
sal_uLong Count() const
Definition: ndarr.hxx:143
bool GetIncludeBOM() const
Definition: shellio.hxx:85
const OUString & GetText() const
Definition: ndtxt.hxx:211
SwDocShell * GetDocShell()
Definition: doc.hxx:1340
SwPaM * m_pCurrentPam
Definition: shellio.hxx:404
SwNodeIndex nNode
Definition: pam.hxx:37
virtual ~SwASCWriter() override
Definition: wrtasc.cxx:84
const SwPosition * GetMark() const
Definition: pam.hxx:209
void GetASCWriter(const OUString &rFltNm, const OUString &, WriterRef &xRet)
Definition: wrtasc.cxx:207
SwNode & GetNode() const
Definition: ndindex.hxx:118
bool m_bASCII_ParaAsCR
Definition: shellio.hxx:409
void EndProgress(SwDocShell const *pDocShell)
Definition: mainwn.cxx:88
void SetParaFlags(LineEnd eVal)
Definition: shellio.hxx:83
LINEEND_CR
const SwFrameFormats * GetSpzFrameFormats() const
Definition: doc.hxx:738
bool m_bWriteAll
Definition: shellio.hxx:405
void StartProgress(const char *pMessResId, long nStartValue, long nEndValue, SwDocShell *pDocShell)
Definition: mainwn.cxx:48
SwIndex nContent
Definition: pam.hxx:38
bool m_bASCII_ParaAsBlank
Definition: shellio.hxx:410
sal_uLong GetIndex() const
Definition: ndindex.hxx:151
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:77
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:403
bool Move(SwMoveFnCollection const &fnMove=fnMoveForward, SwGoInDoc fnGo=GoInContent)
Movement of cursor.
Definition: pam.cxx:483
Style of a layout element.
Definition: frmfmt.hxx:57
LINEEND_LF
bool GoInNode(SwPaM &rPam, SwMoveFnCollection const &fnMove)
Definition: pam.cxx:894
const SwPosition * GetPoint() const
Definition: pam.hxx:207
void Exchange()
Definition: pam.cxx:471
SW_DLLPUBLIC Writer & Out(const SwAttrFnTab, const SfxPoolItem &, Writer &)
Definition: wrt_fn.cxx:32
static SwPaM * NewSwPaM(SwDoc &rDoc, sal_uLong const nStartIdx, sal_uLong const nEndIdx)
Definition: writer.cxx:194
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:412
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:380
#define ERRCODE_NONE
SwMoveFnCollection const & fnMoveForward
SwPam::Move()/Find() default argument.
Definition: paminit.cxx:59
void SetEndian(SvStreamEndian SvStreamEndian)
SvStream & WriteUChar(unsigned char nChar)
sal_Int32 GetIndex() const
Definition: index.hxx:95
SwNodes & GetNodes()
Definition: doc.hxx:403
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
SvStream & Strm()
Definition: writer.cxx:218
void SetAsciiOptions(const SwAsciiOptions &rOpt)
Definition: shellio.hxx:436
const SwAsciiOptions & GetAsciiOptions() const
Definition: shellio.hxx:435
SwNode & GetEndOfExtras() const
This is the last EndNode of a special section.
Definition: ndarr.hxx:162
SwDoc * m_pDoc
Definition: shellio.hxx:402
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:843
SwASCWriter(const OUString &rFilterName)
Definition: wrtasc.cxx:34
bool m_bShowProgress
Definition: shellio.hxx:406