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>
31 const sal_Int32 BIFF_RK_100FLAG
= 0x00000001;
32 const sal_Int32 BIFF_RK_INTFLAG
= 0x00000002;
33 const sal_Int32 BIFF_RK_VALUEMASK
= 0xFFFFFFFC;
38 sal_math_Double maStruct
;
40 explicit DecodedDouble() {}
41 explicit DecodedDouble( double fValue
) : mfValue( fValue
) {}
46 // conversion -----------------------------------------------------------------
48 /*static*/ double BiffHelper::calcDoubleFromRk( sal_Int32 nRkValue
)
50 DecodedDouble
aDecDbl( 0.0 );
51 if( getFlag( nRkValue
, BIFF_RK_INTFLAG
) )
53 sal_Int32 nTemp
= nRkValue
>> 2;
54 setFlag
< sal_Int32
>( nTemp
, 0xE0000000, nRkValue
< 0 );
55 aDecDbl
.mfValue
= nTemp
;
59 aDecDbl
.maStruct
.w32_parts
.msw
= static_cast< sal_uInt32
>( nRkValue
& BIFF_RK_VALUEMASK
);
62 if( getFlag( nRkValue
, BIFF_RK_100FLAG
) )
63 aDecDbl
.mfValue
/= 100.0;
65 return aDecDbl
.mfValue
;
68 /*static*/ double BiffHelper::calcDoubleFromError( sal_uInt8 nErrorCode
)
70 sal_uInt16 nApiError
= 0x7FFF;
73 case BIFF_ERR_NULL
: nApiError
= 521; break;
74 case BIFF_ERR_DIV0
: nApiError
= 532; break;
75 case BIFF_ERR_VALUE
: nApiError
= 519; break;
76 case BIFF_ERR_REF
: nApiError
= 524; break;
77 case BIFF_ERR_NAME
: nApiError
= 525; break;
78 case BIFF_ERR_NUM
: nApiError
= 503; break;
79 case BIFF_ERR_NA
: nApiError
= 0x7FFF; break;
80 default: OSL_FAIL( "BiffHelper::calcDoubleFromError - unknown error code" );
82 DecodedDouble aDecDbl
;
83 ::rtl::math::setNan( &aDecDbl
.mfValue
);
84 aDecDbl
.maStruct
.nan_parts
.fraction_lo
= nApiError
;
85 return aDecDbl
.mfValue
;
88 // BIFF12 import --------------------------------------------------------------
90 /*static*/ OUString
BiffHelper::readString( SequenceInputStream
& rStrm
, bool b32BitLen
)
95 sal_Int32 nCharCount
= b32BitLen
? rStrm
.readValue
< sal_Int32
>() : rStrm
.readValue
< sal_Int16
>();
96 // string length -1 is often used to indicate a missing string
97 OSL_ENSURE( !rStrm
.isEof() && (nCharCount
>= -1), "BiffHelper::readString - invalid string length" );
98 if( !rStrm
.isEof() && (nCharCount
> 0) )
100 // SequenceInputStream always supports getRemaining()
101 nCharCount
= ::std::min( nCharCount
, static_cast< sal_Int32
>( rStrm
.getRemaining() / 2 ) );
102 aString
= rStrm
.readUnicodeArray( nCharCount
);
111 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */