LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sc / source / filter / lotus / lotread.cxx
blob240ac323cebbac8307f6a7f4edab3450dab426df
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>
23 #include <scdll.hxx>
24 #include <scerrors.hxx>
25 #include "lotfilter.hxx"
26 #include <lotimpop.hxx>
27 #include <lotattr.hxx>
28 #include <fprogressbar.hxx>
30 #include <sal/log.hxx>
32 ErrCode ImportLotus::parse()
34 enum STATE
36 S_START, // analyse first BOF
37 S_WK3, // in WK3-Section
38 S_WK4,
39 S_FM3,
40 S_END // Import finished
43 sal_uInt16 nOp;
44 sal_uInt16 nRecLen;
45 sal_uInt32 nNextRec = 0;
46 ErrCode eRet = ERRCODE_NONE;
47 // ScFormulaCell *pLastFormCell;
49 STATE eCurrent = S_START;
51 nTab = 0;
52 nExtTab = -2;
54 pIn->Seek( nNextRec );
56 // start progressbar
57 ScfStreamProgressBar aPrgrsBar( *pIn, rD.GetDocumentShell() );
58 LotusContext &rContext = aConv.getContext();
59 while( eCurrent != S_END )
61 pIn->ReadUInt16( nOp ).ReadUInt16( nRecLen );
63 if (!pIn->good() || nNextRec > SAL_MAX_UINT32 - nRecLen - 4)
65 eRet = SCERR_IMPORT_FORMAT;
66 eCurrent = S_END;
67 if (!pIn->good())
68 break; // while
71 nNextRec += nRecLen + 4;
73 switch( eCurrent )
76 case S_START: // S_START
77 if( nOp )
79 eRet = SCERR_IMPORT_UNKNOWN_WK;
80 eCurrent = S_END;
82 else
84 if( nRecLen > 2 )
86 Bof();
87 switch (rContext.eFirstType)
89 case Lotus123Typ::WK3: eCurrent = S_WK3; break;
90 case Lotus123Typ::WK4: eCurrent = S_WK4; break;
91 default:
92 eRet = SCERR_IMPORT_UNKNOWN_WK;
93 eCurrent = S_END;
96 else
98 eCurrent = S_END; // TODO: add here something for <= WK1!
99 eRet = ErrCode(0xFFFFFFFF);
102 break;
104 case S_WK3: // S_WK3
105 case S_WK4: // S_WK4
106 switch( nOp )
108 case 0x0001: // EOF
109 eCurrent = S_FM3;
110 nTab++;
111 break;
113 case 0x0002: // PASSWORD
114 eRet = SCERR_IMPORT_FILEPASSWD;
115 eCurrent = S_END;
116 break;
118 case 0x0007: // COLUMNWIDTH
119 Columnwidth( nRecLen );
120 break;
122 case 0x0008: // HIDDENCOLUMN
123 Hiddencolumn( nRecLen );
124 break;
126 case 0x0009: // USERRANGE
127 Userrange();
128 break;
130 case 0x0013: // FORMAT
132 break;
133 case 0x0014: // ERRCELL
134 Errcell();
135 break;
137 case 0x0015: // NACELL
138 Nacell();
139 break;
141 case 0x0016: // LABELCELL
142 Labelcell();
143 break;
145 case 0x0017: // NUMBERCELL
146 Numbercell();
147 break;
149 case 0x0018: // SMALLNUMCELL
150 Smallnumcell();
151 break;
153 case 0x0019: // FORMULACELL
154 Formulacell( nRecLen );
155 break;
157 case 0x001b: // extended attributes
158 if (nRecLen > 2)
160 sal_uInt16 nSubType(0);
161 Read( nSubType );
162 nRecLen -= 2;
163 switch( nSubType )
165 case 2007: // ROW PRESENTATION
166 RowPresentation( nRecLen );
167 break;
169 case 14000: // NAMED SHEET
170 NamedSheet();
171 break;
174 else
176 eRet = SCERR_IMPORT_FORMAT;
177 eCurrent = S_END;
181 break;
183 case S_FM3: // S_FM3
184 break;
186 case S_END: // S_END
187 break;
190 SAL_WARN_IF( nNextRec < pIn->Tell(), "sc.filter",
191 "*ImportLotus::Read(): Read too much..." );
193 pIn->Seek( nNextRec );
194 aPrgrsBar.Progress();
197 // TODO: eliminate stupid names
198 SCTAB nTabs = rD.GetTableCount();
199 SCTAB nCnt;
200 OUString aTabName;
201 OUString aBaseName;
202 if( nTabs != 0 )
204 if( nTabs > 1 )
206 rD.GetName( 0, aBaseName );
207 aBaseName = aBaseName.copy(0, aBaseName.getLength()-1);
209 for( nCnt = 1 ; nCnt < nTabs ; nCnt++ )
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" )
216 aTabName = aBaseName;
217 rD.CreateValidTabName( aTabName );
218 rD.RenameTab( nCnt, aTabName );
223 return eRet;
226 ErrCode ImportLotus::Read()
228 ErrCode eRet = parse();
229 rD.CalcAfterLoad();
230 return eRet;
233 ErrCode ImportLotus::Read(SvStream& rIn)
235 pIn = &rIn;
237 bool bRead = true;
238 sal_uInt16 nOp;
239 sal_uInt16 nRecLen;
240 sal_uInt32 nNextRec = 0;
241 ErrCode eRet = ERRCODE_NONE;
243 nTab = 0;
244 nExtTab = -1;
246 pIn->Seek( nNextRec );
248 // start progressbar
249 ScfStreamProgressBar aPrgrsBar( *pIn, rD.GetDocumentShell() );
250 LotusContext &rContext = aConv.getContext();
251 while( bRead )
253 pIn->ReadUInt16( nOp ).ReadUInt16( nRecLen );
255 if (!pIn->good() || nNextRec > SAL_MAX_UINT32 - nRecLen - 4)
256 bRead = false;
257 else
259 nNextRec += nRecLen + 4;
261 switch( nOp )
263 case 0x0000: // BOF
264 if( nRecLen != 26 || !BofFm3() )
266 bRead = false;
267 eRet = SCERR_IMPORT_FORMAT;
269 break;
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;
278 case 174: // FONT_FACE
279 Font_Face();
280 break;
282 case 176: // FONT_TYPE
283 Font_Type();
284 break;
286 case 177: // FONT_YSIZE
287 Font_Ysize();
288 break;
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;
300 SAL_WARN_IF( nNextRec < pIn->Tell(), "sc.filter",
301 "*ImportLotus::Read(): Read too much..." );
302 pIn->Seek( nNextRec );
303 aPrgrsBar.Progress();
307 rContext.maAttrTable.Apply(rContext, static_cast<SCTAB>(nExtTab));
309 return eRet;
312 extern "C" SAL_DLLPUBLIC_EXPORT bool TestImportWKS(SvStream& rStream)
314 ScDLL::Init();
315 ScDocument aDocument;
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);
323 LotusContext aContext(aDocument, RTL_TEXTENCODING_ASCII_US);
324 ImportLotus aLotusImport(aContext, rStream, RTL_TEXTENCODING_ASCII_US);
326 ErrCode eRet = aLotusImport.parse();
327 if (eRet == ErrCode(0xFFFFFFFF))
329 rStream.Seek(0);
330 eRet = ScImportLotus123old(aContext, rStream, RTL_TEXTENCODING_ASCII_US);
333 return eRet == ERRCODE_NONE;
336 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */