bump product version to 4.1.6.2
[LibreOffice.git] / scaddins / source / analysis / analysishelper.hxx
blob7a3da3335cf43bd8c946ad436703a211b50d74f0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
19 #ifndef ANALYSISHELPER_HXX
20 #define ANALYSISHELPER_HXX
23 #include <com/sun/star/lang/XServiceName.hpp>
24 #include <com/sun/star/lang/XServiceInfo.hpp>
25 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 #include <com/sun/star/uno/XComponentContext.hpp>
27 #include <com/sun/star/util/Date.hpp>
28 #include <com/sun/star/util/XNumberFormatter2.hpp>
29 #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
30 #include <com/sun/star/sheet/XAddIn.hpp>
31 #include <com/sun/star/sheet/addin/XAnalysis.hpp>
33 #include <math.h>
35 #include <tools/resid.hxx>
36 #include <tools/rc.hxx>
38 #include "analysisdefs.hxx"
41 class ResMgr;
42 class SortedIndividualInt32List;
43 class ScaAnyConverter;
46 #define PI 3.1415926535897932
47 #define EOL ( ( const sal_Char* ) 1 )
48 #define EOE ( ( const sal_Char* ) 2 )
51 inline sal_Bool IsLeapYear( sal_uInt16 nYear );
53 #ifdef DISABLE_DYNLOADING
55 // Avoid clash with the functions with same name in
56 // scaddins/source/datefunc/datefunc.cxx. I am not sure if each pair
57 // have identical semantics, but if yes, one copy should be enough,
58 // but what would be a suitable library where such functions could go?
59 // Or can the analysis library depend on the date library or the other
60 // way around?
62 #define DaysInMonth analysishelper_DaysInMonth
63 #define DateToDays analysishelper_DateToDays
64 #define DaysToDate analysishelper_DaysToDate
65 #define GetNullDate analysishelper_GetNullDate
67 #endif
69 sal_uInt16 DaysInMonth( sal_uInt16 nMonth, sal_uInt16 nYear );
70 sal_Int32 DateToDays( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear );
71 void DaysToDate( sal_Int32 nDays, sal_uInt16& rDay, sal_uInt16& rMonth, sal_uInt16& rYear ) throw( css::lang::IllegalArgumentException );
72 sal_Int32 GetNullDate( const css::uno::Reference< css::beans::XPropertySet >& xOptions ) throw( css::uno::RuntimeException );
73 sal_Int32 GetDiffDate360(
74 sal_uInt16 nDay1, sal_uInt16 nMonth1, sal_uInt16 nYear1, sal_Bool bLeapYear1,
75 sal_uInt16 nDay2, sal_uInt16 nMonth2, sal_uInt16 nYear2,
76 sal_Bool bUSAMethod );
77 inline sal_Int32 GetDiffDate360( const css::uno::Reference< css::beans::XPropertySet >& xOpt, sal_Int32 nDate1, sal_Int32 nDate2, sal_Bool bUSAMethod );
78 sal_Int32 GetDiffDate360( sal_Int32 nNullDate, sal_Int32 nDate1, sal_Int32 nDate2, sal_Bool bUSAMethod );
80 sal_Int32 GetDaysInYears( sal_uInt16 nYear1, sal_uInt16 nYear2 );
81 inline sal_Int16 GetDayOfWeek( sal_Int32 nDate );
82 sal_Int32 GetDiffDate( sal_Int32 nNullDate, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode,
83 sal_Int32* pOptDaysIn1stYear = NULL ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
84 double GetYearDiff( sal_Int32 nNullDate, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode )
85 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
86 sal_Int32 GetDaysInYear( sal_Int32 nNullDate, sal_Int32 nDate, sal_Int32 nMode ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
87 double GetYearFrac( sal_Int32 nNullDate, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode )
88 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
89 inline double GetYearFrac( const css::uno::Reference< css::beans::XPropertySet >& xOpt, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode )
90 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
91 inline void AlignDate( sal_uInt16& rDay, sal_uInt16 nMonth, sal_uInt16 nYear );
93 double BinomialCoefficient( double n, double k );
94 double GetGcd( double f1, double f2 );
95 double ConvertToDec( const OUString& rFromNum, sal_uInt16 nBaseFrom, sal_uInt16 nCharLim ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
96 OUString ConvertFromDec(
97 double fNum, double fMin, double fMax, sal_uInt16 nBase,
98 sal_Int32 nPlaces, sal_Int32 nMaxPlaces, sal_Bool bUsePlaces ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
99 double Erf( double fX );
100 double Erfc( double fX );
101 sal_Bool ParseDouble( const sal_Unicode*& rpDoubleAsString, double& rReturn );
102 OUString GetString( double fNumber, sal_Bool bLeadingSign = sal_False, sal_uInt16 nMaxNumOfDigits = 15 );
103 inline double Exp10( sal_Int16 nPower ); // 10 ^ nPower
105 double GetAmordegrc( sal_Int32 nNullDate, double fCost, sal_Int32 nDate, sal_Int32 nFirstPer,
106 double fRestVal, double fPer, double fRate, sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
107 double GetAmorlinc( sal_Int32 nNullDate, double fCost, sal_Int32 nDate, sal_Int32 nFirstPer,
108 double fRestVal, double fPer, double fRate, sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
109 double GetDuration( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, double fCoup,
110 double fYield, sal_Int32 nFreq, sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
111 double GetYieldmat( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nIssue,
112 double fRate, double fPrice, sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
113 double GetOddfprice( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nIssue,
114 sal_Int32 nFirstCoup, double fRate, double fYield, double fRedemp,
115 sal_Int32 nFreq, sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
116 double getYield_( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, double fCoup, double fPrice,
117 double fRedemp, sal_Int32 nFreq, sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
118 double getPrice_( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, double fRate, double fYield,
119 double fRedemp, sal_Int32 nFreq, sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
120 double GetOddfyield( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nIssue,
121 sal_Int32 nFirstCoup, double fRate, double fPrice, double fRedemp,
122 sal_Int32 nFreq, sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
123 double GetOddlprice( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nLastInterest,
124 double fRate, double fYield, double fRedemp, sal_Int32 nFreq, sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
125 double GetOddlyield( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nLastInterest,
126 double fRate, double fPrice, double fRedemp, sal_Int32 nFreq, sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
127 double GetRmz( double fZins, double fZzr, double fBw, double fZw, sal_Int32 nF );
128 double GetZw( double fZins, double fZzr, double fRmz, double fBw, sal_Int32 nF );
130 double GetCouppcd( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq,
131 sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
132 double GetCoupncd( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq,
133 sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
134 double GetCoupdaybs( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq,
135 sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
136 double GetCoupdaysnc( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq,
137 sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
139 double GetCoupnum( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat,
140 sal_Int32 nFreq, sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
141 double GetCoupdays( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq,
142 sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
147 //-----------------------------------------------------------------------------
151 class MyList
153 private:
154 static const sal_uInt32 nStartSize;
155 static const sal_uInt32 nIncrSize;
157 void** pData; // pointer array
158 sal_uInt32 nSize; // array size
159 sal_uInt32 nNew; // next index to be inserted at
160 sal_uInt32 nAct; // actual for iterations
162 void _Grow( void );
163 inline void Grow( void );
164 protected:
165 public:
166 MyList( void );
167 virtual ~MyList();
169 inline const void* GetObject( sal_uInt32 nIndex ) const;
170 inline const void* First( void );
171 inline const void* Next( void );
173 inline void Append( void* pNewElement );
174 void Insert( void* pNewLement, sal_uInt32 nPlace );
176 inline sal_uInt32 Count( void ) const;
182 class StringList : protected MyList
184 public:
185 virtual ~StringList();
187 inline const OUString* First( void );
188 inline const OUString* Next( void );
189 inline const OUString* Get( sal_uInt32 nIndex ) const;
191 using MyList::Append;
192 inline void Append( OUString* pNew );
193 inline void Append( const OUString& rNew );
195 using MyList::Count;
201 enum FDCategory
203 FDCat_AddIn,
204 FDCat_DateTime,
205 FDCat_Finance,
206 FDCat_Inf,
207 FDCat_Math,
208 FDCat_Tech
212 struct FuncDataBase
214 const sal_Char* pIntName;
215 sal_uInt16 nUINameID; // resource ID to UI name
216 sal_uInt16 nDescrID; // resource ID to description, parameter names and ~ description
217 sal_Bool bDouble; // name already exist in Calc
218 sal_Bool bWithOpt; // first parameter is internal
219 sal_uInt16 nCompListID; // resource ID to list of valid names
220 sal_uInt16 nNumOfParams; // number of named / described parameters
221 FDCategory eCat; // function category
227 class FuncData
229 private:
230 OUString aIntName;
231 sal_uInt16 nUINameID;
232 sal_uInt16 nDescrID; // leads also to parameter descriptions!
233 sal_Bool bDouble; // flag for names that already exist in Calc
234 sal_Bool bWithOpt; // has internal parameter on first position
236 sal_uInt16 nParam; // num of parameters
237 sal_uInt16 nCompID;
238 StringList aCompList; // list of all valid names
239 FDCategory eCat; // function category
240 public:
241 FuncData( const FuncDataBase& rBaseData, ResMgr& );
242 virtual ~FuncData();
244 inline sal_uInt16 GetUINameID( void ) const;
245 inline sal_uInt16 GetDescrID( void ) const;
246 inline sal_Bool IsDouble( void ) const;
247 inline sal_Bool HasIntParam( void ) const;
249 sal_uInt16 GetStrIndex( sal_uInt16 nParamNum ) const;
250 inline sal_Bool Is( const OUString& rCompareTo ) const;
252 inline const StringList& GetCompNameList( void ) const;
254 inline FDCategory GetCategory( void ) const;
260 class CStrList : private MyList
262 public:
263 using MyList::Append;
264 inline void Append( const sal_Char* pNew );
265 inline const sal_Char* Get( sal_uInt32 nIndex ) const;
266 using MyList::Count;
272 class FuncDataList : private MyList
274 OUString aLastName;
275 sal_uInt32 nLast;
276 public:
277 FuncDataList( ResMgr& );
278 virtual ~FuncDataList();
279 using MyList::Append;
280 inline void Append( FuncData* pNew );
281 inline const FuncData* Get( sal_uInt32 nIndex ) const;
282 using MyList::Count;
284 const FuncData* Get( const OUString& aProgrammaticName ) const;
289 class AnalysisResId : public ResId
291 public:
292 AnalysisResId( sal_uInt16 nId, ResMgr& rResMgr );
298 class AnalysisRscStrLoader : public Resource
300 private:
301 String aStr;
302 public:
303 AnalysisRscStrLoader( sal_uInt16 nRsc, sal_uInt16 nStrId, ResMgr& rResMgr ) :
304 Resource( AnalysisResId( nRsc, rResMgr ) ),
305 aStr( AnalysisResId( nStrId, rResMgr ) )
307 FreeResource();
310 const String& GetString() const { return aStr; }
316 //-----------------------------------------------------------------------------
318 /// sorted list with unique sal_Int32 values
319 class SortedIndividualInt32List : private MyList
321 protected:
322 using MyList::Insert;
323 void Insert( sal_Int32 nDay );
324 void Insert( sal_Int32 nDay, sal_Int32 nNullDate, sal_Bool bInsertOnWeekend );
325 void Insert( double fDay, sal_Int32 nNullDate, sal_Bool bInsertOnWeekend )
326 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
328 /** @param rAnyConv must be an initialized ScaAnyConmverter
329 @param bInsertOnWeekend insertion mode: sal_False = holidays on weekend are omitted */
330 void InsertHolidayList(
331 const ScaAnyConverter& rAnyConv,
332 const css::uno::Any& rHolAny,
333 sal_Int32 nNullDate,
334 sal_Bool bInsertOnWeekend ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
336 public:
337 SortedIndividualInt32List();
338 virtual ~SortedIndividualInt32List();
340 using MyList::Count;
342 /// @return element on position nIndex or 0 on invalid index
343 inline sal_Int32 Get( sal_uInt32 nIndex ) const
344 { return (sal_Int32)(sal_IntPtr) MyList::GetObject( nIndex ); }
346 /// @return sal_True if nVal (internal date representation) is contained
347 sal_Bool Find( sal_Int32 nVal ) const;
349 /** @param rAnyConv is an initialized or uninitialized ScaAnyConverter
350 @param bInsertOnWeekend insertion mode: sal_False = holidays on weekend are omitted */
351 void InsertHolidayList(
352 ScaAnyConverter& rAnyConv,
353 const css::uno::Reference< css::beans::XPropertySet >& xOptions,
354 const css::uno::Any& rHolAny,
355 sal_Int32 nNullDate,
356 sal_Bool bInsertOnWeekend ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
360 //-----------------------------------------------------------------------------
362 class ScaDoubleList : protected MyList
364 protected:
365 inline void ListAppend( double fValue ) { MyList::Append( new double( fValue ) ); }
367 using MyList::Append;
368 inline void Append( double fValue ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException )
369 { if( CheckInsert( fValue ) ) ListAppend( fValue ); }
371 /** @param rAnyConv must be an initialized ScaAnyConmverter
372 @param bIgnoreEmpty handling of empty Any's/strings: sal_False = inserted as 0.0; sal_True = omitted */
373 void Append(
374 const ScaAnyConverter& rAnyConv,
375 const css::uno::Any& rAny,
376 sal_Bool bIgnoreEmpty ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
378 /** @param rAnyConv must be an initialized ScaAnyConmverter
379 @param bIgnoreEmpty handling of empty Any's/strings: sal_False = inserted as 0.0; sal_True = omitted */
380 void Append(
381 const ScaAnyConverter& rAnyConv,
382 const css::uno::Sequence< css::uno::Any >& rAnySeq,
383 sal_Bool bIgnoreEmpty ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
385 /** @param rAnyConv must be an initialized ScaAnyConmverter
386 @param bIgnoreEmpty handling of empty Any's/strings: sal_False = inserted as 0.0; sal_True = omitted */
387 void Append(
388 const ScaAnyConverter& rAnyConv,
389 const css::uno::Sequence< css::uno::Sequence< css::uno::Any > >& rAnySeq,
390 sal_Bool bIgnoreEmpty ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
392 public:
393 virtual ~ScaDoubleList();
395 using MyList::Count;
396 inline const double* Get( sal_uInt32 nIndex ) const
397 { return static_cast< const double* >( MyList::GetObject( nIndex ) ); }
399 inline const double* First() { return static_cast< const double* >( MyList::First() ); }
400 inline const double* Next() { return static_cast< const double* >( MyList::Next() ); }
402 void Append( const css::uno::Sequence< css::uno::Sequence< double > >& rValueArr )
403 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
404 void Append( const css::uno::Sequence< css::uno::Sequence< sal_Int32 > >& rValueArr )
405 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
407 /** @param rAnyConv is an initialized or uninitialized ScaAnyConverter
408 @param bIgnoreEmpty handling of empty Any's/strings: sal_False = inserted as 0.0; sal_True = omitted */
409 void Append(
410 ScaAnyConverter& rAnyConv,
411 const css::uno::Reference< css::beans::XPropertySet >& xOpt,
412 const css::uno::Sequence< css::uno::Any >& rAnySeq,
413 sal_Bool bIgnoreEmpty = sal_True ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
415 virtual sal_Bool CheckInsert( double fValue ) const
416 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
420 //-----------------------------------------------------------------------------
422 /// stores double values >0.0, throws exception for double values <0.0, does nothing for 0.0
423 class ScaDoubleListGT0 : public ScaDoubleList
425 public:
426 virtual sal_Bool CheckInsert( double fValue ) const
427 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
431 //-----------------------------------------------------------------------------
433 /// stores double values >=0.0, throws exception for double values <0.0
434 class ScaDoubleListGE0 : public ScaDoubleList
436 public:
437 virtual sal_Bool CheckInsert( double fValue ) const
438 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
442 //-----------------------------------------------------------------------------
444 class Complex
446 double r;
447 double i;
448 sal_Unicode c;
450 public:
451 inline Complex( double fReal, double fImag = 0.0, sal_Unicode cC = '\0' );
452 Complex( const OUString& rComplexAsString ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
454 inline static sal_Bool IsImagUnit( sal_Unicode c );
455 static sal_Bool ParseString( const OUString& rComplexAsString, Complex& rReturn );
456 OUString GetString() const throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
458 inline double Real( void ) const;
459 inline double Imag( void ) const;
461 double Arg( void ) const throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
462 inline double Abs( void ) const;
464 // following functions change the complex number itself to avoid unnecessary copy actions!
465 void Power( double fPower ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
466 void Sqrt( void );
467 void Sin( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
468 void Cos( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
469 void Div( const Complex& rDivisor ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
470 void Exp( void );
471 inline void Conjugate( void );
472 void Ln( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
473 void Log10( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
474 void Log2( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
475 inline void Mult( double fFact );
476 inline void Mult( const Complex& rMult );
477 inline void Sub( const Complex& rMult );
478 inline void Add( const Complex& rAdd );
479 void Tan( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
480 void Sec( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
481 void Csc( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
482 void Cot( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
483 void Sinh( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
484 void Cosh( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
485 void Sech( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
486 void Csch( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
493 enum ComplListAppendHandl
495 AH_EmptyAsErr,
496 AH_EmpyAs0,
497 AH_IgnoreEmpty
501 class ComplexList : protected MyList
503 public:
504 virtual ~ComplexList();
506 inline const Complex* Get( sal_uInt32 nIndex ) const;
507 inline const Complex* First( void );
508 inline const Complex* Next( void );
510 using MyList::Count;
512 using MyList::Append;
513 inline void Append( Complex* pNew );
514 void Append( const css::uno::Sequence< css::uno::Sequence< OUString > >& rComplexNumList, ComplListAppendHandl eAH = AH_EmpyAs0 ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
515 void Append( const css::uno::Sequence< css::uno::Any >& aMultPars,ComplListAppendHandl eAH = AH_EmpyAs0 ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
521 enum ConvertDataClass
523 CDC_Mass, CDC_Length, CDC_Time, CDC_Pressure, CDC_Force, CDC_Energy, CDC_Power, CDC_Magnetism,
524 CDC_Temperature, CDC_Volume, CDC_Area, CDC_Speed, CDC_Information
528 #define INV_MATCHLEV 1764 // guess, what this is... :-)
531 class ConvertDataList;
536 class ConvertData
538 protected:
539 friend class ConvertDataList;
540 double fConst;
541 OUString aName;
542 ConvertDataClass eClass;
543 sal_Bool bPrefixSupport;
544 public:
545 ConvertData(
546 const sal_Char pUnitName[],
547 double fConvertConstant,
548 ConvertDataClass eClass,
549 sal_Bool bPrefSupport = sal_False );
551 virtual ~ConvertData();
553 sal_Int16 GetMatchingLevel( const OUString& rRef ) const;
554 // 0.0 = no equality
555 // 1.0 = matches exact
556 // rest = matches without an assumed prefix of one character
557 // rest gives power for 10 represented by the prefix (e.g. 3 for k or -9 for n
559 virtual double Convert( double fVal, const ConvertData& rTo,
560 sal_Int16 nMatchLevelFrom, sal_Int16 nMatchLevelTo ) const throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
561 // converts fVal from this unit to rFrom unit
562 // throws exception if not from same class
563 // this implementation is for proportional cases only
564 virtual double ConvertToBase( double fVal, sal_Int16 nMatchLevel ) const;
565 virtual double ConvertFromBase( double fVal, sal_Int16 nMatchLevel ) const;
567 inline ConvertDataClass Class( void ) const;
568 inline sal_Bool IsPrefixSupport( void ) const;
574 class ConvertDataLinear : public ConvertData
576 protected:
577 double fOffs;
578 public:
579 inline ConvertDataLinear(
580 const sal_Char pUnitName[],
581 double fConvertConstant,
582 double fConvertOffset,
583 ConvertDataClass eClass,
584 sal_Bool bPrefSupport = sal_False );
586 virtual ~ConvertDataLinear();
588 virtual double Convert( double fVal, const ConvertData& rTo,
589 sal_Int16 nMatchLevelFrom, sal_Int16 nMatchLevelTo ) const throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
590 // for cases where f(x) = a + bx applies (e.g. Temperatures)
592 virtual double ConvertToBase( double fVal, sal_Int16 nMatchLevel ) const;
593 virtual double ConvertFromBase( double fVal, sal_Int16 nMatchLevel ) const;
599 class ConvertDataList : protected MyList
601 private:
602 protected:
603 inline ConvertData* First( void );
604 inline ConvertData* Next( void );
605 public:
606 ConvertDataList( void );
607 virtual ~ConvertDataList();
609 double Convert( double fVal, const OUString& rFrom, const OUString& rTo ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
615 inline sal_Bool IsLeapYear( sal_uInt16 n )
617 return ( (( ( n % 4 ) == 0 ) && ( ( n % 100 ) != 0)) || ( ( n % 400 ) == 0 ) );
621 inline sal_Int32 GetDiffDate360( const css::uno::Reference< css::beans::XPropertySet >& xOpt, sal_Int32 nDate1, sal_Int32 nDate2, sal_Bool bUSAMethod )
623 return GetDiffDate360( GetNullDate( xOpt ), nDate1, nDate2, bUSAMethod );
627 inline sal_Int16 GetDayOfWeek( sal_Int32 n )
628 { // monday = 0, ..., sunday = 6
629 return static_cast< sal_Int16 >( ( n - 1 ) % 7 );
633 inline double GetYearFrac( const css::uno::Reference< css::beans::XPropertySet >& xOpt, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException )
635 return GetYearFrac( GetNullDate( xOpt ), nStartDate, nEndDate, nMode );
639 inline void AlignDate( sal_uInt16& rD, sal_uInt16 nM, sal_uInt16 nY )
641 sal_uInt16 nMax = DaysInMonth( nM, nY );
643 if( rD > nMax )
644 rD = nMax;
648 inline void MyList::Grow( void )
650 if( nNew >= nSize )
651 _Grow();
655 inline const void* MyList::GetObject( sal_uInt32 n ) const
657 if( n < nNew )
658 return pData[ n ];
659 else
660 return NULL;
664 inline const void* MyList::First( void )
666 nAct = 0;
667 if( nNew )
668 return pData[ 0 ];
669 else
670 return NULL;
674 inline const void* MyList::Next( void )
676 nAct++;
677 if( nAct < nNew )
678 return pData[ nAct ];
679 else
681 nAct--;
682 return NULL;
687 inline void MyList::Append( void* p )
689 Grow();
690 pData[ nNew ] = p;
691 nNew++;
695 inline sal_uInt32 MyList::Count( void ) const
697 return nNew;
703 inline const OUString* StringList::First( void )
705 return ( const OUString* ) MyList::First();
709 inline const OUString* StringList::Next( void )
711 return ( const OUString* ) MyList::Next();
715 inline const OUString* StringList::Get( sal_uInt32 n ) const
717 return ( const OUString* ) MyList::GetObject( n );
721 inline void StringList::Append( OUString* p )
723 MyList::Append( p );
727 inline void StringList::Append( const OUString& r )
729 MyList::Append( new OUString( r ) );
735 inline sal_uInt16 FuncData::GetUINameID( void ) const
737 return nUINameID;
741 inline sal_uInt16 FuncData::GetDescrID( void ) const
743 return nDescrID;
747 inline sal_Bool FuncData::IsDouble( void ) const
749 return bDouble;
753 inline sal_Bool FuncData::HasIntParam( void ) const
755 return bWithOpt;
759 inline sal_Bool FuncData::Is( const OUString& r ) const
761 return aIntName == r;
765 inline const StringList& FuncData::GetCompNameList( void ) const
767 return aCompList;
771 inline FDCategory FuncData::GetCategory( void ) const
773 return eCat;
779 inline void CStrList::Append( const sal_Char* p )
781 MyList::Append( ( void* ) p );
785 inline const sal_Char* CStrList::Get( sal_uInt32 n ) const
787 return ( const sal_Char* ) MyList::GetObject( n );
793 inline void FuncDataList::Append( FuncData* p )
795 MyList::Append( p );
799 inline const FuncData* FuncDataList::Get( sal_uInt32 n ) const
801 return ( const FuncData* ) MyList::GetObject( n );
805 inline Complex::Complex( double fReal, double fImag, sal_Unicode cC ) :
806 r( fReal ), i( fImag ), c( cC )
811 inline double Complex::Real( void ) const
813 return r;
817 inline double Complex::Imag( void ) const
819 return i;
823 inline double Complex::Abs( void ) const
825 return sqrt( r * r + i * i );
829 void Complex::Conjugate( void )
831 i = -i;
835 inline void Complex::Mult( double f )
837 i *= f;
838 r *= f;
842 inline void Complex::Mult( const Complex& rM )
844 double r_ = r;
845 double i_ = i;
847 r = r_ * rM.r - i_ * rM.i;
848 i = r_ * rM.i + i_ * rM.r;
850 if( !c ) c = rM.c;
854 inline void Complex::Sub( const Complex& rC )
856 r -= rC.r;
857 i -= rC.i;
858 if( !c ) c = rC.c;
862 inline void Complex::Add( const Complex& rAdd )
864 r += rAdd.r;
865 i += rAdd.i;
866 if( !c ) c = rAdd.c;
872 inline const Complex* ComplexList::Get( sal_uInt32 n ) const
874 return ( const Complex* ) MyList::GetObject( n );
878 inline const Complex* ComplexList::First( void )
880 return ( const Complex* ) MyList::First();
884 inline const Complex* ComplexList::Next( void )
886 return ( const Complex* ) MyList::Next();
890 inline void ComplexList::Append( Complex* p )
892 MyList::Append( p );
898 inline ConvertDataClass ConvertData::Class( void ) const
900 return eClass;
905 inline sal_Bool ConvertData::IsPrefixSupport( void ) const
907 return bPrefixSupport;
910 inline ConvertDataLinear::ConvertDataLinear( const sal_Char* p, double fC, double fO, ConvertDataClass e,
911 sal_Bool bPrefSupport ) :
912 ConvertData( p, fC, e, bPrefSupport ),
913 fOffs( fO )
920 inline ConvertData* ConvertDataList::First( void )
922 return ( ConvertData* ) MyList::First();
926 inline ConvertData* ConvertDataList::Next( void )
928 return ( ConvertData* ) MyList::Next();
931 //-----------------------------------------------------------------------------
933 /// Helper class for date calculation for various financial functions
934 class ScaDate
936 private:
937 sal_uInt16 nOrigDay; /// is the day of the original date.
938 sal_uInt16 nDay; /// is the calculated day depending on the current month/year.
939 sal_uInt16 nMonth; /// is the current month (one-based).
940 sal_uInt16 nYear; /// is the current year.
941 sal_Bool bLastDayMode : 1; /// if sal_True, recalculate nDay after every calculation.
942 sal_Bool bLastDay : 1; /// is sal_True, if original date was the last day in month.
943 sal_Bool b30Days : 1; /// is sal_True, if every month has 30 days in calculations.
944 sal_Bool bUSMode : 1; /// is sal_True, if the US method of 30-day-calculations is used.
946 /// Calculates nDay from nOrigDay and current date.
947 void setDay();
949 /// @return count of days in current month
950 inline sal_uInt16 getDaysInMonth() const;
951 /// @return count of days in given month
952 inline sal_uInt16 getDaysInMonth( sal_uInt16 _nMon ) const;
954 /// @ return count of days in the given month range
955 sal_Int32 getDaysInMonthRange( sal_uInt16 nFrom, sal_uInt16 nTo ) const;
956 /// @ return count of days in the given year range
957 sal_Int32 getDaysInYearRange( sal_uInt16 nFrom, sal_uInt16 nTo ) const;
959 /// Adds/subtracts the given count of years, does not adjust day.
960 void doAddYears( sal_Int32 nYearCount ) throw( css::lang::IllegalArgumentException );
962 public:
963 ScaDate();
964 /** @param nBase
965 date handling mode (days in month / days in year):
966 0 = 30 days / 360 days (US NASD)
967 1 = exact / exact
968 2 = exact / 360
969 3 = exact / 365
970 4 = 30 days / 360 days (Europe)
971 5 = exact / exact (no last day adjustment) */
972 ScaDate( sal_Int32 nNullDate, sal_Int32 nDate, sal_Int32 nBase );
973 ScaDate( const ScaDate& rCopy );
974 ScaDate& operator=( const ScaDate& rCopy );
976 /// @return the current month.
977 inline sal_uInt16 getMonth() const { return nMonth; };
978 /// @return the current year.
979 inline sal_uInt16 getYear() const { return nYear; };
981 /// adds/subtracts the given count of months, adjusts day
982 void addMonths( sal_Int32 nMonthCount ) throw( css::lang::IllegalArgumentException );
984 /// sets the given year, adjusts day
985 inline void setYear( sal_uInt16 nNewYear );
986 /// adds/subtracts the given count of years, adjusts day
987 inline void addYears( sal_Int32 nYearCount ) throw( css::lang::IllegalArgumentException );
989 /// @return the internal number of the current date
990 sal_Int32 getDate( sal_Int32 nNullDate ) const;
991 /// @return the number of days between the two dates
992 static sal_Int32 getDiff( const ScaDate& rFrom, const ScaDate& rTo ) throw( css::lang::IllegalArgumentException );
994 sal_Bool operator<( const ScaDate& rCmp ) const;
995 inline sal_Bool operator<=( const ScaDate& rCmp ) const { return !(rCmp < *this); }
996 inline sal_Bool operator>( const ScaDate& rCmp ) const { return rCmp < *this; }
997 inline sal_Bool operator>=( const ScaDate& rCmp ) const { return !(*this < rCmp); }
1000 inline sal_uInt16 ScaDate::getDaysInMonth() const
1002 return getDaysInMonth( nMonth );
1005 inline sal_uInt16 ScaDate::getDaysInMonth( sal_uInt16 _nMon ) const
1007 return b30Days ? 30 : DaysInMonth( _nMon, nYear );
1010 inline void ScaDate::setYear( sal_uInt16 nNewYear )
1012 nYear = nNewYear;
1013 setDay();
1016 inline void ScaDate::addYears( sal_Int32 nYearCount ) throw( css::lang::IllegalArgumentException )
1018 doAddYears( nYearCount );
1019 setDay();
1023 //-----------------------------------------------------------------------------
1025 /// Helper class for Any->double conversion, using current language settings
1026 class ScaAnyConverter
1028 private:
1029 css::uno::Reference< css::util::XNumberFormatter2 > xFormatter;
1030 sal_Int32 nDefaultFormat;
1031 sal_Bool bHasValidFormat;
1033 /** Converts a string to double using the number formatter. If the formatter is not
1034 valid, ::rtl::math::stringToDouble() with english separators will be used.
1035 @throws com::sun::star::lang::IllegalArgumentException
1036 on strings not representing any double value.
1037 @return the converted double value. */
1038 double convertToDouble(
1039 const OUString& rString ) const
1040 throw( css::lang::IllegalArgumentException );
1042 public:
1043 ScaAnyConverter(
1044 const css::uno::Reference< css::uno::XComponentContext >& xContext );
1045 ~ScaAnyConverter();
1047 /// Initializing with current language settings
1048 void init(
1049 const css::uno::Reference< css::beans::XPropertySet >& xPropSet )
1050 throw( css::uno::RuntimeException );
1052 /** Converts an Any to double (without initialization).
1053 The Any can be empty or contain a double or string.
1054 @throws com::sun::star::lang::IllegalArgumentException
1055 on other Any types or on invalid strings.
1056 @return sal_True if the Any contains a double or a non-empty valid string,
1057 sal_False if the Any is empty or the string is empty */
1058 sal_Bool getDouble(
1059 double& rfResult,
1060 const css::uno::Any& rAny ) const
1061 throw( css::lang::IllegalArgumentException );
1063 /** Converts an Any to double (with initialization).
1064 The Any can be empty or contain a double or string.
1065 @throws com::sun::star::lang::IllegalArgumentException
1066 on other Any types or on invalid strings.
1067 @return sal_True if the Any contains a double or a non-empty valid string,
1068 sal_False if the Any is empty or the string is empty */
1069 sal_Bool getDouble(
1070 double& rfResult,
1071 const css::uno::Reference< css::beans::XPropertySet >& xPropSet,
1072 const css::uno::Any& rAny )
1073 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
1075 /** Converts an Any to double (with initialization).
1076 The Any can be empty or contain a double or string.
1077 @throws com::sun::star::lang::IllegalArgumentException
1078 on other Any types or on invalid strings.
1079 @return the value of the double or string or fDefault if the Any or string is empty */
1080 double getDouble(
1081 const css::uno::Reference< css::beans::XPropertySet >& xPropSet,
1082 const css::uno::Any& rAny,
1083 double fDefault )
1084 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
1086 /** Converts an Any to sal_Int32 (with initialization).
1087 The Any can be empty or contain a double or string.
1088 @throws com::sun::star::lang::IllegalArgumentException
1089 on other Any types or on invalid values or strings.
1090 @return sal_True if the Any contains a double or a non-empty valid string,
1091 sal_False if the Any is empty or the string is empty */
1092 sal_Bool getInt32(
1093 sal_Int32& rnResult,
1094 const css::uno::Reference< css::beans::XPropertySet >& xPropSet,
1095 const css::uno::Any& rAny )
1096 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
1098 /** Converts an Any to sal_Int32 (with initialization).
1099 The Any can be empty or contain a double or string.
1100 @throws com::sun::star::lang::IllegalArgumentException
1101 on other Any types or on invalid values or strings.
1102 @return the truncated value of the double or string or nDefault if the Any or string is empty */
1103 sal_Int32 getInt32(
1104 const css::uno::Reference< css::beans::XPropertySet >& xPropSet,
1105 const css::uno::Any& rAny,
1106 sal_Int32 nDefault )
1107 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
1111 //-----------------------------------------------------------------------------
1114 #endif
1116 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */