Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sc / source / filter / excel / xihelper.cxx
blobdf62f997daa9fdadff9eca7b99f19d18e4ebe7c9
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 <memory>
21 #include <xihelper.hxx>
22 #include <svl/itemset.hxx>
23 #include <svl/sharedstringpool.hxx>
24 #include <editeng/editobj.hxx>
25 #include <tools/urlobj.hxx>
26 #include <editeng/eeitem.hxx>
27 #include <editeng/flditem.hxx>
28 #include <document.hxx>
29 #include <rangelst.hxx>
30 #include <editutil.hxx>
31 #include <attrib.hxx>
32 #include <xltracer.hxx>
33 #include <xistream.hxx>
34 #include <xistring.hxx>
35 #include <xistyle.hxx>
36 #include <excform.hxx>
37 #include <scmatrix.hxx>
38 #include <documentimport.hxx>
39 #include <sal/log.hxx>
41 // Excel->Calc cell address/range conversion ==================================
43 namespace {
45 /** Fills the passed Calc address with the passed Excel cell coordinates without checking any limits. */
46 void lclFillAddress( ScAddress& rScPos, sal_uInt16 nXclCol, sal_uInt32 nXclRow, SCTAB nScTab )
48 rScPos.SetCol( static_cast< SCCOL >( nXclCol ) );
49 rScPos.SetRow( static_cast< SCROW >( nXclRow ) );
50 rScPos.SetTab( nScTab );
53 } // namespace
55 XclImpAddressConverter::XclImpAddressConverter( const XclImpRoot& rRoot ) :
56 XclAddressConverterBase( rRoot.GetTracer(), rRoot.GetScMaxPos() )
60 // cell address ---------------------------------------------------------------
62 bool XclImpAddressConverter::CheckAddress( const XclAddress& rXclPos, bool bWarn )
64 bool bValidCol = rXclPos.mnCol <= mnMaxCol;
65 bool bValidRow = rXclPos.mnRow <= mnMaxRow;
66 bool bValid = bValidCol && bValidRow;
67 if( !bValid && bWarn )
69 mbColTrunc |= !bValidCol;
70 mbRowTrunc |= !bValidRow;
71 mrTracer.TraceInvalidAddress( ScAddress(
72 static_cast< SCCOL >( rXclPos.mnCol ), static_cast< SCROW >( rXclPos.mnRow ), 0 ), maMaxPos );
74 return bValid;
77 bool XclImpAddressConverter::ConvertAddress( ScAddress& rScPos,
78 const XclAddress& rXclPos, SCTAB nScTab, bool bWarn )
80 bool bValid = CheckAddress( rXclPos, bWarn );
81 if( bValid )
82 lclFillAddress( rScPos, rXclPos.mnCol, rXclPos.mnRow, nScTab );
83 return bValid;
86 ScAddress XclImpAddressConverter::CreateValidAddress(
87 const XclAddress& rXclPos, SCTAB nScTab, bool bWarn )
89 ScAddress aScPos( ScAddress::UNINITIALIZED );
90 if( !ConvertAddress( aScPos, rXclPos, nScTab, bWarn ) )
92 aScPos.SetCol( static_cast< SCCOL >( ::std::min( rXclPos.mnCol, mnMaxCol ) ) );
93 aScPos.SetRow( static_cast< SCROW >( ::std::min( rXclPos.mnRow, mnMaxRow ) ) );
94 aScPos.SetTab( limit_cast< SCTAB >( nScTab, 0, maMaxPos.Tab() ) );
96 return aScPos;
99 // cell range -----------------------------------------------------------------
101 bool XclImpAddressConverter::ConvertRange( ScRange& rScRange,
102 const XclRange& rXclRange, SCTAB nScTab1, SCTAB nScTab2, bool bWarn )
104 // check start position
105 bool bValidStart = CheckAddress( rXclRange.maFirst, bWarn );
106 if( bValidStart )
108 lclFillAddress( rScRange.aStart, rXclRange.maFirst.mnCol, rXclRange.maFirst.mnRow, nScTab1 );
110 // check & correct end position
111 sal_uInt16 nXclCol2 = rXclRange.maLast.mnCol;
112 sal_uInt32 nXclRow2 = rXclRange.maLast.mnRow;
113 if( !CheckAddress( rXclRange.maLast, bWarn ) )
115 nXclCol2 = ::std::min( nXclCol2, mnMaxCol );
116 nXclRow2 = ::std::min( nXclRow2, mnMaxRow );
118 lclFillAddress( rScRange.aEnd, nXclCol2, nXclRow2, nScTab2 );
120 return bValidStart;
123 // cell range list ------------------------------------------------------------
125 void XclImpAddressConverter::ConvertRangeList( ScRangeList& rScRanges,
126 const XclRangeList& rXclRanges, SCTAB nScTab, bool bWarn )
128 rScRanges.RemoveAll();
129 for( const auto& rXclRange : rXclRanges )
131 ScRange aScRange( ScAddress::UNINITIALIZED );
132 if( ConvertRange( aScRange, rXclRange, nScTab, nScTab, bWarn ) )
133 rScRanges.push_back( aScRange );
137 // String->EditEngine conversion ==============================================
139 namespace {
141 std::unique_ptr<EditTextObject> lclCreateTextObject( const XclImpRoot& rRoot,
142 const XclImpString& rString, XclFontItemType eType, sal_uInt16 nXFIndex )
144 std::unique_ptr<EditTextObject> pTextObj;
146 const XclImpXFBuffer& rXFBuffer = rRoot.GetXFBuffer();
147 const XclImpFont* pFirstFont = rXFBuffer.GetFont( nXFIndex );
148 bool bFirstEscaped = pFirstFont && pFirstFont->HasEscapement();
150 if( rString.IsRich() || bFirstEscaped )
152 const XclImpFontBuffer& rFontBuffer = rRoot.GetFontBuffer();
153 const XclFormatRunVec& rFormats = rString.GetFormats();
155 ScEditEngineDefaulter& rEE = rRoot.GetEditEngine();
156 rEE.SetText( rString.GetText() );
158 SfxItemSet aItemSet( rEE.GetEmptyItemSet() );
159 if( bFirstEscaped )
160 rFontBuffer.FillToItemSet( aItemSet, eType, rXFBuffer.GetFontIndex( nXFIndex ) );
161 ESelection aSelection;
163 XclFormatRun aNextRun;
164 XclFormatRunVec::const_iterator aIt = rFormats.begin();
165 XclFormatRunVec::const_iterator aEnd = rFormats.end();
167 if( aIt != aEnd )
168 aNextRun = *aIt++;
169 else
170 aNextRun.mnChar = 0xFFFF;
172 sal_Int32 nLen = rString.GetText().getLength();
173 for( sal_Int32 nChar = 0; nChar < nLen; ++nChar )
175 // reached new different formatted text portion
176 if( nChar >= aNextRun.mnChar )
178 // send items to edit engine
179 rEE.QuickSetAttribs( aItemSet, aSelection );
181 // start new item set
182 aItemSet.ClearItem();
183 rFontBuffer.FillToItemSet( aItemSet, eType, aNextRun.mnFontIdx );
185 // read new formatting information
186 if( aIt != aEnd )
187 aNextRun = *aIt++;
188 else
189 aNextRun.mnChar = 0xFFFF;
191 // reset selection start to current position
192 aSelection.nStartPara = aSelection.nEndPara;
193 aSelection.nStartPos = aSelection.nEndPos;
196 // set end of selection to current position
197 if( rString.GetText()[ nChar ] == '\n' )
199 ++aSelection.nEndPara;
200 aSelection.nEndPos = 0;
202 else
203 ++aSelection.nEndPos;
206 // send items of last text portion to edit engine
207 rEE.QuickSetAttribs( aItemSet, aSelection );
209 pTextObj = rEE.CreateTextObject();
212 return pTextObj;
215 } // namespace
217 std::unique_ptr<EditTextObject> XclImpStringHelper::CreateTextObject(
218 const XclImpRoot& rRoot, const XclImpString& rString )
220 return lclCreateTextObject( rRoot, rString, XclFontItemType::Editeng, 0 );
223 void XclImpStringHelper::SetToDocument(
224 ScDocumentImport& rDoc, const ScAddress& rPos, const XclImpRoot& rRoot,
225 const XclImpString& rString, sal_uInt16 nXFIndex )
227 if (rString.GetText().isEmpty())
228 return;
230 ::std::unique_ptr< EditTextObject > pTextObj( lclCreateTextObject( rRoot, rString, XclFontItemType::Editeng, nXFIndex ) );
232 if (pTextObj)
234 rDoc.setEditCell(rPos, std::move(pTextObj));
236 else
238 const OUString& aStr = rString.GetText();
239 if (aStr.indexOf('\n') != -1 || aStr.indexOf('\r') != -1)
241 // Multiline content.
242 ScFieldEditEngine& rEngine = rDoc.getDoc().GetEditEngine();
243 rEngine.SetText(aStr);
244 rDoc.setEditCell(rPos, rEngine.CreateTextObject());
246 else
248 // Normal text cell.
249 rDoc.setStringCell(rPos, aStr);
254 // Header/footer conversion ===================================================
256 XclImpHFConverter::XclImpHFPortionInfo::XclImpHFPortionInfo() :
257 mnHeight( 0 ),
258 mnMaxLineHt( 0 )
260 maSel.nStartPara = maSel.nEndPara = 0;
261 maSel.nStartPos = maSel.nEndPos = 0;
264 XclImpHFConverter::XclImpHFConverter( const XclImpRoot& rRoot ) :
265 XclImpRoot( rRoot ),
266 mrEE( rRoot.GetHFEditEngine() ),
267 mxFontData( new XclFontData ),
268 meCurrObj( EXC_HF_CENTER )
272 XclImpHFConverter::~XclImpHFConverter()
276 void XclImpHFConverter::ParseString( const OUString& rHFString )
278 // edit engine objects
279 mrEE.SetText( EMPTY_OUSTRING );
280 maInfos.clear();
281 maInfos.resize( EXC_HF_PORTION_COUNT );
282 meCurrObj = EXC_HF_CENTER;
284 // parser temporaries
285 maCurrText.truncate();
286 OUStringBuffer aReadFont; // current font name
287 OUStringBuffer aReadStyle; // current font style
288 sal_uInt16 nReadHeight = 0; // current font height
289 ResetFontData();
291 /** State of the parser. */
292 enum XclHFParserState
294 xlPSText, /// Read text, search for functions.
295 xlPSFunc, /// Read function (token following a '&').
296 xlPSFont, /// Read font name ('&' is followed by '"', reads until next '"' or ',').
297 xlPSFontStyle, /// Read font style name (font part after ',', reads until next '"').
298 xlPSHeight /// Read font height ('&' is followed by num. digits, reads until non-digit).
299 } eState = xlPSText;
301 const sal_Unicode* pChar = rHFString.getStr();
302 const sal_Unicode* pNull = pChar + rHFString.getLength(); // pointer to terminating null char
303 while( *pChar )
305 switch( eState )
308 // --- read text character ---
310 case xlPSText:
312 switch( *pChar )
314 case '&': // new command
315 InsertText();
316 eState = xlPSFunc;
317 break;
318 case '\n': // line break
319 InsertText();
320 InsertLineBreak();
321 break;
322 default:
323 maCurrText.append(OUStringChar(*pChar));
326 break;
328 // --- read control sequence ---
330 case xlPSFunc:
332 eState = xlPSText;
333 switch( *pChar )
335 case '&': maCurrText.append("&"); break; // the '&' character
337 case 'L': SetNewPortion( EXC_HF_LEFT ); break; // Left portion
338 case 'C': SetNewPortion( EXC_HF_CENTER ); break; // Center portion
339 case 'R': SetNewPortion( EXC_HF_RIGHT ); break; // Right portion
341 case 'P': InsertField( SvxFieldItem( SvxPageField(), EE_FEATURE_FIELD ) ); break; // page
342 case 'N': InsertField( SvxFieldItem( SvxPagesField(), EE_FEATURE_FIELD ) ); break; // page count
343 case 'D': InsertField( SvxFieldItem( SvxDateField(), EE_FEATURE_FIELD ) ); break; // date
344 case 'T': InsertField( SvxFieldItem( SvxTimeField(), EE_FEATURE_FIELD ) ); break; // time
345 case 'A': InsertField( SvxFieldItem( SvxTableField(), EE_FEATURE_FIELD ) ); break; // table name
347 case 'Z': // file path
348 InsertField( SvxFieldItem( SvxExtFileField(), EE_FEATURE_FIELD ) ); // convert to full name
349 if( (pNull - pChar >= 2) && (*(pChar + 1) == '&') && (*(pChar + 2) == 'F') )
351 // &Z&F found - ignore the &F part
352 pChar += 2;
354 break;
355 case 'F': // file name
356 InsertField( SvxFieldItem( SvxExtFileField( EMPTY_OUSTRING, SvxFileType::Var, SvxFileFormat::NameAndExt ), EE_FEATURE_FIELD ) );
357 break;
359 case 'U': // underline
360 SetAttribs();
361 mxFontData->mnUnderline = (mxFontData->mnUnderline == EXC_FONTUNDERL_SINGLE) ?
362 EXC_FONTUNDERL_NONE : EXC_FONTUNDERL_SINGLE;
363 break;
364 case 'E': // double underline
365 SetAttribs();
366 mxFontData->mnUnderline = (mxFontData->mnUnderline == EXC_FONTUNDERL_DOUBLE) ?
367 EXC_FONTUNDERL_NONE : EXC_FONTUNDERL_DOUBLE;
368 break;
369 case 'S': // strikeout
370 SetAttribs();
371 mxFontData->mbStrikeout = !mxFontData->mbStrikeout;
372 break;
373 case 'X': // superscript
374 SetAttribs();
375 mxFontData->mnEscapem = (mxFontData->mnEscapem == EXC_FONTESC_SUPER) ?
376 EXC_FONTESC_NONE : EXC_FONTESC_SUPER;
377 break;
378 case 'Y': // subsrcipt
379 SetAttribs();
380 mxFontData->mnEscapem = (mxFontData->mnEscapem == EXC_FONTESC_SUB) ?
381 EXC_FONTESC_NONE : EXC_FONTESC_SUB;
382 break;
384 case '\"': // font name
385 aReadFont.setLength(0);
386 aReadStyle.setLength(0);
387 eState = xlPSFont;
388 break;
389 default:
390 if( ('0' <= *pChar) && (*pChar <= '9') ) // font size
392 nReadHeight = *pChar - '0';
393 eState = xlPSHeight;
397 break;
399 // --- read font name ---
401 case xlPSFont:
403 switch( *pChar )
405 case '\"':
406 --pChar;
407 [[fallthrough]];
408 case ',':
409 eState = xlPSFontStyle;
410 break;
411 default:
412 aReadFont.append(*pChar);
415 break;
417 // --- read font style ---
419 case xlPSFontStyle:
421 switch( *pChar )
423 case '\"':
424 SetAttribs();
425 if( !aReadFont.isEmpty() )
426 mxFontData->maName = aReadFont.toString();
427 mxFontData->maStyle = aReadStyle.toString();
428 eState = xlPSText;
429 break;
430 default:
431 aReadStyle.append(*pChar);
434 break;
436 // --- read font height ---
438 case xlPSHeight:
440 if( ('0' <= *pChar) && (*pChar <= '9') )
442 if( nReadHeight != 0xFFFF )
444 nReadHeight *= 10;
445 nReadHeight += (*pChar - '0');
446 if( nReadHeight > 1600 ) // max 1600pt = 32000twips
447 nReadHeight = 0xFFFF;
450 else
452 if( (nReadHeight != 0) && (nReadHeight != 0xFFFF) )
454 SetAttribs();
455 mxFontData->mnHeight = nReadHeight * 20;
457 --pChar;
458 eState = xlPSText;
461 break;
463 ++pChar;
466 // finalize
467 CreateCurrObject();
468 maInfos[ EXC_HF_LEFT ].mnHeight += GetMaxLineHeight( EXC_HF_LEFT );
469 maInfos[ EXC_HF_CENTER ].mnHeight += GetMaxLineHeight( EXC_HF_CENTER );
470 maInfos[ EXC_HF_RIGHT ].mnHeight += GetMaxLineHeight( EXC_HF_RIGHT );
473 void XclImpHFConverter::FillToItemSet( SfxItemSet& rItemSet, sal_uInt16 nWhichId ) const
475 ScPageHFItem aHFItem( nWhichId );
476 if( maInfos[ EXC_HF_LEFT ].mxObj.get() )
477 aHFItem.SetLeftArea( *maInfos[ EXC_HF_LEFT ].mxObj );
478 if( maInfos[ EXC_HF_CENTER ].mxObj.get() )
479 aHFItem.SetCenterArea( *maInfos[ EXC_HF_CENTER ].mxObj );
480 if( maInfos[ EXC_HF_RIGHT ].mxObj.get() )
481 aHFItem.SetRightArea( *maInfos[ EXC_HF_RIGHT ].mxObj );
482 rItemSet.Put( aHFItem );
485 sal_Int32 XclImpHFConverter::GetTotalHeight() const
487 return ::std::max( maInfos[ EXC_HF_LEFT ].mnHeight,
488 ::std::max( maInfos[ EXC_HF_CENTER ].mnHeight, maInfos[ EXC_HF_RIGHT ].mnHeight ) );
491 // private --------------------------------------------------------------------
493 sal_uInt16 XclImpHFConverter::GetMaxLineHeight( XclImpHFPortion ePortion ) const
495 sal_uInt16 nMaxHt = maInfos[ ePortion ].mnMaxLineHt;
496 return (nMaxHt == 0) ? mxFontData->mnHeight : nMaxHt;
499 void XclImpHFConverter::UpdateMaxLineHeight( XclImpHFPortion ePortion )
501 sal_uInt16& rnMaxHt = maInfos[ ePortion ].mnMaxLineHt;
502 rnMaxHt = ::std::max( rnMaxHt, mxFontData->mnHeight );
505 void XclImpHFConverter::UpdateCurrMaxLineHeight()
507 UpdateMaxLineHeight( meCurrObj );
510 void XclImpHFConverter::SetAttribs()
512 ESelection& rSel = GetCurrSel();
513 if( (rSel.nStartPara != rSel.nEndPara) || (rSel.nStartPos != rSel.nEndPos) )
515 SfxItemSet aItemSet( mrEE.GetEmptyItemSet() );
516 XclImpFont aFont( GetRoot(), *mxFontData );
517 aFont.FillToItemSet( aItemSet, XclFontItemType::HeaderFooter );
518 mrEE.QuickSetAttribs( aItemSet, rSel );
519 rSel.nStartPara = rSel.nEndPara;
520 rSel.nStartPos = rSel.nEndPos;
524 void XclImpHFConverter::ResetFontData()
526 if( const XclImpFont* pFirstFont = GetFontBuffer().GetFont( EXC_FONT_APP ) )
527 *mxFontData = pFirstFont->GetFontData();
528 else
530 mxFontData->Clear();
531 mxFontData->mnHeight = 200;
535 void XclImpHFConverter::InsertText()
537 if( !maCurrText.isEmpty() )
539 ESelection& rSel = GetCurrSel();
540 OUString sString(maCurrText.makeStringAndClear());
541 mrEE.QuickInsertText( sString, ESelection( rSel.nEndPara, rSel.nEndPos, rSel.nEndPara, rSel.nEndPos ) );
542 rSel.nEndPos = rSel.nEndPos + sString.getLength();
543 UpdateCurrMaxLineHeight();
547 void XclImpHFConverter::InsertField( const SvxFieldItem& rFieldItem )
549 ESelection& rSel = GetCurrSel();
550 mrEE.QuickInsertField( rFieldItem, ESelection( rSel.nEndPara, rSel.nEndPos, rSel.nEndPara, rSel.nEndPos ) );
551 ++rSel.nEndPos;
552 UpdateCurrMaxLineHeight();
555 void XclImpHFConverter::InsertLineBreak()
557 ESelection& rSel = GetCurrSel();
558 mrEE.QuickInsertText( OUString('\n'), ESelection( rSel.nEndPara, rSel.nEndPos, rSel.nEndPara, rSel.nEndPos ) );
559 ++rSel.nEndPara;
560 rSel.nEndPos = 0;
561 GetCurrInfo().mnHeight += GetMaxLineHeight( meCurrObj );
562 GetCurrInfo().mnMaxLineHt = 0;
565 void XclImpHFConverter::CreateCurrObject()
567 InsertText();
568 SetAttribs();
569 GetCurrObj() = mrEE.CreateTextObject();
572 void XclImpHFConverter::SetNewPortion( XclImpHFPortion eNew )
574 if( eNew != meCurrObj )
576 CreateCurrObject();
577 meCurrObj = eNew;
578 if( GetCurrObj().get() )
579 mrEE.SetText( *GetCurrObj() );
580 else
581 mrEE.SetText( EMPTY_OUSTRING );
582 ResetFontData();
586 // URL conversion =============================================================
588 namespace {
590 void lclAppendUrlChar( OUString& rUrl, sal_Unicode cChar )
592 // encode special characters
593 switch( cChar )
595 case '#': rUrl += "%23"; break;
596 case '%': rUrl += "%25"; break;
597 default: rUrl += OUStringChar( cChar );
601 } // namespace
603 void XclImpUrlHelper::DecodeUrl(
604 OUString& rUrl, OUString& rTabName, bool& rbSameWb,
605 const XclImpRoot& rRoot, const OUString& rEncodedUrl )
607 enum
609 xlUrlInit, /// Initial state, read string mode character.
610 xlUrlPath, /// Read URL path.
611 xlUrlFileName, /// Read file name.
612 xlUrlSheetName, /// Read sheet name.
613 xlUrlRaw /// Raw mode. No control characters will occur.
614 } eState = xlUrlInit;
616 bool bEncoded = true;
617 rbSameWb = false;
619 sal_Unicode cCurrDrive = 0;
620 OUString aDosBase( INetURLObject( rRoot.GetBasePath() ).getFSysPath( FSysStyle::Dos ) );
621 if (!aDosBase.isEmpty() && aDosBase.match(":\\", 1))
622 cCurrDrive = aDosBase[0];
624 const sal_Unicode* pChar = rEncodedUrl.getStr();
625 while( *pChar )
627 switch( eState )
630 // --- first character ---
632 case xlUrlInit:
634 switch( *pChar )
636 case EXC_URLSTART_ENCODED:
637 eState = xlUrlPath;
638 break;
639 case EXC_URLSTART_SELF:
640 case EXC_URLSTART_SELFENCODED:
641 rbSameWb = true;
642 eState = xlUrlSheetName;
643 break;
644 case '[':
645 bEncoded = false;
646 eState = xlUrlFileName;
647 break;
648 default:
649 bEncoded = false;
650 lclAppendUrlChar( rUrl, *pChar );
651 eState = xlUrlPath;
654 break;
656 // --- URL path ---
658 case xlUrlPath:
660 switch( *pChar )
662 case EXC_URL_DOSDRIVE:
664 if( *(pChar + 1) )
666 ++pChar;
667 if( *pChar == '@' )
668 rUrl += "\\\\";
669 else
671 lclAppendUrlChar( rUrl, *pChar );
672 rUrl += ":\\";
675 else
676 rUrl += "<NULL-DRIVE!>";
678 break;
679 case EXC_URL_DRIVEROOT:
680 if( cCurrDrive )
682 lclAppendUrlChar( rUrl, cCurrDrive );
683 rUrl += ":";
685 [[fallthrough]];
686 case EXC_URL_SUBDIR:
687 if( bEncoded )
688 rUrl += "\\";
689 else // control character in raw name -> DDE link
691 rUrl += OUStringChar(EXC_DDE_DELIM);
692 eState = xlUrlRaw;
694 break;
695 case EXC_URL_PARENTDIR:
696 rUrl += "..\\";
697 break;
698 case EXC_URL_RAW:
700 if( *(pChar + 1) )
702 sal_Int32 nLen = *++pChar;
703 for( sal_Int32 nChar = 0; (nChar < nLen) && *(pChar + 1); ++nChar )
704 lclAppendUrlChar( rUrl, *++pChar );
705 // rUrl.Append( ':' );
708 break;
709 case '[':
710 eState = xlUrlFileName;
711 break;
712 default:
713 lclAppendUrlChar( rUrl, *pChar );
716 break;
718 // --- file name ---
720 case xlUrlFileName:
722 switch( *pChar )
724 case ']': eState = xlUrlSheetName; break;
725 default: lclAppendUrlChar( rUrl, *pChar );
728 break;
730 // --- sheet name ---
732 case xlUrlSheetName:
733 rTabName += OUStringChar( *pChar );
734 break;
736 // --- raw read mode ---
738 case xlUrlRaw:
739 lclAppendUrlChar( rUrl, *pChar );
740 break;
743 ++pChar;
747 void XclImpUrlHelper::DecodeUrl(
748 OUString& rUrl, bool& rbSameWb, const XclImpRoot& rRoot, const OUString& rEncodedUrl )
750 OUString aTabName;
751 OUString aUrl;
752 DecodeUrl( aUrl, aTabName, rbSameWb, rRoot, rEncodedUrl );
753 rUrl = aUrl;
754 OSL_ENSURE( aTabName.isEmpty(), "XclImpUrlHelper::DecodeUrl - sheet name ignored" );
757 bool XclImpUrlHelper::DecodeLink( OUString& rApplic, OUString& rTopic, const OUString& rEncUrl )
759 sal_Int32 nPos = rEncUrl.indexOf( EXC_DDE_DELIM );
760 if( (nPos > 0) && (nPos + 1 < rEncUrl.getLength()) )
762 rApplic = rEncUrl.copy( 0, nPos );
763 rTopic = rEncUrl.copy( nPos + 1 );
764 return true;
766 return false;
769 // Cached Values ==============================================================
771 XclImpCachedValue::XclImpCachedValue( XclImpStream& rStrm ) :
772 mfValue( 0.0 ),
773 mnBoolErr( 0 )
775 mnType = rStrm.ReaduInt8();
776 switch( mnType )
778 case EXC_CACHEDVAL_EMPTY:
779 rStrm.Ignore( 8 );
780 break;
781 case EXC_CACHEDVAL_DOUBLE:
782 mfValue = rStrm.ReadDouble();
783 break;
784 case EXC_CACHEDVAL_STRING:
785 maStr = rStrm.ReadUniString();
786 break;
787 case EXC_CACHEDVAL_BOOL:
788 case EXC_CACHEDVAL_ERROR:
790 double fVal;
791 mnBoolErr = rStrm.ReaduInt8();
792 rStrm.Ignore( 7 );
794 std::unique_ptr<ScTokenArray> pScTokArr = rStrm.GetRoot().GetOldFmlaConverter().GetBoolErr(
795 XclTools::ErrorToEnum( fVal, mnType == EXC_CACHEDVAL_ERROR, mnBoolErr ) );
796 if( pScTokArr )
797 mxTokArr = std::move( pScTokArr );
799 break;
800 default:
801 OSL_FAIL( "XclImpCachedValue::XclImpCachedValue - unknown data type" );
805 XclImpCachedValue::~XclImpCachedValue()
809 FormulaError XclImpCachedValue::GetScError() const
811 return (mnType == EXC_CACHEDVAL_ERROR) ? XclTools::GetScErrorCode( mnBoolErr ) : FormulaError::NONE;
814 // Matrix Cached Values ==============================================================
816 XclImpCachedMatrix::XclImpCachedMatrix( XclImpStream& rStrm ) :
817 mnScCols( 0 ),
818 mnScRows( 0 )
820 mnScCols = rStrm.ReaduInt8();
821 mnScRows = rStrm.ReaduInt16();
823 if( rStrm.GetRoot().GetBiff() <= EXC_BIFF5 )
825 // in BIFF2-BIFF7: 256 columns represented by 0 columns
826 if( mnScCols == 0 )
827 mnScCols = 256;
829 else
831 // in BIFF8: columns and rows decreaed by 1
832 ++mnScCols;
833 ++mnScRows;
836 //assuming worst case scenario of unknown types
837 const size_t nMinRecordSize = 1;
838 const size_t nMaxRows = rStrm.GetRecLeft() / (nMinRecordSize * mnScCols);
839 if (mnScRows > nMaxRows)
841 SAL_WARN("sc", "Parsing error: " << nMaxRows <<
842 " max possible rows, but " << mnScRows << " claimed, truncating");
843 mnScRows = nMaxRows;
846 for( SCSIZE nScRow = 0; nScRow < mnScRows; ++nScRow )
847 for( SCSIZE nScCol = 0; nScCol < mnScCols; ++nScCol )
848 maValueList.push_back( std::make_unique<XclImpCachedValue>( rStrm ) );
851 XclImpCachedMatrix::~XclImpCachedMatrix()
855 ScMatrixRef XclImpCachedMatrix::CreateScMatrix( svl::SharedStringPool& rPool ) const
857 ScMatrixRef xScMatrix;
858 OSL_ENSURE( mnScCols * mnScRows == maValueList.size(), "XclImpCachedMatrix::CreateScMatrix - element count mismatch" );
859 if( mnScCols && mnScRows && static_cast< sal_uLong >( mnScCols * mnScRows ) <= maValueList.size() )
861 xScMatrix = new ScMatrix(mnScCols, mnScRows, 0.0);
862 XclImpValueList::const_iterator itValue = maValueList.begin();
863 for( SCSIZE nScRow = 0; nScRow < mnScRows; ++nScRow )
865 for( SCSIZE nScCol = 0; nScCol < mnScCols; ++nScCol )
867 switch( (*itValue)->GetType() )
869 case EXC_CACHEDVAL_EMPTY:
870 // Excel shows 0.0 here, not an empty cell
871 xScMatrix->PutEmpty( nScCol, nScRow );
872 break;
873 case EXC_CACHEDVAL_DOUBLE:
874 xScMatrix->PutDouble( (*itValue)->GetValue(), nScCol, nScRow );
875 break;
876 case EXC_CACHEDVAL_STRING:
877 xScMatrix->PutString(rPool.intern((*itValue)->GetString()), nScCol, nScRow);
878 break;
879 case EXC_CACHEDVAL_BOOL:
880 xScMatrix->PutBoolean( (*itValue)->GetBool(), nScCol, nScRow );
881 break;
882 case EXC_CACHEDVAL_ERROR:
883 xScMatrix->PutError( (*itValue)->GetScError(), nScCol, nScRow );
884 break;
885 default:
886 OSL_FAIL( "XclImpCachedMatrix::CreateScMatrix - unknown value type" );
887 xScMatrix->PutEmpty( nScCol, nScRow );
889 ++itValue;
893 return xScMatrix;
896 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */