Get the style color and number just once
[LibreOffice.git] / sc / source / filter / lotus / filter.cxx
blob0546c46d772aa83fab33591770507c32190666db
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 // Discover WKS, WK1 and WK3; s.a op.cpp
22 #include <map>
24 #include <filter.hxx>
25 #include <document.hxx>
26 #include <docsh.hxx>
27 #include <scerrors.hxx>
29 #include <optab.h>
30 #include <op.h>
31 #include <scmem.h>
32 #include <decl.h>
33 #include <fprogressbar.hxx>
34 #include "lotfilter.hxx"
35 #include <tools/stream.hxx>
37 static ErrCode
38 generate_Opcodes(LotusContext &rContext, SvStream& aStream,
39 ScfStreamProgressBar& aPrgrsBar)
41 OPCODE_FKT *pOps = nullptr;
42 int nOps = 0;
44 ErrCode nErr = ERRCODE_NONE;
46 switch (rContext.eTyp)
48 case eWK_1:
49 case eWK_2:
50 pOps = LotusContext::pOpFkt;
51 nOps = FKT_LIMIT;
52 break;
53 case eWK123:
54 pOps = LotusContext::pOpFkt123;
55 nOps = FKT_LIMIT123;
56 break;
57 case eWK3:
58 nErr = SCERR_IMPORT_NI;
59 break;
60 case eWK_Error:
61 nErr = SCERR_IMPORT_FORMAT;
62 break;
63 default:
64 nErr = SCERR_IMPORT_UNKNOWN_WK;
65 break;
68 if (nErr != ERRCODE_NONE)
70 MemDelete(rContext);
71 return nErr;
74 // #i76299# seems that SvStream::IsEof() does not work correctly
75 sal_uInt64 const nStrmSize = aStream.TellEnd();
76 aStream.Seek( STREAM_SEEK_TO_BEGIN );
77 while (!rContext.bEOF && aStream.good() && (aStream.Tell() < nStrmSize))
79 sal_uInt16 nOpcode(LOTUS_EOF), nLength(0);
81 aStream.ReadUInt16(nOpcode).ReadUInt16(nLength);
82 if (!aStream.good())
83 break;
85 aPrgrsBar.Progress();
86 if( nOpcode == LOTUS_EOF )
87 rContext.bEOF = true;
88 else if( nOpcode == LOTUS_FILEPASSWD )
90 nErr = SCERR_IMPORT_FILEPASSWD;
91 break;
93 else if( nOpcode < nOps )
95 assert(pOps);
96 pOps[ nOpcode ] (rContext, aStream, nLength);
98 else if (rContext.eTyp == eWK123 && nOpcode == LOTUS_PATTERN)
100 // This is really ugly - needs re-factoring ...
101 aStream.SeekRel(nLength);
102 aStream.ReadUInt16( nOpcode ).ReadUInt16( nLength );
103 if ( nOpcode == 0x29a)
105 aStream.SeekRel(nLength);
106 aStream.ReadUInt16( nOpcode ).ReadUInt16( nLength );
107 if ( nOpcode == 0x804 )
109 aStream.SeekRel(nLength);
110 OP_ApplyPatternArea123(rContext, aStream);
112 else
113 aStream.SeekRel(nLength);
115 else
116 aStream.SeekRel(nLength);
118 else
119 aStream.SeekRel( nLength );
122 MemDelete(rContext);
124 if (!aStream.good())
125 nErr = SCERR_IMPORT_FORMAT;
126 else if (nErr == ERRCODE_NONE)
127 rContext.rDoc.CalcAfterLoad();
129 return nErr;
132 static WKTYP ScanVersion(SvStream& aStream)
134 // PREC: pWKFile: pointer to open file
135 // POST: return: type of file
136 sal_uInt16 nOpcode(0), nVersNr(0), nRecLen(0);
138 // first byte has to be 0 because of BOF!
139 aStream.ReadUInt16( nOpcode );
140 if (nOpcode != LotusContext::nBOF)
141 return eWK_UNKNOWN;
143 aStream.ReadUInt16( nRecLen ).ReadUInt16( nVersNr );
145 if (!aStream.good())
146 return eWK_Error;
148 switch( nVersNr )
150 case 0x0404:
151 if( nRecLen == 2 )
152 return eWK_1;
153 else
154 return eWK_UNKNOWN;
156 case 0x0406:
157 if( nRecLen == 2 )
158 return eWK_2;
159 else
160 return eWK_UNKNOWN;
162 case 0x1000:
163 aStream.ReadUInt16( nVersNr );
164 if (!aStream.good())
165 return eWK_Error;
166 if( nVersNr == 0x0004 && nRecLen == 26 )
168 // 4 bytes of 26 read => skip 22 (read instead of seek to make IsEof() work just in case)
169 char aDummy[22];
170 aStream.ReadBytes(aDummy, 22);
171 return !aStream.good() ? eWK_Error : eWK3;
173 break;
174 case 0x1003:
175 if( nRecLen == 0x1a )
176 return eWK123;
177 else
178 return eWK_UNKNOWN;
179 case 0x1005:
180 if( nRecLen == 0x1a )
181 return eWK123;
182 else
183 return eWK_UNKNOWN;
186 return eWK_UNKNOWN;
189 ErrCode ScImportLotus123old(LotusContext& rContext, SvStream& aStream, rtl_TextEncoding eSrc )
191 aStream.Seek( 0 );
193 // make document pointer global
194 rContext.bEOF = false;
195 rContext.eCharset = eSrc;
197 // allocate memory
198 if( !MemNew(rContext) )
199 return SCERR_IMPORT_OUTOFMEM;
201 // initialize page format (only Tab 0!)
202 // initialize page format; meaning: get defaults from SC TODO:
203 //scGetPageFormat( 0, &aPage );
205 // start progressbar
206 ScfStreamProgressBar aPrgrsBar( aStream, rContext.rDoc.GetDocumentShell() );
208 // detect file type
209 rContext.eTyp = ScanVersion(aStream);
210 rContext.aLotusPatternPool.clear();
212 return generate_Opcodes(rContext, aStream, aPrgrsBar);
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */