1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <biffhelper.hxx>
22 #include <rtl/math.hxx>
23 #include <osl/diagnose.h>
24 #include <oox/helper/binaryinputstream.hxx>
30 const sal_Int32 BIFF_RK_100FLAG
= 0x00000001;
31 const sal_Int32 BIFF_RK_INTFLAG
= 0x00000002;
32 const sal_Int32 BIFF_RK_VALUEMASK
= 0xFFFFFFFC;
36 // conversion -----------------------------------------------------------------
38 /*static*/ double BiffHelper::calcDoubleFromRk( sal_Int32 nRkValue
)
40 sal_math_Double aMathDouble
{};
41 if( getFlag( nRkValue
, BIFF_RK_INTFLAG
) )
43 sal_Int32 nTemp
= nRkValue
>> 2;
44 setFlag
< sal_Int32
>( nTemp
, 0xE0000000, nRkValue
< 0 );
45 aMathDouble
.value
= nTemp
;
49 aMathDouble
.w32_parts
.msw
= static_cast< sal_uInt32
>( nRkValue
& BIFF_RK_VALUEMASK
);
51 if( getFlag( nRkValue
, BIFF_RK_100FLAG
) )
52 aMathDouble
.value
/= 100.0;
54 return aMathDouble
.value
;
56 /*static*/ double BiffHelper::calcDoubleFromError( sal_uInt8 nErrorCode
)
58 sal_uInt16 nApiError
= 0x7FFF;
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" );
70 sal_math_Double aMathDouble
;
71 ::rtl::math::setNan( &aMathDouble
.value
);
72 aMathDouble
.nan_parts
.fraction_lo
= nApiError
;
73 return aMathDouble
.value
;
76 // BIFF12 import --------------------------------------------------------------
78 /*static*/ OUString
BiffHelper::readString( SequenceInputStream
& rStrm
, bool b32BitLen
)
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) )
88 // SequenceInputStream always supports getRemaining()
89 nCharCount
= ::std::min( nCharCount
, static_cast< sal_Int32
>( rStrm
.getRemaining() / 2 ) );
90 aString
= rStrm
.readUnicodeArray( nCharCount
);
96 } // namespace oox::xls
98 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */