LibreOffice Module sc (master)  1
biffhelper.cxx
Go to the documentation of this file.
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 <biffhelper.hxx>
21 
22 #include <rtl/math.hxx>
23 #include <osl/diagnose.h>
25 
26 #include <limits>
27 
28 namespace oox::xls {
29 
30 namespace {
31 
32 const sal_Int32 BIFF_RK_100FLAG = 0x00000001;
33 const sal_Int32 BIFF_RK_INTFLAG = 0x00000002;
34 const sal_Int32 BIFF_RK_VALUEMASK = 0xFFFFFFFC;
35 
36 } // namespace
37 
38 // conversion -----------------------------------------------------------------
39 
40 /*static*/ double BiffHelper::calcDoubleFromRk( sal_Int32 nRkValue )
41 {
42  sal_math_Double aMathDouble{};
43  if( getFlag( nRkValue, BIFF_RK_INTFLAG ) )
44  {
45  sal_Int32 nTemp = nRkValue >> 2;
46  setFlag< sal_Int32 >( nTemp, 0xE0000000, nRkValue < 0 );
47  aMathDouble.value = nTemp;
48  }
49  else
50  {
51  aMathDouble.w32_parts.msw = static_cast< sal_uInt32 >( nRkValue & BIFF_RK_VALUEMASK );
52  }
53  if( getFlag( nRkValue, BIFF_RK_100FLAG ) )
54  aMathDouble.value /= 100.0;
55 
56  return aMathDouble.value;
57 }
58 /*static*/ double BiffHelper::calcDoubleFromError( sal_uInt8 nErrorCode )
59 {
60  sal_uInt16 nApiError = 0x7FFF;
61  switch( nErrorCode )
62  {
63  case BIFF_ERR_NULL: nApiError = 521; break;
64  case BIFF_ERR_DIV0: nApiError = 532; break;
65  case BIFF_ERR_VALUE: nApiError = 519; break;
66  case BIFF_ERR_REF: nApiError = 524; break;
67  case BIFF_ERR_NAME: nApiError = 525; break;
68  case BIFF_ERR_NUM: nApiError = 503; break;
69  case BIFF_ERR_NA: nApiError = 0x7FFF; break;
70  default: OSL_FAIL( "BiffHelper::calcDoubleFromError - unknown error code" );
71  }
72  sal_math_Double aMathDouble;
73  aMathDouble.value = std::numeric_limits<double>::quiet_NaN();
74  aMathDouble.nan_parts.fraction_lo = nApiError;
75  return aMathDouble.value;
76 }
77 
78 // BIFF12 import --------------------------------------------------------------
79 
80 /*static*/ OUString BiffHelper::readString( SequenceInputStream& rStrm, bool b32BitLen )
81 {
82  OUString aString;
83  if( !rStrm.isEof() )
84  {
85  sal_Int32 nCharCount = b32BitLen ? rStrm.readValue< sal_Int32 >() : rStrm.readValue< sal_Int16 >();
86  // string length -1 is often used to indicate a missing string
87  OSL_ENSURE( !rStrm.isEof() && (nCharCount >= -1), "BiffHelper::readString - invalid string length" );
88  if( !rStrm.isEof() && (nCharCount > 0) )
89  {
90  // SequenceInputStream always supports getRemaining()
91  nCharCount = ::std::min( nCharCount, static_cast< sal_Int32 >( rStrm.getRemaining() / 2 ) );
92  aString = rStrm.readUnicodeArray( nCharCount );
93  }
94  }
95  return aString;
96 }
97 
98 } // namespace oox::xls
99 
100 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const sal_uInt8 BIFF_ERR_VALUE
Definition: biffhelper.hxx:562
const sal_uInt8 BIFF_ERR_NA
Definition: biffhelper.hxx:566
static double calcDoubleFromError(sal_uInt8 nErrorCode)
Converts the passed BIFF error to a double containing the respective Calc error code.
Definition: biffhelper.cxx:58
const sal_uInt8 BIFF_ERR_REF
Definition: biffhelper.hxx:563
const sal_uInt8 BIFF_ERR_NAME
Definition: biffhelper.hxx:564
static double calcDoubleFromRk(sal_Int32 nRkValue)
Converts the passed packed number to a double.
Definition: biffhelper.cxx:40
bool getFlag(Type nBitField, Type nMask)
static OUString readString(SequenceInputStream &rStrm, bool b32BitLen=true)
Reads a BIFF12 string with leading 16-bit or 32-bit length field.
Definition: biffhelper.cxx:80
const sal_uInt8 BIFF_ERR_DIV0
Definition: biffhelper.hxx:561
const sal_uInt8 BIFF_ERR_NUM
Definition: biffhelper.hxx:565
sal_Int64 getRemaining() const
unsigned char sal_uInt8
OUString readUnicodeArray(sal_Int32 nChars)
const sal_uInt8 BIFF_ERR_NULL
Common object settings.
Definition: biffhelper.hxx:560
bool isEof() const