Line data Source code
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 <sal/config.h>
21 : #include <stdio.h>
22 : #include <sfx2/docfile.hxx>
23 :
24 : #include "qproform.hxx"
25 : #include "qpro.hxx"
26 : #include "qprostyle.hxx"
27 :
28 : #include "global.hxx"
29 : #include "scerrors.hxx"
30 : #include "docpool.hxx"
31 : #include "patattr.hxx"
32 : #include "filter.hxx"
33 : #include "document.hxx"
34 : #include "formulacell.hxx"
35 : #include "biff.hxx"
36 : #include <tools/stream.hxx>
37 : #include <boost/scoped_array.hpp>
38 :
39 2 : FltError ScQProReader::readSheet( SCTAB nTab, ScDocument* pDoc, ScQProStyle *pStyle )
40 : {
41 2 : FltError eRet = eERR_OK;
42 : sal_uInt8 nCol, nDummy;
43 : sal_uInt16 nRow;
44 : sal_uInt16 nStyle;
45 2 : bool bEndOfSheet = false;
46 :
47 : #if OSL_DEBUG_LEVEL > 1
48 : fprintf( stderr, "Read sheet (%d)\n", nTab );
49 : #endif
50 :
51 6 : while( eERR_OK == eRet && !bEndOfSheet && nextRecord() )
52 : {
53 2 : switch( getId() )
54 : {
55 : case 0x000f:{ // Label cell
56 0 : OUString aLabel;
57 0 : mpStream->ReadUChar( nCol ).ReadUChar( nDummy ).ReadUInt16( nRow ).ReadUInt16( nStyle ).ReadUChar( nDummy );
58 0 : sal_uInt16 nLen = getLength();
59 0 : if (nLen >= 7)
60 : {
61 0 : readString( aLabel, nLen - 7 );
62 0 : nStyle = nStyle >> 3;
63 0 : pStyle->SetFormat( pDoc, nCol, nRow, nTab, nStyle );
64 0 : pDoc->EnsureTable(nTab);
65 0 : pDoc->SetTextCell(ScAddress(nCol,nRow,nTab), aLabel);
66 : }
67 : else
68 0 : eRet = eERR_FORMAT;
69 : }
70 0 : break;
71 :
72 : case 0x00cb: // End of sheet
73 0 : bEndOfSheet = true;
74 0 : break;
75 :
76 : case 0x000c: // Blank cell
77 0 : mpStream->ReadUChar( nCol ).ReadUChar( nDummy ).ReadUInt16( nRow ).ReadUInt16( nStyle );
78 0 : nStyle = nStyle >> 3;
79 0 : pStyle->SetFormat( pDoc, nCol, nRow, nTab, nStyle );
80 0 : break;
81 :
82 : case 0x000d:{ // Integer cell
83 : sal_Int16 nValue;
84 0 : mpStream->ReadUChar( nCol ).ReadUChar( nDummy ).ReadUInt16( nRow ).ReadUInt16( nStyle ).ReadInt16( nValue );
85 0 : nStyle = nStyle >> 3;
86 0 : pStyle->SetFormat( pDoc, nCol, nRow, nTab, nStyle );
87 0 : pDoc->EnsureTable(nTab);
88 0 : pDoc->SetValue(ScAddress(nCol,nRow,nTab), static_cast<double>(nValue));
89 : }
90 0 : break;
91 :
92 : case 0x000e:{ // Floating point cell
93 : double nValue;
94 0 : mpStream->ReadUChar( nCol ).ReadUChar( nDummy ).ReadUInt16( nRow ).ReadUInt16( nStyle ).ReadDouble( nValue );
95 0 : nStyle = nStyle >> 3;
96 0 : pStyle->SetFormat( pDoc, nCol, nRow, nTab, nStyle );
97 0 : pDoc->EnsureTable(nTab);
98 0 : pDoc->SetValue(ScAddress(nCol,nRow,nTab), nValue);
99 : }
100 0 : break;
101 :
102 : case 0x0010:
103 : {
104 : // Formula cell
105 : double nValue;
106 : sal_uInt16 nState, nLen;
107 2 : mpStream->ReadUChar( nCol ).ReadUChar( nDummy ).ReadUInt16( nRow ).ReadUInt16( nStyle ).ReadDouble( nValue ).ReadUInt16( nState ).ReadUInt16( nLen );
108 2 : ScAddress aAddr( nCol, nRow, nTab );
109 : const ScTokenArray *pArray;
110 :
111 2 : QProToSc aConv(*mpStream, pDoc->GetSharedStringPool(), aAddr);
112 2 : if (ConvOK != aConv.Convert( pArray, nLen ))
113 2 : eRet = eERR_FORMAT;
114 : else
115 : {
116 0 : ScFormulaCell* pFormula = new ScFormulaCell(pDoc, aAddr, *pArray);
117 0 : nStyle = nStyle >> 3;
118 0 : pFormula->AddRecalcMode( RECALCMODE_ONLOAD_ONCE );
119 0 : pStyle->SetFormat( pDoc, nCol, nRow, nTab, nStyle );
120 0 : pDoc->EnsureTable(nTab);
121 0 : pDoc->SetFormulaCell(ScAddress(nCol,nRow,nTab), pFormula);
122 2 : }
123 : }
124 2 : break;
125 : }
126 : }
127 2 : return eRet;
128 : }
129 :
130 6 : FltError ScFormatFilterPluginImpl::ScImportQuattroPro( SfxMedium &rMedium, ScDocument *pDoc )
131 : {
132 6 : ScQProReader aReader( rMedium );
133 6 : FltError eRet = aReader.import( pDoc );
134 6 : return eRet;
135 : }
136 :
137 6 : ScQProReader::ScQProReader( SfxMedium &rMedium ):
138 6 : ScBiffReader( rMedium )
139 : {
140 6 : }
141 :
142 6 : FltError ScQProReader::import( ScDocument *pDoc )
143 : {
144 6 : FltError eRet = eERR_OK;
145 : sal_uInt16 nVersion;
146 6 : sal_uInt16 i = 1, j = 1;
147 6 : SCTAB nTab = 0;
148 6 : SetEof( false );
149 :
150 6 : if( !recordsLeft() )
151 0 : return eERR_OPEN;
152 :
153 6 : ScQProStyle *pStyleElement = new ScQProStyle;
154 :
155 20024 : while( nextRecord() && eRet == eERR_OK)
156 : {
157 20012 : switch( getId() )
158 : {
159 : case 0x0000: // Begginning of file
160 10 : mpStream->ReadUInt16( nVersion );
161 10 : break;
162 :
163 : case 0x00ca: // Beginning of sheet
164 2 : if( nTab <= MAXTAB )
165 : {
166 2 : if( nTab < 26 )
167 : {
168 2 : OUString aName;
169 2 : aName += OUString( sal_Unicode( 'A' + nTab ) );
170 2 : if (!nTab)
171 2 : pDoc->RenameTab( nTab, aName, false, false);
172 : else
173 0 : pDoc->InsertTab( nTab, aName );
174 : }
175 2 : eRet = readSheet( nTab, pDoc, pStyleElement );
176 2 : nTab++;
177 : }
178 2 : break;
179 :
180 : case 0x0001: // End of file
181 0 : SetEof( true );
182 0 : break;
183 :
184 : case 0x00ce:{ // Attribute cell
185 : sal_uInt8 nFormat, nAlign, nFont;
186 : sal_Int16 nColor;
187 10000 : mpStream->ReadUChar( nFormat ).ReadUChar( nAlign ).ReadInt16( nColor ).ReadUChar( nFont );
188 10000 : pStyleElement->setAlign( i, nAlign );
189 10000 : pStyleElement->setFont( i, nFont );
190 10000 : i++;
191 : }
192 10000 : break;
193 :
194 : case 0x00cf:{ // Font description
195 : sal_uInt16 nPtSize, nFontAttr;
196 10000 : OUString aLabel;
197 10000 : mpStream->ReadUInt16( nPtSize ).ReadUInt16( nFontAttr );
198 10000 : pStyleElement->setFontRecord( j, nFontAttr, nPtSize );
199 10000 : sal_uInt16 nLen = getLength();
200 10000 : if (nLen >= 4)
201 10000 : readString( aLabel, nLen - 4 );
202 : else
203 0 : eRet = eERR_FORMAT;
204 10000 : pStyleElement->setFontType( j, aLabel );
205 10000 : j++;
206 : }
207 10000 : break;
208 : }
209 : }
210 6 : pDoc->CalcAfterLoad();
211 6 : delete pStyleElement;
212 6 : return eRet;
213 : }
214 :
215 6 : bool ScQProReader::recordsLeft()
216 : {
217 6 : bool bValue = ScBiffReader::recordsLeft();
218 6 : return bValue;
219 : }
220 :
221 20020 : bool ScQProReader::nextRecord()
222 : {
223 20020 : bool bValue = ScBiffReader::nextRecord();
224 20020 : return bValue;
225 : }
226 :
227 10000 : void ScQProReader::readString( OUString &rString, sal_uInt16 nLength )
228 : {
229 10000 : boost::scoped_array<sal_Char> pText(new sal_Char[ nLength + 1 ]);
230 10000 : nLength = mpStream->Read(pText.get(), nLength);
231 10000 : pText[ nLength ] = 0;
232 10000 : rString = OUString( pText.get(), strlen(pText.get()), mpStream->GetStreamCharSet() );
233 10048 : }
234 :
235 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|