Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <com/sun/star/util/XNumberFormatTypes.hpp>
21 : #include <com/sun/star/util/NumberFormatter.hpp>
22 :
23 : #include <string.h>
24 : #include <stdio.h>
25 : #include <tools/resary.hxx>
26 : #include <rtl/math.hxx>
27 : #include <sal/macros.h>
28 : #include "analysishelper.hxx"
29 : #include "analysis.hrc"
30 :
31 : using namespace ::com::sun::star;
32 :
33 : #define UNIQUE sal_False // function name does not exist in Calc
34 : #define DOUBLE sal_True // function name exists in Calc
35 :
36 : #define STDPAR sal_False // all parameters are described
37 : #define INTPAR sal_True // first parameter is internal
38 :
39 : #define FUNCDATA( FUNCNAME, DBL, OPT, NUMOFPAR, CAT ) \
40 : { "get" #FUNCNAME, ANALYSIS_FUNCNAME_##FUNCNAME, ANALYSIS_##FUNCNAME, DBL, OPT, ANALYSIS_DEFFUNCNAME_##FUNCNAME, NUMOFPAR, CAT }
41 :
42 : const FuncDataBase pFuncDatas[] =
43 : {
44 : // UNIQUE or INTPAR or
45 : // function name DOUBLE STDPAR # of param category
46 : FUNCDATA( Workday, UNIQUE, INTPAR, 3, FDCat_DateTime ),
47 : FUNCDATA( Yearfrac, UNIQUE, INTPAR, 3, FDCat_DateTime ),
48 : FUNCDATA( Edate, UNIQUE, INTPAR, 2, FDCat_DateTime ),
49 : FUNCDATA( Weeknum, DOUBLE, INTPAR, 2, FDCat_DateTime ),
50 : FUNCDATA( Eomonth, UNIQUE, INTPAR, 2, FDCat_DateTime ),
51 : FUNCDATA( Networkdays, UNIQUE, INTPAR, 3, FDCat_DateTime ),
52 : FUNCDATA( Iseven, DOUBLE, STDPAR, 1, FDCat_Inf ),
53 : FUNCDATA( Isodd, DOUBLE, STDPAR, 1, FDCat_Inf ),
54 : FUNCDATA( Multinomial, UNIQUE, STDPAR, 1, FDCat_Math ),
55 : FUNCDATA( Seriessum, UNIQUE, STDPAR, 4, FDCat_Math ),
56 : FUNCDATA( Quotient, UNIQUE, STDPAR, 2, FDCat_Math ),
57 : FUNCDATA( Mround, UNIQUE, STDPAR, 2, FDCat_Math ),
58 : FUNCDATA( Sqrtpi, UNIQUE, STDPAR, 1, FDCat_Math ),
59 : FUNCDATA( Randbetween, UNIQUE, STDPAR, 2, FDCat_Math ),
60 : FUNCDATA( Gcd, DOUBLE, INTPAR, 1, FDCat_Math ),
61 : FUNCDATA( Lcm, DOUBLE, INTPAR, 1, FDCat_Math ),
62 : FUNCDATA( Besseli, UNIQUE, STDPAR, 2, FDCat_Tech ),
63 : FUNCDATA( Besselj, UNIQUE, STDPAR, 2, FDCat_Tech ),
64 : FUNCDATA( Besselk, UNIQUE, STDPAR, 2, FDCat_Tech ),
65 : FUNCDATA( Bessely, UNIQUE, STDPAR, 2, FDCat_Tech ),
66 : FUNCDATA( Bin2Oct, UNIQUE, INTPAR, 2, FDCat_Tech ),
67 : FUNCDATA( Bin2Dec, UNIQUE, STDPAR, 1, FDCat_Tech ),
68 : FUNCDATA( Bin2Hex, UNIQUE, INTPAR, 2, FDCat_Tech ),
69 : FUNCDATA( Oct2Bin, UNIQUE, INTPAR, 2, FDCat_Tech ),
70 : FUNCDATA( Oct2Dec, UNIQUE, STDPAR, 1, FDCat_Tech ),
71 : FUNCDATA( Oct2Hex, UNIQUE, INTPAR, 2, FDCat_Tech ),
72 : FUNCDATA( Dec2Bin, UNIQUE, INTPAR, 2, FDCat_Tech ),
73 : FUNCDATA( Dec2Hex, UNIQUE, INTPAR, 2, FDCat_Tech ),
74 : FUNCDATA( Dec2Oct, UNIQUE, INTPAR, 2, FDCat_Tech ),
75 : FUNCDATA( Hex2Bin, UNIQUE, INTPAR, 2, FDCat_Tech ),
76 : FUNCDATA( Hex2Dec, UNIQUE, STDPAR, 1, FDCat_Tech ),
77 : FUNCDATA( Hex2Oct, UNIQUE, INTPAR, 2, FDCat_Tech ),
78 : FUNCDATA( Delta, UNIQUE, INTPAR, 2, FDCat_Tech ),
79 : FUNCDATA( Erf, UNIQUE, INTPAR, 2, FDCat_Tech ),
80 : FUNCDATA( Erfc, UNIQUE, STDPAR, 1, FDCat_Tech ),
81 : FUNCDATA( Gestep, UNIQUE, INTPAR, 2, FDCat_Tech ),
82 : FUNCDATA( Factdouble, UNIQUE, STDPAR, 1, FDCat_Tech ),
83 : FUNCDATA( Imabs, UNIQUE, STDPAR, 1, FDCat_Tech ),
84 : FUNCDATA( Imaginary, UNIQUE, STDPAR, 1, FDCat_Tech ),
85 : FUNCDATA( Impower, UNIQUE, STDPAR, 2, FDCat_Tech ),
86 : FUNCDATA( Imargument, UNIQUE, STDPAR, 1, FDCat_Tech ),
87 : FUNCDATA( Imcos, UNIQUE, STDPAR, 1, FDCat_Tech ),
88 : FUNCDATA( Imdiv, UNIQUE, STDPAR, 2, FDCat_Tech ),
89 : FUNCDATA( Imexp, UNIQUE, STDPAR, 1, FDCat_Tech ),
90 : FUNCDATA( Imconjugate, UNIQUE, STDPAR, 1, FDCat_Tech ),
91 : FUNCDATA( Imln, UNIQUE, STDPAR, 1, FDCat_Tech ),
92 : FUNCDATA( Imlog10, UNIQUE, STDPAR, 1, FDCat_Tech ),
93 : FUNCDATA( Imlog2, UNIQUE, STDPAR, 1, FDCat_Tech ),
94 : FUNCDATA( Improduct, UNIQUE, INTPAR, 2, FDCat_Tech ),
95 : FUNCDATA( Imreal, UNIQUE, STDPAR, 1, FDCat_Tech ),
96 : FUNCDATA( Imsin, UNIQUE, STDPAR, 1, FDCat_Tech ),
97 : FUNCDATA( Imsub, UNIQUE, STDPAR, 2, FDCat_Tech ),
98 : FUNCDATA( Imsqrt, UNIQUE, STDPAR, 1, FDCat_Tech ),
99 : FUNCDATA( Imsum, UNIQUE, INTPAR, 1, FDCat_Tech ),
100 : FUNCDATA( Imtan, UNIQUE, STDPAR, 1, FDCat_Tech ),
101 : FUNCDATA( Imsec, UNIQUE, STDPAR, 1, FDCat_Tech ),
102 : FUNCDATA( Imcsc, UNIQUE, STDPAR, 1, FDCat_Tech ),
103 : FUNCDATA( Imcot, UNIQUE, STDPAR, 1, FDCat_Tech ),
104 : FUNCDATA( Imsinh, UNIQUE, STDPAR, 1, FDCat_Tech ),
105 : FUNCDATA( Imcosh, UNIQUE, STDPAR, 1, FDCat_Tech ),
106 : FUNCDATA( Imsech, UNIQUE, STDPAR, 1, FDCat_Tech ),
107 : FUNCDATA( Imcsch, UNIQUE, STDPAR, 1, FDCat_Tech ),
108 : FUNCDATA( Complex, UNIQUE, STDPAR, 3, FDCat_Tech ),
109 : FUNCDATA( Convert, DOUBLE, STDPAR, 3, FDCat_Tech ),
110 : FUNCDATA( Amordegrc, UNIQUE, INTPAR, 7, FDCat_Finance ),
111 : FUNCDATA( Amorlinc, UNIQUE, INTPAR, 7, FDCat_Finance ),
112 : FUNCDATA( Accrint, UNIQUE, INTPAR, 7, FDCat_Finance ),
113 : FUNCDATA( Accrintm, UNIQUE, INTPAR, 5, FDCat_Finance ),
114 : FUNCDATA( Received, UNIQUE, INTPAR, 5, FDCat_Finance ),
115 : FUNCDATA( Disc, UNIQUE, INTPAR, 5, FDCat_Finance ),
116 : FUNCDATA( Duration, DOUBLE, INTPAR, 6, FDCat_Finance ),
117 : FUNCDATA( Effect, DOUBLE, STDPAR, 2, FDCat_Finance ),
118 : FUNCDATA( Cumprinc, DOUBLE, STDPAR, 6, FDCat_Finance ),
119 : FUNCDATA( Cumipmt, DOUBLE, STDPAR, 6, FDCat_Finance ),
120 : FUNCDATA( Price, UNIQUE, INTPAR, 7, FDCat_Finance ),
121 : FUNCDATA( Pricedisc, UNIQUE, INTPAR, 5, FDCat_Finance ),
122 : FUNCDATA( Pricemat, UNIQUE, INTPAR, 6, FDCat_Finance ),
123 : FUNCDATA( Mduration, UNIQUE, INTPAR, 6, FDCat_Finance ),
124 : FUNCDATA( Nominal, DOUBLE, STDPAR, 2, FDCat_Finance ),
125 : FUNCDATA( Dollarfr, UNIQUE, STDPAR, 2, FDCat_Finance ),
126 : FUNCDATA( Dollarde, UNIQUE, STDPAR, 2, FDCat_Finance ),
127 : FUNCDATA( Yield, UNIQUE, INTPAR, 7, FDCat_Finance ),
128 : FUNCDATA( Yielddisc, UNIQUE, INTPAR, 5, FDCat_Finance ),
129 : FUNCDATA( Yieldmat, UNIQUE, INTPAR, 6, FDCat_Finance ),
130 : FUNCDATA( Tbilleq, UNIQUE, INTPAR, 3, FDCat_Finance ),
131 : FUNCDATA( Tbillprice, UNIQUE, INTPAR, 3, FDCat_Finance ),
132 : FUNCDATA( Tbillyield, UNIQUE, INTPAR, 3, FDCat_Finance ),
133 : FUNCDATA( Oddfprice, UNIQUE, INTPAR, 9, FDCat_Finance ),
134 : FUNCDATA( Oddfyield, UNIQUE, INTPAR, 9, FDCat_Finance ),
135 : FUNCDATA( Oddlprice, UNIQUE, INTPAR, 8, FDCat_Finance ),
136 : FUNCDATA( Oddlyield, UNIQUE, INTPAR, 8, FDCat_Finance ),
137 : FUNCDATA( Xirr, UNIQUE, INTPAR, 3, FDCat_Finance ),
138 : FUNCDATA( Xnpv, UNIQUE, STDPAR, 3, FDCat_Finance ),
139 : FUNCDATA( Intrate, UNIQUE, INTPAR, 5, FDCat_Finance ),
140 : FUNCDATA( Coupncd, UNIQUE, INTPAR, 4, FDCat_Finance ),
141 : FUNCDATA( Coupdays, UNIQUE, INTPAR, 4, FDCat_Finance ),
142 : FUNCDATA( Coupdaysnc, UNIQUE, INTPAR, 4, FDCat_Finance ),
143 : FUNCDATA( Coupdaybs, UNIQUE, INTPAR, 4, FDCat_Finance ),
144 : FUNCDATA( Couppcd, UNIQUE, INTPAR, 4, FDCat_Finance ),
145 : FUNCDATA( Coupnum, UNIQUE, INTPAR, 4, FDCat_Finance ),
146 : FUNCDATA( Fvschedule, UNIQUE, STDPAR, 2, FDCat_Finance )
147 : };
148 : #undef FUNCDATA
149 :
150 :
151 0 : sal_uInt16 DaysInMonth( sal_uInt16 nMonth, sal_uInt16 nYear )
152 : {
153 0 : if( (nMonth == 2) && IsLeapYear( nYear ) )
154 0 : return 29;
155 : static const sal_uInt16 aDaysInMonth[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
156 0 : return aDaysInMonth[ nMonth ];
157 : }
158 :
159 :
160 : /**
161 : * Convert a date to a count of days starting from 01/01/0001
162 : *
163 : * The internal representation of a Date used in this Addin
164 : * is the number of days between 01/01/0001 and the date
165 : * this function converts a Day , Month, Year representation
166 : * to this internal Date value.
167 : *
168 : */
169 :
170 0 : sal_Int32 DateToDays( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear )
171 : {
172 0 : sal_Int32 nDays = ((sal_Int32)nYear-1) * 365;
173 0 : nDays += ((nYear-1) / 4) - ((nYear-1) / 100) + ((nYear-1) / 400);
174 :
175 0 : for( sal_uInt16 i = 1; i < nMonth; i++ )
176 0 : nDays += DaysInMonth(i,nYear);
177 0 : nDays += nDay;
178 :
179 0 : return nDays;
180 : }
181 :
182 :
183 : /**
184 : * Convert a count of days starting from 01/01/0001 to a date
185 : *
186 : * The internal representation of a Date used in this Addin
187 : * is the number of days between 01/01/0001 and the date
188 : * this function converts this internal Date value
189 : * to a Day , Month, Year representation of a Date.
190 : *
191 : */
192 :
193 0 : void DaysToDate( sal_Int32 nDays, sal_uInt16& rDay, sal_uInt16& rMonth, sal_uInt16& rYear )
194 : throw( lang::IllegalArgumentException )
195 : {
196 0 : if( nDays < 0 )
197 0 : throw lang::IllegalArgumentException();
198 :
199 : sal_Int32 nTempDays;
200 0 : sal_Int32 i = 0;
201 : sal_Bool bCalc;
202 :
203 0 : do
204 : {
205 0 : nTempDays = nDays;
206 0 : rYear = (sal_uInt16)((nTempDays / 365) - i);
207 0 : nTempDays -= ((sal_Int32) rYear -1) * 365;
208 0 : nTempDays -= (( rYear -1) / 4) - (( rYear -1) / 100) + ((rYear -1) / 400);
209 0 : bCalc = sal_False;
210 0 : if ( nTempDays < 1 )
211 : {
212 0 : i++;
213 0 : bCalc = sal_True;
214 : }
215 : else
216 : {
217 0 : if ( nTempDays > 365 )
218 : {
219 0 : if ( (nTempDays != 366) || !IsLeapYear( rYear ) )
220 : {
221 0 : i--;
222 0 : bCalc = sal_True;
223 : }
224 : }
225 : }
226 : }
227 : while ( bCalc );
228 :
229 0 : rMonth = 1;
230 0 : while ( (sal_Int32)nTempDays > DaysInMonth( rMonth, rYear ) )
231 : {
232 0 : nTempDays -= DaysInMonth( rMonth, rYear );
233 0 : rMonth++;
234 : }
235 0 : rDay = (sal_uInt16)nTempDays;
236 0 : }
237 :
238 :
239 : /**
240 : * Get the null date used by the spreadsheet document
241 : *
242 : * The internal representation of a Date used in this Addin
243 : * is the number of days between 01/01/0001 and the date
244 : * this function returns this internal Date value for the document null date
245 : *
246 : */
247 :
248 0 : sal_Int32 GetNullDate( const uno::Reference< beans::XPropertySet >& xOpt ) throw( uno::RuntimeException )
249 : {
250 0 : if( xOpt.is() )
251 : {
252 : try
253 : {
254 0 : uno::Any aAny = xOpt->getPropertyValue( "NullDate" );
255 0 : util::Date aDate;
256 0 : if( aAny >>= aDate )
257 0 : return DateToDays( aDate.Day, aDate.Month, aDate.Year );
258 : }
259 0 : catch( uno::Exception& )
260 : {
261 : }
262 : }
263 :
264 : // no null date available -> no calculations possible
265 0 : throw uno::RuntimeException();
266 : }
267 :
268 :
269 0 : sal_Int32 GetDiffDate360(
270 : sal_uInt16 nDay1, sal_uInt16 nMonth1, sal_uInt16 nYear1, sal_Bool bLeapYear1,
271 : sal_uInt16 nDay2, sal_uInt16 nMonth2, sal_uInt16 nYear2,
272 : sal_Bool bUSAMethod )
273 : {
274 0 : if( nDay1 == 31 )
275 0 : nDay1--;
276 0 : else if( bUSAMethod && ( nMonth1 == 2 && ( nDay1 == 29 || ( nDay1 == 28 && !bLeapYear1 ) ) ) )
277 0 : nDay1 = 30;
278 :
279 0 : if( nDay2 == 31 )
280 : {
281 0 : if( bUSAMethod && nDay1 != 30 )
282 : {
283 0 : nDay2 = 1;
284 0 : if( nMonth2 == 12 )
285 : {
286 0 : nYear2++;
287 0 : nMonth2 = 1;
288 : }
289 : else
290 0 : nMonth2++;
291 : }
292 : else
293 0 : nDay2 = 30;
294 : }
295 :
296 0 : return nDay2 + nMonth2 * 30 + nYear2 * 360 - nDay1 - nMonth1 * 30 - nYear1 * 360;
297 : }
298 :
299 :
300 0 : sal_Int32 GetDiffDate360( sal_Int32 nNullDate, sal_Int32 nDate1, sal_Int32 nDate2, sal_Bool bUSAMethod )
301 : {
302 0 : nDate1 += nNullDate;
303 0 : nDate2 += nNullDate;
304 :
305 : sal_uInt16 nDay1, nMonth1, nYear1, nDay2, nMonth2, nYear2;
306 :
307 0 : DaysToDate( nDate1, nDay1, nMonth1, nYear1 );
308 0 : DaysToDate( nDate2, nDay2, nMonth2, nYear2 );
309 :
310 0 : return GetDiffDate360( nDay1, nMonth1, nYear1, IsLeapYear( nYear1 ), nDay2, nMonth2, nYear2, bUSAMethod );
311 : }
312 :
313 :
314 0 : sal_Int32 GetDaysInYears( sal_uInt16 nYear1, sal_uInt16 nYear2 )
315 : {
316 0 : sal_uInt16 nLeaps = 0;
317 0 : for( sal_uInt16 n = nYear1 ; n <= nYear2 ; n++ )
318 : {
319 0 : if( IsLeapYear( n ) )
320 0 : nLeaps++;
321 : }
322 :
323 0 : sal_uInt32 nSum = 1;
324 0 : nSum += nYear2;
325 0 : nSum -= nYear1;
326 0 : nSum *= 365;
327 0 : nSum += nLeaps;
328 :
329 0 : return nSum;
330 : }
331 :
332 :
333 0 : sal_Int32 GetDiffDate( sal_Int32 nNullDate, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode,
334 : sal_Int32* pOptDaysIn1stYear ) throw( uno::RuntimeException, lang::IllegalArgumentException )
335 : {
336 0 : sal_Bool bNeg = nStartDate > nEndDate;
337 :
338 0 : if( bNeg )
339 : {
340 0 : sal_Int32 n = nEndDate;
341 0 : nEndDate = nStartDate;
342 0 : nStartDate = n;
343 : }
344 :
345 : sal_Int32 nRet;
346 :
347 0 : switch( nMode )
348 : {
349 : case 0: // 0=USA (NASD) 30/360
350 : case 4: // 4=Europe 30/360
351 : {
352 : sal_uInt16 nD1, nM1, nY1, nD2, nM2, nY2;
353 :
354 0 : nStartDate += nNullDate;
355 0 : nEndDate += nNullDate;
356 :
357 0 : DaysToDate( nStartDate, nD1, nM1, nY1 );
358 0 : DaysToDate( nEndDate, nD2, nM2, nY2 );
359 :
360 0 : sal_Bool bLeap = IsLeapYear( nY1 );
361 : sal_Int32 nDays, nMonths;
362 :
363 0 : nMonths = nM2 - nM1;
364 0 : nDays = nD2 - nD1;
365 :
366 0 : nMonths += ( nY2 - nY1 ) * 12;
367 :
368 0 : nRet = nMonths * 30 + nDays;
369 0 : if( nMode == 0 && nM1 == 2 && nM2 != 2 && nY1 == nY2 )
370 0 : nRet -= bLeap? 1 : 2;
371 :
372 0 : if( pOptDaysIn1stYear )
373 0 : *pOptDaysIn1stYear = 360;
374 : }
375 0 : break;
376 : case 1: // 1=exact/exact
377 0 : if( pOptDaysIn1stYear )
378 : {
379 : sal_uInt16 nD, nM, nY;
380 :
381 0 : DaysToDate( nStartDate + nNullDate, nD, nM, nY );
382 :
383 0 : *pOptDaysIn1stYear = IsLeapYear( nY )? 366 : 365;
384 : }
385 0 : nRet = nEndDate - nStartDate;
386 0 : break;
387 : case 2: // 2=exact/360
388 0 : nRet = nEndDate - nStartDate;
389 0 : if( pOptDaysIn1stYear )
390 0 : *pOptDaysIn1stYear = 360;
391 0 : break;
392 : case 3: //3=exact/365
393 0 : nRet = nEndDate - nStartDate;
394 0 : if( pOptDaysIn1stYear )
395 0 : *pOptDaysIn1stYear = 365;
396 0 : break;
397 : default:
398 0 : throw lang::IllegalArgumentException();
399 : }
400 :
401 0 : return bNeg? -nRet : nRet;
402 : }
403 :
404 :
405 0 : double GetYearDiff( sal_Int32 nNullDate, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode ) throw( uno::RuntimeException, lang::IllegalArgumentException )
406 : {
407 : sal_Int32 nDays1stYear;
408 0 : sal_Int32 nTotalDays = GetDiffDate( nNullDate, nStartDate, nEndDate, nMode, &nDays1stYear );
409 :
410 0 : return double( nTotalDays ) / double( nDays1stYear );
411 : }
412 :
413 :
414 0 : sal_Int32 GetDaysInYear( sal_Int32 nNullDate, sal_Int32 nDate, sal_Int32 nMode ) throw( uno::RuntimeException, lang::IllegalArgumentException )
415 : {
416 0 : switch( nMode )
417 : {
418 : case 0: // 0=USA (NASD) 30/360
419 : case 2: // 2=exact/360
420 : case 4: // 4=Europe 30/360
421 0 : return 360;
422 : case 1: // 1=exact/exact
423 : {
424 : sal_uInt16 nD, nM, nY;
425 0 : nDate += nNullDate;
426 0 : DaysToDate( nDate, nD, nM, nY );
427 0 : return IsLeapYear( nY )? 366 : 365;
428 : }
429 : case 3: //3=exact/365
430 0 : return 365;
431 : default:
432 0 : throw lang::IllegalArgumentException();
433 : }
434 : }
435 :
436 :
437 : //fdo40100 toDo: make function fully compliant with ODFF1.2
438 : // LEM: I fixed case nMode==1; anything else to fix?
439 : /**
440 : * Function GetYearFrac implements YEARFRAC as defined in:
441 : * Open Document Format for Office Applications version 1.2 Part 2, par. 6.10.24
442 : * The calculations are defined in:
443 : * Open Document Format for Office Applications version 1.2 Part 2, par. 4.11.7
444 : */
445 0 : double GetYearFrac( sal_Int32 nNullDate, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode ) throw( uno::RuntimeException, lang::IllegalArgumentException )
446 : {
447 0 : if( nStartDate == nEndDate )
448 0 : return 0.0; // nothing to do...
449 :
450 0 : if( nStartDate > nEndDate )
451 : {
452 0 : sal_Int32 n = nEndDate;
453 0 : nEndDate = nStartDate;
454 0 : nStartDate = n;
455 : }
456 :
457 0 : sal_Int32 nDate1 = nStartDate + nNullDate;
458 0 : sal_Int32 nDate2 = nEndDate + nNullDate;
459 :
460 : sal_uInt16 nDay1, nDay2;
461 : sal_uInt16 nMonth1, nMonth2;
462 : sal_uInt16 nYear1, nYear2;
463 :
464 0 : DaysToDate( nDate1, nDay1, nMonth1, nYear1 );
465 0 : DaysToDate( nDate2, nDay2, nMonth2, nYear2 );
466 :
467 : // calculate days between nDate1 and nDate2
468 : sal_Int32 nDayDiff;
469 0 : switch( nMode )
470 : {
471 : case 0: // 0=USA (NASD) 30/360
472 0 : if ( nDay1 == 31 )
473 : {
474 0 : nDay1--;
475 : }
476 0 : if ( nDay1 == 30 && nDay2 == 31 )
477 : {
478 0 : nDay2--;
479 : }
480 : else
481 : {
482 0 : if ( nMonth1 == 2 && nDay1 == ( IsLeapYear( nYear1 ) ? 29 : 28 ) )
483 : {
484 0 : nDay1 = 30;
485 0 : if ( nMonth2 == 2 && nDay2 == ( IsLeapYear( nYear2 ) ? 29 : 28 ) )
486 : {
487 0 : nDay2 = 30;
488 : }
489 : }
490 : }
491 0 : nDayDiff = ( nYear2 - nYear1 ) * 360 + ( nMonth2 - nMonth1 ) * 30 + ( nDay2 - nDay1 );
492 0 : break;
493 : case 1: // 1=exact/exact
494 : case 2: // 2=exact/360
495 : case 3: // 3=exact/365
496 0 : nDayDiff = nDate2 - nDate1;
497 0 : break;
498 : case 4: // 4=Europe 30/360
499 0 : if ( nDay1 == 31 )
500 : {
501 0 : nDay1--;
502 : }
503 0 : if ( nDay2 == 31 )
504 : {
505 0 : nDay2--;
506 : }
507 0 : nDayDiff = ( nYear2 - nYear1 ) * 360 + ( nMonth2 - nMonth1 ) * 30 + ( nDay2 - nDay1 );
508 0 : break;
509 : default:
510 0 : throw lang::IllegalArgumentException();
511 : }
512 :
513 : //calculate days in year
514 : double nDaysInYear;
515 0 : switch( nMode )
516 : {
517 : case 0: // 0=USA (NASD) 30/360
518 : case 2: // 2=exact/360
519 : case 4: // 4=Europe 30/360
520 0 : nDaysInYear = 360;
521 0 : break;
522 : case 1: // 1=exact/exact
523 : {
524 0 : const bool isYearDifferent = ( nYear1 != nYear2 );
525 : // ODFv1.2 part 2 section 4.11.7.7.7
526 0 : if ( isYearDifferent &&
527 0 : ( ( nYear2 != nYear1 + 1 ) ||
528 0 : ( nMonth1 < nMonth2 ) ||
529 0 : ( nMonth1 == nMonth2 && nDay1 < nDay2 ) ) )
530 : {
531 : // return average of days in year between nDate1 and nDate2, inclusive
532 0 : sal_Int32 nDayCount = 0;
533 0 : for ( sal_Int16 i = nYear1; i <= nYear2; i++ )
534 0 : nDayCount += ( IsLeapYear( i ) ? 366 : 365 );
535 :
536 0 : nDaysInYear = ( double ) nDayCount / ( double ) ( nYear2 - nYear1 + 1 );
537 : }
538 : // we take advantage of the fact that (ODFv1.2 part 2) 4.11.7.7.9
539 : // 4.11.7.7.10 can be permuted without changing the end result
540 : // ODFv1.2 part 2 section 4.11.7.7.8 and 4.11.7.7.10
541 0 : else if ( ( isYearDifferent && IsLeapYear( nYear1 ) ) ||
542 0 : ( nMonth2 == 2 && nDay2 == 29) )
543 : {
544 0 : nDaysInYear = 366;
545 : }
546 : else
547 : {
548 : // ODFv1.2 part 2 section 4.11.7.7.9:
549 : // we need to determine whether there is a 29 February
550 : // between nDate1 and nDate2
551 : // LEM FIXME: I have a doubt concerning nDate1 == "29 February YYYY"
552 : // In this case, is the "29 February YYYY" between nDate1 and nDate2
553 : // in the meaning of ODFv1.2 part 2, section 4.11.7.7.9?
554 : // I assume "no", since if "between" is to be understood as "inclusive"
555 : // then 4.11.7.7.10 has no point.
556 : // OTOH, it could theoretically be possible that "between"
557 : // is to be understood as "inclusive the lower bound, exclusive in upper bound".
558 :
559 : assert(nYear1 == nYear2 || nYear1 + 1 == nYear2);
560 : // as a consequence, nYearDifferent iff nYear2 == nYear + 1, and
561 : // there are only two possible 29 Februaries to consider:
562 : // "29 February nYear1" and "29 February nYear2"
563 :
564 : // nDate2=="29 February YYYY" is handled above and the following conditions
565 : // rely on that for simplification.
566 : assert( ! (nMonth2 == 2 && nDay2 == 29));
567 :
568 0 : if( IsLeapYear( nYear1 ) )
569 : assert(nYear1 == nYear2);
570 :
571 : // is 29/2/nYear1 between nDate1 and nDate2?
572 : // that is only possible if IsLeapYear( nYear1 ),
573 : // which implies nYear1 == nYear2
574 0 : if( IsLeapYear( nYear1 ) &&
575 0 : ( nMonth1 == 1 || ( nMonth1 == 2 && nDay1 <= 28 )) &&
576 0 : nMonth2 > 2 )
577 : {
578 0 : nDaysInYear = 366;
579 : }
580 : // is 29/2/nYear2 between nDate1 and nDate2?
581 : // if nYear1==nYear2, then that is adequately tested by the previous test,
582 : // so no need to retest it here.
583 0 : else if(isYearDifferent && nMonth2 > 2 && IsLeapYear( nYear2 ))
584 : {
585 0 : nDaysInYear = 366;
586 : }
587 : else
588 : {
589 : assert( !( IsLeapYear( nYear2 ) &&
590 : nYear1 == nYear2 &&
591 : (nMonth1 == 1 || (nMonth1==2 && nDay1 <= 28)) &&
592 : nMonth2 > 2));
593 0 : nDaysInYear = 365;
594 : }
595 : }
596 : }
597 0 : break;
598 : case 3: // 3=exact/365
599 0 : nDaysInYear = 365;
600 0 : break;
601 : #ifdef _MSC_VER
602 : // Coverity complains this is dead code, but MSVC thinks nDaysInYear
603 : // is uninitialized if it's missing.
604 : default:
605 : throw lang::IllegalArgumentException();
606 : #endif
607 : }
608 :
609 0 : return double( nDayDiff ) / nDaysInYear;
610 : }
611 :
612 0 : double BinomialCoefficient( double n, double k )
613 : {
614 : // This method is a copy of BinomKoeff()
615 : // found in sc/source/core/tool/interpr3.cxx
616 :
617 0 : double nVal = 0.0;
618 0 : k = ::rtl::math::approxFloor(k);
619 0 : if (n < k)
620 0 : nVal = 0.0;
621 0 : else if (k == 0.0)
622 0 : nVal = 1.0;
623 : else
624 : {
625 0 : nVal = n/k;
626 0 : n--;
627 0 : k--;
628 0 : while (k > 0.0)
629 : {
630 0 : nVal *= n/k;
631 0 : k--;
632 0 : n--;
633 : }
634 : }
635 0 : return nVal;
636 : }
637 :
638 0 : double GetGcd( double f1, double f2 )
639 : {
640 0 : double f = fmod( f1, f2 );
641 0 : while( f > 0.0 )
642 : {
643 0 : f1 = f2;
644 0 : f2 = f;
645 0 : f = fmod( f1, f2 );
646 : }
647 :
648 0 : return f2;
649 : }
650 :
651 :
652 0 : double ConvertToDec( const OUString& aStr, sal_uInt16 nBase, sal_uInt16 nCharLim ) throw( uno::RuntimeException, lang::IllegalArgumentException )
653 : {
654 0 : if ( nBase < 2 || nBase > 36 )
655 0 : throw lang::IllegalArgumentException();
656 :
657 0 : sal_uInt32 nStrLen = aStr.getLength();
658 0 : if( nStrLen > nCharLim )
659 0 : throw lang::IllegalArgumentException();
660 0 : else if( !nStrLen )
661 0 : return 0.0;
662 :
663 0 : double fVal = 0.0;
664 :
665 0 : const sal_Unicode* p = aStr.getStr();
666 :
667 0 : sal_uInt16 nFirstDig = 0;
668 0 : sal_Bool bFirstDig = sal_True;
669 0 : double fBase = nBase;
670 :
671 0 : while ( *p )
672 : {
673 : sal_uInt16 n;
674 :
675 0 : if( '0' <= *p && *p <= '9' )
676 0 : n = *p - '0';
677 0 : else if( 'A' <= *p && *p <= 'Z' )
678 0 : n = 10 + ( *p - 'A' );
679 0 : else if ( 'a' <= *p && *p <= 'z' )
680 0 : n = 10 + ( *p - 'a' );
681 : else
682 0 : n = nBase;
683 :
684 0 : if( n < nBase )
685 : {
686 0 : if( bFirstDig )
687 : {
688 0 : bFirstDig = sal_False;
689 0 : nFirstDig = n;
690 : }
691 0 : fVal = fVal * fBase + double( n );
692 : }
693 : else
694 : // illegal char!
695 0 : throw lang::IllegalArgumentException();
696 :
697 0 : p++;
698 :
699 : }
700 :
701 0 : if( nStrLen == nCharLim && !bFirstDig && (nFirstDig >= nBase / 2) )
702 : { // handling negativ values
703 0 : fVal = ( pow( double( nBase ), double( nCharLim ) ) - fVal ); // complement
704 0 : fVal *= -1.0;
705 : }
706 :
707 0 : return fVal;
708 : }
709 :
710 :
711 0 : static inline sal_Char GetMaxChar( sal_uInt16 nBase )
712 : {
713 0 : const sal_Char* c = "--123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
714 0 : return c[ nBase ];
715 : }
716 :
717 :
718 0 : OUString ConvertFromDec( double fNum, double fMin, double fMax, sal_uInt16 nBase,
719 : sal_Int32 nPlaces, sal_Int32 nMaxPlaces, sal_Bool bUsePlaces ) throw( uno::RuntimeException, lang::IllegalArgumentException )
720 : {
721 0 : fNum = ::rtl::math::approxFloor( fNum );
722 0 : fMin = ::rtl::math::approxFloor( fMin );
723 0 : fMax = ::rtl::math::approxFloor( fMax );
724 :
725 0 : if( fNum < fMin || fNum > fMax || ( bUsePlaces && ( nPlaces <= 0 || nPlaces > nMaxPlaces ) ) )
726 0 : throw lang::IllegalArgumentException();
727 :
728 0 : sal_Int64 nNum = static_cast< sal_Int64 >( fNum );
729 0 : sal_Bool bNeg = nNum < 0;
730 0 : if( bNeg )
731 0 : nNum = sal_Int64( pow( double( nBase ), double( nMaxPlaces ) ) ) + nNum;
732 :
733 0 : OUString aRet( OUString::number( nNum, nBase ).toAsciiUpperCase() );
734 :
735 :
736 0 : if( bUsePlaces )
737 : {
738 0 : sal_Int32 nLen = aRet.getLength();
739 0 : if( !bNeg && nLen > nPlaces )
740 : {
741 0 : throw lang::IllegalArgumentException();
742 : }
743 0 : else if( ( bNeg && nLen < nMaxPlaces ) || ( !bNeg && nLen < nPlaces ) )
744 : {
745 0 : sal_Int32 nLeft = nPlaces - nLen;
746 0 : sal_Char* p = new sal_Char[ nLeft + 1 ];
747 0 : memset( p, bNeg? GetMaxChar( nBase ) : '0', nLeft );
748 0 : p[ nLeft ] = 0x00;
749 0 : OUString aTmp( p, nLeft, RTL_TEXTENCODING_MS_1252 );
750 0 : aTmp += aRet;
751 0 : aRet = aTmp;
752 :
753 0 : delete[] p;
754 : }
755 : }
756 :
757 0 : return aRet;
758 : }
759 :
760 : // implementation moved to module sal, see #i97091#
761 0 : double Erf( double x )
762 : {
763 0 : return ::rtl::math::erf(x);
764 : }
765 :
766 : // implementation moved to module sal, see #i97091#
767 0 : double Erfc( double x )
768 : {
769 0 : return ::rtl::math::erfc(x);
770 : }
771 :
772 0 : inline sal_Bool IsNum( sal_Unicode c )
773 : {
774 0 : return c >= '0' && c <= '9';
775 : }
776 :
777 :
778 0 : inline sal_Bool IsComma( sal_Unicode c )
779 : {
780 0 : return c == '.' || c == ',';
781 : }
782 :
783 :
784 0 : inline sal_Bool IsExpStart( sal_Unicode c )
785 : {
786 0 : return c == 'e' || c == 'E';
787 : }
788 :
789 :
790 0 : inline sal_Bool IsImagUnit( sal_Unicode c )
791 : {
792 0 : return c == 'i' || c == 'j';
793 : }
794 :
795 :
796 0 : inline sal_uInt16 GetVal( sal_Unicode c )
797 : {
798 0 : return sal_uInt16( c - '0' );
799 : }
800 :
801 :
802 0 : sal_Bool ParseDouble( const sal_Unicode*& rp, double& rRet )
803 : {
804 0 : double fInt = 0.0;
805 0 : double fFrac = 0.0;
806 0 : double fMult = 0.1; // multiplier to multiply digits with, when adding fractional ones
807 0 : sal_Int32 nExp = 0;
808 0 : sal_Int32 nMaxExp = 307;
809 0 : sal_uInt16 nDigCnt = 18; // max. number of digits to read in, rest doesn't matter
810 :
811 : enum State { S_End = 0, S_Sign, S_IntStart, S_Int, S_IgnoreIntDigs, S_Frac, S_IgnoreFracDigs, S_ExpSign, S_Exp };
812 :
813 0 : State eS = S_Sign;
814 :
815 0 : sal_Bool bNegNum = sal_False;
816 0 : sal_Bool bNegExp = sal_False;
817 :
818 0 : const sal_Unicode* p = rp;
819 : sal_Unicode c;
820 :
821 0 : while( eS )
822 : {
823 0 : c = *p;
824 0 : switch( eS )
825 : {
826 : case S_Sign:
827 0 : if( IsNum( c ) )
828 : {
829 0 : fInt = GetVal( c );
830 0 : nDigCnt--;
831 0 : eS = S_Int;
832 : }
833 0 : else if( c == '-' )
834 : {
835 0 : bNegNum = sal_True;
836 0 : eS = S_IntStart;
837 : }
838 0 : else if( c == '+' )
839 0 : eS = S_IntStart;
840 0 : else if( IsComma( c ) )
841 0 : eS = S_Frac;
842 : else
843 0 : return sal_False;
844 0 : break;
845 : case S_IntStart:
846 0 : if( IsNum( c ) )
847 : {
848 0 : fInt = GetVal( c );
849 0 : nDigCnt--;
850 0 : eS = S_Int;
851 : }
852 0 : else if( IsComma( c ) )
853 0 : eS = S_Frac;
854 0 : else if( IsImagUnit( c ) )
855 : {
856 0 : rRet = 0.0;
857 0 : return sal_True;
858 : }
859 : else
860 0 : return sal_False;
861 0 : break;
862 : case S_Int:
863 0 : if( IsNum( c ) )
864 : {
865 0 : fInt *= 10.0;
866 0 : fInt += double( GetVal( c ) );
867 0 : nDigCnt--;
868 0 : if( !nDigCnt )
869 0 : eS = S_IgnoreIntDigs;
870 : }
871 0 : else if( IsComma( c ) )
872 0 : eS = S_Frac;
873 0 : else if( IsExpStart( c ) )
874 0 : eS = S_ExpSign;
875 : else
876 0 : eS = S_End;
877 0 : break;
878 : case S_IgnoreIntDigs:
879 0 : if( IsNum( c ) )
880 0 : nExp++; // just multiply num with 10... ;-)
881 0 : else if( IsComma( c ) )
882 0 : eS = S_Frac;
883 0 : else if( IsExpStart( c ) )
884 0 : eS = S_ExpSign;
885 : else
886 0 : eS = S_End;
887 0 : break;
888 : case S_Frac:
889 0 : if( IsNum( c ) )
890 : {
891 0 : fFrac += double( GetVal( c ) ) * fMult;
892 0 : nDigCnt--;
893 0 : if( nDigCnt )
894 0 : fMult *= 0.1;
895 : else
896 0 : eS = S_IgnoreFracDigs;
897 : }
898 0 : else if( IsExpStart( c ) )
899 0 : eS = S_ExpSign;
900 : else
901 0 : eS = S_End;
902 0 : break;
903 : case S_IgnoreFracDigs:
904 0 : if( IsExpStart( c ) )
905 0 : eS = S_ExpSign;
906 0 : else if( !IsNum( c ) )
907 0 : eS = S_End;
908 0 : break;
909 : case S_ExpSign:
910 0 : if( IsNum( c ) )
911 : {
912 0 : nExp = GetVal( c );
913 0 : eS = S_Exp;
914 : }
915 0 : else if( c == '-' )
916 : {
917 0 : bNegExp = sal_True;
918 0 : eS = S_Exp;
919 : }
920 0 : else if( c != '+' )
921 0 : eS = S_End;
922 0 : break;
923 : case S_Exp:
924 0 : if( IsNum( c ) )
925 : {
926 0 : nExp *= 10;
927 0 : nExp += GetVal( c );
928 0 : if( nExp > nMaxExp )
929 0 : return sal_False;
930 : }
931 : else
932 0 : eS = S_End;
933 0 : break;
934 : case S_End: // to avoid compiler warning
935 0 : break; // loop exits anyway
936 : }
937 :
938 0 : p++;
939 : }
940 :
941 0 : p--; // set pointer back to last
942 0 : rp = p;
943 :
944 0 : fInt += fFrac;
945 0 : sal_Int32 nLog10 = sal_Int32( log10( fInt ) );
946 :
947 0 : if( bNegExp )
948 0 : nExp = -nExp;
949 :
950 0 : if( nLog10 + nExp > nMaxExp )
951 0 : return sal_False;
952 :
953 0 : fInt = ::rtl::math::pow10Exp( fInt, nExp );
954 :
955 0 : if( bNegNum )
956 0 : fInt = -fInt;
957 :
958 0 : rRet = fInt;
959 :
960 0 : return sal_True;
961 : }
962 :
963 :
964 0 : OUString GetString( double f, sal_Bool bLeadingSign, sal_uInt16 nMaxDig )
965 : {
966 0 : const int nBuff = 256;
967 : sal_Char aBuff[ nBuff + 1 ];
968 0 : const char* pFormStr = bLeadingSign? "%+.*g" : "%.*g";
969 0 : int nLen = snprintf( aBuff, nBuff, pFormStr, int( nMaxDig ), f );
970 : // you never know which underlying implementation you get ...
971 0 : aBuff[nBuff] = 0;
972 0 : if ( nLen < 0 || nLen > nBuff )
973 0 : nLen = strlen( aBuff );
974 :
975 0 : OUString aRet( aBuff, nLen, RTL_TEXTENCODING_MS_1252 );
976 :
977 0 : return aRet;
978 : }
979 :
980 :
981 0 : double GetAmordegrc( sal_Int32 nNullDate, double fCost, sal_Int32 nDate, sal_Int32 nFirstPer,
982 : double fRestVal, double fPer, double fRate, sal_Int32 nBase ) throw( uno::RuntimeException, lang::IllegalArgumentException )
983 : {
984 0 : if( nBase == 2 )
985 0 : throw lang::IllegalArgumentException();
986 :
987 0 : sal_uInt32 nPer = sal_uInt32( fPer );
988 0 : double fUsePer = 1.0 / fRate;
989 : double fAmorCoeff;
990 :
991 0 : if( fUsePer < 3.0 )
992 0 : fAmorCoeff = 1.0;
993 0 : else if( fUsePer < 5.0 )
994 0 : fAmorCoeff = 1.5;
995 0 : else if( fUsePer <= 6.0 )
996 0 : fAmorCoeff = 2.0;
997 : else
998 0 : fAmorCoeff = 2.5;
999 :
1000 0 : fRate *= fAmorCoeff;
1001 0 : double fNRate = ::rtl::math::round( GetYearFrac( nNullDate, nDate, nFirstPer, nBase ) * fRate * fCost, 0 );
1002 0 : fCost -= fNRate;
1003 0 : double fRest = fCost - fRestVal; // Anschaffungskosten - Restwert - Summe aller Abschreibungen
1004 :
1005 0 : for( sal_uInt32 n = 0 ; n < nPer ; n++ )
1006 : {
1007 0 : fNRate = ::rtl::math::round( fRate * fCost, 0 );
1008 0 : fRest -= fNRate;
1009 :
1010 0 : if( fRest < 0.0 )
1011 : {
1012 0 : switch( nPer - n )
1013 : {
1014 : case 0:
1015 : case 1:
1016 0 : return ::rtl::math::round( fCost * 0.5, 0 );
1017 : default:
1018 0 : return 0.0;
1019 : }
1020 : }
1021 :
1022 0 : fCost -= fNRate;
1023 : }
1024 :
1025 0 : return fNRate;
1026 : }
1027 :
1028 :
1029 0 : double GetAmorlinc( sal_Int32 nNullDate, double fCost, sal_Int32 nDate, sal_Int32 nFirstPer,
1030 : double fRestVal, double fPer, double fRate, sal_Int32 nBase ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1031 : {
1032 0 : if( nBase == 2 )
1033 0 : throw lang::IllegalArgumentException();
1034 :
1035 0 : sal_uInt32 nPer = sal_uInt32( fPer );
1036 0 : double fOneRate = fCost * fRate;
1037 0 : double fCostDelta = fCost - fRestVal;
1038 0 : double f0Rate = GetYearFrac( nNullDate, nDate, nFirstPer, nBase ) * fRate * fCost;
1039 0 : sal_uInt32 nNumOfFullPeriods = sal_uInt32( ( fCost - fRestVal - f0Rate) / fOneRate );
1040 :
1041 0 : if( nPer == 0 )
1042 0 : return f0Rate;
1043 0 : else if( nPer <= nNumOfFullPeriods )
1044 0 : return fOneRate;
1045 0 : else if( nPer == nNumOfFullPeriods + 1 )
1046 0 : return fCostDelta - fOneRate * nNumOfFullPeriods - f0Rate;
1047 : else
1048 0 : return 0.0;
1049 : }
1050 :
1051 :
1052 0 : double GetDuration( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, double fCoup,
1053 : double fYield, sal_Int32 nFreq, sal_Int32 nBase ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1054 : {
1055 0 : double fYearfrac = GetYearFrac( nNullDate, nSettle, nMat, nBase );
1056 0 : double fNumOfCoups = GetCoupnum( nNullDate, nSettle, nMat, nFreq, nBase );
1057 0 : double fDur = 0.0;
1058 0 : const double f100 = 100.0;
1059 0 : fCoup *= f100 / double( nFreq ); // fCoup is used as cash flow
1060 0 : fYield /= nFreq;
1061 0 : fYield += 1.0;
1062 :
1063 0 : double nDiff = fYearfrac * nFreq - fNumOfCoups;
1064 :
1065 : double t;
1066 :
1067 0 : for( t = 1.0 ; t < fNumOfCoups ; t++ )
1068 0 : fDur += ( t + nDiff ) * ( fCoup ) / pow( fYield, t + nDiff );
1069 :
1070 0 : fDur += ( fNumOfCoups + nDiff ) * ( fCoup + f100 ) / pow( fYield, fNumOfCoups + nDiff );
1071 :
1072 0 : double p = 0.0;
1073 0 : for( t = 1.0 ; t < fNumOfCoups ; t++ )
1074 0 : p += fCoup / pow( fYield, t + nDiff );
1075 :
1076 0 : p += ( fCoup + f100 ) / pow( fYield, fNumOfCoups + nDiff );
1077 :
1078 0 : fDur /= p;
1079 0 : fDur /= double( nFreq );
1080 :
1081 0 : return fDur;
1082 : }
1083 :
1084 :
1085 0 : double GetYieldmat( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nIssue,
1086 : double fRate, double fPrice, sal_Int32 nBase ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1087 : {
1088 0 : double fIssMat = GetYearFrac( nNullDate, nIssue, nMat, nBase );
1089 0 : double fIssSet = GetYearFrac( nNullDate, nIssue, nSettle, nBase );
1090 0 : double fSetMat = GetYearFrac( nNullDate, nSettle, nMat, nBase );
1091 :
1092 0 : double y = 1.0 + fIssMat * fRate;
1093 0 : y /= fPrice / 100.0 + fIssSet * fRate;
1094 0 : y--;
1095 0 : y /= fSetMat;
1096 :
1097 0 : return y;
1098 : }
1099 :
1100 :
1101 0 : double GetOddfprice( sal_Int32 /*nNullDate*/, sal_Int32 /*nSettle*/, sal_Int32 /*nMat*/, sal_Int32 /*nIssue*/,
1102 : sal_Int32 /*nFirstCoup*/, double /*fRate*/, double /*fYield*/, double /*fRedemp*/, sal_Int32 /*nFreq*/,
1103 : sal_Int32 /*nBase*/ ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1104 : {
1105 : // If you change this to not unconditionally throw, the
1106 : // SAL_WNOUNREACHABLE_CODE_PUSH/POP around the caller in
1107 : // financial.cxx can be removed.
1108 0 : throw uno::RuntimeException(); // #87380#
1109 : }
1110 :
1111 :
1112 0 : double getYield_( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, double fCoup, double fPrice,
1113 : double fRedemp, sal_Int32 nFreq, sal_Int32 nBase ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1114 : {
1115 0 : double fRate = fCoup;
1116 0 : double fPriceN = 0.0;
1117 0 : double fYield1 = 0.0;
1118 0 : double fYield2 = 1.0;
1119 0 : double fPrice1 = getPrice_( nNullDate, nSettle, nMat, fRate, fYield1, fRedemp, nFreq, nBase );
1120 0 : double fPrice2 = getPrice_( nNullDate, nSettle, nMat, fRate, fYield2, fRedemp, nFreq, nBase );
1121 0 : double fYieldN = ( fYield2 - fYield1 ) * 0.5;
1122 :
1123 0 : for( sal_uInt32 nIter = 0 ; nIter < 100 && fPriceN != fPrice ; nIter++ )
1124 : {
1125 0 : fPriceN = getPrice_( nNullDate, nSettle, nMat, fRate, fYieldN, fRedemp, nFreq, nBase );
1126 :
1127 0 : if( fPrice == fPrice1 )
1128 0 : return fYield1;
1129 0 : else if( fPrice == fPrice2 )
1130 0 : return fYield2;
1131 0 : else if( fPrice == fPriceN )
1132 0 : return fYieldN;
1133 0 : else if( fPrice < fPrice2 )
1134 : {
1135 0 : fYield2 *= 2.0;
1136 0 : fPrice2 = getPrice_( nNullDate, nSettle, nMat, fRate, fYield2, fRedemp, nFreq, nBase );
1137 :
1138 0 : fYieldN = ( fYield2 - fYield1 ) * 0.5;
1139 : }
1140 : else
1141 : {
1142 0 : if( fPrice < fPriceN )
1143 : {
1144 0 : fYield1 = fYieldN;
1145 0 : fPrice1 = fPriceN;
1146 : }
1147 : else
1148 : {
1149 0 : fYield2 = fYieldN;
1150 0 : fPrice2 = fPriceN;
1151 : }
1152 :
1153 0 : fYieldN = fYield2 - ( fYield2 - fYield1 ) * ( ( fPrice - fPrice2 ) / ( fPrice1 - fPrice2 ) );
1154 : }
1155 : }
1156 :
1157 0 : if( fabs( fPrice - fPriceN ) > fPrice / 100.0 )
1158 0 : throw lang::IllegalArgumentException(); // result not precise enough
1159 :
1160 0 : return fYieldN;
1161 : }
1162 :
1163 :
1164 0 : double getPrice_( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, double fRate, double fYield,
1165 : double fRedemp, sal_Int32 nFreq, sal_Int32 nBase ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1166 : {
1167 0 : double fFreq = nFreq;
1168 :
1169 0 : double fE = GetCoupdays( nNullDate, nSettle, nMat, nFreq, nBase );
1170 0 : double fDSC_E = GetCoupdaysnc( nNullDate, nSettle, nMat, nFreq, nBase ) / fE;
1171 0 : double fN = GetCoupnum( nNullDate, nSettle, nMat, nFreq, nBase );
1172 0 : double fA = GetCoupdaybs( nNullDate, nSettle, nMat, nFreq, nBase );
1173 :
1174 0 : double fRet = fRedemp / ( pow( 1.0 + fYield / fFreq, fN - 1.0 + fDSC_E ) );
1175 0 : fRet -= 100.0 * fRate / fFreq * fA / fE;
1176 :
1177 0 : double fT1 = 100.0 * fRate / fFreq;
1178 0 : double fT2 = 1.0 + fYield / fFreq;
1179 :
1180 0 : for( double fK = 0.0 ; fK < fN ; fK++ )
1181 0 : fRet += fT1 / pow( fT2, fK + fDSC_E );
1182 :
1183 0 : return fRet;
1184 : }
1185 :
1186 :
1187 0 : double GetOddfyield( sal_Int32 /*nNullDate*/, sal_Int32 /*nSettle*/, sal_Int32 /*nMat*/, sal_Int32 /*nIssue*/,
1188 : sal_Int32 /*nFirstCoup*/, double /*fRate*/, double /*fPrice*/, double /*fRedemp*/, sal_Int32 /*nFreq*/,
1189 : sal_Int32 /*nBase*/ ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1190 : {
1191 : // If you change this to not unconditionally throw, the
1192 : // SAL_WNOUNREACHABLE_CODE_PUSH/POP around the caller in
1193 : // financial.cxx can be removed.
1194 0 : throw uno::RuntimeException(); // #87380#
1195 : }
1196 :
1197 :
1198 0 : double GetOddlprice( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nLastCoup,
1199 : double fRate, double fYield, double fRedemp, sal_Int32 nFreq, sal_Int32 nBase ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1200 : {
1201 0 : double fFreq = double( nFreq );
1202 0 : double fDCi = GetYearFrac( nNullDate, nLastCoup, nMat, nBase ) * fFreq;
1203 0 : double fDSCi = GetYearFrac( nNullDate, nSettle, nMat, nBase ) * fFreq;
1204 0 : double fAi = GetYearFrac( nNullDate, nLastCoup, nSettle, nBase ) * fFreq;
1205 :
1206 0 : double p = fRedemp + fDCi * 100.0 * fRate / fFreq;
1207 0 : p /= fDSCi * fYield / fFreq + 1.0;
1208 0 : p -= fAi * 100.0 * fRate / fFreq;
1209 :
1210 0 : return p;
1211 : }
1212 :
1213 :
1214 0 : double GetOddlyield( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nLastCoup,
1215 : double fRate, double fPrice, double fRedemp, sal_Int32 nFreq, sal_Int32 nBase ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1216 : {
1217 0 : double fFreq = double( nFreq );
1218 0 : double fDCi = GetYearFrac( nNullDate, nLastCoup, nMat, nBase ) * fFreq;
1219 0 : double fDSCi = GetYearFrac( nNullDate, nSettle, nMat, nBase ) * fFreq;
1220 0 : double fAi = GetYearFrac( nNullDate, nLastCoup, nSettle, nBase ) * fFreq;
1221 :
1222 0 : double y = fRedemp + fDCi * 100.0 * fRate / fFreq;
1223 0 : y /= fPrice + fAi * 100.0 * fRate / fFreq;
1224 0 : y--;
1225 0 : y *= fFreq / fDSCi;
1226 :
1227 0 : return y;
1228 : }
1229 :
1230 :
1231 0 : double GetRmz( double fZins, double fZzr, double fBw, double fZw, sal_Int32 nF )
1232 : {
1233 : double fRmz;
1234 0 : if( fZins == 0.0 )
1235 0 : fRmz = ( fBw + fZw ) / fZzr;
1236 : else
1237 : {
1238 0 : double fTerm = pow( 1.0 + fZins, fZzr );
1239 0 : if( nF > 0 )
1240 0 : fRmz = ( fZw * fZins / ( fTerm - 1.0 ) + fBw * fZins / ( 1.0 - 1.0 / fTerm ) ) / ( 1.0 + fZins );
1241 : else
1242 0 : fRmz = fZw * fZins / ( fTerm - 1.0 ) + fBw * fZins / ( 1.0 - 1.0 / fTerm );
1243 : }
1244 :
1245 0 : return -fRmz;
1246 : }
1247 :
1248 :
1249 0 : double GetZw( double fZins, double fZzr, double fRmz, double fBw, sal_Int32 nF )
1250 : {
1251 : double fZw;
1252 0 : if( fZins == 0.0 )
1253 0 : fZw = fBw + fRmz * fZzr;
1254 : else
1255 : {
1256 0 : double fTerm = pow( 1.0 + fZins, fZzr );
1257 0 : if( nF > 0 )
1258 0 : fZw = fBw * fTerm + fRmz * ( 1.0 + fZins ) * ( fTerm - 1.0 ) / fZins;
1259 : else
1260 0 : fZw = fBw * fTerm + fRmz * ( fTerm - 1.0 ) / fZins;
1261 : }
1262 :
1263 0 : return -fZw;
1264 : }
1265 :
1266 : // financial functions COUP***
1267 :
1268 : // COUPPCD: find last coupon date before settlement (can be equal to settlement)
1269 0 : static void lcl_GetCouppcd( ScaDate& rDate, const ScaDate& rSettle, const ScaDate& rMat, sal_Int32 nFreq )
1270 : throw( lang::IllegalArgumentException )
1271 : {
1272 0 : rDate = rMat;
1273 0 : rDate.setYear( rSettle.getYear() );
1274 0 : if( rDate < rSettle )
1275 0 : rDate.addYears( 1 );
1276 0 : while( rDate > rSettle )
1277 0 : rDate.addMonths( -12 / nFreq );
1278 0 : }
1279 :
1280 0 : double GetCouppcd( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq, sal_Int32 nBase )
1281 : throw( uno::RuntimeException, lang::IllegalArgumentException )
1282 : {
1283 0 : if( nSettle >= nMat || CHK_Freq )
1284 0 : throw lang::IllegalArgumentException();
1285 :
1286 0 : ScaDate aDate;
1287 0 : lcl_GetCouppcd( aDate, ScaDate( nNullDate, nSettle, nBase ), ScaDate( nNullDate, nMat, nBase ), nFreq );
1288 0 : return aDate.getDate( nNullDate );
1289 : }
1290 :
1291 : // COUPNCD: find first coupon date after settlement (is never equal to settlement)
1292 0 : static void lcl_GetCoupncd( ScaDate& rDate, const ScaDate& rSettle, const ScaDate& rMat, sal_Int32 nFreq )
1293 : throw( lang::IllegalArgumentException )
1294 : {
1295 0 : rDate = rMat;
1296 0 : rDate.setYear( rSettle.getYear() );
1297 0 : if( rDate > rSettle )
1298 0 : rDate.addYears( -1 );
1299 0 : while( rDate <= rSettle )
1300 0 : rDate.addMonths( 12 / nFreq );
1301 0 : }
1302 :
1303 0 : double GetCoupncd( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq, sal_Int32 nBase )
1304 : throw( uno::RuntimeException, lang::IllegalArgumentException )
1305 : {
1306 0 : if( nSettle >= nMat || CHK_Freq )
1307 0 : throw lang::IllegalArgumentException();
1308 :
1309 0 : ScaDate aDate;
1310 0 : lcl_GetCoupncd( aDate, ScaDate( nNullDate, nSettle, nBase ), ScaDate( nNullDate, nMat, nBase ), nFreq );
1311 0 : return aDate.getDate( nNullDate );
1312 : }
1313 :
1314 : // COUPDAYBS: get day count: coupon date before settlement <-> settlement
1315 0 : double GetCoupdaybs( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq, sal_Int32 nBase )
1316 : throw( uno::RuntimeException, lang::IllegalArgumentException )
1317 : {
1318 0 : if( nSettle >= nMat || CHK_Freq )
1319 0 : throw lang::IllegalArgumentException();
1320 :
1321 0 : ScaDate aSettle( nNullDate, nSettle, nBase );
1322 0 : ScaDate aDate;
1323 0 : lcl_GetCouppcd( aDate, aSettle, ScaDate( nNullDate, nMat, nBase ), nFreq );
1324 0 : return ScaDate::getDiff( aDate, aSettle );
1325 : }
1326 :
1327 : // COUPDAYSNC: get day count: settlement <-> coupon date after settlement
1328 0 : double GetCoupdaysnc( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq, sal_Int32 nBase )
1329 : throw( uno::RuntimeException, lang::IllegalArgumentException )
1330 : {
1331 0 : if( nSettle >= nMat || CHK_Freq )
1332 0 : throw lang::IllegalArgumentException();
1333 :
1334 0 : if( (nBase != 0) && (nBase != 4) )
1335 : {
1336 0 : ScaDate aSettle( nNullDate, nSettle, nBase );
1337 0 : ScaDate aDate;
1338 0 : lcl_GetCoupncd( aDate, aSettle, ScaDate( nNullDate, nMat, nBase ), nFreq );
1339 0 : return ScaDate::getDiff( aSettle, aDate );
1340 : }
1341 0 : return GetCoupdays( nNullDate, nSettle, nMat, nFreq, nBase ) - GetCoupdaybs( nNullDate, nSettle, nMat, nFreq, nBase );
1342 : }
1343 :
1344 : // COUPDAYS: get day count: coupon date before settlement <-> coupon date after settlement
1345 0 : double GetCoupdays( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq, sal_Int32 nBase )
1346 : throw( uno::RuntimeException, lang::IllegalArgumentException )
1347 : {
1348 0 : if( nSettle >= nMat || CHK_Freq )
1349 0 : throw lang::IllegalArgumentException();
1350 :
1351 0 : if( nBase == 1 )
1352 : {
1353 0 : ScaDate aDate;
1354 0 : lcl_GetCouppcd( aDate, ScaDate( nNullDate, nSettle, nBase ), ScaDate( nNullDate, nMat, nBase ), nFreq );
1355 0 : ScaDate aNextDate( aDate );
1356 0 : aNextDate.addMonths( 12 / nFreq );
1357 0 : return ScaDate::getDiff( aDate, aNextDate );
1358 : }
1359 0 : return static_cast< double >( GetDaysInYear( 0, 0, nBase ) ) / nFreq;
1360 : }
1361 :
1362 : // COUPNUM: get count of coupon dates
1363 0 : double GetCoupnum( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq, sal_Int32 nBase )
1364 : throw( uno::RuntimeException, lang::IllegalArgumentException )
1365 : {
1366 0 : if( nSettle >= nMat || CHK_Freq )
1367 0 : throw lang::IllegalArgumentException();
1368 :
1369 0 : ScaDate aMat( nNullDate, nMat, nBase );
1370 0 : ScaDate aDate;
1371 0 : lcl_GetCouppcd( aDate, ScaDate( nNullDate, nSettle, nBase ), aMat, nFreq );
1372 0 : sal_uInt16 nMonths = (aMat.getYear() - aDate.getYear()) * 12 + aMat.getMonth() - aDate.getMonth();
1373 0 : return static_cast< double >( nMonths * nFreq / 12 );
1374 : }
1375 :
1376 :
1377 :
1378 0 : class AnalysisRscStrArrLoader : public Resource
1379 : {
1380 : private:
1381 : ResStringArray aStrArray;
1382 : public:
1383 0 : AnalysisRscStrArrLoader( sal_uInt16 nRsc, sal_uInt16 nArrayId, ResMgr& rResMgr ) :
1384 : Resource( AnalysisResId( nRsc, rResMgr ) ),
1385 0 : aStrArray( AnalysisResId( nArrayId, rResMgr ) )
1386 : {
1387 0 : FreeResource();
1388 0 : }
1389 :
1390 0 : const ResStringArray& GetStringArray() const { return aStrArray; }
1391 : };
1392 :
1393 :
1394 0 : FuncData::FuncData( const FuncDataBase& r, ResMgr& rResMgr ) :
1395 : aIntName( OUString::createFromAscii( r.pIntName ) ),
1396 : nUINameID( r.nUINameID ),
1397 : nDescrID( r.nDescrID ),
1398 : bDouble( r.bDouble ),
1399 : bWithOpt( r.bWithOpt ),
1400 : nParam( r.nNumOfParams ),
1401 : nCompID( r.nCompListID ),
1402 0 : eCat( r.eCat )
1403 : {
1404 0 : AnalysisRscStrArrLoader aArrLoader( RID_ANALYSIS_DEFFUNCTION_NAMES, nCompID, rResMgr );
1405 0 : const ResStringArray& rArr = aArrLoader.GetStringArray();
1406 :
1407 0 : sal_uInt16 nCount = sal::static_int_cast<sal_uInt16>( rArr.Count() );
1408 :
1409 0 : aCompList.resize( nCount );
1410 0 : for( sal_uInt16 n = 0 ; n < nCount ; n++ )
1411 0 : aCompList[n] = rArr.GetString( n );
1412 0 : }
1413 :
1414 :
1415 0 : FuncData::~FuncData()
1416 : {
1417 0 : }
1418 :
1419 :
1420 0 : sal_uInt16 FuncData::GetStrIndex( sal_uInt16 nParamNum ) const
1421 : {
1422 0 : if( !bWithOpt )
1423 0 : nParamNum++;
1424 :
1425 0 : if( nParamNum > nParam )
1426 0 : return nParam * 2;
1427 : else
1428 0 : return nParamNum * 2;
1429 : }
1430 :
1431 :
1432 0 : FuncDataList::FuncDataList( ResMgr& rResMgr )
1433 0 : : nLast(0)
1434 : {
1435 0 : for( sal_uInt16 n = 0 ; n < SAL_N_ELEMENTS(pFuncDatas) ; n++ )
1436 0 : Append( new FuncData( pFuncDatas[ n ], rResMgr ) );
1437 0 : }
1438 :
1439 :
1440 0 : FuncDataList::~FuncDataList()
1441 : {
1442 0 : for( std::vector<FuncData*>::iterator it = maVector.begin(); it != maVector.end(); ++it )
1443 0 : delete *it;
1444 0 : }
1445 :
1446 :
1447 0 : const FuncData* FuncDataList::Get( const OUString& aProgrammaticName ) const
1448 : {
1449 0 : if( aLastName == aProgrammaticName )
1450 0 : return Get( nLast );
1451 :
1452 0 : ( ( FuncDataList* ) this )->aLastName = aProgrammaticName;
1453 :
1454 0 : sal_uInt32 nE = Count();
1455 0 : for( sal_uInt32 n = 0 ; n < nE ; n++ )
1456 : {
1457 0 : const FuncData* p = Get( n );
1458 0 : if( p->Is( aProgrammaticName ) )
1459 : {
1460 0 : ( ( FuncDataList* ) this )->nLast = n;
1461 0 : return p;
1462 : }
1463 : }
1464 :
1465 0 : ( ( FuncDataList* ) this )->nLast = 0xFFFFFFFF;
1466 0 : return NULL;
1467 : }
1468 :
1469 :
1470 0 : AnalysisResId::AnalysisResId( sal_uInt16 nId, ResMgr& rResMgr ) : ResId( nId, rResMgr )
1471 : {
1472 0 : }
1473 :
1474 :
1475 0 : SortedIndividualInt32List::SortedIndividualInt32List()
1476 : {
1477 0 : }
1478 :
1479 :
1480 0 : SortedIndividualInt32List::~SortedIndividualInt32List()
1481 : {
1482 0 : }
1483 :
1484 :
1485 0 : void SortedIndividualInt32List::Insert( sal_Int32 nDay )
1486 : {
1487 0 : sal_uInt32 nIndex = Count();
1488 0 : while( nIndex )
1489 : {
1490 0 : nIndex--;
1491 0 : sal_Int32 nRef = Get( nIndex );
1492 0 : if( nDay == nRef )
1493 0 : return;
1494 0 : else if( nDay > nRef )
1495 : {
1496 0 : maVector.insert( maVector.begin() + nIndex + 1, nDay );
1497 0 : return;
1498 : }
1499 : }
1500 0 : maVector.insert( maVector.begin(), nDay );
1501 : }
1502 :
1503 :
1504 0 : void SortedIndividualInt32List::Insert( sal_Int32 nDay, sal_Int32 nNullDate, sal_Bool bInsertOnWeekend )
1505 : {
1506 0 : if( !nDay )
1507 0 : return;
1508 :
1509 0 : nDay += nNullDate;
1510 0 : if( bInsertOnWeekend || (GetDayOfWeek( nDay ) < 5) )
1511 0 : Insert( nDay );
1512 : }
1513 :
1514 :
1515 0 : void SortedIndividualInt32List::Insert(
1516 : double fDay, sal_Int32 nNullDate, sal_Bool bInsertOnWeekend ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1517 : {
1518 0 : if( (fDay < -2147483648.0) || (fDay > 2147483649.0) )
1519 0 : throw lang::IllegalArgumentException();
1520 0 : Insert( static_cast< sal_Int32 >( fDay ), nNullDate, bInsertOnWeekend );
1521 0 : }
1522 :
1523 :
1524 0 : sal_Bool SortedIndividualInt32List::Find( sal_Int32 nVal ) const
1525 : {
1526 0 : sal_uInt32 nE = Count();
1527 :
1528 0 : if( !nE || nVal < Get( 0 ) || nVal > Get( nE - 1 ) )
1529 0 : return sal_False;
1530 :
1531 : // linear search
1532 :
1533 0 : for( sal_uInt32 n = 0 ; n < nE ; n++ )
1534 : {
1535 0 : sal_Int32 nRef = Get( n );
1536 :
1537 0 : if( nRef == nVal )
1538 0 : return sal_True;
1539 0 : else if( nRef > nVal )
1540 0 : return sal_False;
1541 : }
1542 0 : return sal_False;
1543 : }
1544 :
1545 :
1546 0 : void SortedIndividualInt32List::InsertHolidayList(
1547 : const ScaAnyConverter& rAnyConv,
1548 : const uno::Any& rHolAny,
1549 : sal_Int32 nNullDate,
1550 : sal_Bool bInsertOnWeekend ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1551 : {
1552 : double fDay;
1553 0 : if( rAnyConv.getDouble( fDay, rHolAny ) )
1554 0 : Insert( fDay, nNullDate, bInsertOnWeekend );
1555 0 : }
1556 :
1557 :
1558 0 : void SortedIndividualInt32List::InsertHolidayList(
1559 : ScaAnyConverter& rAnyConv,
1560 : const uno::Reference< beans::XPropertySet >& xOptions,
1561 : const uno::Any& rHolAny,
1562 : sal_Int32 nNullDate,
1563 : sal_Bool bInsertOnWeekend ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1564 : {
1565 0 : rAnyConv.init( xOptions );
1566 0 : if( rHolAny.getValueTypeClass() == uno::TypeClass_SEQUENCE )
1567 : {
1568 0 : uno::Sequence< uno::Sequence< uno::Any > > aAnySeq;
1569 0 : if( rHolAny >>= aAnySeq )
1570 : {
1571 0 : const uno::Sequence< uno::Any >* pSeqArray = aAnySeq.getConstArray();
1572 0 : for( sal_Int32 nIndex1 = 0; nIndex1 < aAnySeq.getLength(); nIndex1++ )
1573 : {
1574 0 : const uno::Sequence< uno::Any >& rSubSeq = pSeqArray[ nIndex1 ];
1575 0 : const uno::Any* pAnyArray = rSubSeq.getConstArray();
1576 :
1577 0 : for( sal_Int32 nIndex2 = 0; nIndex2 < rSubSeq.getLength(); nIndex2++ )
1578 0 : InsertHolidayList( rAnyConv, pAnyArray[ nIndex2 ], nNullDate, bInsertOnWeekend );
1579 : }
1580 : }
1581 : else
1582 0 : throw lang::IllegalArgumentException();
1583 : }
1584 : else
1585 0 : InsertHolidayList( rAnyConv, rHolAny, nNullDate, bInsertOnWeekend );
1586 0 : }
1587 :
1588 :
1589 :
1590 0 : void ScaDoubleList::Append(
1591 : const uno::Sequence< uno::Sequence< double > >& rValueSeq ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1592 : {
1593 0 : const uno::Sequence< double >* pSeqArray = rValueSeq.getConstArray();
1594 0 : for( sal_Int32 nIndex1 = 0; nIndex1 < rValueSeq.getLength(); nIndex1++ )
1595 : {
1596 0 : const uno::Sequence< double >& rSubSeq = pSeqArray[ nIndex1 ];
1597 0 : const double* pArray = rSubSeq.getConstArray();
1598 0 : for( sal_Int32 nIndex2 = 0; nIndex2 < rSubSeq.getLength(); nIndex2++ )
1599 0 : Append( pArray[ nIndex2 ] );
1600 : }
1601 0 : }
1602 :
1603 :
1604 0 : void ScaDoubleList::Append(
1605 : const uno::Sequence< uno::Sequence< sal_Int32 > >& rValueSeq ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1606 : {
1607 0 : const uno::Sequence< sal_Int32 >* pSeqArray = rValueSeq.getConstArray();
1608 0 : for( sal_Int32 nIndex1 = 0; nIndex1 < rValueSeq.getLength(); nIndex1++ )
1609 : {
1610 0 : const uno::Sequence< sal_Int32 >& rSubSeq = pSeqArray[ nIndex1 ];
1611 0 : const sal_Int32* pArray = rSubSeq.getConstArray();
1612 0 : for( sal_Int32 nIndex2 = 0; nIndex2 < rSubSeq.getLength(); nIndex2++ )
1613 0 : Append( pArray[ nIndex2 ] );
1614 : }
1615 0 : }
1616 :
1617 0 : void ScaDoubleList::Append(
1618 : const ScaAnyConverter& rAnyConv,
1619 : const uno::Any& rAny,
1620 : sal_Bool bIgnoreEmpty ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1621 : {
1622 0 : if( rAny.getValueTypeClass() == uno::TypeClass_SEQUENCE )
1623 0 : Append( rAnyConv, *static_cast< const uno::Sequence< uno::Sequence< uno::Any > >* >( rAny.getValue() ), bIgnoreEmpty );
1624 : else
1625 : {
1626 : double fValue;
1627 0 : if( rAnyConv.getDouble( fValue, rAny ) )
1628 0 : Append( fValue );
1629 0 : else if( !bIgnoreEmpty )
1630 0 : Append( 0.0 );
1631 : }
1632 0 : }
1633 :
1634 :
1635 0 : void ScaDoubleList::Append(
1636 : const ScaAnyConverter& rAnyConv,
1637 : const uno::Sequence< uno::Any >& rAnySeq,
1638 : sal_Bool bIgnoreEmpty ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1639 : {
1640 0 : const uno::Any* pArray = rAnySeq.getConstArray();
1641 0 : for( sal_Int32 nIndex = 0; nIndex < rAnySeq.getLength(); nIndex++ )
1642 0 : Append( rAnyConv, pArray[ nIndex ], bIgnoreEmpty );
1643 0 : }
1644 :
1645 :
1646 0 : void ScaDoubleList::Append(
1647 : const ScaAnyConverter& rAnyConv,
1648 : const uno::Sequence< uno::Sequence< uno::Any > >& rAnySeq,
1649 : sal_Bool bIgnoreEmpty ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1650 : {
1651 0 : const uno::Sequence< uno::Any >* pArray = rAnySeq.getConstArray();
1652 0 : for( sal_Int32 nIndex = 0; nIndex < rAnySeq.getLength(); nIndex++ )
1653 0 : Append( rAnyConv, pArray[ nIndex ], bIgnoreEmpty );
1654 0 : }
1655 :
1656 0 : void ScaDoubleList::Append(
1657 : ScaAnyConverter& rAnyConv,
1658 : const uno::Reference< beans::XPropertySet >& xOpt,
1659 : const uno::Sequence< uno::Any >& rAnySeq,
1660 : sal_Bool bIgnoreEmpty ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1661 : {
1662 0 : rAnyConv.init( xOpt );
1663 0 : Append( rAnyConv, rAnySeq, bIgnoreEmpty );
1664 0 : }
1665 :
1666 :
1667 0 : sal_Bool ScaDoubleList::CheckInsert( double ) const throw( uno::RuntimeException, lang::IllegalArgumentException )
1668 : {
1669 0 : return sal_True;
1670 : }
1671 :
1672 :
1673 :
1674 0 : sal_Bool ScaDoubleListGT0::CheckInsert( double fValue ) const throw( uno::RuntimeException, lang::IllegalArgumentException )
1675 : {
1676 0 : if( fValue < 0.0 )
1677 0 : throw lang::IllegalArgumentException();
1678 0 : return fValue > 0.0;
1679 : }
1680 :
1681 :
1682 :
1683 0 : sal_Bool ScaDoubleListGE0::CheckInsert( double fValue ) const throw( uno::RuntimeException, lang::IllegalArgumentException )
1684 : {
1685 0 : if( fValue < 0.0 )
1686 0 : throw lang::IllegalArgumentException();
1687 0 : return sal_True;
1688 : }
1689 :
1690 :
1691 :
1692 0 : Complex::Complex( const OUString& rStr ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1693 : {
1694 0 : if( !ParseString( rStr, *this ) )
1695 0 : throw lang::IllegalArgumentException();
1696 0 : }
1697 :
1698 :
1699 0 : inline sal_Bool Complex::IsImagUnit( sal_Unicode c )
1700 : {
1701 0 : return c == 'i' || c == 'j';
1702 : }
1703 :
1704 0 : sal_Bool Complex::ParseString( const OUString& rStr, Complex& rCompl )
1705 : {
1706 0 : rCompl.c = '\0'; // do not force a symbol, if only real part present
1707 :
1708 0 : const sal_Unicode* pStr = rStr.getStr();
1709 :
1710 0 : if( IsImagUnit( *pStr ) && rStr.getLength() == 1)
1711 : {
1712 0 : rCompl.r = 0.0;
1713 0 : rCompl.i = 1.0;
1714 0 : rCompl.c = *pStr;
1715 0 : return sal_True;
1716 : }
1717 :
1718 : double f;
1719 :
1720 0 : if( !ParseDouble( pStr, f ) )
1721 0 : return sal_False;
1722 :
1723 0 : switch( *pStr )
1724 : {
1725 : case '-': // imag part follows
1726 : case '+':
1727 : {
1728 0 : double r = f;
1729 0 : if( IsImagUnit( pStr[ 1 ] ) )
1730 : {
1731 0 : rCompl.c = pStr[ 1 ];
1732 0 : if( pStr[ 2 ] == 0 )
1733 : {
1734 0 : rCompl.r = f;
1735 0 : rCompl.i = ( *pStr == '+' )? 1.0 : -1.0;
1736 0 : return sal_True;
1737 : }
1738 : }
1739 0 : else if( ParseDouble( pStr, f ) && IsImagUnit( *pStr ) )
1740 : {
1741 0 : rCompl.c = *pStr;
1742 0 : pStr++;
1743 0 : if( *pStr == 0 )
1744 : {
1745 0 : rCompl.r = r;
1746 0 : rCompl.i = f;
1747 0 : return sal_True;
1748 : }
1749 : }
1750 : }
1751 0 : break;
1752 : case 'j':
1753 : case 'i':
1754 0 : rCompl.c = *pStr;
1755 0 : pStr++;
1756 0 : if( *pStr == 0 )
1757 : {
1758 0 : rCompl.i = f;
1759 0 : rCompl.r = 0.0;
1760 0 : return sal_True;
1761 : }
1762 0 : break;
1763 : case 0: // only real-part
1764 0 : rCompl.r = f;
1765 0 : rCompl.i = 0.0;
1766 0 : return sal_True;
1767 : }
1768 :
1769 0 : return sal_False;
1770 : }
1771 :
1772 :
1773 0 : OUString Complex::GetString() const throw( uno::RuntimeException, lang::IllegalArgumentException )
1774 : {
1775 0 : CHK_FINITE(r);
1776 0 : CHK_FINITE(i);
1777 0 : OUStringBuffer aRet;
1778 :
1779 0 : bool bHasImag = i != 0.0;
1780 0 : bool bHasReal = !bHasImag || (r != 0.0);
1781 :
1782 0 : if( bHasReal )
1783 0 : aRet.append(::GetString( r ));
1784 0 : if( bHasImag )
1785 : {
1786 0 : if( i == 1.0 )
1787 : {
1788 0 : if( bHasReal )
1789 0 : aRet.append('+');
1790 : }
1791 0 : else if( i == -1.0 )
1792 0 : aRet.append('-');
1793 : else
1794 0 : aRet.append(::GetString( i, bHasReal ));
1795 0 : aRet.append((c != 'j') ? 'i' : 'j');
1796 : }
1797 :
1798 0 : return aRet.makeStringAndClear();
1799 : }
1800 :
1801 :
1802 0 : double Complex::Arg( void ) const throw( uno::RuntimeException, lang::IllegalArgumentException )
1803 : {
1804 0 : if( r == 0.0 && i == 0.0 )
1805 0 : throw lang::IllegalArgumentException();
1806 :
1807 0 : double phi = acos( r / Abs() );
1808 :
1809 0 : if( i < 0.0 )
1810 0 : phi = -phi;
1811 :
1812 0 : return phi;
1813 : }
1814 :
1815 :
1816 0 : void Complex::Power( double fPower ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1817 : {
1818 0 : if( r == 0.0 && i == 0.0 )
1819 : {
1820 0 : if( fPower > 0 )
1821 : {
1822 0 : r = i = 0.0;
1823 0 : return;
1824 : }
1825 : else
1826 0 : throw lang::IllegalArgumentException();
1827 : }
1828 :
1829 : double p, phi;
1830 :
1831 0 : p = Abs();
1832 :
1833 0 : phi = acos( r / p );
1834 0 : if( i < 0.0 )
1835 0 : phi = -phi;
1836 :
1837 0 : p = pow( p, fPower );
1838 0 : phi *= fPower;
1839 :
1840 0 : r = cos( phi ) * p;
1841 0 : i = sin( phi ) * p;
1842 : }
1843 :
1844 :
1845 0 : void Complex::Sqrt( void )
1846 : {
1847 : static const double fMultConst = 0.7071067811865475; // ...2440084436210485 = 1/sqrt(2)
1848 0 : double p = Abs();
1849 0 : double i_ = sqrt( p - r ) * fMultConst;
1850 :
1851 0 : r = sqrt( p + r ) * fMultConst;
1852 0 : i = ( i < 0.0 )? -i_ : i_;
1853 0 : }
1854 :
1855 :
1856 0 : void Complex::Sin( void ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1857 : {
1858 0 : if( !::rtl::math::isValidArcArg( r ) )
1859 0 : throw lang::IllegalArgumentException();
1860 :
1861 0 : if( i )
1862 : {
1863 : double r_;
1864 :
1865 0 : r_ = sin( r ) * cosh( i );
1866 0 : i = cos( r ) * sinh( i );
1867 0 : r = r_;
1868 : }
1869 : else
1870 0 : r = sin( r );
1871 0 : }
1872 :
1873 :
1874 0 : void Complex::Cos( void ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1875 : {
1876 0 : if( !::rtl::math::isValidArcArg( r ) )
1877 0 : throw lang::IllegalArgumentException();
1878 :
1879 0 : if( i )
1880 : {
1881 : double r_;
1882 :
1883 0 : r_ = cos( r ) * cosh( i );
1884 0 : i = -( sin( r ) * sinh( i ) );
1885 0 : r = r_;
1886 : }
1887 : else
1888 0 : r = cos( r );
1889 0 : }
1890 :
1891 :
1892 0 : void Complex::Div( const Complex& z ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1893 : {
1894 0 : if( z.r == 0 && z.i == 0 )
1895 0 : throw lang::IllegalArgumentException();
1896 :
1897 0 : double a1 = r;
1898 0 : double a2 = z.r;
1899 0 : double b1 = i;
1900 0 : double b2 = z.i;
1901 :
1902 0 : double f = 1.0 / ( a2 * a2 + b2 * b2 );
1903 :
1904 0 : r = ( a1 * a2 + b1 * b2 ) * f;
1905 0 : i = ( a2 * b1 - a1 * b2 ) * f;
1906 :
1907 0 : if( !c ) c = z.c;
1908 0 : }
1909 :
1910 :
1911 0 : void Complex::Exp( void )
1912 : {
1913 0 : double fE = exp( r );
1914 0 : r = fE * cos( i );
1915 0 : i = fE * sin( i );
1916 0 : }
1917 :
1918 :
1919 0 : void Complex::Ln( void ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1920 : {
1921 0 : if( r == 0.0 && i == 0.0 )
1922 0 : throw lang::IllegalArgumentException();
1923 :
1924 0 : double fAbs = Abs();
1925 0 : sal_Bool bNegi = i < 0.0;
1926 :
1927 0 : i = acos( r / fAbs );
1928 :
1929 0 : if( bNegi )
1930 0 : i = -i;
1931 :
1932 0 : r = log( fAbs );
1933 0 : }
1934 :
1935 :
1936 0 : void Complex::Log10( void ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1937 : {
1938 0 : Ln();
1939 0 : Mult( 0.434294481903251828 ); // * log10( e )
1940 0 : }
1941 :
1942 :
1943 0 : void Complex::Log2( void ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1944 : {
1945 0 : Ln();
1946 0 : Mult( 1.442695040888963407 ); // * log2( e )
1947 0 : }
1948 :
1949 :
1950 0 : void Complex::Tan(void) throw( uno::RuntimeException, lang::IllegalArgumentException )
1951 : {
1952 0 : if ( i )
1953 : {
1954 0 : if( !::rtl::math::isValidArcArg( 2.0 * r ) )
1955 0 : throw lang::IllegalArgumentException();
1956 0 : double fScale =1.0 / ( cos( 2.0 * r ) + cosh( 2.0 * i ));
1957 0 : r = sin( 2.0 * r ) * fScale;
1958 0 : i = sinh( 2.0 * i ) * fScale;
1959 : }
1960 : else
1961 : {
1962 0 : if( !::rtl::math::isValidArcArg( r ) )
1963 0 : throw lang::IllegalArgumentException();
1964 0 : r = tan( r );
1965 : }
1966 0 : }
1967 :
1968 :
1969 0 : void Complex::Sec( void ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1970 : {
1971 0 : if( i )
1972 : {
1973 0 : if( !::rtl::math::isValidArcArg( 2 * r ) )
1974 0 : throw lang::IllegalArgumentException();
1975 0 : double fScale = 1.0 / (cosh( 2.0 * i) + cos ( 2.0 * r));
1976 : double r_;
1977 0 : r_ = 2.0 * cos( r ) * cosh( i ) * fScale;
1978 0 : i = 2.0 * sin( r ) * sinh( i ) * fScale;
1979 0 : r = r_;
1980 : }
1981 : else
1982 : {
1983 0 : if( !::rtl::math::isValidArcArg( r ) )
1984 0 : throw lang::IllegalArgumentException();
1985 0 : r = 1.0 / cos( r );
1986 : }
1987 0 : }
1988 :
1989 :
1990 0 : void Complex::Csc( void ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1991 : {
1992 0 : if( i )
1993 : {
1994 0 : if( !::rtl::math::isValidArcArg( 2 * r ) )
1995 0 : throw lang::IllegalArgumentException();
1996 0 : double fScale = 1.0 / (cosh( 2.0 * i) - cos ( 2.0 * r));
1997 : double r_;
1998 0 : r_ = 2.0 * sin( r ) * cosh( i ) * fScale;
1999 0 : i = -2.0 * cos( r ) * sinh( i ) * fScale;
2000 0 : r = r_;
2001 : }
2002 : else
2003 : {
2004 0 : if( !::rtl::math::isValidArcArg( r ) )
2005 0 : throw lang::IllegalArgumentException();
2006 0 : r = 1.0 / sin( r );
2007 : }
2008 0 : }
2009 :
2010 :
2011 0 : void Complex::Cot(void) throw( uno::RuntimeException, lang::IllegalArgumentException )
2012 : {
2013 0 : if ( i )
2014 : {
2015 0 : if( !::rtl::math::isValidArcArg( 2.0 * r ) )
2016 0 : throw lang::IllegalArgumentException();
2017 0 : double fScale =1.0 / ( cosh( 2.0 * i ) - cos( 2.0 * r ) );
2018 0 : r = sin( 2.0 * r ) * fScale;
2019 0 : i = - ( sinh( 2.0 * i ) * fScale );
2020 : }
2021 : else
2022 : {
2023 0 : if( !::rtl::math::isValidArcArg( r ) )
2024 0 : throw lang::IllegalArgumentException();
2025 0 : r = 1.0 / tan( r );
2026 : }
2027 0 : }
2028 :
2029 :
2030 0 : void Complex::Sinh( void ) throw( uno::RuntimeException, lang::IllegalArgumentException )
2031 : {
2032 0 : if( !::rtl::math::isValidArcArg( r ) )
2033 0 : throw lang::IllegalArgumentException();
2034 :
2035 0 : if( i )
2036 : {
2037 : double r_;
2038 0 : r_ = sinh( r ) * cos( i );
2039 0 : i = cosh( r ) * sin( i );
2040 0 : r = r_;
2041 : }
2042 : else
2043 0 : r = sinh( r );
2044 0 : }
2045 :
2046 :
2047 0 : void Complex::Cosh( void ) throw( uno::RuntimeException, lang::IllegalArgumentException )
2048 : {
2049 0 : if( !::rtl::math::isValidArcArg( r ) )
2050 0 : throw lang::IllegalArgumentException();
2051 :
2052 0 : if( i )
2053 : {
2054 : double r_;
2055 0 : r_ = cosh( r ) * cos( i );
2056 0 : i = sinh( r ) * sin( i );
2057 0 : r = r_;
2058 : }
2059 : else
2060 0 : r = cosh( r );
2061 0 : }
2062 :
2063 :
2064 0 : void Complex::Sech(void) throw( uno::RuntimeException, lang::IllegalArgumentException )
2065 : {
2066 0 : if ( i )
2067 : {
2068 0 : if( !::rtl::math::isValidArcArg( 2.0 * r ) )
2069 0 : throw lang::IllegalArgumentException();
2070 0 : double fScale =1.0 / ( cosh( 2.0 * r ) + cos( 2.0 * i ));
2071 : double r_;
2072 0 : r_ = 2.0 * cosh( r ) * cos( i ) * fScale;
2073 0 : i = - (2.0 * sinh( r ) * sin( i ) * fScale );
2074 0 : r = r_ ;
2075 : }
2076 : else
2077 : {
2078 0 : if( !::rtl::math::isValidArcArg( r ) )
2079 0 : throw lang::IllegalArgumentException();
2080 0 : r = 1.0 / cosh( r );
2081 : }
2082 0 : }
2083 :
2084 :
2085 0 : void Complex::Csch(void) throw( uno::RuntimeException, lang::IllegalArgumentException )
2086 : {
2087 0 : if ( i )
2088 : {
2089 0 : if( !::rtl::math::isValidArcArg( 2.0 * r ) )
2090 0 : throw lang::IllegalArgumentException();
2091 0 : double fScale =1.0 / ( cosh( 2.0 * r ) - cos( 2.0 * i ));
2092 : double r_;
2093 0 : r_ = 2.0 * sinh( r ) * cos( i ) * fScale;
2094 0 : i = - ( 2.0 * cosh( r ) * sin( i ) * fScale );
2095 0 : r = r_ ;
2096 : }
2097 : else
2098 : {
2099 0 : if( !::rtl::math::isValidArcArg( r ) )
2100 0 : throw lang::IllegalArgumentException();
2101 0 : r = 1.0 / sinh( r );
2102 : }
2103 0 : }
2104 :
2105 :
2106 0 : ComplexList::~ComplexList()
2107 : {
2108 0 : for( sal_uInt32 i = 0; i < maVector.size(); ++i )
2109 0 : delete maVector[i];
2110 0 : }
2111 :
2112 :
2113 0 : void ComplexList::Append( const uno::Sequence< uno::Sequence< OUString > >& r, ComplListAppendHandl eAH ) throw( uno::RuntimeException, lang::IllegalArgumentException )
2114 : {
2115 : sal_Int32 n1, n2;
2116 0 : sal_Int32 nE1 = r.getLength();
2117 : sal_Int32 nE2;
2118 0 : sal_Bool bEmpty0 = eAH == AH_EmpyAs0;
2119 0 : sal_Bool bErrOnEmpty = eAH == AH_EmptyAsErr;
2120 :
2121 0 : for( n1 = 0 ; n1 < nE1 ; n1++ )
2122 : {
2123 0 : const uno::Sequence< OUString >& rList = r[ n1 ];
2124 0 : nE2 = rList.getLength();
2125 :
2126 0 : for( n2 = 0 ; n2 < nE2 ; n2++ )
2127 : {
2128 0 : const OUString& rStr = rList[ n2 ];
2129 :
2130 0 : if( !rStr.isEmpty() )
2131 0 : Append( new Complex( rStr ) );
2132 0 : else if( bEmpty0 )
2133 0 : Append( new Complex( 0.0 ) );
2134 0 : else if( bErrOnEmpty )
2135 0 : throw lang::IllegalArgumentException();
2136 : }
2137 : }
2138 0 : }
2139 :
2140 :
2141 0 : void ComplexList::Append( const uno::Sequence< uno::Any >& aMultPars, ComplListAppendHandl eAH ) throw( uno::RuntimeException, lang::IllegalArgumentException )
2142 : {
2143 0 : sal_Int32 nEle = aMultPars.getLength();
2144 0 : sal_Bool bEmpty0 = eAH == AH_EmpyAs0;
2145 0 : sal_Bool bErrOnEmpty = eAH == AH_EmptyAsErr;
2146 :
2147 0 : for( sal_Int32 i = 0 ; i < nEle ; i++ )
2148 : {
2149 0 : const uno::Any& r = aMultPars[ i ];
2150 0 : switch( r.getValueTypeClass() )
2151 : {
2152 0 : case uno::TypeClass_VOID: break;
2153 : case uno::TypeClass_STRING:
2154 : {
2155 0 : const OUString* pStr = ( const OUString* ) r.getValue();
2156 :
2157 0 : if( !pStr->isEmpty() )
2158 0 : Append( new Complex( *( OUString* ) r.getValue() ) );
2159 0 : else if( bEmpty0 )
2160 0 : Append( new Complex( 0.0 ) );
2161 0 : else if( bErrOnEmpty )
2162 0 : throw lang::IllegalArgumentException();
2163 : }
2164 0 : break;
2165 : case uno::TypeClass_DOUBLE:
2166 0 : Append( new Complex( *( double* ) r.getValue(), 0.0 ) );
2167 0 : break;
2168 : case uno::TypeClass_SEQUENCE:
2169 : {
2170 0 : uno::Sequence< uno::Sequence< uno::Any > > aValArr;
2171 0 : if( r >>= aValArr )
2172 : {
2173 0 : sal_Int32 nE = aValArr.getLength();
2174 0 : const uno::Sequence< uno::Any >* pArr = aValArr.getConstArray();
2175 0 : for( sal_Int32 n = 0 ; n < nE ; n++ )
2176 0 : Append( pArr[ n ], eAH );
2177 : }
2178 : else
2179 0 : throw lang::IllegalArgumentException();
2180 : }
2181 0 : break;
2182 : default:
2183 0 : throw lang::IllegalArgumentException();
2184 : }
2185 : }
2186 0 : }
2187 :
2188 :
2189 0 : ConvertData::ConvertData( const sal_Char p[], double fC, ConvertDataClass e, sal_Bool bPrefSupport ) : aName( p, strlen( p ), RTL_TEXTENCODING_MS_1252 )
2190 : {
2191 0 : fConst = fC;
2192 0 : eClass = e;
2193 0 : bPrefixSupport = bPrefSupport;
2194 0 : }
2195 :
2196 0 : ConvertData::~ConvertData()
2197 : {
2198 0 : }
2199 :
2200 :
2201 0 : sal_Int16 ConvertData::GetMatchingLevel( const OUString& rRef ) const
2202 : {
2203 0 : OUString aStr = rRef;
2204 0 : sal_Int32 nLen = rRef.getLength();
2205 0 : sal_Int32 nIndex = rRef.lastIndexOf( '^' );
2206 0 : if( nIndex > 0 && nIndex == ( nLen - 2 ) )
2207 : {
2208 0 : const sal_Unicode* p = aStr.getStr();
2209 0 : aStr = OUString( p, nLen - 2 );
2210 0 : aStr += OUString( p[ nLen - 1 ] );
2211 : }
2212 0 : if( aName.equals( aStr ) )
2213 0 : return 0;
2214 : else
2215 : {
2216 0 : const sal_Unicode* p = aStr.getStr();
2217 :
2218 0 : nLen = aStr.getLength();
2219 0 : bool bPref = IsPrefixSupport();
2220 0 : bool bOneChar = (bPref && nLen > 1 && (aName == p + 1));
2221 0 : if (bOneChar || (bPref && nLen > 2 && (aName == p + 2) &&
2222 0 : *p == 'd' && *(p+1) == 'a'))
2223 : {
2224 : sal_Int16 n;
2225 0 : switch( *p )
2226 : {
2227 0 : case 'y': n = -24; break; // yocto
2228 0 : case 'z': n = -21; break; // zepto
2229 0 : case 'a': n = -18; break;
2230 0 : case 'f': n = -15; break;
2231 0 : case 'p': n = -12; break;
2232 0 : case 'n': n = -9; break;
2233 0 : case 'u': n = -6; break;
2234 0 : case 'm': n = -3; break;
2235 0 : case 'c': n = -2; break;
2236 : case 'd':
2237 : {
2238 0 : if ( bOneChar )
2239 0 : n = -1; // deci
2240 : else
2241 0 : n = 1; // deca
2242 : }
2243 0 : break;
2244 0 : case 'e': n = 1; break;
2245 0 : case 'h': n = 2; break;
2246 0 : case 'k': n = 3; break;
2247 0 : case 'M': n = 6; break;
2248 0 : case 'G': n = 9; break;
2249 0 : case 'T': n = 12; break;
2250 0 : case 'P': n = 15; break;
2251 0 : case 'E': n = 18; break;
2252 0 : case 'Z': n = 21; break; // zetta
2253 0 : case 'Y': n = 24; break; // yotta
2254 : default:
2255 0 : n = INV_MATCHLEV;
2256 : }
2257 :
2258 : // We could weed some nonsense out, ODFF doesn't say so though.
2259 : #if 0
2260 : if (n < 0 && Class() == CDC_Information)
2261 : n = INV_MATCHLEV; // milli-bits doesn't make sense
2262 : #endif
2263 :
2264 : //! <HACK> "cm3" is not 10^-2 m^3 but 10^-6 m^3 !!! ------------------
2265 0 : if( n != INV_MATCHLEV )
2266 : {
2267 0 : sal_Unicode cLast = p[ aStr.getLength() - 1 ];
2268 0 : if( cLast == '2' )
2269 0 : n *= 2;
2270 0 : else if( cLast == '3' )
2271 0 : n *= 3;
2272 : }
2273 : //! </HACK> -------------------------------------------------------------------
2274 :
2275 0 : return n;
2276 : }
2277 0 : else if ( nLen > 2 && ( aName == p + 2 ) && ( Class() == CDC_Information ) )
2278 : {
2279 0 : const sal_Unicode* pStr = aStr.getStr();
2280 0 : if ( *(pStr + 1) != 'i')
2281 0 : return INV_MATCHLEV;
2282 : sal_Int16 n;
2283 0 : switch( *pStr )
2284 : {
2285 0 : case 'k': n = 10; break;
2286 0 : case 'M': n = 20; break;
2287 0 : case 'G': n = 30; break;
2288 0 : case 'T': n = 40; break;
2289 0 : case 'P': n = 50; break;
2290 0 : case 'E': n = 60; break;
2291 0 : case 'Z': n = 70; break;
2292 0 : case 'Y': n = 80; break;
2293 : default:
2294 0 : n = INV_MATCHLEV;
2295 : }
2296 0 : return n;
2297 : }
2298 : else
2299 0 : return INV_MATCHLEV;
2300 0 : }
2301 : }
2302 :
2303 :
2304 0 : double ConvertData::Convert(
2305 : double f, const ConvertData& r, sal_Int16 nLevFrom, sal_Int16 nLevTo ) const throw( uno::RuntimeException, lang::IllegalArgumentException )
2306 : {
2307 0 : if( Class() != r.Class() )
2308 0 : throw lang::IllegalArgumentException();
2309 :
2310 0 : sal_Bool bBinFromLev = ( nLevFrom > 0 && ( nLevFrom % 10 ) == 0 );
2311 0 : sal_Bool bBinToLev = ( nLevTo > 0 && ( nLevTo % 10 ) == 0 );
2312 :
2313 0 : if ( Class() == CDC_Information && ( bBinFromLev || bBinToLev ) )
2314 : {
2315 0 : if ( bBinFromLev && bBinToLev )
2316 : {
2317 0 : nLevFrom = sal::static_int_cast<sal_Int16>( nLevFrom - nLevTo );
2318 0 : f *= r.fConst / fConst;
2319 0 : if( nLevFrom )
2320 0 : f *= pow( 2.0, nLevFrom );
2321 : }
2322 0 : else if ( bBinFromLev )
2323 0 : f *= ( r.fConst / fConst ) * ( pow( 2.0, nLevFrom ) / pow( 10.0, nLevTo ) );
2324 : else
2325 0 : f *= ( r.fConst / fConst ) * ( pow( 10.0, nLevFrom ) / pow( 2.0, nLevTo ) );
2326 0 : return f;
2327 : }
2328 :
2329 0 : nLevFrom = sal::static_int_cast<sal_Int16>( nLevFrom - nLevTo ); // effective level
2330 :
2331 0 : f *= r.fConst / fConst;
2332 :
2333 0 : if( nLevFrom )
2334 0 : f = ::rtl::math::pow10Exp( f, nLevFrom );
2335 :
2336 0 : return f;
2337 : }
2338 :
2339 :
2340 0 : double ConvertData::ConvertToBase( double f, sal_Int16 n ) const
2341 : {
2342 0 : return ::rtl::math::pow10Exp( f / fConst, n );
2343 : }
2344 :
2345 :
2346 0 : double ConvertData::ConvertFromBase( double f, sal_Int16 n ) const
2347 : {
2348 0 : return ::rtl::math::pow10Exp( f * fConst, -n );
2349 : }
2350 :
2351 0 : ConvertDataLinear::~ConvertDataLinear()
2352 : {
2353 0 : }
2354 :
2355 0 : double ConvertDataLinear::Convert(
2356 : double f, const ConvertData& r, sal_Int16 nLevFrom, sal_Int16 nLevTo ) const throw( uno::RuntimeException, lang::IllegalArgumentException )
2357 : {
2358 0 : if( Class() != r.Class() )
2359 0 : throw lang::IllegalArgumentException();
2360 0 : return r.ConvertFromBase( ConvertToBase( f, nLevFrom ), nLevTo );
2361 : }
2362 :
2363 :
2364 0 : double ConvertDataLinear::ConvertToBase( double f, sal_Int16 n ) const
2365 : {
2366 0 : if( n )
2367 0 : f = ::rtl::math::pow10Exp( f, n );
2368 :
2369 0 : f /= fConst;
2370 0 : f -= fOffs;
2371 :
2372 0 : return f;
2373 : }
2374 :
2375 :
2376 0 : double ConvertDataLinear::ConvertFromBase( double f, sal_Int16 n ) const
2377 : {
2378 0 : f += fOffs;
2379 0 : f *= fConst;
2380 :
2381 0 : if( n )
2382 0 : f = ::rtl::math::pow10Exp( f, -n );
2383 :
2384 0 : return f;
2385 : }
2386 :
2387 :
2388 0 : ConvertDataList::ConvertDataList( void )
2389 : {
2390 : #define NEWD(str,unit,cl) maVector.push_back(new ConvertData(str,unit,cl))
2391 : #define NEWDP(str,unit,cl) maVector.push_back(new ConvertData(str,unit,cl,sal_True))
2392 : #define NEWL(str,unit,offs,cl) maVector.push_back(new ConvertDataLinear(str,unit,offs,cl))
2393 : #define NEWLP(str,unit,offs,cl) maVector.push_back(new ConvertDataLinear(str,unit,offs,cl,sal_True))
2394 :
2395 : // *** are extra and not standard Excel Analysis Addin!
2396 :
2397 : // MASS: 1 Gram is...
2398 0 : NEWDP( "g", 1.0000000000000000E00, CDC_Mass ); // Gram
2399 0 : NEWD( "sg", 6.8522050005347800E-05, CDC_Mass ); // Pieces
2400 0 : NEWD( "lbm", 2.2046229146913400E-03, CDC_Mass ); // Pound (commercial weight)
2401 0 : NEWDP( "u", 6.0221370000000000E23, CDC_Mass ); // U (atomic mass)
2402 0 : NEWD( "ozm", 3.5273971800362700E-02, CDC_Mass ); // Ounce (commercial weight)
2403 0 : NEWD( "stone", 1.574730e-04, CDC_Mass ); // *** Stone
2404 0 : NEWD( "ton", 1.102311e-06, CDC_Mass ); // *** Ton
2405 0 : NEWD( "grain", 1.543236E01, CDC_Mass ); // *** Grain
2406 0 : NEWD( "pweight", 7.054792E-01, CDC_Mass ); // *** Pennyweight
2407 0 : NEWD( "hweight", 1.968413E-05, CDC_Mass ); // *** Hundredweight
2408 0 : NEWD( "shweight", 2.204623E-05, CDC_Mass ); // *** Shorthundredweight
2409 0 : NEWD( "brton", 9.842065E-07, CDC_Mass ); // *** Gross Registered Ton
2410 0 : NEWD( "cwt", 2.2046226218487758E-05, CDC_Mass ); // U.S. (short) hundredweight
2411 0 : NEWD( "shweight", 2.2046226218487758E-05, CDC_Mass ); // U.S. (short) hundredweight also
2412 0 : NEWD( "uk_cwt", 1.9684130552221213E-05, CDC_Mass ); // Imperial hundredweight
2413 0 : NEWD( "lcwt", 1.9684130552221213E-05, CDC_Mass ); // Imperial hundredweight also
2414 0 : NEWD( "hweight", 1.9684130552221213E-05, CDC_Mass ); // Imperial hundredweight also
2415 0 : NEWD( "uk_ton", 9.8420652761106063E-07, CDC_Mass ); // Imperial ton
2416 0 : NEWD( "LTON", 9.8420652761106063E-07, CDC_Mass ); // Imperial ton also
2417 :
2418 : // LENGTH: 1 Meter is...
2419 0 : NEWDP( "m", 1.0000000000000000E00, CDC_Length ); // Meter
2420 0 : NEWD( "mi", 6.2137119223733397E-04, CDC_Length ); // Britsh Mile 6,21371192237333969617434184363e-4
2421 0 : NEWD( "Nmi", 5.3995680345572354E-04, CDC_Length ); // Nautical Mile 5,39956803455723542116630669546e-4
2422 0 : NEWD( "in", 3.9370078740157480E01, CDC_Length ); // Inch 39,37007874015748031496062992126
2423 0 : NEWD( "ft", 3.2808398950131234E00, CDC_Length ); // Foot 3,2808398950131233595800524934383
2424 0 : NEWD( "yd", 1.0936132983377078E00, CDC_Length ); // Yard 1,0936132983377077865266841644794
2425 0 : NEWDP( "ang", 1.0000000000000000E10, CDC_Length ); // Angstroem
2426 0 : NEWD( "Pica", 2.8346456692913386E03, CDC_Length ); // Pica (1/72 Inch) 2834,6456692913385826771653543307
2427 0 : NEWD( "ell", 8.748906E-01, CDC_Length ); // *** Ell
2428 0 : NEWDP( "parsec", 3.240779E-17, CDC_Length ); // *** Parsec
2429 0 : NEWDP( "pc", 3.240779E-17, CDC_Length ); // *** Parsec also
2430 0 : NEWDP( "lightyear", 1.0570234557732930E-16, CDC_Length ); // *** Light Year
2431 0 : NEWDP( "ly", 1.0570234557732930E-16, CDC_Length ); // *** Light Year also
2432 0 : NEWD( "survey_mi", 6.2136994949494949E-04, CDC_Length ); // U.S. survey mile
2433 :
2434 : // TIME: 1 Second is...
2435 0 : NEWD( "yr", 3.1688087814028950E-08, CDC_Time ); // Year
2436 0 : NEWD( "day", 1.1574074074074074E-05, CDC_Time ); // Day
2437 0 : NEWD( "d", 1.1574074074074074E-05, CDC_Time ); // Day also
2438 0 : NEWD( "hr", 2.7777777777777778E-04, CDC_Time ); // Hour
2439 0 : NEWD( "mn", 1.6666666666666667E-02, CDC_Time ); // Minute
2440 0 : NEWD( "min", 1.6666666666666667E-02, CDC_Time ); // Minute also
2441 0 : NEWDP( "sec", 1.0000000000000000E00, CDC_Time ); // Second
2442 0 : NEWDP( "s", 1.0000000000000000E00, CDC_Time ); // Second also
2443 :
2444 : // PRESSURE: 1 Pascal is...
2445 0 : NEWDP( "Pa", 1.0000000000000000E00, CDC_Pressure ); // Pascal
2446 0 : NEWDP( "atm", 9.8692329999819300E-06, CDC_Pressure ); // Atmosphere
2447 0 : NEWDP( "at", 9.8692329999819300E-06, CDC_Pressure ); // Atmosphere also
2448 0 : NEWDP( "mmHg", 7.5006170799862700E-03, CDC_Pressure ); // mm Hg (Mercury)
2449 0 : NEWD( "Torr", 7.5006380000000000E-03, CDC_Pressure ); // *** Torr
2450 0 : NEWD( "psi", 1.4503770000000000E-04, CDC_Pressure ); // *** Psi
2451 :
2452 : // FORCE: 1 Newton is...
2453 0 : NEWDP( "N", 1.0000000000000000E00, CDC_Force ); // Newton
2454 0 : NEWDP( "dyn", 1.0000000000000000E05, CDC_Force ); // Dyn
2455 0 : NEWDP( "dy", 1.0000000000000000E05, CDC_Force ); // Dyn also
2456 0 : NEWD( "lbf", 2.24808923655339E-01, CDC_Force ); // Pound-Force
2457 0 : NEWDP( "pond", 1.019716E02, CDC_Force ); // *** Pond
2458 :
2459 : // ENERGY: 1 Joule is...
2460 0 : NEWDP( "J", 1.0000000000000000E00, CDC_Energy ); // Joule
2461 0 : NEWDP( "e", 1.0000000000000000E07, CDC_Energy ); // Erg -> http://www.chemie.fu-berlin.de/chemistry/general/si.html
2462 0 : NEWDP( "c", 2.3900624947346700E-01, CDC_Energy ); // Thermodynamical Calorie
2463 0 : NEWDP( "cal", 2.3884619064201700E-01, CDC_Energy ); // Calorie
2464 0 : NEWDP( "eV", 6.2414570000000000E18, CDC_Energy ); // Electronvolt
2465 0 : NEWDP( "ev", 6.2414570000000000E18, CDC_Energy ); // Electronvolt also
2466 0 : NEWD( "HPh", 3.7250611111111111E-07, CDC_Energy ); // Horsepower Hours
2467 0 : NEWD( "hh", 3.7250611111111111E-07, CDC_Energy ); // Horsepower Hours also
2468 0 : NEWDP( "Wh", 2.7777777777777778E-04, CDC_Energy ); // Watt Hours
2469 0 : NEWDP( "wh", 2.7777777777777778E-04, CDC_Energy ); // Watt Hours also
2470 0 : NEWD( "flb", 2.37304222192651E01, CDC_Energy ); // Foot Pound
2471 0 : NEWD( "BTU", 9.4781506734901500E-04, CDC_Energy ); // British Thermal Unit
2472 0 : NEWD( "btu", 9.4781506734901500E-04, CDC_Energy ); // British Thermal Unit also
2473 :
2474 : // POWER: 1 Watt is...
2475 0 : NEWDP( "W", 1.0000000000000000E00, CDC_Power ); // Watt
2476 0 : NEWDP( "w", 1.0000000000000000E00, CDC_Power ); // Watt also
2477 0 : NEWD( "HP", 1.341022E-03, CDC_Power ); // Horsepower
2478 0 : NEWD( "h", 1.341022E-03, CDC_Power ); // Horsepower also
2479 0 : NEWD( "PS", 1.359622E-03, CDC_Power ); // *** German Pferdestaerke
2480 :
2481 : // MAGNETISM: 1 Tesla is...
2482 0 : NEWDP( "T", 1.0000000000000000E00, CDC_Magnetism ); // Tesla
2483 0 : NEWDP( "ga", 1.0000000000000000E04, CDC_Magnetism ); // Gauss
2484 :
2485 : // TEMERATURE: 1 Kelvin is...
2486 0 : NEWL( "C", 1.0000000000000000E00, -2.7315000000000000E02, CDC_Temperature ); // Celsius
2487 0 : NEWL( "cel", 1.0000000000000000E00, -2.7315000000000000E02, CDC_Temperature ); // Celsius also
2488 0 : NEWL( "F", 1.8000000000000000E00, -2.5537222222222222E02, CDC_Temperature ); // Fahrenheit
2489 0 : NEWL( "fah", 1.8000000000000000E00, -2.5537222222222222E02, CDC_Temperature ); // Fahrenheit also
2490 0 : NEWLP( "K", 1.0000000000000000E00, +0.0000000000000000E00, CDC_Temperature ); // Kelvin
2491 0 : NEWLP( "kel", 1.0000000000000000E00, +0.0000000000000000E00, CDC_Temperature ); // Kelvin also
2492 0 : NEWL( "Reau", 8.0000000000000000E-01, -2.7315000000000000E02, CDC_Temperature ); // *** Reaumur
2493 0 : NEWL( "Rank", 1.8000000000000000E00, +0.0000000000000000E00, CDC_Temperature ); // *** Rankine
2494 :
2495 : // VOLUMNE: 1 Liter is...
2496 0 : NEWD( "tsp", 2.0288413621105798E02, CDC_Volume ); // US teaspoon 1/768 gallon
2497 0 : NEWD( "tbs", 6.7628045403685994E01, CDC_Volume ); // US tablespoon 1/256 gallon
2498 0 : NEWD( "oz", 3.3814022701842997E01, CDC_Volume ); // Ounce Liquid 1/128 gallon
2499 0 : NEWD( "cup", 4.2267528377303746E00, CDC_Volume ); // Cup 1/16 gallon
2500 0 : NEWD( "pt", 2.1133764188651873E00, CDC_Volume ); // US Pint 1/8 gallon
2501 0 : NEWD( "us_pt", 2.1133764188651873E00, CDC_Volume ); // US Pint also
2502 0 : NEWD( "uk_pt", 1.7597539863927023E00, CDC_Volume ); // UK Pint 1/8 imperial gallon
2503 0 : NEWD( "qt", 1.0566882094325937E00, CDC_Volume ); // Quart 1/4 gallon
2504 0 : NEWD( "gal", 2.6417205235814842E-01, CDC_Volume ); // Gallon 1/3.785411784
2505 0 : NEWDP( "l", 1.0000000000000000E00, CDC_Volume ); // Liter
2506 0 : NEWDP( "L", 1.0000000000000000E00, CDC_Volume ); // Liter also
2507 0 : NEWDP( "lt", 1.0000000000000000E00, CDC_Volume ); // Liter also
2508 0 : NEWDP( "m3", 1.0000000000000000E-03, CDC_Volume ); // *** Cubic Meter
2509 0 : NEWD( "mi3", 2.3991275857892772E-13, CDC_Volume ); // *** Cubic Britsh Mile
2510 0 : NEWD( "Nmi3", 1.5742621468581148E-13, CDC_Volume ); // *** Cubic Nautical Mile
2511 0 : NEWD( "in3", 6.1023744094732284E01, CDC_Volume ); // *** Cubic Inch
2512 0 : NEWD( "ft3", 3.5314666721488590E-02, CDC_Volume ); // *** Cubic Foot
2513 0 : NEWD( "yd3", 1.3079506193143922E-03, CDC_Volume ); // *** Cubic Yard
2514 0 : NEWDP( "ang3", 1.0000000000000000E27, CDC_Volume ); // *** Cubic Angstroem
2515 0 : NEWD( "Pica3", 2.2776990435870636E07, CDC_Volume ); // *** Cubic Pica
2516 0 : NEWD( "barrel", 6.2898107704321051E-03, CDC_Volume ); // *** Barrel (=42gal)
2517 0 : NEWD( "bushel", 2.837759E-02, CDC_Volume ); // *** Bushel
2518 0 : NEWD( "regton", 3.531467E-04, CDC_Volume ); // *** Register ton
2519 0 : NEWD( "GRT", 3.531467E-04, CDC_Volume ); // *** Register ton also
2520 0 : NEWD( "Schooner", 2.3529411764705882E00, CDC_Volume ); // *** austr. Schooner
2521 0 : NEWD( "Middy", 3.5087719298245614E00, CDC_Volume ); // *** austr. Middy
2522 0 : NEWD( "Glass", 5.0000000000000000E00, CDC_Volume ); // *** austr. Glass
2523 0 : NEWD( "Sixpack", 0.5, CDC_Volume ); // ***
2524 0 : NEWD( "Humpen", 2.0, CDC_Volume ); // ***
2525 0 : NEWD( "ly3", 1.1810108125623799E-51, CDC_Volume ); // *** Cubic light-year
2526 0 : NEWD( "MTON", 1.4125866688595436E00, CDC_Volume ); // *** Measurement ton
2527 0 : NEWD( "tspm", 2.0000000000000000E02, CDC_Volume ); // *** Modern teaspoon
2528 0 : NEWD( "uk_gal", 2.1996924829908779E-01, CDC_Volume ); // U.K. / Imperial gallon 1/4.54609
2529 0 : NEWD( "uk_qt", 8.7987699319635115E-01, CDC_Volume ); // U.K. / Imperial quart 1/4 imperial gallon
2530 :
2531 : // 1 Square Meter is...
2532 0 : NEWDP( "m2", 1.0000000000000000E00, CDC_Area ); // *** Square Meter
2533 0 : NEWD( "mi2", 3.8610215854244585E-07, CDC_Area ); // *** Square Britsh Mile
2534 0 : NEWD( "Nmi2", 2.9155334959812286E-07, CDC_Area ); // *** Square Nautical Mile
2535 0 : NEWD( "in2", 1.5500031000062000E03, CDC_Area ); // *** Square Inch
2536 0 : NEWD( "ft2", 1.0763910416709722E01, CDC_Area ); // *** Square Foot
2537 0 : NEWD( "yd2", 1.1959900463010803E00, CDC_Area ); // *** Square Yard
2538 0 : NEWDP( "ang2", 1.0000000000000000E20, CDC_Area ); // *** Square Angstroem
2539 0 : NEWD( "Pica2", 8.0352160704321409E06, CDC_Area ); // *** Square Pica
2540 0 : NEWD( "Morgen", 4.0000000000000000E-04, CDC_Area ); // *** Morgen
2541 0 : NEWDP( "ar", 1.000000E-02, CDC_Area ); // *** Ar
2542 0 : NEWD( "acre", 2.471053815E-04, CDC_Area ); // *** Acre
2543 0 : NEWD( "uk_acre", 2.4710538146716534E-04, CDC_Area ); // *** International acre
2544 0 : NEWD( "us_acre", 2.4710439304662790E-04, CDC_Area ); // *** U.S. survey/statute acre
2545 0 : NEWD( "ly2", 1.1172985860549147E-32, CDC_Area ); // *** Square Light-year
2546 0 : NEWD( "ha", 1.000000E-04, CDC_Area ); // *** Hectare
2547 0 : NEWD( "Quadratlatschen",5.6689342403628117914,CDC_Area ); // ***
2548 :
2549 : // SPEED: 1 Meter per Second is...
2550 0 : NEWDP( "m/s", 1.0000000000000000E00, CDC_Speed ); // *** Meters per Second
2551 0 : NEWDP( "m/sec", 1.0000000000000000E00, CDC_Speed ); // *** Meters per Second also
2552 0 : NEWDP( "m/h", 3.6000000000000000E03, CDC_Speed ); // *** Meters per Hour
2553 0 : NEWDP( "m/hr", 3.6000000000000000E03, CDC_Speed ); // *** Meters per Hour also
2554 0 : NEWD( "mph", 2.2369362920544023E00, CDC_Speed ); // *** Britsh Miles per Hour
2555 0 : NEWD( "kn", 1.9438444924406048E00, CDC_Speed ); // *** Knot = Nautical Miles per Hour
2556 0 : NEWD( "admkn", 1.9438446603753486E00, CDC_Speed ); // *** Admiralty Knot
2557 0 : NEWD( "wahnsinnige Geschwindigkeit", 2.0494886343432328E-14, CDC_Speed ); // ***
2558 0 : NEWD( "ludicrous speed", 2.0494886343432328E-14, CDC_Speed ); // ***
2559 0 : NEWD( "laecherliche Geschwindigkeit", 4.0156958471424288E-06, CDC_Speed); // ***
2560 0 : NEWD( "ridiculous speed", 4.0156958471424288E-06, CDC_Speed); // ***
2561 :
2562 : // INFORMATION: 1 Bit is...
2563 0 : NEWDP( "bit", 1.00E00, CDC_Information); // *** Bit
2564 0 : NEWDP( "byte", 1.25E-01, CDC_Information); // *** Byte
2565 0 : }
2566 :
2567 :
2568 0 : ConvertDataList::~ConvertDataList()
2569 : {
2570 0 : for( std::vector<ConvertData*>::const_iterator it = maVector.begin(); it != maVector.end(); ++it )
2571 0 : delete *it;
2572 0 : }
2573 :
2574 :
2575 0 : double ConvertDataList::Convert( double fVal, const OUString& rFrom, const OUString& rTo ) throw( uno::RuntimeException, lang::IllegalArgumentException )
2576 : {
2577 0 : ConvertData* pFrom = NULL;
2578 0 : ConvertData* pTo = NULL;
2579 0 : sal_Bool bSearchFrom = sal_True;
2580 0 : sal_Bool bSearchTo = sal_True;
2581 0 : sal_Int16 nLevelFrom = 0;
2582 0 : sal_Int16 nLevelTo = 0;
2583 :
2584 0 : std::vector<ConvertData*>::iterator it = maVector.begin();
2585 0 : while( it != maVector.end() && ( bSearchFrom || bSearchTo ) )
2586 : {
2587 0 : ConvertData* p = *it;
2588 0 : if( bSearchFrom )
2589 : {
2590 0 : sal_Int16 n = p->GetMatchingLevel( rFrom );
2591 0 : if( n != INV_MATCHLEV )
2592 : {
2593 0 : if( n )
2594 : { // only first match for partial equality rulz a little bit more
2595 0 : pFrom = p;
2596 0 : nLevelFrom = n;
2597 : }
2598 : else
2599 : { // ... but exact match rulz most
2600 0 : pFrom = p;
2601 0 : bSearchFrom = sal_False;
2602 0 : nLevelFrom = n;
2603 : }
2604 : }
2605 : }
2606 :
2607 0 : if( bSearchTo )
2608 : {
2609 0 : sal_Int16 n = p->GetMatchingLevel( rTo );
2610 0 : if( n != INV_MATCHLEV )
2611 : {
2612 0 : if( n )
2613 : { // only first match for partial equality rulz a little bit more
2614 0 : pTo = p;
2615 0 : nLevelTo = n;
2616 : }
2617 : else
2618 : { // ... but exact match rulz most
2619 0 : pTo = p;
2620 0 : bSearchTo = sal_False;
2621 0 : nLevelTo = n;
2622 : }
2623 : }
2624 : }
2625 :
2626 0 : ++it;
2627 : }
2628 :
2629 0 : if( pFrom && pTo )
2630 0 : return pFrom->Convert( fVal, *pTo, nLevelFrom, nLevelTo );
2631 : else
2632 0 : throw lang::IllegalArgumentException();
2633 : }
2634 :
2635 :
2636 :
2637 0 : ScaDate::ScaDate() :
2638 : nOrigDay( 1 ),
2639 : nDay( 1 ),
2640 : nMonth( 1 ),
2641 : nYear( 1900 ),
2642 : bLastDayMode( sal_True ),
2643 : bLastDay( sal_False ),
2644 : b30Days( sal_False ),
2645 0 : bUSMode( sal_False )
2646 : {
2647 0 : }
2648 :
2649 0 : ScaDate::ScaDate( sal_Int32 nNullDate, sal_Int32 nDate, sal_Int32 nBase )
2650 : {
2651 0 : DaysToDate( nNullDate + nDate, nOrigDay, nMonth, nYear );
2652 0 : bLastDayMode = (nBase != 5);
2653 0 : bLastDay = (nOrigDay >= ::DaysInMonth( nMonth, nYear ));
2654 0 : b30Days = (nBase == 0) || (nBase == 4);
2655 0 : bUSMode = (nBase == 0);
2656 0 : setDay();
2657 0 : }
2658 :
2659 0 : ScaDate::ScaDate( const ScaDate& rCopy ) :
2660 : nOrigDay( rCopy.nOrigDay ),
2661 : nDay( rCopy.nDay ),
2662 : nMonth( rCopy.nMonth ),
2663 : nYear( rCopy.nYear ),
2664 : bLastDayMode( rCopy.bLastDayMode ),
2665 : bLastDay( rCopy.bLastDay ),
2666 : b30Days( rCopy.b30Days ),
2667 0 : bUSMode( rCopy.bUSMode )
2668 : {
2669 0 : }
2670 :
2671 0 : ScaDate& ScaDate::operator=( const ScaDate& rCopy )
2672 : {
2673 0 : if( this != &rCopy )
2674 : {
2675 0 : nOrigDay = rCopy.nOrigDay;
2676 0 : nDay = rCopy.nDay;
2677 0 : nMonth = rCopy.nMonth;
2678 0 : nYear = rCopy.nYear;
2679 0 : bLastDayMode = rCopy.bLastDayMode;
2680 0 : bLastDay = rCopy.bLastDay;
2681 0 : b30Days = rCopy.b30Days;
2682 0 : bUSMode = rCopy.bUSMode;
2683 : }
2684 0 : return *this;
2685 : }
2686 :
2687 0 : void ScaDate::setDay()
2688 : {
2689 0 : if( b30Days )
2690 : {
2691 : // 30-days-mode: set nDay to 30 if original was last day in month
2692 0 : nDay = std::min( nOrigDay, static_cast< sal_uInt16 >( 30 ) );
2693 0 : if( bLastDay || (nDay >= ::DaysInMonth( nMonth, nYear )) )
2694 0 : nDay = 30;
2695 : }
2696 : else
2697 : {
2698 : // set nDay to last day in this month if original was last day
2699 0 : sal_uInt16 nLastDay = ::DaysInMonth( nMonth, nYear );
2700 0 : nDay = bLastDay ? nLastDay : std::min( nOrigDay, nLastDay );
2701 : }
2702 0 : }
2703 :
2704 0 : sal_Int32 ScaDate::getDaysInMonthRange( sal_uInt16 nFrom, sal_uInt16 nTo ) const
2705 : {
2706 0 : if( nFrom > nTo )
2707 0 : return 0;
2708 :
2709 0 : sal_Int32 nRet = 0;
2710 0 : if( b30Days )
2711 0 : nRet = (nTo - nFrom + 1) * 30;
2712 : else
2713 : {
2714 0 : for( sal_uInt16 nMonthIx = nFrom; nMonthIx <= nTo; ++nMonthIx )
2715 0 : nRet += getDaysInMonth( nMonthIx );
2716 : }
2717 0 : return nRet;
2718 : }
2719 :
2720 0 : sal_Int32 ScaDate::getDaysInYearRange( sal_uInt16 nFrom, sal_uInt16 nTo ) const
2721 : {
2722 0 : if( nFrom > nTo )
2723 0 : return 0;
2724 :
2725 0 : return b30Days ? ((nTo - nFrom + 1) * 360) : ::GetDaysInYears( nFrom, nTo );
2726 : }
2727 :
2728 0 : void ScaDate::doAddYears( sal_Int32 nYearCount ) throw( lang::IllegalArgumentException )
2729 : {
2730 0 : sal_Int32 nNewYear = nYearCount + nYear;
2731 0 : if( (nNewYear < 0) || (nNewYear > 0x7FFF) )
2732 0 : throw lang::IllegalArgumentException();
2733 0 : nYear = static_cast< sal_uInt16 >( nNewYear );
2734 0 : }
2735 :
2736 0 : void ScaDate::addMonths( sal_Int32 nMonthCount ) throw( lang::IllegalArgumentException )
2737 : {
2738 0 : sal_Int32 nNewMonth = nMonthCount + nMonth;
2739 0 : if( nNewMonth > 12 )
2740 : {
2741 0 : --nNewMonth;
2742 0 : doAddYears( nNewMonth / 12 );
2743 0 : nMonth = static_cast< sal_uInt16 >( nNewMonth % 12 ) + 1;
2744 : }
2745 0 : else if( nNewMonth < 1 )
2746 : {
2747 0 : doAddYears( nNewMonth / 12 - 1 );
2748 0 : nMonth = static_cast< sal_uInt16 >( nNewMonth % 12 + 12 );
2749 : }
2750 : else
2751 0 : nMonth = static_cast< sal_uInt16 >( nNewMonth );
2752 0 : setDay();
2753 0 : }
2754 :
2755 0 : sal_Int32 ScaDate::getDate( sal_Int32 nNullDate ) const
2756 : {
2757 0 : sal_uInt16 nLastDay = ::DaysInMonth( nMonth, nYear );
2758 0 : sal_uInt16 nRealDay = (bLastDayMode && bLastDay) ? nLastDay : std::min( nLastDay, nOrigDay );
2759 0 : return ::DateToDays( nRealDay, nMonth, nYear ) - nNullDate;
2760 : }
2761 :
2762 0 : sal_Int32 ScaDate::getDiff( const ScaDate& rFrom, const ScaDate& rTo ) throw( lang::IllegalArgumentException )
2763 : {
2764 0 : if( rFrom > rTo )
2765 0 : return getDiff( rTo, rFrom );
2766 :
2767 0 : sal_Int32 nDiff = 0;
2768 0 : ScaDate aFrom( rFrom );
2769 0 : ScaDate aTo( rTo );
2770 :
2771 0 : if( rTo.b30Days )
2772 : {
2773 : // corrections for base 0 (US NASD)
2774 0 : if( rTo.bUSMode )
2775 : {
2776 0 : if( ((rFrom.nMonth == 2) || (rFrom.nDay < 30)) && (aTo.nOrigDay == 31) )
2777 0 : aTo.nDay = 31;
2778 0 : else if( (aTo.nMonth == 2) && aTo.bLastDay )
2779 0 : aTo.nDay = ::DaysInMonth( 2, aTo.nYear );
2780 : }
2781 : // corrections for base 4 (Europe)
2782 : else
2783 : {
2784 0 : if( (aFrom.nMonth == 2) && (aFrom.nDay == 30) )
2785 0 : aFrom.nDay = ::DaysInMonth( 2, aFrom.nYear );
2786 0 : if( (aTo.nMonth == 2) && (aTo.nDay == 30) )
2787 0 : aTo.nDay = ::DaysInMonth( 2, aTo.nYear );
2788 : }
2789 : }
2790 :
2791 0 : if( (aFrom.nYear < aTo.nYear) || ((aFrom.nYear == aTo.nYear) && (aFrom.nMonth < aTo.nMonth)) )
2792 : {
2793 : // move aFrom to 1st day of next month
2794 0 : nDiff = aFrom.getDaysInMonth() - aFrom.nDay + 1;
2795 0 : aFrom.nOrigDay = aFrom.nDay = 1;
2796 0 : aFrom.bLastDay = sal_False;
2797 0 : aFrom.addMonths( 1 );
2798 :
2799 0 : if( aFrom.nYear < aTo.nYear )
2800 : {
2801 : // move aFrom to 1st day of next year
2802 0 : nDiff += aFrom.getDaysInMonthRange( aFrom.nMonth, 12 );
2803 0 : aFrom.addMonths( 13 - aFrom.nMonth );
2804 :
2805 : // move aFrom to 1st day of this year
2806 0 : nDiff += aFrom.getDaysInYearRange( aFrom.nYear, aTo.nYear - 1 );
2807 0 : aFrom.addYears( aTo.nYear - aFrom.nYear );
2808 : }
2809 :
2810 : // move aFrom to 1st day of this month
2811 0 : nDiff += aFrom.getDaysInMonthRange( aFrom.nMonth, aTo.nMonth - 1 );
2812 0 : aFrom.addMonths( aTo.nMonth - aFrom.nMonth );
2813 : }
2814 : // finally add remaining days in this month
2815 0 : nDiff += aTo.nDay - aFrom.nDay;
2816 0 : return nDiff > 0 ? nDiff : 0;
2817 : }
2818 :
2819 0 : sal_Bool ScaDate::operator<( const ScaDate& rCmp ) const
2820 : {
2821 0 : if( nYear != rCmp.nYear )
2822 0 : return nYear < rCmp.nYear;
2823 0 : if( nMonth != rCmp.nMonth )
2824 0 : return nMonth < rCmp.nMonth;
2825 0 : if( nDay != rCmp.nDay )
2826 0 : return nDay < rCmp.nDay;
2827 0 : if( bLastDay || rCmp.bLastDay )
2828 0 : return !bLastDay && rCmp.bLastDay;
2829 0 : return nOrigDay < rCmp.nOrigDay;
2830 : }
2831 :
2832 :
2833 :
2834 0 : ScaAnyConverter::ScaAnyConverter( const uno::Reference< uno::XComponentContext >& xContext ) :
2835 0 : bHasValidFormat( sal_False )
2836 : {
2837 0 : xFormatter = util::NumberFormatter::create(xContext);
2838 0 : }
2839 :
2840 0 : ScaAnyConverter::~ScaAnyConverter()
2841 : {
2842 0 : }
2843 :
2844 0 : void ScaAnyConverter::init( const uno::Reference< beans::XPropertySet >& xPropSet ) throw( uno::RuntimeException )
2845 : {
2846 : // try to get default number format
2847 0 : bHasValidFormat = sal_False;
2848 0 : if( xFormatter.is() )
2849 : {
2850 : // get XFormatsSupplier from outer XPropertySet
2851 0 : uno::Reference< util::XNumberFormatsSupplier > xFormatsSupp( xPropSet, uno::UNO_QUERY );
2852 0 : if( xFormatsSupp.is() )
2853 : {
2854 : // get XNumberFormatTypes from XNumberFormatsSupplier to get standard index
2855 0 : uno::Reference< util::XNumberFormats > xFormats( xFormatsSupp->getNumberFormats() );
2856 0 : uno::Reference< util::XNumberFormatTypes > xFormatTypes( xFormats, uno::UNO_QUERY );
2857 0 : if( xFormatTypes.is() )
2858 : {
2859 0 : lang::Locale eLocale;
2860 0 : nDefaultFormat = xFormatTypes->getStandardIndex( eLocale );
2861 0 : xFormatter->attachNumberFormatsSupplier( xFormatsSupp );
2862 0 : bHasValidFormat = sal_True;
2863 0 : }
2864 0 : }
2865 : }
2866 0 : }
2867 :
2868 0 : double ScaAnyConverter::convertToDouble( const OUString& rString ) const throw( lang::IllegalArgumentException )
2869 : {
2870 0 : double fValue = 0.0;
2871 0 : if( bHasValidFormat )
2872 : {
2873 : try
2874 : {
2875 0 : fValue = xFormatter->convertStringToNumber( nDefaultFormat, rString );
2876 : }
2877 0 : catch( uno::Exception& )
2878 : {
2879 0 : throw lang::IllegalArgumentException();
2880 : }
2881 : }
2882 : else
2883 : {
2884 : rtl_math_ConversionStatus eStatus;
2885 : sal_Int32 nEnd;
2886 0 : fValue = ::rtl::math::stringToDouble( rString, '.', ',', &eStatus, &nEnd );
2887 0 : if( (eStatus != rtl_math_ConversionStatus_Ok) || (nEnd < rString.getLength()) )
2888 0 : throw lang::IllegalArgumentException();
2889 : }
2890 0 : return fValue;
2891 : }
2892 :
2893 0 : sal_Bool ScaAnyConverter::getDouble(
2894 : double& rfResult,
2895 : const uno::Any& rAny ) const throw( lang::IllegalArgumentException )
2896 : {
2897 0 : rfResult = 0.0;
2898 0 : sal_Bool bContainsVal = sal_True;
2899 0 : switch( rAny.getValueTypeClass() )
2900 : {
2901 : case uno::TypeClass_VOID:
2902 0 : bContainsVal = sal_False;
2903 0 : break;
2904 : case uno::TypeClass_DOUBLE:
2905 0 : rAny >>= rfResult;
2906 0 : break;
2907 : case uno::TypeClass_STRING:
2908 : {
2909 0 : const OUString* pString = static_cast< const OUString* >( rAny.getValue() );
2910 0 : if( !pString->isEmpty() )
2911 0 : rfResult = convertToDouble( *pString );
2912 : else
2913 0 : bContainsVal = sal_False;
2914 : }
2915 0 : break;
2916 : default:
2917 0 : throw lang::IllegalArgumentException();
2918 : }
2919 0 : return bContainsVal;
2920 : }
2921 :
2922 0 : sal_Bool ScaAnyConverter::getDouble(
2923 : double& rfResult,
2924 : const uno::Reference< beans::XPropertySet >& xPropSet,
2925 : const uno::Any& rAny ) throw( uno::RuntimeException, lang::IllegalArgumentException )
2926 : {
2927 0 : init( xPropSet );
2928 0 : return getDouble( rfResult, rAny );
2929 : }
2930 :
2931 0 : double ScaAnyConverter::getDouble(
2932 : const uno::Reference< beans::XPropertySet >& xPropSet,
2933 : const uno::Any& rAny,
2934 : double fDefault ) throw( uno::RuntimeException, lang::IllegalArgumentException )
2935 : {
2936 : double fResult;
2937 0 : if( !getDouble( fResult, xPropSet, rAny ) )
2938 0 : fResult = fDefault;
2939 0 : return fResult;
2940 : }
2941 :
2942 0 : sal_Bool ScaAnyConverter::getInt32(
2943 : sal_Int32& rnResult,
2944 : const uno::Reference< beans::XPropertySet >& xPropSet,
2945 : const uno::Any& rAny ) throw( uno::RuntimeException, lang::IllegalArgumentException )
2946 : {
2947 : double fResult;
2948 0 : sal_Bool bContainsVal = getDouble( fResult, xPropSet, rAny );
2949 0 : if( (fResult <= -2147483649.0) || (fResult >= 2147483648.0) )
2950 0 : throw lang::IllegalArgumentException();
2951 :
2952 0 : rnResult = static_cast< sal_Int32 >( fResult );
2953 0 : return bContainsVal;
2954 : }
2955 :
2956 0 : sal_Int32 ScaAnyConverter::getInt32(
2957 : const uno::Reference< beans::XPropertySet >& xPropSet,
2958 : const uno::Any& rAny,
2959 : sal_Int32 nDefault ) throw( uno::RuntimeException, lang::IllegalArgumentException )
2960 : {
2961 : sal_Int32 nResult;
2962 0 : if( !getInt32( nResult, xPropSet, rAny ) )
2963 0 : nResult = nDefault;
2964 0 : return nResult;
2965 : }
2966 :
2967 :
2968 :
2969 :
2970 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|