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