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