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