/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ #include #include #include #include #include #include #include "scitems.hxx" #include "patattr.hxx" #include "docpool.hxx" #include #include #include #include #include #include "formulacell.hxx" #include "rangenam.hxx" #include "document.hxx" #include "postit.hxx" #include "op.h" #include "optab.h" #include "tool.h" #include "decl.h" #include "lotform.hxx" #include "lotrange.hxx" #include "root.hxx" #include "ftools.hxx" #include #include #include extern WKTYP eTyp; // -> filter.cxx, aktueller Dateityp extern bool bEOF; // -> filter.cxx, zeigt Dateiende an extern sal_uInt8 nDefaultFormat; // -> tool.cxx, Default-Zellenformat extern ScDocument* pDoc; // -> filter.cxx, Aufhaenger zum Dokumentzugriff extern rtl_TextEncoding eCharVon; // -> filter.cxx, character set specified static sal_uInt16 nDefWidth = ( sal_uInt16 ) ( TWIPS_PER_CHAR * 10 ); extern std::map aLotusPatternPool; void NI( SvStream& r, sal_uInt16 n ) { r.SeekRel( n ); } void OP_BOF( SvStream& r, sal_uInt16 /*n*/ ) { r.SeekRel( 2 ); // Versionsnummer ueberlesen } void OP_EOF( SvStream& /*r*/, sal_uInt16 /*n*/ ) { bEOF = true; } void OP_Integer( SvStream& r, sal_uInt16 /*n*/ ) { sal_uInt8 nFormat; sal_uInt16 nCol, nRow; SCTAB nTab = 0; sal_Int16 nValue; r.ReadUChar( nFormat ).ReadUInt16( nCol ).ReadUInt16( nRow ).ReadInt16( nValue ); if (ValidColRow( static_cast(nCol), nRow)) { pDoc->EnsureTable(nTab); pDoc->SetValue(ScAddress(nCol,nRow,nTab), static_cast(nValue)); // 0 Stellen nach'm Komma! SetFormat( static_cast (nCol), static_cast (nRow), nTab, nFormat, 0 ); } } void OP_Number( SvStream& r, sal_uInt16 /*n*/ ) { sal_uInt8 nFormat; sal_uInt16 nCol, nRow; SCTAB nTab = 0; double fValue; r.ReadUChar( nFormat ).ReadUInt16( nCol ).ReadUInt16( nRow ).ReadDouble( fValue ); if (ValidColRow( static_cast(nCol), nRow)) { fValue = ::rtl::math::round( fValue, 15 ); pDoc->EnsureTable(nTab); pDoc->SetValue(ScAddress(nCol,nRow,nTab), fValue); SetFormat( static_cast (nCol), static_cast (nRow), nTab, nFormat, nDezFloat ); } } void OP_Label( SvStream& r, sal_uInt16 n ) { sal_uInt8 nFormat; sal_uInt16 nCol, nRow; SCTAB nTab = 0; r.ReadUChar( nFormat ).ReadUInt16( nCol ).ReadUInt16( nRow ); n -= (n > 5) ? 5 : n; boost::scoped_array pText(new sal_Char[n + 1]); r.Read( pText.get(), n ); pText[n] = 0; if (ValidColRow( static_cast(nCol), nRow)) { nFormat &= 0x80; // Bit 7 belassen nFormat |= 0x75; // protected egal, special-text gesetzt PutFormString( static_cast (nCol), static_cast (nRow), nTab, pText.get() ); SetFormat( static_cast (nCol), static_cast (nRow), nTab, nFormat, nDezStd ); } } void OP_Formula( SvStream& r, sal_uInt16 /*n*/ ) { sal_uInt8 nFormat; sal_uInt16 nCol, nRow, nFormulaSize; SCTAB nTab = 0; r.ReadUChar( nFormat ).ReadUInt16( nCol ).ReadUInt16( nRow ); r.SeekRel( 8 ); // Ergebnis ueberspringen r.ReadUInt16( nFormulaSize ); const ScTokenArray* pErg; sal_Int32 nBytesLeft = nFormulaSize; ScAddress aAddress( static_cast (nCol), static_cast (nRow), nTab ); svl::SharedStringPool& rSPool = pLotusRoot->pDoc->GetSharedStringPool(); LotusToSc aConv(r, rSPool, pLotusRoot->eCharsetQ, false); aConv.Reset( aAddress ); aConv.Convert( pErg, nBytesLeft ); if (ValidColRow( static_cast(nCol), nRow)) { ScFormulaCell* pCell = new ScFormulaCell(pLotusRoot->pDoc, aAddress, *pErg); pCell->AddRecalcMode( RECALCMODE_ONLOAD_ONCE ); pDoc->EnsureTable(nTab); pDoc->SetFormulaCell(ScAddress(nCol,nRow,nTab), pCell); // nFormat = Standard -> Nachkommastellen wie Float SetFormat( static_cast (nCol), static_cast (nRow), nTab, nFormat, nDezFloat ); } } void OP_ColumnWidth( SvStream& r, sal_uInt16 /*n*/ ) { sal_uInt16 nCol, nBreite; sal_uInt8 nWidthSpaces; SCTAB nTab = 0; r.ReadUInt16( nCol ).ReadUChar( nWidthSpaces ); if (ValidCol( static_cast(nCol))) { if( nWidthSpaces ) // Annahme: 10cpi-Zeichensatz nBreite = ( sal_uInt16 ) ( TWIPS_PER_CHAR * nWidthSpaces ); else { pDoc->SetColHidden(static_cast(nCol), static_cast(nCol), 0, true); nBreite = nDefWidth; } pDoc->SetColWidth( static_cast (nCol), nTab, nBreite ); } } void OP_NamedRange( SvStream& r, sal_uInt16 /*n*/ ) { // POST: waren Koordinaten ungueltig, wird nicht gespeichert sal_uInt16 nColSt, nRowSt, nColEnd, nRowEnd; sal_Char cPuffer[ 16+1 ]; r.Read( cPuffer, 16 ); cPuffer[ 16 ] = 0; r.ReadUInt16( nColSt ).ReadUInt16( nRowSt ).ReadUInt16( nColEnd ).ReadUInt16( nRowEnd ); if (ValidColRow( static_cast(nColSt), nRowSt) && ValidColRow( static_cast(nColEnd), nRowEnd)) { LotusRange* pRange; if( nColSt == nColEnd && nRowSt == nRowEnd ) pRange = new LotusRange( static_cast (nColSt), static_cast (nRowSt) ); else pRange = new LotusRange( static_cast (nColSt), static_cast (nRowSt), static_cast (nColEnd), static_cast (nRowEnd) ); sal_Char cBuf[sizeof(cPuffer)+1]; if( isdigit( *cPuffer ) ) { // erstes Zeichen im Namen eine Zahl -> 'A' vor Namen setzen cBuf[0] = 'A'; strcpy( cBuf + 1, cPuffer ); // #100211# - checked } else strcpy( cBuf, cPuffer ); // #100211# - checked OUString aTmp( cBuf, strlen(cBuf), pLotusRoot->eCharsetQ ); aTmp = ScfTools::ConvertToScDefinedName( aTmp ); pLotusRoot->pRangeNames->Append( pRange, aTmp ); } } void OP_SymphNamedRange( SvStream& r, sal_uInt16 /*n*/ ) { // POST: waren Koordinaten ungueltig, wird nicht gespeichert sal_uInt16 nColSt, nRowSt, nColEnd, nRowEnd; sal_uInt8 nType; sal_Char cPuffer[ 16+1 ]; r.Read( cPuffer, 16 ); cPuffer[ 16 ] = 0; r.ReadUInt16( nColSt ).ReadUInt16( nRowSt ).ReadUInt16( nColEnd ).ReadUInt16( nRowEnd ).ReadUChar( nType ); if (ValidColRow( static_cast(nColSt), nRowSt) && ValidColRow( static_cast(nColEnd), nRowEnd)) { LotusRange* pRange; if( nType ) pRange = new LotusRange( static_cast (nColSt), static_cast (nRowSt) ); else pRange = new LotusRange( static_cast (nColSt), static_cast (nRowSt), static_cast (nColEnd), static_cast (nRowEnd) ); sal_Char cBuf[sizeof(cPuffer)+1]; if( isdigit( *cPuffer ) ) { // erstes Zeichen im Namen eine Zahl -> 'A' vor Namen setzen cBuf[0] = 'A'; strcpy( cBuf + 1, cPuffer ); // #100211# - checked } else strcpy( cBuf, cPuffer ); // #100211# - checked OUString aTmp( cBuf, strlen(cBuf), pLotusRoot->eCharsetQ ); aTmp = ScfTools::ConvertToScDefinedName( aTmp ); pLotusRoot->pRangeNames->Append( pRange, aTmp ); } } void OP_Footer( SvStream& r, sal_uInt16 n ) { r.SeekRel( n ); } void OP_Header( SvStream& r, sal_uInt16 n ) { r.SeekRel( n ); } void OP_Margins( SvStream& r, sal_uInt16 n ) { r.SeekRel( n ); } void OP_HiddenCols( SvStream& r, sal_uInt16 /*n*/ ) { sal_uInt16 nByte, nBit; SCCOL nCount; sal_uInt8 nAkt; nCount = 0; for( nByte = 0 ; nByte < 32 ; nByte++ ) // 32 Bytes mit ... { r.ReadUChar( nAkt ); for( nBit = 0 ; nBit < 8 ; nBit++ ) // ...jeweils 8 Bits = 256 Bits { if( nAkt & 0x01 ) // unterstes Bit gesetzt? // -> Hidden Col pDoc->SetColHidden(nCount, nCount, 0, true); nCount++; nAkt = nAkt / 2; // der Naechste bitte... } } } void OP_Window1( SvStream& r, sal_uInt16 n ) { r.SeekRel( 4 ); // Cursor Pos ueberspringen r.ReadUChar( nDefaultFormat ); r.SeekRel( 1 ); // 'unused' ueberspringen r.ReadUInt16( nDefWidth ); r.SeekRel( n - 8 ); // und den Rest ueberspringen nDefWidth = ( sal_uInt16 ) ( TWIPS_PER_CHAR * nDefWidth ); // statt Defaulteinstellung in SC alle Cols zu Fuss setzen for( SCCOL nCol = 0 ; nCol <= MAXCOL ; nCol++ ) pDoc->SetColWidth( nCol, 0, nDefWidth ); } void OP_Blank( SvStream& r, sal_uInt16 /*n*/ ) { sal_uInt16 nCol, nRow; sal_uInt8 nFormat; r.ReadUChar( nFormat ).ReadUInt16( nCol ).ReadUInt16( nRow ); SetFormat( static_cast (nCol), static_cast (nRow), 0, nFormat, nDezFloat ); } void OP_BOF123( SvStream& r, sal_uInt16 /*n*/ ) { r.SeekRel( 26 ); } void OP_EOF123( SvStream& /*r*/, sal_uInt16 /*n*/ ) { bEOF = true; } void OP_Label123( SvStream& r, sal_uInt16 n ) { sal_uInt8 nTab, nCol; sal_uInt16 nRow; r.ReadUInt16( nRow ).ReadUChar( nTab ).ReadUChar( nCol ); n -= (n > 4) ? 4 : n; boost::scoped_array pText(new sal_Char[n + 1]); r.Read( pText.get(), n ); pText[ n ] = 0; PutFormString( static_cast(nCol), static_cast(nRow), static_cast(nTab), pText.get() ); } void OP_Number123( SvStream& r, sal_uInt16 /*n*/ ) { sal_uInt8 nCol,nTab; sal_uInt16 nRow; sal_uInt32 nValue; r.ReadUInt16( nRow ).ReadUChar( nTab ).ReadUChar( nCol ).ReadUInt32( nValue ); if (ValidColRow( static_cast(nCol), nRow) && nTab <= pDoc->GetMaxTableNumber()) { double fValue = Snum32ToDouble( nValue ); pDoc->EnsureTable(nTab); pDoc->SetValue(ScAddress(nCol,nRow,nTab), fValue); } } void OP_Formula123( SvStream& r, sal_uInt16 n ) { sal_uInt8 nCol,nTab; sal_uInt16 nRow; r.ReadUInt16( nRow ).ReadUChar( nTab ).ReadUChar( nCol ); r.SeekRel( 8 ); // Result- jump over const ScTokenArray* pErg; sal_Int32 nBytesLeft = (n > 12) ? n - 12 : 0; ScAddress aAddress( nCol, nRow, nTab ); svl::SharedStringPool& rSPool = pLotusRoot->pDoc->GetSharedStringPool(); LotusToSc aConv(r, rSPool, pLotusRoot->eCharsetQ, true); aConv.Reset( aAddress ); aConv.Convert( pErg, nBytesLeft ); if (ValidColRow( static_cast(nCol), nRow) && nTab <= pDoc->GetMaxTableNumber()) { ScFormulaCell* pCell = new ScFormulaCell(pLotusRoot->pDoc, aAddress, *pErg); pCell->AddRecalcMode( RECALCMODE_ONLOAD_ONCE ); pDoc->EnsureTable(nTab); pDoc->SetFormulaCell(ScAddress(nCol,nRow,nTab), pCell); } } void OP_IEEENumber123( SvStream& r, sal_uInt16 /*n*/ ) { sal_uInt8 nCol,nTab; sal_uInt16 nRow; double dValue; r.ReadUInt16( nRow ).ReadUChar( nTab ).ReadUChar( nCol ).ReadDouble( dValue ); if (ValidColRow( static_cast(nCol), nRow) && nTab <= pDoc->GetMaxTableNumber()) { pDoc->EnsureTable(nTab); pDoc->SetValue(ScAddress(nCol,nRow,nTab), dValue); } } void OP_Note123( SvStream& r, sal_uInt16 n) { sal_uInt8 nTab, nCol; sal_uInt16 nRow; r.ReadUInt16( nRow ).ReadUChar( nTab ).ReadUChar( nCol ); n -= (n > 4) ? 4 : n; boost::scoped_array pText(new sal_Char[n + 1]); r.Read( pText.get(), n ); pText[ n ] = 0; OUString aNoteText(pText.get(), strlen(pText.get()), pLotusRoot->eCharsetQ); pText.reset(); ScAddress aPos( static_cast(nCol), static_cast(nRow), static_cast(nTab) ); ScNoteUtil::CreateNoteFromString( *pDoc, aPos, aNoteText, false, false ); } void OP_HorAlign123( sal_uInt8 nAlignPattern, SfxItemSet& rPatternItemSet ) { // pre: Pattern is stored in the last 3 bites of the 21st byte // post: Appropriate Horizontal Alignement is set in rPattern according to the bit pattern. // // LEFT:001, RIGHT:010, CENTER:011, JUSTIFY:110, // LEFT-Text/RIGHT-NUMBER:100, DEFAULT:000 nAlignPattern = ( nAlignPattern & 0x07); switch (nAlignPattern) { case 1: rPatternItemSet.Put( SvxHorJustifyItem( SVX_HOR_JUSTIFY_LEFT, ATTR_HOR_JUSTIFY ) ); break; case 2: rPatternItemSet.Put( SvxHorJustifyItem( SVX_HOR_JUSTIFY_RIGHT, ATTR_HOR_JUSTIFY ) ); break; case 3: rPatternItemSet.Put( SvxHorJustifyItem( SVX_HOR_JUSTIFY_CENTER, ATTR_HOR_JUSTIFY) ); break; case 4: rPatternItemSet.Put( SvxHorJustifyItem( SVX_HOR_JUSTIFY_STANDARD, ATTR_HOR_JUSTIFY ) ); break; case 6: rPatternItemSet.Put( SvxHorJustifyItem( SVX_HOR_JUSTIFY_BLOCK, ATTR_HOR_JUSTIFY ) ); break; default: rPatternItemSet.Put( SvxHorJustifyItem( SVX_HOR_JUSTIFY_STANDARD, ATTR_HOR_JUSTIFY ) ); break; } } void OP_VerAlign123( sal_uInt8 nAlignPattern,SfxItemSet& rPatternItemSet ) { // pre: Pattern is stored in the last 3 bites of the 22nd byte // post: Appropriate Verticle Alignement is set in rPattern according to the bit pattern. // // TOP:001, MIDDLE:010, DOWN:100, DEFAULT:000 nAlignPattern = ( nAlignPattern & 0x07); switch (nAlignPattern) { case 0: rPatternItemSet.Put( SvxVerJustifyItem(SVX_VER_JUSTIFY_STANDARD, ATTR_VER_JUSTIFY) ); break; case 1: rPatternItemSet.Put( SvxVerJustifyItem(SVX_VER_JUSTIFY_TOP, ATTR_VER_JUSTIFY) ); break; case 2: rPatternItemSet.Put( SvxVerJustifyItem(SVX_VER_JUSTIFY_CENTER, ATTR_VER_JUSTIFY) ); break; case 4: rPatternItemSet.Put( SvxVerJustifyItem(SVX_VER_JUSTIFY_BOTTOM, ATTR_VER_JUSTIFY) ); break; default: rPatternItemSet.Put( SvxVerJustifyItem(SVX_VER_JUSTIFY_STANDARD, ATTR_VER_JUSTIFY) ); break; } } void OP_CreatePattern123( SvStream& r, sal_uInt16 n) { sal_uInt16 nCode,nPatternId; ScPatternAttr aPattern(pDoc->GetPool()); SfxItemSet& rItemSet = aPattern.GetItemSet(); r.ReadUInt16( nCode ); n -= (n > 2) ? 2 : n; if ( nCode == 0x0fd2 ) { r.ReadUInt16( nPatternId ); sal_uInt8 Hor_Align, Ver_Align, temp; bool bIsBold,bIsUnderLine,bIsItalics; r.SeekRel(12); // Read 17th Byte r.ReadUChar( temp ); bIsBold = (temp & 0x01); bIsItalics = (temp & 0x02); bIsUnderLine = (temp & 0x04); if ( bIsBold ) rItemSet.Put( SvxWeightItem(WEIGHT_BOLD,ATTR_FONT_WEIGHT) ); if ( bIsItalics ) rItemSet.Put( SvxPostureItem(ITALIC_NORMAL, ATTR_FONT_POSTURE ) ); if ( bIsUnderLine ) rItemSet.Put( SvxUnderlineItem( UNDERLINE_SINGLE, ATTR_FONT_UNDERLINE ) ); r.SeekRel(3); // Read 21st Byte r.ReadUChar( Hor_Align ); OP_HorAlign123( Hor_Align, rItemSet ); r.ReadUChar( Ver_Align ); OP_VerAlign123( Ver_Align, rItemSet ); aLotusPatternPool.insert( std::map::value_type( nPatternId, aPattern ) ); n -= (n > 20) ? 20 : n; } r.SeekRel(n); } void OP_SheetName123( SvStream& rStream, sal_uInt16 nLength ) { if (nLength <= 4) { rStream.SeekRel(nLength); return; } // B0 36 [sheet number (2 bytes?)] [sheet name (null terminated char array)] sal_uInt16 nDummy; rStream.ReadUInt16( nDummy ); // ignore the first 2 bytes (B0 36). rStream.ReadUInt16( nDummy ); SCTAB nSheetNum = static_cast(nDummy); pDoc->MakeTable(nSheetNum); ::std::vector sSheetName; sSheetName.reserve(nLength-4); for (sal_uInt16 i = 4; i < nLength; ++i) { sal_Char c; rStream.ReadChar( c ); sSheetName.push_back(c); } if (!sSheetName.empty()) { OUString aName(&sSheetName[0], strlen(&sSheetName[0]), eCharVon); pDoc->RenameTab(nSheetNum, aName); } } void OP_ApplyPatternArea123( SvStream& rStream ) { sal_uInt16 nOpcode, nLength; sal_uInt16 nCol = 0, nColCount = 0, nRow = 0, nRowCount = 0, nTab = 0, nData, nTabCount = 0, nLevel = 0; do { rStream.ReadUInt16( nOpcode ).ReadUInt16( nLength ); switch ( nOpcode ) { case ROW_FORMAT_MARKER: nLevel++; break; case COL_FORMAT_MARKER: nLevel--; if( nLevel == 1 ) { nTab = nTab + nTabCount; nCol = 0; nColCount = 0; nRow = 0; nRowCount = 0; } break; case LOTUS_FORMAT_INDEX: if( nLength >= 2 ) { rStream.ReadUInt16( nData ); rStream.SeekRel( nLength - 2 ); if( nLevel == 1 ) nTabCount = nData; else if( nLevel == 2 ) { nCol = nCol + nColCount; nColCount = nData; if ( nCol > 0xff ) // 256 is the max col size supported by 123 nCol = 0; } else if( nLevel == 3 ) { nRow = nRow + nRowCount; nRowCount = nData; if ( nRow > 0x1fff ) // 8192 is the max row size supported by 123 nRow = 0; } } else rStream.SeekRel( nLength ); break; case LOTUS_FORMAT_INFO: if( nLength >= 2 ) { rStream.ReadUInt16( nData ); rStream.SeekRel( nLength - 2 ); std::map::iterator loc = aLotusPatternPool.find( nData ); // #126338# apparently, files with invalid index occur in the wild -> don't crash then if ( loc != aLotusPatternPool.end() ) for( int i = 0; i < nTabCount; i++) { pDoc->ApplyPatternAreaTab( nCol, nRow, nCol + nColCount - 1, nRow + nRowCount - 1, static_cast< SCTAB >( nTab + i ), loc->second ); } } else rStream.SeekRel( nLength ); break; default: rStream.SeekRel( nLength ); break; } } while( nLevel && !rStream.IsEof() ); aLotusPatternPool.clear(); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */