Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / scaddins / source / analysis / analysishelper.hxx
blobcaf511594259212c298ae611f0a9b63cdaff375e
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 INCLUDED_SCADDINS_SOURCE_ANALYSIS_ANALYSISHELPER_HXX
20 #define INCLUDED_SCADDINS_SOURCE_ANALYSIS_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 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, bool bLeapYear1,
75 sal_uInt16 nDay2, sal_uInt16 nMonth2, sal_uInt16 nYear2,
76 bool bUSAMethod );
77 inline sal_Int32 GetDiffDate360( const css::uno::Reference< css::beans::XPropertySet >& xOpt, sal_Int32 nDate1, sal_Int32 nDate2, bool bUSAMethod );
78 sal_Int32 GetDiffDate360( sal_Int32 nNullDate, sal_Int32 nDate1, sal_Int32 nDate2, 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, bool bUsePlaces ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
99 double Erf( double fX );
100 double Erfc( double fX );
101 bool ParseDouble( const sal_Unicode*& rpDoubleAsString, double& rReturn );
102 OUString GetString( double fNumber, bool bLeadingSign = 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 enum FDCategory
149 FDCat_AddIn,
150 FDCat_DateTime,
151 FDCat_Finance,
152 FDCat_Inf,
153 FDCat_Math,
154 FDCat_Tech
158 struct FuncDataBase
160 const sal_Char* pIntName;
161 sal_uInt16 nUINameID; // resource ID to UI name
162 sal_uInt16 nDescrID; // resource ID to description, parameter names and ~ description
163 bool bDouble; // name already exist in Calc
164 bool bWithOpt; // first parameter is internal
165 sal_uInt16 nCompListID; // resource ID to list of valid names
166 sal_uInt16 nNumOfParams; // number of named / described parameters
167 FDCategory eCat; // function category
171 class FuncData
173 private:
174 OUString aIntName;
175 sal_uInt16 nUINameID;
176 sal_uInt16 nDescrID; // leads also to parameter descriptions!
177 bool bDouble; // flag for names that already exist in Calc
178 bool bWithOpt; // has internal parameter on first position
180 sal_uInt16 nParam; // num of parameters
181 sal_uInt16 nCompID;
182 std::vector<OUString> aCompList; // list of all valid names
183 FDCategory eCat; // function category
184 public:
185 FuncData( const FuncDataBase& rBaseData, ResMgr& );
186 virtual ~FuncData();
188 inline sal_uInt16 GetUINameID( void ) const;
189 inline sal_uInt16 GetDescrID( void ) const;
190 inline bool IsDouble( void ) const;
191 inline bool HasIntParam( void ) const;
193 sal_uInt16 GetStrIndex( sal_uInt16 nParamNum ) const;
194 inline bool Is( const OUString& rCompareTo ) const;
196 inline const std::vector<OUString> &
197 GetCompNameList( void ) const;
199 inline FDCategory GetCategory( void ) const;
203 class FuncDataList
205 OUString aLastName;
206 sal_uInt32 nLast;
207 std::vector<FuncData*> maVector;
208 public:
209 FuncDataList( ResMgr& );
210 virtual ~FuncDataList();
212 inline void Append( FuncData* pNew );
213 inline const FuncData* Get( sal_uInt32 nIndex ) const;
214 inline sal_uInt32 Count() const
215 { return maVector.size(); }
217 const FuncData* Get( const OUString& aProgrammaticName ) const;
220 class AnalysisResId : public ResId
222 public:
223 AnalysisResId( sal_uInt16 nId, ResMgr& rResMgr );
227 class AnalysisRscStrLoader : public Resource
229 private:
230 OUString aStr;
231 public:
232 AnalysisRscStrLoader( sal_uInt16 nRsc, sal_uInt16 nStrId, ResMgr& rResMgr ) :
233 Resource( AnalysisResId( nRsc, rResMgr ) ),
234 aStr( AnalysisResId( nStrId, rResMgr ) )
236 FreeResource();
239 const OUString& GetString() const { return aStr; }
245 /// sorted list with unique sal_Int32 values
246 class SortedIndividualInt32List
248 private:
249 std::vector<sal_Int32> maVector;
250 protected:
251 void Insert( sal_Int32 nDay );
252 void Insert( sal_Int32 nDay, sal_Int32 nNullDate, bool bInsertOnWeekend );
253 void Insert( double fDay, sal_Int32 nNullDate, bool bInsertOnWeekend )
254 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
256 /** @param rAnyConv must be an initialized ScaAnyConmverter
257 @param bInsertOnWeekend insertion mode: false = holidays on weekend are omitted */
258 void InsertHolidayList(
259 const ScaAnyConverter& rAnyConv,
260 const css::uno::Any& rHolAny,
261 sal_Int32 nNullDate,
262 bool bInsertOnWeekend ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
264 public:
265 SortedIndividualInt32List();
266 virtual ~SortedIndividualInt32List();
268 inline sal_uInt32 Count() const
269 { return maVector.size(); }
271 /// @return element on position nIndex or 0 on invalid index
272 inline sal_Int32 Get( sal_uInt32 n ) const
273 { return maVector[n]; }
275 /// @return true if nVal (internal date representation) is contained
276 bool Find( sal_Int32 nVal ) const;
278 /** @param rAnyConv is an initialized or uninitialized ScaAnyConverter
279 @param bInsertOnWeekend insertion mode: false = holidays on weekend are omitted */
280 void InsertHolidayList(
281 ScaAnyConverter& rAnyConv,
282 const css::uno::Reference< css::beans::XPropertySet >& xOptions,
283 const css::uno::Any& rHolAny,
284 sal_Int32 nNullDate,
285 bool bInsertOnWeekend ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
289 class ScaDoubleList
291 private:
292 std::vector<double> maVector;
293 protected:
294 inline void ListAppend( double fValue ) { maVector.push_back(fValue); }
296 inline void Append( double fValue ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException )
297 { if( CheckInsert( fValue ) ) ListAppend( fValue ); }
299 /** @param rAnyConv must be an initialized ScaAnyConmverter
300 @param bIgnoreEmpty handling of empty Any's/strings: false = inserted as 0.0; true = omitted */
301 void Append(
302 const ScaAnyConverter& rAnyConv,
303 const css::uno::Any& rAny,
304 bool bIgnoreEmpty ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
306 /** @param rAnyConv must be an initialized ScaAnyConmverter
307 @param bIgnoreEmpty handling of empty Any's/strings: false = inserted as 0.0; true = omitted */
308 void Append(
309 const ScaAnyConverter& rAnyConv,
310 const css::uno::Sequence< css::uno::Any >& rAnySeq,
311 bool bIgnoreEmpty ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
313 /** @param rAnyConv must be an initialized ScaAnyConmverter
314 @param bIgnoreEmpty handling of empty Any's/strings: false = inserted as 0.0; true = omitted */
315 void Append(
316 const ScaAnyConverter& rAnyConv,
317 const css::uno::Sequence< css::uno::Sequence< css::uno::Any > >& rAnySeq,
318 bool bIgnoreEmpty ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
320 public:
321 virtual ~ScaDoubleList() {}
323 inline sal_uInt32 Count() const
324 { return maVector.size(); }
325 inline double Get( sal_uInt32 n ) const
326 { return maVector[n]; }
328 void Append( const css::uno::Sequence< css::uno::Sequence< double > >& rValueArr )
329 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
330 void Append( const css::uno::Sequence< css::uno::Sequence< sal_Int32 > >& rValueArr )
331 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
333 /** @param rAnyConv is an initialized or uninitialized ScaAnyConverter
334 @param bIgnoreEmpty handling of empty Any's/strings: false = inserted as 0.0; true = omitted */
335 void Append(
336 ScaAnyConverter& rAnyConv,
337 const css::uno::Reference< css::beans::XPropertySet >& xOpt,
338 const css::uno::Sequence< css::uno::Any >& rAnySeq,
339 bool bIgnoreEmpty = true ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
341 virtual bool CheckInsert( double fValue ) const
342 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
346 /// stores double values >0.0, throws exception for double values <0.0, does nothing for 0.0
347 class ScaDoubleListGT0 : public ScaDoubleList
349 public:
350 virtual bool CheckInsert( double fValue ) const
351 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException ) SAL_OVERRIDE;
355 /// stores double values >=0.0, throws exception for double values <0.0
356 class ScaDoubleListGE0 : public ScaDoubleList
358 public:
359 virtual bool CheckInsert( double fValue ) const
360 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException ) SAL_OVERRIDE;
364 class Complex
366 double r;
367 double i;
368 sal_Unicode c;
370 public:
371 inline Complex( double fReal, double fImag = 0.0, sal_Unicode cC = '\0' );
372 Complex( const OUString& rComplexAsString ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
374 inline static bool IsImagUnit( sal_Unicode c );
375 static bool ParseString( const OUString& rComplexAsString, Complex& rReturn );
376 OUString GetString() const throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
378 inline double Real( void ) const;
379 inline double Imag( void ) const;
381 double Arg( void ) const throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
382 inline double Abs( void ) const;
384 // following functions change the complex number itself to avoid unnecessary copy actions!
385 void Power( double fPower ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
386 void Sqrt( void );
387 void Sin( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
388 void Cos( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
389 void Div( const Complex& rDivisor ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
390 void Exp( void );
391 inline void Conjugate( void );
392 void Ln( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
393 void Log10( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
394 void Log2( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
395 inline void Mult( double fFact );
396 inline void Mult( const Complex& rMult );
397 inline void Sub( const Complex& rMult );
398 inline void Add( const Complex& rAdd );
399 void Tan( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
400 void Sec( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
401 void Csc( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
402 void Cot( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
403 void Sinh( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
404 void Cosh( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
405 void Sech( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
406 void Csch( void ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
411 enum ComplListAppendHandl
413 AH_EmptyAsErr,
414 AH_EmpyAs0,
415 AH_IgnoreEmpty
419 class ComplexList
421 private:
422 std::vector<Complex*> maVector;
423 public:
424 virtual ~ComplexList();
426 inline const Complex* Get( sal_uInt32 nIndex ) const;
428 inline bool empty( void ) const
429 { return maVector.empty(); }
430 inline sal_uInt32 Count( void ) const
431 { return maVector.size(); }
433 inline void Append( Complex* pNew );
434 void Append( const css::uno::Sequence< css::uno::Sequence< OUString > >& rComplexNumList, ComplListAppendHandl eAH = AH_EmpyAs0 ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
435 void Append( const css::uno::Sequence< css::uno::Any >& aMultPars,ComplListAppendHandl eAH = AH_EmpyAs0 ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
439 enum ConvertDataClass
441 CDC_Mass, CDC_Length, CDC_Time, CDC_Pressure, CDC_Force, CDC_Energy, CDC_Power, CDC_Magnetism,
442 CDC_Temperature, CDC_Volume, CDC_Area, CDC_Speed, CDC_Information
446 #define INV_MATCHLEV 1764 // guess, what this is... :-)
449 class ConvertDataList;
452 class ConvertData
454 protected:
455 friend class ConvertDataList;
456 double fConst;
457 OUString aName;
458 ConvertDataClass eClass;
459 bool bPrefixSupport;
460 public:
461 ConvertData(
462 const sal_Char pUnitName[],
463 double fConvertConstant,
464 ConvertDataClass eClass,
465 bool bPrefSupport = false );
467 virtual ~ConvertData();
469 sal_Int16 GetMatchingLevel( const OUString& rRef ) const;
470 // 0.0 = no equality
471 // 1.0 = matches exact
472 // rest = matches without an assumed prefix of one character
473 // rest gives power for 10 represented by the prefix (e.g. 3 for k or -9 for n
475 virtual double Convert( double fVal, const ConvertData& rTo,
476 sal_Int16 nMatchLevelFrom, sal_Int16 nMatchLevelTo ) const throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
477 // converts fVal from this unit to rFrom unit
478 // throws exception if not from same class
479 // this implementation is for proportional cases only
480 virtual double ConvertToBase( double fVal, sal_Int16 nMatchLevel ) const;
481 virtual double ConvertFromBase( double fVal, sal_Int16 nMatchLevel ) const;
483 inline ConvertDataClass Class( void ) const;
484 inline bool IsPrefixSupport( void ) const;
488 class ConvertDataLinear : public ConvertData
490 protected:
491 double fOffs;
492 public:
493 inline ConvertDataLinear(
494 const sal_Char pUnitName[],
495 double fConvertConstant,
496 double fConvertOffset,
497 ConvertDataClass eClass,
498 bool bPrefSupport = false );
500 virtual ~ConvertDataLinear();
502 virtual double Convert( double fVal, const ConvertData& rTo,
503 sal_Int16 nMatchLevelFrom, sal_Int16 nMatchLevelTo ) const throw( css::uno::RuntimeException, css::lang::IllegalArgumentException ) SAL_OVERRIDE;
504 // for cases where f(x) = a + bx applies (e.g. Temperatures)
506 virtual double ConvertToBase( double fVal, sal_Int16 nMatchLevel ) const SAL_OVERRIDE;
507 virtual double ConvertFromBase( double fVal, sal_Int16 nMatchLevel ) const SAL_OVERRIDE;
511 class ConvertDataList
513 private:
514 std::vector<ConvertData*> maVector;
515 public:
516 ConvertDataList( void );
517 virtual ~ConvertDataList();
519 double Convert( double fVal, const OUString& rFrom, const OUString& rTo ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
523 inline bool IsLeapYear( sal_uInt16 n )
525 return ( (( ( n % 4 ) == 0 ) && ( ( n % 100 ) != 0)) || ( ( n % 400 ) == 0 ) );
529 inline sal_Int32 GetDiffDate360( const css::uno::Reference< css::beans::XPropertySet >& xOpt, sal_Int32 nDate1, sal_Int32 nDate2, bool bUSAMethod )
531 return GetDiffDate360( GetNullDate( xOpt ), nDate1, nDate2, bUSAMethod );
535 inline sal_Int16 GetDayOfWeek( sal_Int32 n )
536 { // monday = 0, ..., sunday = 6
537 return static_cast< sal_Int16 >( ( n - 1 ) % 7 );
541 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 )
543 return GetYearFrac( GetNullDate( xOpt ), nStartDate, nEndDate, nMode );
547 inline void AlignDate( sal_uInt16& rD, sal_uInt16 nM, sal_uInt16 nY )
549 sal_uInt16 nMax = DaysInMonth( nM, nY );
551 if( rD > nMax )
552 rD = nMax;
556 inline sal_uInt16 FuncData::GetUINameID( void ) const
558 return nUINameID;
562 inline sal_uInt16 FuncData::GetDescrID( void ) const
564 return nDescrID;
568 inline bool FuncData::IsDouble( void ) const
570 return bDouble;
574 inline bool FuncData::HasIntParam( void ) const
576 return bWithOpt;
580 inline bool FuncData::Is( const OUString& r ) const
582 return aIntName == r;
586 inline const std::vector<OUString> & FuncData::GetCompNameList( void ) const
588 return aCompList;
592 inline FDCategory FuncData::GetCategory( void ) const
594 return eCat;
598 inline void FuncDataList::Append( FuncData* p )
600 maVector.push_back( p );
604 inline const FuncData* FuncDataList::Get( sal_uInt32 n ) const
606 return maVector[n];
610 inline Complex::Complex( double fReal, double fImag, sal_Unicode cC ) :
611 r( fReal ), i( fImag ), c( cC )
616 inline double Complex::Real( void ) const
618 return r;
622 inline double Complex::Imag( void ) const
624 return i;
628 inline double Complex::Abs( void ) const
630 return sqrt( r * r + i * i );
634 void Complex::Conjugate( void )
636 i = -i;
640 inline void Complex::Mult( double f )
642 i *= f;
643 r *= f;
647 inline void Complex::Mult( const Complex& rM )
649 double r_ = r;
650 double i_ = i;
652 r = r_ * rM.r - i_ * rM.i;
653 i = r_ * rM.i + i_ * rM.r;
655 if( !c ) c = rM.c;
659 inline void Complex::Sub( const Complex& rC )
661 r -= rC.r;
662 i -= rC.i;
663 if( !c ) c = rC.c;
667 inline void Complex::Add( const Complex& rAdd )
669 r += rAdd.r;
670 i += rAdd.i;
671 if( !c ) c = rAdd.c;
675 inline const Complex* ComplexList::Get( sal_uInt32 n ) const
677 return maVector[n];
681 inline void ComplexList::Append( Complex* p )
683 maVector.push_back(p);
687 inline ConvertDataClass ConvertData::Class( void ) const
689 return eClass;
692 inline bool ConvertData::IsPrefixSupport( void ) const
694 return bPrefixSupport;
697 inline ConvertDataLinear::ConvertDataLinear( const sal_Char* p, double fC, double fO, ConvertDataClass e,
698 bool bPrefSupport ) :
699 ConvertData( p, fC, e, bPrefSupport ),
700 fOffs( fO )
707 /// Helper class for date calculation for various financial functions
708 class ScaDate
710 private:
711 sal_uInt16 nOrigDay; /// is the day of the original date.
712 sal_uInt16 nDay; /// is the calculated day depending on the current month/year.
713 sal_uInt16 nMonth; /// is the current month (one-based).
714 sal_uInt16 nYear; /// is the current year.
715 bool bLastDayMode : 1; /// if true, recalculate nDay after every calculation.
716 bool bLastDay : 1; /// is true, if original date was the last day in month.
717 bool b30Days : 1; /// is true, if every month has 30 days in calculations.
718 bool bUSMode : 1; /// is true, if the US method of 30-day-calculations is used.
720 /// Calculates nDay from nOrigDay and current date.
721 void setDay();
723 /// @return count of days in current month
724 inline sal_uInt16 getDaysInMonth() const;
725 /// @return count of days in given month
726 inline sal_uInt16 getDaysInMonth( sal_uInt16 _nMon ) const;
728 /// @ return count of days in the given month range
729 sal_Int32 getDaysInMonthRange( sal_uInt16 nFrom, sal_uInt16 nTo ) const;
730 /// @ return count of days in the given year range
731 sal_Int32 getDaysInYearRange( sal_uInt16 nFrom, sal_uInt16 nTo ) const;
733 /// Adds/subtracts the given count of years, does not adjust day.
734 void doAddYears( sal_Int32 nYearCount ) throw( css::lang::IllegalArgumentException );
736 public:
737 ScaDate();
738 /** @param nBase
739 date handling mode (days in month / days in year):
740 0 = 30 days / 360 days (US NASD)
741 1 = exact / exact
742 2 = exact / 360
743 3 = exact / 365
744 4 = 30 days / 360 days (Europe)
745 5 = exact / exact (no last day adjustment) */
746 ScaDate( sal_Int32 nNullDate, sal_Int32 nDate, sal_Int32 nBase );
747 ScaDate( const ScaDate& rCopy );
748 ScaDate& operator=( const ScaDate& rCopy );
750 /// @return the current month.
751 inline sal_uInt16 getMonth() const { return nMonth; };
752 /// @return the current year.
753 inline sal_uInt16 getYear() const { return nYear; };
755 /// adds/subtracts the given count of months, adjusts day
756 void addMonths( sal_Int32 nMonthCount ) throw( css::lang::IllegalArgumentException );
758 /// sets the given year, adjusts day
759 inline void setYear( sal_uInt16 nNewYear );
760 /// adds/subtracts the given count of years, adjusts day
761 inline void addYears( sal_Int32 nYearCount ) throw( css::lang::IllegalArgumentException );
763 /// @return the internal number of the current date
764 sal_Int32 getDate( sal_Int32 nNullDate ) const;
765 /// @return the number of days between the two dates
766 static sal_Int32 getDiff( const ScaDate& rFrom, const ScaDate& rTo ) throw( css::lang::IllegalArgumentException );
768 bool operator<( const ScaDate& rCmp ) const;
769 inline bool operator<=( const ScaDate& rCmp ) const { return !(rCmp < *this); }
770 inline bool operator>( const ScaDate& rCmp ) const { return rCmp < *this; }
771 inline bool operator>=( const ScaDate& rCmp ) const { return !(*this < rCmp); }
774 inline sal_uInt16 ScaDate::getDaysInMonth() const
776 return getDaysInMonth( nMonth );
779 inline sal_uInt16 ScaDate::getDaysInMonth( sal_uInt16 _nMon ) const
781 return b30Days ? 30 : DaysInMonth( _nMon, nYear );
784 inline void ScaDate::setYear( sal_uInt16 nNewYear )
786 nYear = nNewYear;
787 setDay();
790 inline void ScaDate::addYears( sal_Int32 nYearCount ) throw( css::lang::IllegalArgumentException )
792 doAddYears( nYearCount );
793 setDay();
797 /// Helper class for Any->double conversion, using current language settings
798 class ScaAnyConverter
800 private:
801 css::uno::Reference< css::util::XNumberFormatter2 > xFormatter;
802 sal_Int32 nDefaultFormat;
803 bool bHasValidFormat;
805 /** Converts a string to double using the number formatter. If the formatter is not
806 valid, ::rtl::math::stringToDouble() with english separators will be used.
807 @throws com::sun::star::lang::IllegalArgumentException
808 on strings not representing any double value.
809 @return the converted double value. */
810 double convertToDouble(
811 const OUString& rString ) const
812 throw( css::lang::IllegalArgumentException );
814 public:
815 ScaAnyConverter(
816 const css::uno::Reference< css::uno::XComponentContext >& xContext );
817 ~ScaAnyConverter();
819 /// Initializing with current language settings
820 void init(
821 const css::uno::Reference< css::beans::XPropertySet >& xPropSet )
822 throw( css::uno::RuntimeException );
824 /** Converts an Any to double (without initialization).
825 The Any can be empty or contain a double or string.
826 @throws com::sun::star::lang::IllegalArgumentException
827 on other Any types or on invalid strings.
828 @return true if the Any contains a double or a non-empty valid string,
829 false if the Any is empty or the string is empty */
830 bool getDouble(
831 double& rfResult,
832 const css::uno::Any& rAny ) const
833 throw( css::lang::IllegalArgumentException );
835 /** Converts an Any to double (with initialization).
836 The Any can be empty or contain a double or string.
837 @throws com::sun::star::lang::IllegalArgumentException
838 on other Any types or on invalid strings.
839 @return true if the Any contains a double or a non-empty valid string,
840 false if the Any is empty or the string is empty */
841 bool getDouble(
842 double& rfResult,
843 const css::uno::Reference< css::beans::XPropertySet >& xPropSet,
844 const css::uno::Any& rAny )
845 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
847 /** Converts an Any to double (with initialization).
848 The Any can be empty or contain a double or string.
849 @throws com::sun::star::lang::IllegalArgumentException
850 on other Any types or on invalid strings.
851 @return the value of the double or string or fDefault if the Any or string is empty */
852 double getDouble(
853 const css::uno::Reference< css::beans::XPropertySet >& xPropSet,
854 const css::uno::Any& rAny,
855 double fDefault )
856 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
858 /** Converts an Any to sal_Int32 (with initialization).
859 The Any can be empty or contain a double or string.
860 @throws com::sun::star::lang::IllegalArgumentException
861 on other Any types or on invalid values or strings.
862 @return true if the Any contains a double or a non-empty valid string,
863 false if the Any is empty or the string is empty */
864 bool getInt32(
865 sal_Int32& rnResult,
866 const css::uno::Reference< css::beans::XPropertySet >& xPropSet,
867 const css::uno::Any& rAny )
868 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
870 /** Converts an Any to sal_Int32 (with initialization).
871 The Any can be empty or contain a double or string.
872 @throws com::sun::star::lang::IllegalArgumentException
873 on other Any types or on invalid values or strings.
874 @return the truncated value of the double or string or nDefault if the Any or string is empty */
875 sal_Int32 getInt32(
876 const css::uno::Reference< css::beans::XPropertySet >& xPropSet,
877 const css::uno::Any& rAny,
878 sal_Int32 nDefault )
879 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
884 #endif
886 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */