LibreOffice Module sc (master)  1
lotread.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 <document.hxx>
21 #include <docoptio.hxx>
22 
23 #include <scdll.hxx>
24 #include <scerrors.hxx>
25 #include "lotfilter.hxx"
26 #include <lotimpop.hxx>
27 #include <lotattr.hxx>
28 #include <fprogressbar.hxx>
29 
30 #include <sal/log.hxx>
31 
33 {
34  enum STATE
35  {
36  S_START, // analyse first BOF
37  S_WK3, // in WK3-Section
38  S_WK4,
39  S_FM3,
40  S_END // Import finished
41  };
42 
43  sal_uInt16 nOp;
44  sal_uInt16 nSubType;
45  sal_uInt16 nRecLen;
46  sal_uInt32 nNextRec = 0;
47  ErrCode eRet = ERRCODE_NONE;
48 // ScFormulaCell *pLastFormCell;
49 
50  STATE eCurrent = S_START;
51 
52  nTab = 0;
53  nExtTab = -2;
54 
55  pIn->Seek( nNextRec );
56 
57  // start progressbar
59  LotusContext &rContext = aConv.getContext();
60  while( eCurrent != S_END )
61  {
62  pIn->ReadUInt16( nOp ).ReadUInt16( nRecLen );
63 
64  if (!pIn->good() || nNextRec > SAL_MAX_UINT32 - nRecLen - 4)
65  {
66  eRet = SCERR_IMPORT_FORMAT;
67  eCurrent = S_END;
68  if (!pIn->good())
69  break; // while
70  }
71 
72  nNextRec += nRecLen + 4;
73 
74  switch( eCurrent )
75  {
76 
77  case S_START: // S_START
78  if( nOp )
79  {
81  eCurrent = S_END;
82  }
83  else
84  {
85  if( nRecLen > 2 )
86  {
87  Bof();
88  switch (rContext.eFirstType)
89  {
90  case Lotus123Typ::WK3: eCurrent = S_WK3; break;
91  case Lotus123Typ::WK4: eCurrent = S_WK4; break;
92  default:
94  eCurrent = S_END;
95  }
96  }
97  else
98  {
99  eCurrent = S_END; // TODO: add here something for <= WK1!
100  eRet = ErrCode(0xFFFFFFFF);
101  }
102  }
103  break;
104 
105  case S_WK3: // S_WK3
106  case S_WK4: // S_WK4
107  switch( nOp )
108  {
109  case 0x0001: // EOF
110  eCurrent = S_FM3;
111  nTab++;
112  break;
113 
114  case 0x0002: // PASSWORD
116  eCurrent = S_END;
117  break;
118 
119  case 0x0007: // COLUMNWIDTH
120  Columnwidth( nRecLen );
121  break;
122 
123  case 0x0008: // HIDDENCOLUMN
124  Hiddencolumn( nRecLen );
125  break;
126 
127  case 0x0009: // USERRANGE
128  Userrange();
129  break;
130 
131  case 0x0013: // FORMAT
132 
133  break;
134  case 0x0014: // ERRCELL
135  Errcell();
136  break;
137 
138  case 0x0015: // NACELL
139  Nacell();
140  break;
141 
142  case 0x0016: // LABELCELL
143  Labelcell();
144  break;
145 
146  case 0x0017: // NUMBERCELL
147  Numbercell();
148  break;
149 
150  case 0x0018: // SMALLNUMCELL
151  Smallnumcell();
152  break;
153 
154  case 0x0019: // FORMULACELL
155  Formulacell( nRecLen );
156  break;
157 
158  case 0x001b: // extended attributes
159  if (nRecLen > 2)
160  {
161  Read( nSubType );
162  nRecLen -= 2;
163  switch( nSubType )
164  {
165  case 2007: // ROW PRESENTATION
166  RowPresentation( nRecLen );
167  break;
168 
169  case 14000: // NAMED SHEET
170  NamedSheet();
171  break;
172  }
173  }
174  else
175  {
176  eRet = SCERR_IMPORT_FORMAT;
177  eCurrent = S_END;
178  }
179  }
180 
181  break;
182 
183  case S_FM3: // S_FM3
184  break;
185 
186  case S_END: // S_END
187  break;
188  }
189 
190  SAL_WARN_IF( nNextRec < pIn->Tell(), "sc.filter",
191  "*ImportLotus::Read(): Read too much..." );
192 
193  pIn->Seek( nNextRec );
194  aPrgrsBar.Progress();
195  }
196 
197  // TODO: eliminate stupid names
198  SCTAB nTabs = rD.GetTableCount();
199  SCTAB nCnt;
200  OUString aTabName;
201  OUString aBaseName;
202  if( nTabs != 0 )
203  {
204  if( nTabs > 1 )
205  {
206  rD.GetName( 0, aBaseName );
207  aBaseName = aBaseName.copy(0, aBaseName.getLength()-1);
208  }
209  for( nCnt = 1 ; nCnt < nTabs ; nCnt++ )
210  {
211  SAL_WARN_IF( !rD.HasTable( nCnt ), "sc.filter",
212  "-ImportLotus::Read(): Where is my table?!" );
213  rD.GetName( nCnt, aTabName );
214  if( aTabName == "temp" )
215  {
216  aTabName = aBaseName;
217  rD.CreateValidTabName( aTabName );
218  rD.RenameTab( nCnt, aTabName );
219  }
220  }
221  }
222 
223  return eRet;
224 }
225 
227 {
228  ErrCode eRet = parse();
229  rD.CalcAfterLoad();
230  return eRet;
231 }
232 
234 {
235  pIn = &rIn;
236 
237  bool bRead = true;
238  sal_uInt16 nOp;
239  sal_uInt16 nRecLen;
240  sal_uInt32 nNextRec = 0;
241  ErrCode eRet = ERRCODE_NONE;
242 
243  nTab = 0;
244  nExtTab = -1;
245 
246  pIn->Seek( nNextRec );
247 
248  // start progressbar
249  ScfStreamProgressBar aPrgrsBar( *pIn, rD.GetDocumentShell() );
250  LotusContext &rContext = aConv.getContext();
251  while( bRead )
252  {
253  pIn->ReadUInt16( nOp ).ReadUInt16( nRecLen );
254 
255  if (!pIn->good() || nNextRec > SAL_MAX_UINT32 - nRecLen - 4)
256  bRead = false;
257  else
258  {
259  nNextRec += nRecLen + 4;
260 
261  switch( nOp )
262  {
263  case 0x0000: // BOF
264  if( nRecLen != 26 || !BofFm3() )
265  {
266  bRead = false;
267  eRet = SCERR_IMPORT_FORMAT;
268  }
269  break;
270 
271  case 0x0001: // EOF
272  bRead = false;
273  SAL_WARN_IF( nTab != 0, "sc.filter",
274  "-ImportLotus::Read( SvStream& ): EOF twice!" );
275  nTab++;
276  break;
277 
278  case 174: // FONT_FACE
279  Font_Face();
280  break;
281 
282  case 176: // FONT_TYPE
283  Font_Type();
284  break;
285 
286  case 177: // FONT_YSIZE
287  Font_Ysize();
288  break;
289 
290  case 195:
291  if( nExtTab >= 0 )
292  rContext.maAttrTable.Apply(rContext, static_cast<SCTAB>(nExtTab));
293  nExtTab++;
294  break;
295  case 197:
296  Row_( nRecLen );
297  break;
298  }
299 
300  SAL_WARN_IF( nNextRec < pIn->Tell(), "sc.filter",
301  "*ImportLotus::Read(): Read too much..." );
302  pIn->Seek( nNextRec );
303  aPrgrsBar.Progress();
304  }
305  }
306 
307  rContext.maAttrTable.Apply(rContext, static_cast<SCTAB>(nExtTab));
308 
309  return eRet;
310 }
311 
312 extern "C" SAL_DLLPUBLIC_EXPORT bool TestImportWKS(SvStream& rStream)
313 {
314  ScDLL::Init();
316  ScDocOptions aDocOpt = aDocument.GetDocOptions();
317  aDocOpt.SetLookUpColRowNames(false);
318  aDocument.SetDocOptions(aDocOpt);
319  aDocument.MakeTable(0);
320  aDocument.EnableExecuteLink(false);
321  aDocument.SetInsertingFromOtherDoc(true);
322 
323  LotusContext aContext(aDocument, RTL_TEXTENCODING_ASCII_US);
324  ImportLotus aLotusImport(aContext, rStream, RTL_TEXTENCODING_ASCII_US);
325 
326  ErrCode eRet = aLotusImport.parse();
327  if (eRet == ErrCode(0xFFFFFFFF))
328  {
329  rStream.Seek(0);
330  eRet = ScImportLotus123old(aContext, rStream, RTL_TEXTENCODING_ASCII_US);
331  }
332 
333  return eRet == ERRCODE_NONE;
334 }
335 
336 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void NamedSheet()
Definition: lotimpop.cxx:299
void Font_Ysize()
Definition: lotimpop.cxx:346
A simplified progress bar based on the stream position of an existing stream.
sal_uInt16 nTab
Definition: lotimpop.hxx:37
ScriptDocument aDocument
SvStream & ReadUInt16(sal_uInt16 &rUInt16)
#define SCERR_IMPORT_UNKNOWN_WK
Definition: scerrors.hxx:32
void Row_(const sal_uInt16 nRecLen)
Definition: lotimpop.cxx:357
sal_uInt64 Seek(sal_uInt64 nPos)
void RowPresentation(sal_uInt16 nRecLen)
Definition: lotimpop.cxx:265
#define SAL_MAX_UINT32
void Nacell()
Definition: lotimpop.cxx:182
void Userrange()
Definition: lotimpop.cxx:152
ScDocument & rD
Definition: imp_op.hxx:47
void Formulacell(sal_uInt16 nRecLen)
Definition: lotimpop.cxx:234
SC_DLLPUBLIC bool RenameTab(SCTAB nTab, const OUString &rName, bool bExternalDocument=false)
Definition: document.cxx:851
SC_DLLPUBLIC SCTAB GetTableCount() const
Definition: document.cxx:312
void Numbercell()
Definition: lotimpop.cxx:210
void SetLookUpColRowNames(bool bVal)
Definition: docoptio.hxx:53
SC_DLLPUBLIC bool HasTable(SCTAB nTab) const
Definition: document.cxx:189
void SetInsertingFromOtherDoc(bool bVal)
Definition: document.hxx:2130
SC_DLLPUBLIC const ScDocOptions & GetDocOptions() const
Definition: documen3.cxx:1922
void EnableExecuteLink(bool bVal)
Definition: document.hxx:1536
void Font_Type()
Definition: lotimpop.cxx:335
void Labelcell()
Definition: lotimpop.cxx:194
ErrCode ScImportLotus123old(LotusContext &rContext, SvStream &aStream, rtl_TextEncoding eSrc)
Definition: filter.cxx:185
static SC_DLLPUBLIC void Init()
DLL-init/exit-code must be linked to the DLL only.
Definition: scdll.cxx:99
void Bof()
Definition: lotimpop.cxx:57
SvStream * pIn
Definition: lotimpop.hxx:35
SAL_DLLPUBLIC_EXPORT bool TestImportWKS(SvStream &rStream)
Definition: lotread.cxx:312
SC_DLLPUBLIC void CreateValidTabName(OUString &rName) const
Definition: document.cxx:390
bool BofFm3()
Definition: lotimpop.cxx:85
void Hiddencolumn(sal_uInt16 nRecLen)
Definition: lotimpop.cxx:126
void Smallnumcell()
Definition: lotimpop.cxx:222
void Font_Face()
Definition: lotimpop.cxx:319
SC_DLLPUBLIC void CalcAfterLoad(bool bStartListening=true)
Definition: document.cxx:4024
SC_DLLPUBLIC void SetDocOptions(const ScDocOptions &rOpt)
Definition: documen3.cxx:1928
ErrCode Read()
Definition: lotread.cxx:226
#define SAL_WARN_IF(condition, area, stream)
#define ERRCODE_NONE
sal_Int32 nExtTab
Definition: lotimpop.hxx:38
STATE
#define SCERR_IMPORT_FORMAT
Definition: scerrors.hxx:34
LotusContext & getContext()
Definition: lotform.hxx:95
bool good() const
SfxObjectShell * GetDocumentShell() const
Definition: document.hxx:1055
SC_DLLPUBLIC void MakeTable(SCTAB nTab, bool _bNeedsNameCheck=true)
Definition: document.cxx:166
ErrCode parse()
Definition: lotread.cxx:32
LotusToSc aConv
Definition: lotimpop.hxx:36
void Errcell()
Definition: lotimpop.cxx:170
SC_DLLPUBLIC bool GetName(SCTAB nTab, OUString &rName) const
Definition: document.cxx:211
sal_Int16 SCTAB
Definition: types.hxx:23
void Columnwidth(sal_uInt16 nRecLen)
Definition: lotimpop.cxx:95
#define SCERR_IMPORT_FILEPASSWD
Definition: scerrors.hxx:41