merge the formfield patch from ooo-build
[ooovba.git] / scaddins / source / analysis / analysishelper.hxx
blobc7ca244208f7214ae6363aab3935b26d87351a9e
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, CDC_Information
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 sal_Bool bPrefixSupport;
537 public:
538 ConvertData(
539 const sal_Char pUnitName[],
540 double fConvertConstant,
541 ConvertDataClass eClass,
542 sal_Bool bPrefSupport = sal_False );
544 virtual ~ConvertData();
546 sal_Int16 GetMatchingLevel( const STRING& rRef ) const;
547 // 0.0 = no equality
548 // 1.0 = matches exact
549 // rest = matches without an assumed prefix of one character
550 // rest gives power for 10 represented by the prefix (e.g. 3 for k or -9 for n
552 virtual double Convert( double fVal, const ConvertData& rTo,
553 sal_Int16 nMatchLevelFrom, sal_Int16 nMatchLevelTo ) const THROWDEF_RTE_IAE;
554 // converts fVal from this unit to rFrom unit
555 // throws exception if not from same class
556 // this implementation is for proportional cases only
557 virtual double ConvertToBase( double fVal, sal_Int16 nMatchLevel ) const;
558 virtual double ConvertFromBase( double fVal, sal_Int16 nMatchLevel ) const;
560 inline ConvertDataClass Class( void ) const;
561 inline sal_Bool IsPrefixSupport( void ) const;
567 class ConvertDataLinear : public ConvertData
569 protected:
570 double fOffs;
571 public:
572 inline ConvertDataLinear(
573 const sal_Char pUnitName[],
574 double fConvertConstant,
575 double fConvertOffset,
576 ConvertDataClass eClass,
577 sal_Bool bPrefSupport = sal_False );
579 virtual ~ConvertDataLinear();
581 virtual double Convert( double fVal, const ConvertData& rTo,
582 sal_Int16 nMatchLevelFrom, sal_Int16 nMatchLevelTo ) const THROWDEF_RTE_IAE;
583 // for cases where f(x) = a + bx applies (e.g. Temperatures)
585 virtual double ConvertToBase( double fVal, sal_Int16 nMatchLevel ) const;
586 virtual double ConvertFromBase( double fVal, sal_Int16 nMatchLevel ) const;
592 class ConvertDataList : protected MyList
594 private:
595 protected:
596 inline ConvertData* First( void );
597 inline ConvertData* Next( void );
598 public:
599 ConvertDataList( void );
600 virtual ~ConvertDataList();
602 double Convert( double fVal, const STRING& rFrom, const STRING& rTo ) THROWDEF_RTE_IAE;
608 inline sal_Bool IsLeapYear( sal_uInt16 n )
610 return ( (( ( n % 4 ) == 0 ) && ( ( n % 100 ) != 0)) || ( ( n % 400 ) == 0 ) );
614 inline sal_Int32 GetDiffDate360( constREFXPS& xOpt, sal_Int32 nDate1, sal_Int32 nDate2, sal_Bool bUSAMethod )
616 return GetDiffDate360( GetNullDate( xOpt ), nDate1, nDate2, bUSAMethod );
620 inline sal_Int16 GetDayOfWeek( sal_Int32 n )
621 { // monday = 0, ..., sunday = 6
622 return static_cast< sal_Int16 >( ( n - 1 ) % 7 );
626 inline double GetYearFrac( constREFXPS& xOpt, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode ) THROWDEF_RTE_IAE
628 return GetYearFrac( GetNullDate( xOpt ), nStartDate, nEndDate, nMode );
632 inline void AlignDate( sal_uInt16& rD, sal_uInt16 nM, sal_uInt16 nY )
634 sal_uInt16 nMax = DaysInMonth( nM, nY );
636 if( rD > nMax )
637 rD = nMax;
641 inline void MyList::Grow( void )
643 if( nNew >= nSize )
644 _Grow();
648 inline const void* MyList::GetObject( sal_uInt32 n ) const
650 if( n < nNew )
651 return pData[ n ];
652 else
653 return NULL;
657 inline const void* MyList::First( void )
659 nAct = 0;
660 if( nNew )
661 return pData[ 0 ];
662 else
663 return NULL;
667 inline const void* MyList::Next( void )
669 nAct++;
670 if( nAct < nNew )
671 return pData[ nAct ];
672 else
674 nAct--;
675 return NULL;
680 inline void MyList::Append( void* p )
682 Grow();
683 pData[ nNew ] = p;
684 nNew++;
688 inline sal_uInt32 MyList::Count( void ) const
690 return nNew;
696 inline const STRING* StringList::First( void )
698 return ( const STRING* ) MyList::First();
702 inline const STRING* StringList::Next( void )
704 return ( const STRING* ) MyList::Next();
708 inline const STRING* StringList::Get( sal_uInt32 n ) const
710 return ( const STRING* ) MyList::GetObject( n );
714 inline void StringList::Append( STRING* p )
716 MyList::Append( p );
720 inline void StringList::Append( const STRING& r )
722 MyList::Append( new STRING( r ) );
728 inline sal_uInt16 FuncData::GetUINameID( void ) const
730 return nUINameID;
734 inline sal_uInt16 FuncData::GetDescrID( void ) const
736 return nDescrID;
740 inline sal_Bool FuncData::IsDouble( void ) const
742 return bDouble;
746 inline sal_Bool FuncData::HasIntParam( void ) const
748 return bWithOpt;
752 inline sal_Bool FuncData::Is( const ::rtl::OUString& r ) const
754 return aIntName == r;
758 inline const StringList& FuncData::GetCompNameList( void ) const
760 return aCompList;
764 inline FDCategory FuncData::GetCategory( void ) const
766 return eCat;
772 inline void CStrList::Append( const sal_Char* p )
774 MyList::Append( ( void* ) p );
778 inline const sal_Char* CStrList::Get( sal_uInt32 n ) const
780 return ( const sal_Char* ) MyList::GetObject( n );
786 inline void FuncDataList::Append( FuncData* p )
788 MyList::Append( p );
792 inline const FuncData* FuncDataList::Get( sal_uInt32 n ) const
794 return ( const FuncData* ) MyList::GetObject( n );
798 inline Complex::Complex( double fReal, double fImag, sal_Char cC ) :
799 r( fReal ), i( fImag ), c( cC )
804 inline double Complex::Real( void ) const
806 return r;
810 inline double Complex::Imag( void ) const
812 return i;
816 inline double Complex::Abs( void ) const
818 return sqrt( r * r + i * i );
822 void Complex::Conjugate( void )
824 i = -i;
828 inline void Complex::Mult( double f )
830 i *= f;
831 r *= f;
835 inline void Complex::Mult( const Complex& rM )
837 double r_ = r;
838 double i_ = i;
840 r = r_ * rM.r - i_ * rM.i;
841 i = r_ * rM.i + i_ * rM.r;
843 if( !c ) c = rM.c;
847 inline void Complex::Sub( const Complex& rC )
849 r -= rC.r;
850 i -= rC.i;
851 if( !c ) c = rC.c;
855 inline void Complex::Add( const Complex& rAdd )
857 r += rAdd.r;
858 i += rAdd.i;
859 if( !c ) c = rAdd.c;
865 inline const Complex* ComplexList::Get( sal_uInt32 n ) const
867 return ( const Complex* ) MyList::GetObject( n );
871 inline const Complex* ComplexList::First( void )
873 return ( const Complex* ) MyList::First();
877 inline const Complex* ComplexList::Next( void )
879 return ( const Complex* ) MyList::Next();
883 inline void ComplexList::Append( Complex* p )
885 MyList::Append( p );
891 inline ConvertDataClass ConvertData::Class( void ) const
893 return eClass;
898 inline sal_Bool ConvertData::IsPrefixSupport( void ) const
900 return bPrefixSupport;
903 inline ConvertDataLinear::ConvertDataLinear( const sal_Char* p, double fC, double fO, ConvertDataClass e,
904 sal_Bool bPrefSupport ) :
905 ConvertData( p, fC, e, bPrefSupport ),
906 fOffs( fO )
913 inline ConvertData* ConvertDataList::First( void )
915 return ( ConvertData* ) MyList::First();
919 inline ConvertData* ConvertDataList::Next( void )
921 return ( ConvertData* ) MyList::Next();
924 //-----------------------------------------------------------------------------
926 /// Helper class for date calculation for various financial functions
927 class ScaDate
929 private:
930 sal_uInt16 nOrigDay; /// is the day of the original date.
931 sal_uInt16 nDay; /// is the calculated day depending on the current month/year.
932 sal_uInt16 nMonth; /// is the current month (one-based).
933 sal_uInt16 nYear; /// is the current year.
934 sal_Bool bLastDayMode : 1; /// if sal_True, recalculate nDay after every calculation.
935 sal_Bool bLastDay : 1; /// is sal_True, if original date was the last day in month.
936 sal_Bool b30Days : 1; /// is sal_True, if every month has 30 days in calculations.
937 sal_Bool bUSMode : 1; /// is sal_True, if the US method of 30-day-calculations is used.
939 /// Calculates nDay from nOrigDay and current date.
940 void setDay();
942 /// @return count of days in current month
943 inline sal_uInt16 getDaysInMonth() const;
944 /// @return count of days in given month
945 inline sal_uInt16 getDaysInMonth( sal_uInt16 _nMon ) const;
947 /// @ return count of days in the given month range
948 sal_Int32 getDaysInMonthRange( sal_uInt16 nFrom, sal_uInt16 nTo ) const;
949 /// @ return count of days in the given year range
950 sal_Int32 getDaysInYearRange( sal_uInt16 nFrom, sal_uInt16 nTo ) const;
952 /// Adds/subtracts the given count of years, does not adjust day.
953 void doAddYears( sal_Int32 nYearCount ) throw( CSS::lang::IllegalArgumentException );
955 public:
956 ScaDate();
957 /** @param nBase
958 date handling mode (days in month / days in year):
959 0 = 30 days / 360 days (US NASD)
960 1 = exact / exact
961 2 = exact / 360
962 3 = exact / 365
963 4 = 30 days / 360 days (Europe)
964 5 = exact / exact (no last day adjustment) */
965 ScaDate( sal_Int32 nNullDate, sal_Int32 nDate, sal_Int32 nBase );
966 ScaDate( const ScaDate& rCopy );
967 ScaDate& operator=( const ScaDate& rCopy );
969 /// @return the current month.
970 inline sal_uInt16 getMonth() const { return nMonth; };
971 /// @return the current year.
972 inline sal_uInt16 getYear() const { return nYear; };
974 /// adds/subtracts the given count of months, adjusts day
975 void addMonths( sal_Int32 nMonthCount ) throw( CSS::lang::IllegalArgumentException );
977 /// sets the given year, adjusts day
978 inline void setYear( sal_uInt16 nNewYear );
979 /// adds/subtracts the given count of years, adjusts day
980 inline void addYears( sal_Int32 nYearCount ) throw( CSS::lang::IllegalArgumentException );
982 /// @return the internal number of the current date
983 sal_Int32 getDate( sal_Int32 nNullDate ) const;
984 /// @return the number of days between the two dates
985 static sal_Int32 getDiff( const ScaDate& rFrom, const ScaDate& rTo ) throw( CSS::lang::IllegalArgumentException );
987 sal_Bool operator<( const ScaDate& rCmp ) const;
988 inline sal_Bool operator<=( const ScaDate& rCmp ) const { return !(rCmp < *this); }
989 inline sal_Bool operator>( const ScaDate& rCmp ) const { return rCmp < *this; }
990 inline sal_Bool operator>=( const ScaDate& rCmp ) const { return !(*this < rCmp); }
993 inline sal_uInt16 ScaDate::getDaysInMonth() const
995 return getDaysInMonth( nMonth );
998 inline sal_uInt16 ScaDate::getDaysInMonth( sal_uInt16 _nMon ) const
1000 return b30Days ? 30 : DaysInMonth( _nMon, nYear );
1003 inline void ScaDate::setYear( sal_uInt16 nNewYear )
1005 nYear = nNewYear;
1006 setDay();
1009 inline void ScaDate::addYears( sal_Int32 nYearCount ) throw( CSS::lang::IllegalArgumentException )
1011 doAddYears( nYearCount );
1012 setDay();
1016 //-----------------------------------------------------------------------------
1018 /// Helper class for Any->double conversion, using current language settings
1019 class ScaAnyConverter
1021 private:
1022 CSS::uno::Reference< CSS::util::XNumberFormatter > xFormatter;
1023 sal_Int32 nDefaultFormat;
1024 sal_Bool bHasValidFormat;
1026 /** Converts a string to double using the number formatter. If the formatter is not
1027 valid, ::rtl::math::stringToDouble() with english separators will be used.
1028 @throws com::sun::star::lang::IllegalArgumentException
1029 on strings not representing any double value.
1030 @return the converted double value. */
1031 double convertToDouble(
1032 const ::rtl::OUString& rString ) const
1033 throw( CSS::lang::IllegalArgumentException );
1035 public:
1036 ScaAnyConverter(
1037 const CSS::uno::Reference< CSS::lang::XMultiServiceFactory >& xServiceFact );
1038 ~ScaAnyConverter();
1040 /// Initializing with current language settings
1041 void init(
1042 const CSS::uno::Reference< CSS::beans::XPropertySet >& xPropSet )
1043 throw( CSS::uno::RuntimeException );
1045 /** Converts an Any to double (without initialization).
1046 The Any can be empty or contain a double or string.
1047 @throws com::sun::star::lang::IllegalArgumentException
1048 on other Any types or on invalid strings.
1049 @return sal_True if the Any contains a double or a non-empty valid string,
1050 sal_False if the Any is empty or the string is empty */
1051 sal_Bool getDouble(
1052 double& rfResult,
1053 const CSS::uno::Any& rAny ) const
1054 throw( CSS::lang::IllegalArgumentException );
1056 /** Converts an Any to double (with initialization).
1057 The Any can be empty or contain a double or string.
1058 @throws com::sun::star::lang::IllegalArgumentException
1059 on other Any types or on invalid strings.
1060 @return sal_True if the Any contains a double or a non-empty valid string,
1061 sal_False if the Any is empty or the string is empty */
1062 sal_Bool getDouble(
1063 double& rfResult,
1064 const CSS::uno::Reference< CSS::beans::XPropertySet >& xPropSet,
1065 const CSS::uno::Any& rAny )
1066 throw( CSS::uno::RuntimeException, CSS::lang::IllegalArgumentException );
1068 /** Converts an Any to double (with initialization).
1069 The Any can be empty or contain a double or string.
1070 @throws com::sun::star::lang::IllegalArgumentException
1071 on other Any types or on invalid strings.
1072 @return the value of the double or string or fDefault if the Any or string is empty */
1073 double getDouble(
1074 const CSS::uno::Reference< CSS::beans::XPropertySet >& xPropSet,
1075 const CSS::uno::Any& rAny,
1076 double fDefault )
1077 throw( CSS::uno::RuntimeException, CSS::lang::IllegalArgumentException );
1079 /** Converts an Any to sal_Int32 (with initialization).
1080 The Any can be empty or contain a double or string.
1081 @throws com::sun::star::lang::IllegalArgumentException
1082 on other Any types or on invalid values or strings.
1083 @return sal_True if the Any contains a double or a non-empty valid string,
1084 sal_False if the Any is empty or the string is empty */
1085 sal_Bool getInt32(
1086 sal_Int32& rnResult,
1087 const CSS::uno::Reference< CSS::beans::XPropertySet >& xPropSet,
1088 const CSS::uno::Any& rAny )
1089 throw( CSS::uno::RuntimeException, CSS::lang::IllegalArgumentException );
1091 /** Converts an Any to sal_Int32 (with initialization).
1092 The Any can be empty or contain a double or string.
1093 @throws com::sun::star::lang::IllegalArgumentException
1094 on other Any types or on invalid values or strings.
1095 @return the truncated value of the double or string or nDefault if the Any or string is empty */
1096 sal_Int32 getInt32(
1097 const CSS::uno::Reference< CSS::beans::XPropertySet >& xPropSet,
1098 const CSS::uno::Any& rAny,
1099 sal_Int32 nDefault )
1100 throw( CSS::uno::RuntimeException, CSS::lang::IllegalArgumentException );
1104 //-----------------------------------------------------------------------------
1107 #endif