LibreOffice Module sc (master)  1
filter.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 // Discover WKS, WK1 and WK3; s.a op.cpp
21 
22 #include <map>
23 
24 #include <filter.hxx>
25 #include <document.hxx>
26 #include <scerrors.hxx>
27 
28 #include <optab.h>
29 #include <op.h>
30 #include <scmem.h>
31 #include <decl.h>
32 #include <fprogressbar.hxx>
33 #include "lotfilter.hxx"
34 #include <tools/stream.hxx>
35 
36 static ErrCode
38  ScfStreamProgressBar& aPrgrsBar)
39 {
40  OPCODE_FKT *pOps = nullptr;
41  int nOps = 0;
42 
43  ErrCode nErr = ERRCODE_NONE;
44 
45  switch (rContext.eTyp)
46  {
47  case eWK_1:
48  case eWK_2:
49  pOps = LotusContext::pOpFkt;
50  nOps = FKT_LIMIT;
51  break;
52  case eWK123:
54  nOps = FKT_LIMIT123;
55  break;
56  case eWK3:
57  nErr = SCERR_IMPORT_NI;
58  break;
59  case eWK_Error:
60  nErr = SCERR_IMPORT_FORMAT;
61  break;
62  default:
64  break;
65  }
66 
67  if (nErr != ERRCODE_NONE)
68  {
69  MemDelete(rContext);
70  return nErr;
71  }
72 
73  // #i76299# seems that SvStream::IsEof() does not work correctly
74  sal_uInt64 const nStrmSize = aStream.TellEnd();
75  aStream.Seek( STREAM_SEEK_TO_BEGIN );
76  while (!rContext.bEOF && aStream.good() && (aStream.Tell() < nStrmSize))
77  {
78  sal_uInt16 nOpcode(LOTUS_EOF), nLength(0);
79 
80  aStream.ReadUInt16(nOpcode).ReadUInt16(nLength);
81  if (!aStream.good())
82  break;
83 
84  aPrgrsBar.Progress();
85  if( nOpcode == LOTUS_EOF )
86  rContext.bEOF = true;
87  else if( nOpcode == LOTUS_FILEPASSWD )
88  {
90  break;
91  }
92  else if( nOpcode < nOps )
93  pOps[ nOpcode ] (rContext, aStream, nLength);
94  else if (rContext.eTyp == eWK123 && nOpcode == LOTUS_PATTERN)
95  {
96  // This is really ugly - needs re-factoring ...
97  aStream.SeekRel(nLength);
98  aStream.ReadUInt16( nOpcode ).ReadUInt16( nLength );
99  if ( nOpcode == 0x29a)
100  {
101  aStream.SeekRel(nLength);
102  aStream.ReadUInt16( nOpcode ).ReadUInt16( nLength );
103  if ( nOpcode == 0x804 )
104  {
105  aStream.SeekRel(nLength);
106  OP_ApplyPatternArea123(rContext, aStream);
107  }
108  else
109  aStream.SeekRel(nLength);
110  }
111  else
112  aStream.SeekRel(nLength);
113  }
114  else
115  aStream.SeekRel( nLength );
116  }
117 
118  MemDelete(rContext);
119 
120  if (!aStream.good())
121  nErr = SCERR_IMPORT_FORMAT;
122  else if (nErr == ERRCODE_NONE)
123  rContext.pDoc->CalcAfterLoad();
124 
125  return nErr;
126 }
127 
128 static WKTYP ScanVersion(SvStream& aStream)
129 {
130  // PREC: pWKFile: pointer to open file
131  // POST: return: type of file
132  sal_uInt16 nOpcode(0), nVersNr(0), nRecLen(0);
133 
134  // first byte has to be 0 because of BOF!
135  aStream.ReadUInt16( nOpcode );
136  if (nOpcode != LotusContext::nBOF)
137  return eWK_UNKNOWN;
138 
139  aStream.ReadUInt16( nRecLen ).ReadUInt16( nVersNr );
140 
141  if (!aStream.good())
142  return eWK_Error;
143 
144  switch( nVersNr )
145  {
146  case 0x0404:
147  if( nRecLen == 2 )
148  return eWK_1;
149  else
150  return eWK_UNKNOWN;
151 
152  case 0x0406:
153  if( nRecLen == 2 )
154  return eWK_2;
155  else
156  return eWK_UNKNOWN;
157 
158  case 0x1000:
159  aStream.ReadUInt16( nVersNr );
160  if (!aStream.good())
161  return eWK_Error;
162  if( nVersNr == 0x0004 && nRecLen == 26 )
163  {
164  // 4 bytes of 26 read => skip 22 (read instead of seek to make IsEof() work just in case)
165  sal_Char aDummy[22];
166  aStream.ReadBytes(aDummy, 22);
167  return !aStream.good() ? eWK_Error : eWK3;
168  }
169  break;
170  case 0x1003:
171  if( nRecLen == 0x1a )
172  return eWK123;
173  else
174  return eWK_UNKNOWN;
175  case 0x1005:
176  if( nRecLen == 0x1a )
177  return eWK123;
178  else
179  return eWK_UNKNOWN;
180  }
181 
182  return eWK_UNKNOWN;
183 }
184 
185 ErrCode ScImportLotus123old(LotusContext& rContext, SvStream& aStream, ScDocument* pDocument, rtl_TextEncoding eSrc )
186 {
187  aStream.Seek( 0 );
188 
189  // make document pointer global
190  rContext.pDoc = pDocument;
191  rContext.bEOF = false;
192  rContext.eCharVon = eSrc;
193 
194  // allocate memory
195  if( !MemNew(rContext) )
196  return SCERR_IMPORT_OUTOFMEM;
197 
198  // initialize page format (only Tab 0!)
199  // initialize page format; meaning: get defaults from SC TODO:
200  //scGetPageFormat( 0, &aPage );
201 
202  // start progressbar
203  ScfStreamProgressBar aPrgrsBar( aStream, pDocument->GetDocumentShell() );
204 
205  // detect file type
206  rContext.eTyp = ScanVersion(aStream);
207  rContext.aLotusPatternPool.clear();
208 
209  return generate_Opcodes(rContext, aStream, aPrgrsBar);
210 }
211 
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool MemNew(LotusContext &rContext)
Definition: memory.cxx:30
Definition: decl.h:24
#define LOTUS_FILEPASSWD
Definition: optab.h:37
void OP_ApplyPatternArea123(LotusContext &rContext, SvStream &r)
Definition: op.cxx:602
A simplified progress bar based on the stream position of an existing stream.
static OPCODE_FKT pOpFkt[FKT_LIMIT]
Definition: lotfilter.hxx:39
Definition: decl.h:24
static WKTYP ScanVersion(SvStream &aStream)
Definition: filter.cxx:128
SvStream & ReadUInt16(sal_uInt16 &rUInt16)
void MemDelete(LotusContext &rContext)
Definition: memory.cxx:44
virtual sal_uInt64 TellEnd()
static const sal_uInt16 nBOF
Definition: lotfilter.hxx:34
#define SCERR_IMPORT_UNKNOWN_WK
Definition: scerrors.hxx:32
static ErrCode generate_Opcodes(LotusContext &rContext, SvStream &aStream, ScfStreamProgressBar &aPrgrsBar)
Definition: filter.cxx:37
Definition: decl.h:24
sal_uInt64 Seek(sal_uInt64 nPos)
#define LOTUS_EOF
Definition: optab.h:35
char sal_Char
static OPCODE_FKT pOpFkt123[FKT_LIMIT123]
Definition: lotfilter.hxx:40
#define SCERR_IMPORT_NI
Definition: scerrors.hxx:36
ErrCode ScImportLotus123old(LotusContext &rContext, SvStream &aStream, ScDocument *pDocument, rtl_TextEncoding eSrc)
Definition: filter.cxx:185
#define STREAM_SEEK_TO_BEGIN
Definition: decl.h:24
void Progress()
Sets the progress bar to the current stream position.
#define FKT_LIMIT123
Definition: optab.h:33
void(* OPCODE_FKT)(LotusContext &rContext, SvStream &rStream, sal_uInt16 nLength)
Definition: optab.h:29
#define SCERR_IMPORT_OUTOFMEM
Definition: scerrors.hxx:30
WKTYP
Definition: decl.h:24
SC_DLLPUBLIC void CalcAfterLoad(bool bStartListening=true)
Definition: document.cxx:4033
std::size_t ReadBytes(void *pData, std::size_t nSize)
#define ERRCODE_NONE
#define LOTUS_PATTERN
Definition: optab.h:39
sal_uInt64 Tell() const
ScDocument * pDoc
Definition: lotfilter.hxx:38
#define SCERR_IMPORT_FORMAT
Definition: scerrors.hxx:34
bool good() const
rtl_TextEncoding eCharVon
Definition: lotfilter.hxx:37
SfxObjectShell * GetDocumentShell() const
Definition: document.hxx:1030
sal_Int32 const nLength
Definition: decl.h:24
std::map< sal_uInt16, ScPatternAttr > aLotusPatternPool
Definition: lotfilter.hxx:42
#define FKT_LIMIT
Definition: optab.h:31
#define SCERR_IMPORT_FILEPASSWD
Definition: scerrors.hxx:41