Get the style color and number just once
[LibreOffice.git] / sc / source / filter / lotus / lotread.cxx
blob4696156ffa307a452640726a13727b3a51d4b9fa
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <document.hxx>
21 #include <docoptio.hxx>
22 #include <docsh.hxx>
24 #include <scdll.hxx>
25 #include <scerrors.hxx>
26 #include "lotfilter.hxx"
27 #include <lotimpop.hxx>
28 #include <lotattr.hxx>
29 #include <fprogressbar.hxx>
31 #include <sal/log.hxx>
33 ErrCode ImportLotus::parse()
35 enum STATE
37 S_START, // analyse first BOF
38 S_WK3, // in WK3-Section
39 S_WK4,
40 S_FM3,
41 S_END // Import finished
44 sal_uInt16 nOp;
45 sal_uInt16 nRecLen;
46 sal_uInt32 nNextRec = 0;
47 ErrCode eRet = ERRCODE_NONE;
48 // ScFormulaCell *pLastFormCell;
50 STATE eCurrent = S_START;
52 nTab = 0;
53 nExtTab = -2;
55 pIn->Seek( nNextRec );
57 // start progressbar
58 ScfStreamProgressBar aPrgrsBar( *pIn, rD.GetDocumentShell() );
59 LotusContext &rContext = aConv.getContext();
60 while( eCurrent != S_END )
62 pIn->ReadUInt16( nOp ).ReadUInt16( nRecLen );
64 if (!pIn->good() || nNextRec > SAL_MAX_UINT32 - nRecLen - 4)
66 eRet = SCERR_IMPORT_FORMAT;
67 eCurrent = S_END;
68 if (!pIn->good())
69 break; // while
72 nNextRec += nRecLen + 4;
74 switch( eCurrent )
77 case S_START: // S_START
78 if( nOp )
80 eRet = SCERR_IMPORT_UNKNOWN_WK;
81 eCurrent = S_END;
83 else
85 if( nRecLen > 2 )
87 Bof();
88 switch (rContext.eFirstType)
90 case Lotus123Typ::WK3: eCurrent = S_WK3; break;
91 case Lotus123Typ::WK4: eCurrent = S_WK4; break;
92 default:
93 eRet = SCERR_IMPORT_UNKNOWN_WK;
94 eCurrent = S_END;
97 else
99 eCurrent = S_END; // TODO: add here something for <= WK1!
100 eRet = ErrCode(0xFFFFFFFF);
103 break;
105 case S_WK3: // S_WK3
106 case S_WK4: // S_WK4
107 switch( nOp )
109 case 0x0001: // EOF
110 eCurrent = S_FM3;
111 nTab++;
112 break;
114 case 0x0002: // PASSWORD
115 eRet = SCERR_IMPORT_FILEPASSWD;
116 eCurrent = S_END;
117 break;
119 case 0x0007: // COLUMNWIDTH
120 Columnwidth( nRecLen );
121 break;
123 case 0x0008: // HIDDENCOLUMN
124 Hiddencolumn( nRecLen );
125 break;
127 case 0x0009: // USERRANGE
128 Userrange();
129 break;
131 case 0x0013: // FORMAT
133 break;
134 case 0x0014: // ERRCELL
135 Errcell();
136 break;
138 case 0x0015: // NACELL
139 Nacell();
140 break;
142 case 0x0016: // LABELCELL
143 Labelcell();
144 break;
146 case 0x0017: // NUMBERCELL
147 Numbercell();
148 break;
150 case 0x0018: // SMALLNUMCELL
151 Smallnumcell();
152 break;
154 case 0x0019: // FORMULACELL
155 Formulacell( nRecLen );
156 break;
158 case 0x001b: // extended attributes
159 if (nRecLen > 2)
161 sal_uInt16 nSubType(0);
162 Read( nSubType );
163 nRecLen -= 2;
164 switch( nSubType )
166 case 2007: // ROW PRESENTATION
167 RowPresentation( nRecLen );
168 break;
170 case 14000: // NAMED SHEET
171 NamedSheet();
172 break;
175 else
177 eRet = SCERR_IMPORT_FORMAT;
178 eCurrent = S_END;
182 break;
184 case S_FM3: // S_FM3
185 break;
187 case S_END: // S_END
188 break;
191 SAL_WARN_IF( nNextRec < pIn->Tell(), "sc.filter",
192 "*ImportLotus::Read(): Read too much..." );
194 pIn->Seek( nNextRec );
195 aPrgrsBar.Progress();
198 // TODO: eliminate stupid names
199 SCTAB nTabs = rD.GetTableCount();
200 SCTAB nCnt;
201 OUString aTabName;
202 OUString aBaseName;
203 if( nTabs != 0 )
205 if( nTabs > 1 )
207 rD.GetName( 0, aBaseName );
208 aBaseName = aBaseName.copy(0, aBaseName.getLength()-1);
210 for( nCnt = 1 ; nCnt < nTabs ; nCnt++ )
212 SAL_WARN_IF( !rD.HasTable( nCnt ), "sc.filter",
213 "-ImportLotus::Read(): Where is my table?!" );
214 rD.GetName( nCnt, aTabName );
215 if( aTabName == "temp" )
217 aTabName = aBaseName;
218 rD.CreateValidTabName( aTabName );
219 rD.RenameTab( nCnt, aTabName );
224 return eRet;
227 ErrCode ImportLotus::Read()
229 ErrCode eRet = parse();
230 rD.CalcAfterLoad();
231 return eRet;
234 ErrCode ImportLotus::Read(SvStream& rIn)
236 pIn = &rIn;
238 bool bRead = true;
239 sal_uInt16 nOp;
240 sal_uInt16 nRecLen;
241 sal_uInt32 nNextRec = 0;
242 ErrCode eRet = ERRCODE_NONE;
244 nTab = 0;
245 nExtTab = -1;
247 pIn->Seek( nNextRec );
249 // start progressbar
250 ScfStreamProgressBar aPrgrsBar( *pIn, rD.GetDocumentShell() );
251 LotusContext &rContext = aConv.getContext();
252 while( bRead )
254 pIn->ReadUInt16( nOp ).ReadUInt16( nRecLen );
256 if (!pIn->good() || nNextRec > SAL_MAX_UINT32 - nRecLen - 4)
257 bRead = false;
258 else
260 nNextRec += nRecLen + 4;
262 switch( nOp )
264 case 0x0000: // BOF
265 if( nRecLen != 26 || !BofFm3() )
267 bRead = false;
268 eRet = SCERR_IMPORT_FORMAT;
270 break;
272 case 0x0001: // EOF
273 bRead = false;
274 SAL_WARN_IF( nTab != 0, "sc.filter",
275 "-ImportLotus::Read( SvStream& ): EOF twice!" );
276 nTab++;
277 break;
279 case 174: // FONT_FACE
280 Font_Face();
281 break;
283 case 176: // FONT_TYPE
284 Font_Type();
285 break;
287 case 177: // FONT_YSIZE
288 Font_Ysize();
289 break;
291 case 195:
292 if( nExtTab >= 0 )
293 rContext.maAttrTable.Apply(rContext, static_cast<SCTAB>(nExtTab));
294 nExtTab++;
295 break;
296 case 197:
297 Row_( nRecLen );
298 break;
301 SAL_WARN_IF( nNextRec < pIn->Tell(), "sc.filter",
302 "*ImportLotus::Read(): Read too much..." );
303 pIn->Seek( nNextRec );
304 aPrgrsBar.Progress();
308 rContext.maAttrTable.Apply(rContext, static_cast<SCTAB>(nExtTab));
310 return eRet;
313 extern "C" SAL_DLLPUBLIC_EXPORT bool TestImportWKS(SvStream& rStream)
315 ScDLL::Init();
316 ScDocument aDocument;
317 ScDocOptions aDocOpt = aDocument.GetDocOptions();
318 aDocOpt.SetLookUpColRowNames(false);
319 aDocument.SetDocOptions(aDocOpt);
320 aDocument.MakeTable(0);
321 aDocument.EnableExecuteLink(false);
322 aDocument.SetInsertingFromOtherDoc(true);
324 LotusContext aContext(aDocument, RTL_TEXTENCODING_ASCII_US);
325 ImportLotus aLotusImport(aContext, rStream, RTL_TEXTENCODING_ASCII_US);
327 ErrCode eRet = aLotusImport.parse();
328 if (eRet == ErrCode(0xFFFFFFFF))
330 rStream.Seek(0);
331 eRet = ScImportLotus123old(aContext, rStream, RTL_TEXTENCODING_ASCII_US);
334 return eRet == ERRCODE_NONE;
337 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */