summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--scaddins/source/analysis/analysishelper.cxx131
-rw-r--r--scaddins/source/analysis/analysishelper.hxx67
2 files changed, 137 insertions, 61 deletions
diff --git a/scaddins/source/analysis/analysishelper.cxx b/scaddins/source/analysis/analysishelper.cxx
index 5ae9fff8b462..e8dc3d398be7 100644
--- a/scaddins/source/analysis/analysishelper.cxx
+++ b/scaddins/source/analysis/analysishelper.cxx
@@ -2,9 +2,9 @@
*
* $RCSfile: analysishelper.cxx,v $
*
- * $Revision: 1.34 $
+ * $Revision: 1.35 $
*
- * last change: $Author: dr $ $Date: 2001-10-12 09:27:13 $
+ * last change: $Author: dr $ $Date: 2001-10-25 11:08:29 $
*
* The Contents of this file are made available subject to the terms of
* either of the following licenses
@@ -60,6 +60,10 @@
************************************************************************/
+#ifndef _COM_SUN_STAR_UTIL_XNUMBERFORMATTYPES_HPP_
+#include <com/sun/star/util/XNumberFormatTypes.hpp>
+#endif
+
#include <string.h>
#include <stdio.h>
#include <tools/resary.hxx>
@@ -1831,7 +1835,7 @@ void lcl_GetCouppcd( ScaDate& rDate, const ScaDate& rSettle, const ScaDate& rMat
throw( lang::IllegalArgumentException )
{
rDate = rMat;
- rDate.setYear( rSettle.nYear );
+ rDate.setYear( rSettle.getYear() );
if( rDate < rSettle )
rDate.addYears( 1 );
while( rDate > rSettle )
@@ -1856,7 +1860,7 @@ void lcl_GetCoupncd( ScaDate& rDate, const ScaDate& rSettle, const ScaDate& rMat
throw( lang::IllegalArgumentException )
{
rDate = rMat;
- rDate.setYear( rSettle.nYear );
+ rDate.setYear( rSettle.getYear() );
if( rDate > rSettle )
rDate.addYears( -1 );
while( rDate <= rSettle )
@@ -1940,7 +1944,7 @@ double GetCoupnum( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_I
ScaDate aMat( nNullDate, nMat, nBase );
ScaDate aDate;
lcl_GetCouppcd( aDate, ScaDate( nNullDate, nSettle, nBase ), aMat, nFreq );
- sal_uInt16 nMonths = (aMat.nYear - aDate.nYear) * 12 + aMat.nMonth - aDate.nMonth;
+ sal_uInt16 nMonths = (aMat.getYear() - aDate.getYear()) * 12 + aMat.getMonth() - aDate.getMonth();
return static_cast< double >( nMonths * nFreq / 12 );
}
@@ -3068,8 +3072,6 @@ double ConvertDataList::Convert( double fVal, const STRING& rFrom, const STRING&
ConvertData* p = First();
while( p && ( bSearchFrom || bSearchTo ) )
{
-#define CHECKIT(part)
-
if( bSearchFrom )
{
sal_Int16 n = p->GetMatchingLevel( rFrom );
@@ -3137,7 +3139,7 @@ ScaDate::ScaDate( sal_Int32 nNullDate, sal_Int32 nDate, sal_Int32 nBase )
{
DaysToDate( nNullDate + nDate, nOrigDay, nMonth, nYear );
bLastDayMode = (nBase != 5);
- bLastDay = (nOrigDay >= DaysInMonth( nMonth, nYear ));
+ bLastDay = (nOrigDay >= ::DaysInMonth( nMonth, nYear ));
b30Days = (nBase == 0) || (nBase == 4);
bUSMode = (nBase == 0);
setDay();
@@ -3157,14 +3159,17 @@ ScaDate::ScaDate( const ScaDate& rCopy ) :
ScaDate& ScaDate::operator=( const ScaDate& rCopy )
{
- nOrigDay = rCopy.nOrigDay;
- nDay = rCopy.nDay;
- nMonth = rCopy.nMonth;
- nYear = rCopy.nYear;
- bLastDayMode = rCopy.bLastDayMode;
- bLastDay = rCopy.bLastDay;
- b30Days = rCopy.b30Days;
- bUSMode = rCopy.bUSMode;
+ if( this != &rCopy )
+ {
+ nOrigDay = rCopy.nOrigDay;
+ nDay = rCopy.nDay;
+ nMonth = rCopy.nMonth;
+ nYear = rCopy.nYear;
+ bLastDayMode = rCopy.bLastDayMode;
+ bLastDay = rCopy.bLastDay;
+ b30Days = rCopy.b30Days;
+ bUSMode = rCopy.bUSMode;
+ }
return *this;
}
@@ -3172,13 +3177,15 @@ void ScaDate::setDay()
{
if( b30Days )
{
+ // 30-days-mode: set nDay to 30 if original was last day in month
nDay = Min( nOrigDay, static_cast< sal_uInt16 >( 30 ) );
- if( bLastDay || (nDay >= DaysInMonth( nMonth, nYear )) )
+ if( bLastDay || (nDay >= ::DaysInMonth( nMonth, nYear )) )
nDay = 30;
}
else
{
- sal_uInt16 nLastDay = DaysInMonth( nMonth, nYear );
+ // set nDay to last day in this month if original was last day
+ sal_uInt16 nLastDay = ::DaysInMonth( nMonth, nYear );
nDay = bLastDay ? nLastDay : Min( nOrigDay, nLastDay );
}
}
@@ -3193,7 +3200,7 @@ sal_Int32 ScaDate::getDaysInMonthRange( sal_uInt16 nFrom, sal_uInt16 nTo ) const
nRet = (nTo - nFrom + 1) * 30;
else
{
- for( sal_uInt16 nMonthIx = nFrom; nMonthIx <= nTo; nMonthIx++ )
+ for( sal_uInt16 nMonthIx = nFrom; nMonthIx <= nTo; ++nMonthIx )
nRet += getDaysInMonth( nMonthIx );
}
return nRet;
@@ -3204,7 +3211,7 @@ sal_Int32 ScaDate::getDaysInYearRange( sal_uInt16 nFrom, sal_uInt16 nTo ) const
if( nFrom > nTo )
return 0;
- return b30Days ? ((nTo - nFrom + 1) * 360) : GetDaysInYears( nFrom, nTo );
+ return b30Days ? ((nTo - nFrom + 1) * 360) : ::GetDaysInYears( nFrom, nTo );
}
void ScaDate::doAddYears( sal_Int32 nYearCount ) throw( lang::IllegalArgumentException )
@@ -3220,8 +3227,9 @@ void ScaDate::addMonths( sal_Int32 nMonthCount ) throw( lang::IllegalArgumentExc
sal_Int32 nNewMonth = nMonthCount + nMonth;
if( nNewMonth > 12 )
{
- doAddYears( (nNewMonth - 1) / 12 );
- nMonth = static_cast< sal_uInt16 >( (nNewMonth - 1) % 12 ) + 1;
+ --nNewMonth;
+ doAddYears( nNewMonth / 12 );
+ nMonth = static_cast< sal_uInt16 >( nNewMonth % 12 ) + 1;
}
else if( nNewMonth < 1 )
{
@@ -3235,9 +3243,9 @@ void ScaDate::addMonths( sal_Int32 nMonthCount ) throw( lang::IllegalArgumentExc
sal_Int32 ScaDate::getDate( sal_Int32 nNullDate ) const
{
- sal_uInt16 nLastDay = DaysInMonth( nMonth, nYear );
+ sal_uInt16 nLastDay = ::DaysInMonth( nMonth, nYear );
sal_uInt16 nRealDay = (bLastDayMode && bLastDay) ? nLastDay : Min( nLastDay, nOrigDay );
- return DateToDays( nRealDay, nMonth, nYear ) - nNullDate;
+ return ::DateToDays( nRealDay, nMonth, nYear ) - nNullDate;
}
sal_Int32 ScaDate::getDiff( const ScaDate& rFrom, const ScaDate& rTo ) throw( lang::IllegalArgumentException )
@@ -3257,15 +3265,15 @@ sal_Int32 ScaDate::getDiff( const ScaDate& rFrom, const ScaDate& rTo ) throw( la
if( ((rFrom.nMonth == 2) || (rFrom.nDay < 30)) && (aTo.nOrigDay == 31) )
aTo.nDay = 31;
else if( (aTo.nMonth == 2) && aTo.bLastDay )
- aTo.nDay = DaysInMonth( 2, aTo.nYear );
+ aTo.nDay = ::DaysInMonth( 2, aTo.nYear );
}
// corrections for base 4 (Europe)
else
{
if( (aFrom.nMonth == 2) && (aFrom.nDay == 30) )
- aFrom.nDay = DaysInMonth( 2, aFrom.nYear );
+ aFrom.nDay = ::DaysInMonth( 2, aFrom.nYear );
if( (aTo.nMonth == 2) && (aTo.nDay == 30) )
- aTo.nDay = DaysInMonth( 2, aTo.nYear );
+ aTo.nDay = ::DaysInMonth( 2, aTo.nYear );
}
}
@@ -3315,8 +3323,14 @@ sal_Bool ScaDate::operator<( const ScaDate& rCmp ) const
//-----------------------------------------------------------------------------
ScaAnyConverter::ScaAnyConverter( const uno::Reference< lang::XMultiServiceFactory >& xServiceFact ) :
- xServiceFactory( xServiceFact )
+ bHasValidFormat( sal_False )
{
+ if( xServiceFact.is() )
+ {
+ uno::Reference< uno::XInterface > xInstance = xServiceFact->createInstance(
+ OUString::createFromAscii( "com.sun.star.util.NumberFormatter" ) );
+ xFormatter = uno::Reference< util::XNumberFormatter >( xInstance, uno::UNO_QUERY );
+ }
}
ScaAnyConverter::~ScaAnyConverter()
@@ -3325,15 +3339,51 @@ ScaAnyConverter::~ScaAnyConverter()
void ScaAnyConverter::init( const uno::Reference< beans::XPropertySet >& xPropSet ) throw( uno::RuntimeException )
{
-//! current behaviour: only english separators are allowed: ',' for thousands, '.' for decimal
-// 2do: get locale separators
-
-// uno::Reference< util::XNumberFormatsSupplier > xFormatsSupp( xPropSet, uno::UNO_QUERY );
-// if( !xFormatsSupp.is() )
-// throw uno::RuntimeException();
+ // try to get default number format
+ bHasValidFormat = sal_False;
+ if( xFormatter.is() )
+ {
+ // get XFormatsSupplier from outer XPropertySet
+ uno::Reference< util::XNumberFormatsSupplier > xFormatsSupp( xPropSet, uno::UNO_QUERY );
+ if( xFormatsSupp.is() )
+ {
+ // get XNumberFormatTypes from XNumberFormatsSupplier to get standard index
+ uno::Reference< util::XNumberFormats > xFormats( xFormatsSupp->getNumberFormats() );
+ uno::Reference< util::XNumberFormatTypes > xFormatTypes( xFormats, uno::UNO_QUERY );
+ if( xFormatTypes.is() )
+ {
+ lang::Locale eLocale;
+ nDefaultFormat = xFormatTypes->getStandardIndex( eLocale );
+ xFormatter->attachNumberFormatsSupplier( xFormatsSupp );
+ bHasValidFormat = sal_True;
+ }
+ }
+ }
+}
- cGroupSep = ',';
- cDecSep = '.';
+double ScaAnyConverter::convertToDouble( const OUString& rString ) const throw( lang::IllegalArgumentException )
+{
+ double fValue = 0.0;
+ if( bHasValidFormat )
+ {
+ try
+ {
+ fValue = xFormatter->convertStringToNumber( nDefaultFormat, rString );
+ }
+ catch( uno::Exception& )
+ {
+ throw lang::IllegalArgumentException();
+ }
+ }
+ else
+ {
+ int nErrorNum;
+ const sal_Unicode* pLastChar;
+ fValue = SolarMath::StringToDouble( rString.getStr(), ',', '.', nErrorNum, &pLastChar );
+ if( (nErrorNum != 0) || (*pLastChar != '\0') )
+ throw lang::IllegalArgumentException();
+ }
+ return fValue;
}
sal_Bool ScaAnyConverter::getDouble(
@@ -3354,14 +3404,7 @@ sal_Bool ScaAnyConverter::getDouble(
{
const OUString* pString = static_cast< const OUString* >( rAny.getValue() );
if( pString->getLength() )
- {
- int nErrorNum;
- const sal_Unicode* pLastChar;
- rfResult = SolarMath::StringToDouble(
- pString->getStr(), cGroupSep, cDecSep, nErrorNum, &pLastChar );
- if( (nErrorNum != 0) || (*pLastChar != '\0') )
- throw lang::IllegalArgumentException();
- }
+ rfResult = convertToDouble( *pString );
else
bContainsVal = sal_False;
}
diff --git a/scaddins/source/analysis/analysishelper.hxx b/scaddins/source/analysis/analysishelper.hxx
index acda6d9ba070..a1f1ae3b8048 100644
--- a/scaddins/source/analysis/analysishelper.hxx
+++ b/scaddins/source/analysis/analysishelper.hxx
@@ -2,9 +2,9 @@
*
* $RCSfile: analysishelper.hxx,v $
*
- * $Revision: 1.20 $
+ * $Revision: 1.21 $
*
- * last change: $Author: dr $ $Date: 2001-10-12 09:27:13 $
+ * last change: $Author: dr $ $Date: 2001-10-25 11:08:29 $
*
* The Contents of this file are made available subject to the terms of
* either of the following licenses
@@ -63,13 +63,32 @@
#define ANALYSISHELPER_HXX
-#include <com/sun/star/sheet/XAddIn.hpp>
+#ifndef _COM_SUN_STAR_LANG_XSERVICENAME_HPP_
#include <com/sun/star/lang/XServiceName.hpp>
+#endif
+#ifndef _COM_SUN_STAR_LANG_XSERVICEINFO_HPP_
#include <com/sun/star/lang/XServiceInfo.hpp>
+#endif
+#ifndef _COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP_
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
+#endif
+
+#ifndef _COM_SUN_STAR_UTIL_DATE_HPP_
+#include <com/sun/star/util/Date.hpp>
+#endif
+#ifndef _COM_SUN_STAR_UTIL_XNUMBERFORMATTER_HPP_
+#include <com/sun/star/util/XNumberFormatter.hpp>
+#endif
+#ifndef _COM_SUN_STAR_UTIL_XNUMBERFORMATSSUPPLIER_HPP_
#include <com/sun/star/util/XNumberFormatsSupplier.hpp>
+#endif
+
+#ifndef _COM_SUN_STAR_SHEET_XADDIN_HPP_
+#include <com/sun/star/sheet/XAddIn.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SHEET_ADDIN_XANALYSIS_HPP_
#include <com/sun/star/sheet/addin/XAnalysis.hpp>
-#include <com/sun/star/util/Date.hpp>
+#endif
#include <math.h>
@@ -998,6 +1017,16 @@ inline ConvertData* ConvertDataList::Next( void )
class ScaDate
{
private:
+ sal_uInt16 nOrigDay; /// is the day of the original date.
+ sal_uInt16 nDay; /// is the calculated day depending on the current month/year.
+ sal_uInt16 nMonth; /// is the current month (one-based).
+ sal_uInt16 nYear; /// is the current year.
+ sal_Bool bLastDayMode : 1; /// if sal_True, recalculate nDay after every calculation.
+ sal_Bool bLastDay : 1; /// is sal_True, if original date was the last day in month.
+ sal_Bool b30Days : 1; /// is sal_True, if every month has 30 days in calculations.
+ sal_Bool bUSMode : 1; /// is sal_True, if the US method of 30-day-calculations is used.
+
+ /// Calculates nDay from nOrigDay and current date.
void setDay();
/// @return count of days in current month
@@ -1010,19 +1039,10 @@ private:
/// @ return count of days in the given year range
sal_Int32 getDaysInYearRange( sal_uInt16 nFrom, sal_uInt16 nTo ) const;
- /// adds/subtracts the given count of years, does not adjust day
+ /// Adds/subtracts the given count of years, does not adjust day.
void doAddYears( sal_Int32 nYearCount ) throw( CSS::lang::IllegalArgumentException );
public:
- sal_uInt16 nOrigDay;
- sal_uInt16 nDay;
- sal_uInt16 nMonth;
- sal_uInt16 nYear;
- sal_Bool bLastDayMode : 1;
- sal_Bool bLastDay : 1;
- sal_Bool b30Days : 1;
- sal_Bool bUSMode : 1;
-
ScaDate();
/** @param nBase
date handling mode (days in month / days in year):
@@ -1036,6 +1056,11 @@ public:
ScaDate( const ScaDate& rCopy );
ScaDate& operator=( const ScaDate& rCopy );
+ /// @return the current month.
+ inline sal_uInt16 getMonth() const { return nMonth; };
+ /// @return the current year.
+ inline sal_uInt16 getYear() const { return nYear; };
+
/// adds/subtracts the given count of months, adjusts day
void addMonths( sal_Int32 nMonthCount ) throw( CSS::lang::IllegalArgumentException );
@@ -1084,10 +1109,18 @@ inline void ScaDate::addYears( sal_Int32 nYearCount ) throw( CSS::lang::IllegalA
class ScaAnyConverter
{
private:
- CSS::uno::Reference< CSS::lang::XMultiServiceFactory > xServiceFactory;
+ CSS::uno::Reference< CSS::util::XNumberFormatter > xFormatter;
+ sal_Int32 nDefaultFormat;
+ sal_Bool bHasValidFormat;
- sal_Unicode cGroupSep;
- sal_Unicode cDecSep;
+ /** Converts a string to double using the number formatter. If the formatter is not
+ valid, SolarMath::StringToDouble() with english separators will be used.
+ @throws com::sun::star::lang::IllegalArgumentException
+ on strings not representing any double value.
+ @return the converted double value. */
+ double convertToDouble(
+ const ::rtl::OUString& rString ) const
+ throw( CSS::lang::IllegalArgumentException );
public:
ScaAnyConverter(