update dev300-m58
[ooovba.git] / scaddins / source / analysis / analysishelper.hxx
blobf8987854bb194a9eecfdec05338566da433cee62
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: analysishelper.hxx,v $
10 * $Revision: 1.34 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #ifndef ANALYSISHELPER_HXX
31 #define ANALYSISHELPER_HXX
34 #include <com/sun/star/lang/XServiceName.hpp>
35 #include <com/sun/star/lang/XServiceInfo.hpp>
36 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
37 #include <com/sun/star/util/Date.hpp>
38 #include <com/sun/star/util/XNumberFormatter.hpp>
39 #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
40 #include <com/sun/star/sheet/XAddIn.hpp>
41 #include <com/sun/star/sheet/addin/XAnalysis.hpp>
43 #include <math.h>
45 #include <tools/resid.hxx>
46 #include <tools/rc.hxx>
48 #include "analysisdefs.hxx"
51 class ResMgr;
52 class SortedIndividualInt32List;
53 class ScaAnyConverter;
56 #define PI 3.1415926535897932
57 #define PI_2 (PI/2.0)
58 //#define EULER 2.7182818284590452
59 #define EOL ( ( const sal_Char* ) 1 )
60 #define EOE ( ( const sal_Char* ) 2 )
63 //double _Test( sal_Int32 nMode, double f1, double f2, double f3 );
64 inline sal_Bool IsLeapYear( sal_uInt16 nYear );
65 sal_uInt16 DaysInMonth( sal_uInt16 nMonth, sal_uInt16 nYear );
66 sal_Int32 DateToDays( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear );
67 void DaysToDate( sal_Int32 nDays, sal_uInt16& rDay, sal_uInt16& rMonth, sal_uInt16& rYear ) throw( ::com::sun::star::lang::IllegalArgumentException );
68 sal_Int32 GetNullDate( const REF( CSS::beans::XPropertySet )& xOptions ) THROWDEF_RTE;
69 sal_Int32 GetDiffDate360(
70 sal_uInt16 nDay1, sal_uInt16 nMonth1, sal_uInt16 nYear1, sal_Bool bLeapYear1,
71 sal_uInt16 nDay2, sal_uInt16 nMonth2, sal_uInt16 nYear2,
72 sal_Bool bUSAMethod );
73 inline sal_Int32 GetDiffDate360( constREFXPS& xOpt, sal_Int32 nDate1, sal_Int32 nDate2, sal_Bool bUSAMethod );
74 sal_Int32 GetDiffDate360( sal_Int32 nNullDate, sal_Int32 nDate1, sal_Int32 nDate2, sal_Bool bUSAMethod );
76 sal_Int32 GetDaysInYears( sal_uInt16 nYear1, sal_uInt16 nYear2 );
77 inline sal_Int16 GetDayOfWeek( sal_Int32 nDate );
78 void GetDiffParam( sal_Int32 nNullDate, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode,
79 sal_uInt16& rYears, sal_Int32& rDayDiffPart, sal_Int32& rDaysInYear ) THROWDEF_RTE_IAE;
80 // rYears = full num of years
81 // rDayDiffPart = num of days for last year
82 // rDaysInYear = num of days in first year
83 sal_Int32 GetDiffDate( sal_Int32 nNullDate, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode,
84 sal_Int32* pOptDaysIn1stYear = NULL ) THROWDEF_RTE_IAE;
85 double GetYearDiff( sal_Int32 nNullDate, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode )
86 THROWDEF_RTE_IAE;
87 sal_Int32 GetDaysInYear( sal_Int32 nNullDate, sal_Int32 nDate, sal_Int32 nMode ) THROWDEF_RTE_IAE;
88 double GetYearFrac( sal_Int32 nNullDate, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode )
89 THROWDEF_RTE_IAE;
90 inline double GetYearFrac( constREFXPS& xOpt, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode )
91 THROWDEF_RTE_IAE;
92 inline void AlignDate( sal_uInt16& rDay, sal_uInt16 nMonth, sal_uInt16 nYear );
94 double Fak( sal_Int32 n );
95 double GetGcd( double f1, double f2 );
96 double ConvertToDec( const STRING& rFromNum, sal_uInt16 nBaseFrom, sal_uInt16 nCharLim ) THROWDEF_RTE_IAE;
97 STRING ConvertFromDec(
98 double fNum, double fMin, double fMax, sal_uInt16 nBase,
99 sal_Int32 nPlaces, sal_Int32 nMaxPlaces, sal_Bool bUsePlaces ) THROWDEF_RTE_IAE;
100 double Erf( double fX );
101 double Erfc( double fX );
102 sal_Bool ParseDouble( const sal_Unicode*& rpDoubleAsString, double& rReturn );
103 STRING GetString( double fNumber, sal_Bool bLeadingSign = sal_False, sal_uInt16 nMaxNumOfDigits = 15 );
104 inline double Exp10( sal_Int16 nPower ); // 10 ^ nPower
106 double GetAmordegrc( sal_Int32 nNullDate, double fCost, sal_Int32 nDate, sal_Int32 nFirstPer,
107 double fRestVal, double fPer, double fRate, sal_Int32 nBase ) THROWDEF_RTE_IAE;
108 double GetAmorlinc( sal_Int32 nNullDate, double fCost, sal_Int32 nDate, sal_Int32 nFirstPer,
109 double fRestVal, double fPer, double fRate, sal_Int32 nBase ) THROWDEF_RTE_IAE;
110 double GetDuration( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, double fCoup,
111 double fYield, sal_Int32 nFreq, sal_Int32 nBase ) THROWDEF_RTE_IAE;
112 double GetYieldmat( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nIssue,
113 double fRate, double fPrice, sal_Int32 nBase ) THROWDEF_RTE_IAE;
114 double GetOddfprice( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nIssue,
115 sal_Int32 nFirstCoup, double fRate, double fYield, double fRedemp,
116 sal_Int32 nFreq, sal_Int32 nBase ) THROWDEF_RTE_IAE;
117 double getYield_( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, double fCoup, double fPrice,
118 double fRedemp, sal_Int32 nFreq, sal_Int32 nBase ) THROWDEF_RTE_IAE;
119 double getPrice_( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, double fRate, double fYield,
120 double fRedemp, sal_Int32 nFreq, sal_Int32 nBase ) THROWDEF_RTE_IAE;
121 double GetOddfyield( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nIssue,
122 sal_Int32 nFirstCoup, double fRate, double fPrice, double fRedemp,
123 sal_Int32 nFreq, sal_Int32 nBase ) THROWDEF_RTE_IAE;
124 double GetOddlprice( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nLastInterest,
125 double fRate, double fYield, double fRedemp, sal_Int32 nFreq, sal_Int32 nBase ) THROWDEF_RTE_IAE;
126 double GetOddlyield( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nLastInterest,
127 double fRate, double fPrice, double fRedemp, sal_Int32 nFreq, sal_Int32 nBase ) THROWDEF_RTE_IAE;
128 double GetRmz( double fZins, double fZzr, double fBw, double fZw, sal_Int32 nF );
129 double GetZw( double fZins, double fZzr, double fRmz, double fBw, sal_Int32 nF );
130 //double TBillYield( constREFXPS& xOpt, sal_Int32 nSettle, sal_Int32 nMat, double fPrice )THROWDEF_RTE_IAE;
132 double GetCouppcd( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq,
133 sal_Int32 nBase ) THROWDEF_RTE_IAE;
134 double GetCoupncd( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq,
135 sal_Int32 nBase ) THROWDEF_RTE_IAE;
136 double GetCoupdaybs( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq,
137 sal_Int32 nBase ) THROWDEF_RTE_IAE;
138 double GetCoupdaysnc( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq,
139 sal_Int32 nBase ) THROWDEF_RTE_IAE;
141 double GetCoupnum( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat,
142 sal_Int32 nFreq, sal_Int32 nBase ) THROWDEF_RTE_IAE;
143 double GetCoupdays( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq,
144 sal_Int32 nBase ) THROWDEF_RTE_IAE;
149 //-----------------------------------------------------------------------------
153 class MyList
155 private:
156 static const sal_uInt32 nStartSize;
157 static const sal_uInt32 nIncrSize;
159 void** pData; // pointer array
160 sal_uInt32 nSize; // array size
161 sal_uInt32 nNew; // next index to be inserted at
162 sal_uInt32 nAct; // actual for iterations
164 void _Grow( void );
165 inline void Grow( void );
166 protected:
167 public:
168 MyList( void );
169 virtual ~MyList();
171 inline const void* GetObject( sal_uInt32 nIndex ) const;
172 inline const void* First( void );
173 inline const void* Next( void );
175 inline void Append( void* pNewElement );
176 void Insert( void* pNewLement, sal_uInt32 nPlace );
178 inline sal_uInt32 Count( void ) const;
184 class StringList : protected MyList
186 public:
187 virtual ~StringList();
189 inline const STRING* First( void );
190 inline const STRING* Next( void );
191 inline const STRING* Get( sal_uInt32 nIndex ) const;
193 using MyList::Append;
194 inline void Append( STRING* pNew );
195 inline void Append( const STRING& rNew );
197 using MyList::Count;
203 enum FDCategory
205 FDCat_AddIn,
206 FDCat_DateTime,
207 FDCat_Finance,
208 FDCat_Inf,
209 FDCat_Math,
210 FDCat_Tech
214 struct FuncDataBase
216 const sal_Char* pIntName;
217 sal_uInt16 nUINameID; // resource ID to UI name
218 sal_uInt16 nDescrID; // resource ID to description, parameter names and ~ description
219 sal_Bool bDouble; // name already exist in Calc
220 sal_Bool bWithOpt; // first parameter is internal
221 sal_uInt16 nCompListID; // resource ID to list of valid names
222 sal_uInt16 nNumOfParams; // number of named / described parameters
223 FDCategory eCat; // function category
229 class FuncData
231 private:
232 ::rtl::OUString aIntName;
233 sal_uInt16 nUINameID;
234 sal_uInt16 nDescrID; // leads also to parameter descriptions!
235 sal_Bool bDouble; // flag for names, wich already exist in Calc
236 sal_Bool bWithOpt; // has internal parameter on first position
238 sal_uInt16 nParam; // num of parameters
239 sal_uInt16 nCompID;
240 StringList aCompList; // list of all valid names
241 FDCategory eCat; // function category
242 public:
243 FuncData( const FuncDataBase& rBaseData, ResMgr& );
244 virtual ~FuncData();
246 inline sal_uInt16 GetUINameID( void ) const;
247 inline sal_uInt16 GetDescrID( void ) const;
248 inline sal_Bool IsDouble( void ) const;
249 inline sal_Bool HasIntParam( void ) const;
251 sal_uInt16 GetStrIndex( sal_uInt16 nParamNum ) const;
252 inline sal_Bool Is( const ::rtl::OUString& rCompareTo ) const;
254 inline const StringList& GetCompNameList( void ) const;
256 inline FDCategory GetCategory( void ) const;
262 class CStrList : private MyList
264 public:
265 using MyList::Append;
266 inline void Append( const sal_Char* pNew );
267 inline const sal_Char* Get( sal_uInt32 nIndex ) const;
268 using MyList::Count;
274 class FuncDataList : private MyList
276 ::rtl::OUString aLastName;
277 sal_uInt32 nLast;
278 public:
279 FuncDataList( ResMgr& );
280 virtual ~FuncDataList();
281 using MyList::Append;
282 inline void Append( FuncData* pNew );
283 inline const FuncData* Get( sal_uInt32 nIndex ) const;
284 using MyList::Count;
286 const FuncData* Get( const ::rtl::OUString& aProgrammaticName ) const;
291 class AnalysisResId : public ResId
293 public:
294 AnalysisResId( sal_uInt16 nId, ResMgr& rResMgr );
300 class AnalysisRscStrLoader : public Resource
302 private:
303 String aStr;
304 public:
305 AnalysisRscStrLoader( sal_uInt16 nRsc, sal_uInt16 nStrId, ResMgr& rResMgr ) :
306 Resource( AnalysisResId( nRsc, rResMgr ) ),
307 aStr( AnalysisResId( nStrId, rResMgr ) )
309 FreeResource();
312 const String& GetString() const { return aStr; }
318 //-----------------------------------------------------------------------------
320 /// sorted list with unique sal_Int32 values
321 class SortedIndividualInt32List : private MyList
323 protected:
324 using MyList::Insert;
325 void Insert( sal_Int32 nDay );
326 void Insert( sal_Int32 nDay, sal_Int32 nNullDate, sal_Bool bInsertOnWeekend );
327 void Insert( double fDay, sal_Int32 nNullDate, sal_Bool bInsertOnWeekend )
328 throw( CSS::uno::RuntimeException, CSS::lang::IllegalArgumentException );
330 /** @param rAnyConv must be an initialized ScaAnyConmverter
331 @param bInsertOnWeekend insertion mode: sal_False = holidays on weekend are omitted */
332 void InsertHolidayList(
333 const ScaAnyConverter& rAnyConv,
334 const CSS::uno::Any& rHolAny,
335 sal_Int32 nNullDate,
336 sal_Bool bInsertOnWeekend ) throw( CSS::uno::RuntimeException, CSS::lang::IllegalArgumentException );
338 public:
339 SortedIndividualInt32List();
340 virtual ~SortedIndividualInt32List();
342 using MyList::Count;
344 /// @return element on position nIndex or 0 on invalid index
345 inline sal_Int32 Get( sal_uInt32 nIndex ) const
346 { return (sal_Int32)(sal_IntPtr) MyList::GetObject( nIndex ); }
348 /// @return sal_True if nVal (internal date representation) is contained
349 sal_Bool Find( sal_Int32 nVal ) const;
351 /** @param rAnyConv is an initialized or uninitialized ScaAnyConverter
352 @param bInsertOnWeekend insertion mode: sal_False = holidays on weekend are omitted */
353 void InsertHolidayList(
354 ScaAnyConverter& rAnyConv,
355 const CSS::uno::Reference< CSS::beans::XPropertySet >& xOptions,
356 const CSS::uno::Any& rHolAny,
357 sal_Int32 nNullDate,
358 sal_Bool bInsertOnWeekend ) throw( CSS::uno::RuntimeException, CSS::lang::IllegalArgumentException );
362 //-----------------------------------------------------------------------------
364 class ScaDoubleList : protected MyList
366 protected:
367 inline void ListAppend( double fValue ) { MyList::Append( new double( fValue ) ); }
369 using MyList::Append;
370 inline void Append( double fValue ) throw( CSS::uno::RuntimeException, CSS::lang::IllegalArgumentException )
371 { if( CheckInsert( fValue ) ) ListAppend( fValue ); }
373 /** @param rAnyConv must be an initialized ScaAnyConmverter
374 @param bIgnoreEmpty handling of empty Any's/strings: sal_False = inserted as 0.0; sal_True = omitted */
375 void Append(
376 const ScaAnyConverter& rAnyConv,
377 const CSS::uno::Any& rAny,
378 sal_Bool bIgnoreEmpty ) throw( CSS::uno::RuntimeException, CSS::lang::IllegalArgumentException );
380 /** @param rAnyConv must be an initialized ScaAnyConmverter
381 @param bIgnoreEmpty handling of empty Any's/strings: sal_False = inserted as 0.0; sal_True = omitted */
382 void Append(
383 const ScaAnyConverter& rAnyConv,
384 const CSS::uno::Sequence< CSS::uno::Any >& rAnySeq,
385 sal_Bool bIgnoreEmpty ) throw( CSS::uno::RuntimeException, CSS::lang::IllegalArgumentException );
387 /** @param rAnyConv must be an initialized ScaAnyConmverter
388 @param bIgnoreEmpty handling of empty Any's/strings: sal_False = inserted as 0.0; sal_True = omitted */
389 void Append(
390 const ScaAnyConverter& rAnyConv,
391 const CSS::uno::Sequence< CSS::uno::Sequence< CSS::uno::Any > >& rAnySeq,
392 sal_Bool bIgnoreEmpty ) throw( CSS::uno::RuntimeException, CSS::lang::IllegalArgumentException );
394 public:
395 virtual ~ScaDoubleList();
397 using MyList::Count;
398 inline const double* Get( sal_uInt32 nIndex ) const
399 { return static_cast< const double* >( MyList::GetObject( nIndex ) ); }
401 inline const double* First() { return static_cast< const double* >( MyList::First() ); }
402 inline const double* Next() { return static_cast< const double* >( MyList::Next() ); }
404 void Append( const CSS::uno::Sequence< CSS::uno::Sequence< double > >& rValueArr )
405 throw( CSS::uno::RuntimeException, CSS::lang::IllegalArgumentException );
406 void Append( const CSS::uno::Sequence< CSS::uno::Sequence< sal_Int32 > >& rValueArr )
407 throw( CSS::uno::RuntimeException, CSS::lang::IllegalArgumentException );
409 /** @param rAnyConv is an initialized or uninitialized ScaAnyConverter
410 @param bIgnoreEmpty handling of empty Any's/strings: sal_False = inserted as 0.0; sal_True = omitted */
411 void Append(
412 ScaAnyConverter& rAnyConv,
413 const CSS::uno::Reference< CSS::beans::XPropertySet >& xOpt,
414 const CSS::uno::Sequence< CSS::uno::Any >& rAnySeq,
415 sal_Bool bIgnoreEmpty = sal_True ) throw( CSS::uno::RuntimeException, CSS::lang::IllegalArgumentException );
417 virtual sal_Bool CheckInsert( double fValue ) const
418 throw( CSS::uno::RuntimeException, CSS::lang::IllegalArgumentException );
422 //-----------------------------------------------------------------------------
424 /// stores double values >0.0, throws exception for double values <0.0, does nothing for 0.0
425 class ScaDoubleListGT0 : public ScaDoubleList
427 public:
428 virtual sal_Bool CheckInsert( double fValue ) const
429 throw( CSS::uno::RuntimeException, CSS::lang::IllegalArgumentException );
433 //-----------------------------------------------------------------------------
435 /// stores double values >=0.0, throws exception for double values <0.0
436 class ScaDoubleListGE0 : public ScaDoubleList
438 public:
439 virtual sal_Bool CheckInsert( double fValue ) const
440 throw( CSS::uno::RuntimeException, CSS::lang::IllegalArgumentException );
444 //-----------------------------------------------------------------------------
446 class Complex
448 double r;
449 double i;
450 sal_Unicode c;
452 public:
453 inline Complex( double fReal, double fImag = 0.0, sal_Char cC = '\0' );
454 Complex( const STRING& rComplexAsString ) THROWDEF_RTE_IAE;
456 inline static sal_Bool IsImagUnit( sal_Unicode c );
457 static sal_Bool ParseString( const STRING& rComplexAsString, Complex& rReturn );
458 STRING GetString() const THROWDEF_RTE_IAE;
460 inline double Real( void ) const;
461 inline double Imag( void ) const;
463 double Arg( void ) const THROWDEF_RTE_IAE;
464 inline double Abs( void ) const;
466 // following functions change the complex number itself to avoid unnecessary copy actions!
467 void Power( double fPower ) THROWDEF_RTE_IAE;
468 void Sqrt( void );
469 void Sin( void ) THROWDEF_RTE_IAE;
470 void Cos( void ) THROWDEF_RTE_IAE;
471 void Div( const Complex& rDivisor ) THROWDEF_RTE_IAE;
472 void Exp( void );
473 inline void Conjugate( void );
474 void Ln( void ) THROWDEF_RTE_IAE;
475 void Log10( void ) THROWDEF_RTE_IAE;
476 void Log2( void ) THROWDEF_RTE_IAE;
477 inline void Mult( double fFact );
478 inline void Mult( const Complex& rMult );
479 inline void Sub( const Complex& rMult );
480 inline void Add( const Complex& rAdd );
486 enum ComplListAppendHandl
488 AH_EmptyAsErr,
489 AH_EmpyAs0,
490 AH_IgnoreEmpty
494 class ComplexList : protected MyList
496 public:
497 virtual ~ComplexList();
499 inline const Complex* Get( sal_uInt32 nIndex ) const;
500 inline const Complex* First( void );
501 inline const Complex* Next( void );
503 using MyList::Count;
505 using MyList::Append;
506 inline void Append( Complex* pNew );
507 void Append( const SEQSEQ( STRING )& rComplexNumList, ComplListAppendHandl eAH = AH_EmpyAs0 ) THROWDEF_RTE_IAE;
508 void Append( const SEQ( ANY )& aMultPars,ComplListAppendHandl eAH = AH_EmpyAs0 ) THROWDEF_RTE_IAE;
514 enum ConvertDataClass
516 CDC_Mass, CDC_Length, CDC_Time, CDC_Pressure, CDC_Force, CDC_Energy, CDC_Power, CDC_Magnetism,
517 CDC_Temperature, CDC_Volume, CDC_Area, CDC_Speed
521 #define INV_MATCHLEV 1764 // guess, what this is... :-)
524 class ConvertDataList;
529 class ConvertData
531 protected:
532 friend class ConvertDataList;
533 double fConst;
534 STRING aName;
535 ConvertDataClass eClass;
536 public:
537 ConvertData(
538 const sal_Char pUnitName[],
539 double fConvertConstant,
540 ConvertDataClass eClass );
542 virtual ~ConvertData();
544 sal_Int16 GetMatchingLevel( const STRING& rRef ) const;
545 // 0.0 = no equality
546 // 1.0 = matches exact
547 // rest = matches without an assumed prefix of one character
548 // rest gives power for 10 represented by the prefix (e.g. 3 for k or -9 for n
550 virtual double Convert( double fVal, const ConvertData& rTo,
551 sal_Int16 nMatchLevelFrom, sal_Int16 nMatchLevelTo ) const THROWDEF_RTE_IAE;
552 // converts fVal from this unit to rFrom unit
553 // throws exception if not from same class
554 // this implementation is for proportional cases only
555 virtual double ConvertToBase( double fVal, sal_Int16 nMatchLevel ) const;
556 virtual double ConvertFromBase( double fVal, sal_Int16 nMatchLevel ) const;
558 inline ConvertDataClass Class( void ) const;
564 class ConvertDataLinear : public ConvertData
566 protected:
567 double fOffs;
568 public:
569 inline ConvertDataLinear(
570 const sal_Char pUnitName[],
571 double fConvertConstant,
572 double fConvertOffset,
573 ConvertDataClass eClass );
575 virtual ~ConvertDataLinear();
577 virtual double Convert( double fVal, const ConvertData& rTo,
578 sal_Int16 nMatchLevelFrom, sal_Int16 nMatchLevelTo ) const THROWDEF_RTE_IAE;
579 // for cases where f(x) = a + bx applies (e.g. Temperatures)
581 virtual double ConvertToBase( double fVal, sal_Int16 nMatchLevel ) const;
582 virtual double ConvertFromBase( double fVal, sal_Int16 nMatchLevel ) const;
588 class ConvertDataList : protected MyList
590 private:
591 protected:
592 inline ConvertData* First( void );
593 inline ConvertData* Next( void );
594 public:
595 ConvertDataList( void );
596 virtual ~ConvertDataList();
598 double Convert( double fVal, const STRING& rFrom, const STRING& rTo ) THROWDEF_RTE_IAE;
604 inline sal_Bool IsLeapYear( sal_uInt16 n )
606 return ( (( ( n % 4 ) == 0 ) && ( ( n % 100 ) != 0)) || ( ( n % 400 ) == 0 ) );
610 inline sal_Int32 GetDiffDate360( constREFXPS& xOpt, sal_Int32 nDate1, sal_Int32 nDate2, sal_Bool bUSAMethod )
612 return GetDiffDate360( GetNullDate( xOpt ), nDate1, nDate2, bUSAMethod );
616 inline sal_Int16 GetDayOfWeek( sal_Int32 n )
617 { // monday = 0, ..., sunday = 6
618 return static_cast< sal_Int16 >( ( n - 1 ) % 7 );
622 inline double GetYearFrac( constREFXPS& xOpt, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode ) THROWDEF_RTE_IAE
624 return GetYearFrac( GetNullDate( xOpt ), nStartDate, nEndDate, nMode );
628 inline void AlignDate( sal_uInt16& rD, sal_uInt16 nM, sal_uInt16 nY )
630 sal_uInt16 nMax = DaysInMonth( nM, nY );
632 if( rD > nMax )
633 rD = nMax;
637 inline void MyList::Grow( void )
639 if( nNew >= nSize )
640 _Grow();
644 inline const void* MyList::GetObject( sal_uInt32 n ) const
646 if( n < nNew )
647 return pData[ n ];
648 else
649 return NULL;
653 inline const void* MyList::First( void )
655 nAct = 0;
656 if( nNew )
657 return pData[ 0 ];
658 else
659 return NULL;
663 inline const void* MyList::Next( void )
665 nAct++;
666 if( nAct < nNew )
667 return pData[ nAct ];
668 else
670 nAct--;
671 return NULL;
676 inline void MyList::Append( void* p )
678 Grow();
679 pData[ nNew ] = p;
680 nNew++;
684 inline sal_uInt32 MyList::Count( void ) const
686 return nNew;
692 inline const STRING* StringList::First( void )
694 return ( const STRING* ) MyList::First();
698 inline const STRING* StringList::Next( void )
700 return ( const STRING* ) MyList::Next();
704 inline const STRING* StringList::Get( sal_uInt32 n ) const
706 return ( const STRING* ) MyList::GetObject( n );
710 inline void StringList::Append( STRING* p )
712 MyList::Append( p );
716 inline void StringList::Append( const STRING& r )
718 MyList::Append( new STRING( r ) );
724 inline sal_uInt16 FuncData::GetUINameID( void ) const
726 return nUINameID;
730 inline sal_uInt16 FuncData::GetDescrID( void ) const
732 return nDescrID;
736 inline sal_Bool FuncData::IsDouble( void ) const
738 return bDouble;
742 inline sal_Bool FuncData::HasIntParam( void ) const
744 return bWithOpt;
748 inline sal_Bool FuncData::Is( const ::rtl::OUString& r ) const
750 return aIntName == r;
754 inline const StringList& FuncData::GetCompNameList( void ) const
756 return aCompList;
760 inline FDCategory FuncData::GetCategory( void ) const
762 return eCat;
768 inline void CStrList::Append( const sal_Char* p )
770 MyList::Append( ( void* ) p );
774 inline const sal_Char* CStrList::Get( sal_uInt32 n ) const
776 return ( const sal_Char* ) MyList::GetObject( n );
782 inline void FuncDataList::Append( FuncData* p )
784 MyList::Append( p );
788 inline const FuncData* FuncDataList::Get( sal_uInt32 n ) const
790 return ( const FuncData* ) MyList::GetObject( n );
794 inline Complex::Complex( double fReal, double fImag, sal_Char cC ) :
795 r( fReal ), i( fImag ), c( cC )
800 inline double Complex::Real( void ) const
802 return r;
806 inline double Complex::Imag( void ) const
808 return i;
812 inline double Complex::Abs( void ) const
814 return sqrt( r * r + i * i );
818 void Complex::Conjugate( void )
820 i = -i;
824 inline void Complex::Mult( double f )
826 i *= f;
827 r *= f;
831 inline void Complex::Mult( const Complex& rM )
833 double r_ = r;
834 double i_ = i;
836 r = r_ * rM.r - i_ * rM.i;
837 i = r_ * rM.i + i_ * rM.r;
839 if( !c ) c = rM.c;
843 inline void Complex::Sub( const Complex& rC )
845 r -= rC.r;
846 i -= rC.i;
847 if( !c ) c = rC.c;
851 inline void Complex::Add( const Complex& rAdd )
853 r += rAdd.r;
854 i += rAdd.i;
855 if( !c ) c = rAdd.c;
861 inline const Complex* ComplexList::Get( sal_uInt32 n ) const
863 return ( const Complex* ) MyList::GetObject( n );
867 inline const Complex* ComplexList::First( void )
869 return ( const Complex* ) MyList::First();
873 inline const Complex* ComplexList::Next( void )
875 return ( const Complex* ) MyList::Next();
879 inline void ComplexList::Append( Complex* p )
881 MyList::Append( p );
887 inline ConvertDataClass ConvertData::Class( void ) const
889 return eClass;
895 inline ConvertDataLinear::ConvertDataLinear( const sal_Char* p, double fC, double fO, ConvertDataClass e ) :
896 ConvertData( p, fC, e ),
897 fOffs( fO )
904 inline ConvertData* ConvertDataList::First( void )
906 return ( ConvertData* ) MyList::First();
910 inline ConvertData* ConvertDataList::Next( void )
912 return ( ConvertData* ) MyList::Next();
915 //-----------------------------------------------------------------------------
917 /// Helper class for date calculation for various financial functions
918 class ScaDate
920 private:
921 sal_uInt16 nOrigDay; /// is the day of the original date.
922 sal_uInt16 nDay; /// is the calculated day depending on the current month/year.
923 sal_uInt16 nMonth; /// is the current month (one-based).
924 sal_uInt16 nYear; /// is the current year.
925 sal_Bool bLastDayMode : 1; /// if sal_True, recalculate nDay after every calculation.
926 sal_Bool bLastDay : 1; /// is sal_True, if original date was the last day in month.
927 sal_Bool b30Days : 1; /// is sal_True, if every month has 30 days in calculations.
928 sal_Bool bUSMode : 1; /// is sal_True, if the US method of 30-day-calculations is used.
930 /// Calculates nDay from nOrigDay and current date.
931 void setDay();
933 /// @return count of days in current month
934 inline sal_uInt16 getDaysInMonth() const;
935 /// @return count of days in given month
936 inline sal_uInt16 getDaysInMonth( sal_uInt16 _nMon ) const;
938 /// @ return count of days in the given month range
939 sal_Int32 getDaysInMonthRange( sal_uInt16 nFrom, sal_uInt16 nTo ) const;
940 /// @ return count of days in the given year range
941 sal_Int32 getDaysInYearRange( sal_uInt16 nFrom, sal_uInt16 nTo ) const;
943 /// Adds/subtracts the given count of years, does not adjust day.
944 void doAddYears( sal_Int32 nYearCount ) throw( CSS::lang::IllegalArgumentException );
946 public:
947 ScaDate();
948 /** @param nBase
949 date handling mode (days in month / days in year):
950 0 = 30 days / 360 days (US NASD)
951 1 = exact / exact
952 2 = exact / 360
953 3 = exact / 365
954 4 = 30 days / 360 days (Europe)
955 5 = exact / exact (no last day adjustment) */
956 ScaDate( sal_Int32 nNullDate, sal_Int32 nDate, sal_Int32 nBase );
957 ScaDate( const ScaDate& rCopy );
958 ScaDate& operator=( const ScaDate& rCopy );
960 /// @return the current month.
961 inline sal_uInt16 getMonth() const { return nMonth; };
962 /// @return the current year.
963 inline sal_uInt16 getYear() const { return nYear; };
965 /// adds/subtracts the given count of months, adjusts day
966 void addMonths( sal_Int32 nMonthCount ) throw( CSS::lang::IllegalArgumentException );
968 /// sets the given year, adjusts day
969 inline void setYear( sal_uInt16 nNewYear );
970 /// adds/subtracts the given count of years, adjusts day
971 inline void addYears( sal_Int32 nYearCount ) throw( CSS::lang::IllegalArgumentException );
973 /// @return the internal number of the current date
974 sal_Int32 getDate( sal_Int32 nNullDate ) const;
975 /// @return the number of days between the two dates
976 static sal_Int32 getDiff( const ScaDate& rFrom, const ScaDate& rTo ) throw( CSS::lang::IllegalArgumentException );
978 sal_Bool operator<( const ScaDate& rCmp ) const;
979 inline sal_Bool operator<=( const ScaDate& rCmp ) const { return !(rCmp < *this); }
980 inline sal_Bool operator>( const ScaDate& rCmp ) const { return rCmp < *this; }
981 inline sal_Bool operator>=( const ScaDate& rCmp ) const { return !(*this < rCmp); }
984 inline sal_uInt16 ScaDate::getDaysInMonth() const
986 return getDaysInMonth( nMonth );
989 inline sal_uInt16 ScaDate::getDaysInMonth( sal_uInt16 _nMon ) const
991 return b30Days ? 30 : DaysInMonth( _nMon, nYear );
994 inline void ScaDate::setYear( sal_uInt16 nNewYear )
996 nYear = nNewYear;
997 setDay();
1000 inline void ScaDate::addYears( sal_Int32 nYearCount ) throw( CSS::lang::IllegalArgumentException )
1002 doAddYears( nYearCount );
1003 setDay();
1007 //-----------------------------------------------------------------------------
1009 /// Helper class for Any->double conversion, using current language settings
1010 class ScaAnyConverter
1012 private:
1013 CSS::uno::Reference< CSS::util::XNumberFormatter > xFormatter;
1014 sal_Int32 nDefaultFormat;
1015 sal_Bool bHasValidFormat;
1017 /** Converts a string to double using the number formatter. If the formatter is not
1018 valid, ::rtl::math::stringToDouble() with english separators will be used.
1019 @throws com::sun::star::lang::IllegalArgumentException
1020 on strings not representing any double value.
1021 @return the converted double value. */
1022 double convertToDouble(
1023 const ::rtl::OUString& rString ) const
1024 throw( CSS::lang::IllegalArgumentException );
1026 public:
1027 ScaAnyConverter(
1028 const CSS::uno::Reference< CSS::lang::XMultiServiceFactory >& xServiceFact );
1029 ~ScaAnyConverter();
1031 /// Initializing with current language settings
1032 void init(
1033 const CSS::uno::Reference< CSS::beans::XPropertySet >& xPropSet )
1034 throw( CSS::uno::RuntimeException );
1036 /** Converts an Any to double (without initialization).
1037 The Any can be empty or contain a double or string.
1038 @throws com::sun::star::lang::IllegalArgumentException
1039 on other Any types or on invalid strings.
1040 @return sal_True if the Any contains a double or a non-empty valid string,
1041 sal_False if the Any is empty or the string is empty */
1042 sal_Bool getDouble(
1043 double& rfResult,
1044 const CSS::uno::Any& rAny ) const
1045 throw( CSS::lang::IllegalArgumentException );
1047 /** Converts an Any to double (with initialization).
1048 The Any can be empty or contain a double or string.
1049 @throws com::sun::star::lang::IllegalArgumentException
1050 on other Any types or on invalid strings.
1051 @return sal_True if the Any contains a double or a non-empty valid string,
1052 sal_False if the Any is empty or the string is empty */
1053 sal_Bool getDouble(
1054 double& rfResult,
1055 const CSS::uno::Reference< CSS::beans::XPropertySet >& xPropSet,
1056 const CSS::uno::Any& rAny )
1057 throw( CSS::uno::RuntimeException, CSS::lang::IllegalArgumentException );
1059 /** Converts an Any to double (with initialization).
1060 The Any can be empty or contain a double or string.
1061 @throws com::sun::star::lang::IllegalArgumentException
1062 on other Any types or on invalid strings.
1063 @return the value of the double or string or fDefault if the Any or string is empty */
1064 double getDouble(
1065 const CSS::uno::Reference< CSS::beans::XPropertySet >& xPropSet,
1066 const CSS::uno::Any& rAny,
1067 double fDefault )
1068 throw( CSS::uno::RuntimeException, CSS::lang::IllegalArgumentException );
1070 /** Converts an Any to sal_Int32 (with initialization).
1071 The Any can be empty or contain a double or string.
1072 @throws com::sun::star::lang::IllegalArgumentException
1073 on other Any types or on invalid values or strings.
1074 @return sal_True if the Any contains a double or a non-empty valid string,
1075 sal_False if the Any is empty or the string is empty */
1076 sal_Bool getInt32(
1077 sal_Int32& rnResult,
1078 const CSS::uno::Reference< CSS::beans::XPropertySet >& xPropSet,
1079 const CSS::uno::Any& rAny )
1080 throw( CSS::uno::RuntimeException, CSS::lang::IllegalArgumentException );
1082 /** Converts an Any to sal_Int32 (with initialization).
1083 The Any can be empty or contain a double or string.
1084 @throws com::sun::star::lang::IllegalArgumentException
1085 on other Any types or on invalid values or strings.
1086 @return the truncated value of the double or string or nDefault if the Any or string is empty */
1087 sal_Int32 getInt32(
1088 const CSS::uno::Reference< CSS::beans::XPropertySet >& xPropSet,
1089 const CSS::uno::Any& rAny,
1090 sal_Int32 nDefault )
1091 throw( CSS::uno::RuntimeException, CSS::lang::IllegalArgumentException );
1095 //-----------------------------------------------------------------------------
1098 #endif