summaryrefslogtreecommitdiff
path: root/scaddins/source/analysis/analysis.cxx
blob: ea52bdfee002258e18e03b7f4d231ce7d9c99545 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

#include "analysis.hxx"

#include <cppuhelper/factory.hxx>
#include <osl/diagnose.h>
#include <rtl/ustrbuf.hxx>
#include <rtl/math.hxx>
#include <string.h>

#include <tools/resmgr.hxx>
#include <tools/rcid.h>
#include "analysis.hrc"
#include "bessel.hxx"

#define ADDIN_SERVICE               "com.sun.star.sheet.AddIn"
#define MY_SERVICE                  "com.sun.star.sheet.addin.Analysis"
#define MY_IMPLNAME                 "com.sun.star.sheet.addin.AnalysisImpl"

using namespace                 ::rtl;
using namespace                 ::com::sun::star;

//------------------------------------------------------------------
//
//  entry points for service registration / instantiation
//
//------------------------------------------------------------------

extern "C" {


void SAL_CALL component_getImplementationEnvironment( const sal_Char** ppEnvTypeName, uno_Environment** /*ppEnv*/ )
{
    *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
}


sal_Bool SAL_CALL component_writeInfo( void* /*pServiceManager*/, registry::XRegistryKey* pRegistryKey )
{
    if( pRegistryKey )
    {
        try
        {
            STRING                          aImpl = STRFROMASCII( "/" );
            aImpl += AnalysisAddIn::getImplementationName_Static();
            aImpl += STRFROMASCII( "/UNO/SERVICES" );

            REF( registry::XRegistryKey )   xNewKey(
                reinterpret_cast< registry::XRegistryKey* >( pRegistryKey )->createKey( aImpl ) );

            SEQ( STRING )                   aSequ = AnalysisAddIn::getSupportedServiceNames_Static();
            const STRING*                   pArray = aSequ.getConstArray();

            for( sal_Int32 i = 0 ; i < aSequ.getLength() ; i++ )
                xNewKey->createKey( pArray[ i ] );

            return sal_True;
        }
        catch( registry::InvalidRegistryException& )
        {
            OSL_ENSURE( sal_False, "### InvalidRegistryException!" );
        }
    }
    return sal_False;
}


void* SAL_CALL component_getFactory( const sal_Char* pImplName, void* pServiceManager, void* /*pRegistryKey*/ )
{
    void*                                   pRet = 0;

    if( pServiceManager && STRING::createFromAscii( pImplName ) == AnalysisAddIn::getImplementationName_Static() )
    {
        REF( lang::XSingleServiceFactory )  xFactory( cppu::createOneInstanceFactory(
                reinterpret_cast< lang::XMultiServiceFactory* >( pServiceManager ),
                AnalysisAddIn::getImplementationName_Static(),
                AnalysisAddIn_CreateInstance,
                AnalysisAddIn::getSupportedServiceNames_Static() ) );

        if( xFactory.is() )
        {
            xFactory->acquire();
            pRet = xFactory.get();
        }
    }

    return pRet;
}


}   // extern C




//------------------------------------------------------------------------
//
//  "normal" service implementation
//
//------------------------------------------------------------------------


ResMgr& AnalysisAddIn::GetResMgr( void ) THROWDEF_RTE
{
    if( !pResMgr )
    {
        InitData();     // try to get resource manager

        if( !pResMgr )
            THROW_RTE;
    }

    return *pResMgr;
}


STRING AnalysisAddIn::GetDisplFuncStr( sal_uInt16 nFuncNum ) THROWDEF_RTE
{
    return String( AnalysisRscStrLoader( RID_ANALYSIS_FUNCTION_NAMES, nFuncNum, GetResMgr() ).GetString() );
}


class AnalysisResourcePublisher : public Resource
{
public:
                    AnalysisResourcePublisher( const AnalysisResId& rId ) : Resource( rId ) {}
    BOOL            IsAvailableRes( const ResId& rId ) const { return Resource::IsAvailableRes( rId ); }
    void            FreeResource() { Resource::FreeResource(); }
};


class AnalysisFuncRes : public Resource
{
public:
    AnalysisFuncRes( ResId& rRes, ResMgr& rResMgr, sal_uInt16 nInd, STRING& rRet );
};


AnalysisFuncRes::AnalysisFuncRes( ResId& rRes, ResMgr& rResMgr, sal_uInt16 nInd, STRING& rRet ) : Resource( rRes )
{
    rRet = String( AnalysisResId( nInd, rResMgr ) );

    FreeResource();
}


STRING AnalysisAddIn::GetFuncDescrStr( sal_uInt16 nResId, sal_uInt16 nStrIndex ) THROWDEF_RTE
{
    STRING                      aRet;
    AnalysisResourcePublisher   aResPubl( AnalysisResId( RID_ANALYSIS_FUNCTION_DESCRIPTIONS, GetResMgr() ) );
    AnalysisResId               aRes( nResId, GetResMgr() );
    aRes.SetRT( RSC_RESOURCE );
    if( aResPubl.IsAvailableRes( aRes ) )
    {
        AnalysisFuncRes         aSubRes( aRes, GetResMgr(), nStrIndex, aRet );
    }

    aResPubl.FreeResource();

    return aRet;
}


void AnalysisAddIn::InitData( void )
{
    if( pResMgr )
        delete pResMgr;

    OString             aModName( "analysis" );
    pResMgr = ResMgr::CreateResMgr( ( const sal_Char* ) aModName,
                                        aFuncLoc );

    if( pFD )
        delete pFD;

    if( pResMgr )
        pFD = new FuncDataList( *pResMgr );
    else
        pFD = NULL;

    if( pDefLocales )
    {
        delete pDefLocales;
        pDefLocales = NULL;
    }
}


AnalysisAddIn::AnalysisAddIn( const uno::Reference< lang::XMultiServiceFactory >& xServiceFact ) :
    pDefLocales( NULL ),
    pFD( NULL ),
    pFactDoubles( NULL ),
    pCDL( NULL ),
    pResMgr( NULL ),
    aAnyConv( xServiceFact )
{
}


AnalysisAddIn::~AnalysisAddIn()
{
    if( pFD )
        delete pFD;

    if( pFactDoubles )
        delete[] pFactDoubles;

    if( pCDL )
        delete pCDL;

    if( pDefLocales )
        delete[] pDefLocales;
}


sal_Int32 AnalysisAddIn::getDateMode(
        const uno::Reference< beans::XPropertySet >& xPropSet,
        const uno::Any& rAny ) throw( uno::RuntimeException, lang::IllegalArgumentException )
{
    sal_Int32 nMode = aAnyConv.getInt32( xPropSet, rAny, 0 );
    if( (nMode < 0) || (nMode > 4) )
        throw lang::IllegalArgumentException();
    return nMode;
}



//-----------------------------------------------------------------------------


#define MAXFACTDOUBLE   300

double AnalysisAddIn::FactDouble( sal_Int32 nNum ) THROWDEF_RTE_IAE
{
    if( nNum < 0 || nNum > MAXFACTDOUBLE )
        THROW_IAE;

    if( !pFactDoubles )
    {
        pFactDoubles = new double[ MAXFACTDOUBLE + 1 ];

        pFactDoubles[ 0 ] = 1.0;    // by default

        double      fOdd = 1.0;
        double      fEven = 2.0;

        pFactDoubles[ 1 ] = fOdd;
        pFactDoubles[ 2 ] = fEven;

        sal_Bool    bOdd = sal_True;

        for( sal_uInt16 nCnt = 3 ; nCnt <= MAXFACTDOUBLE ; nCnt++ )
        {
            if( bOdd )
            {
                fOdd *= nCnt;
                pFactDoubles[ nCnt ] = fOdd;
            }
            else
            {
                fEven *= nCnt;
                pFactDoubles[ nCnt ] = fEven;
            }

            bOdd = !bOdd;

        }
    }

    return pFactDoubles[ nNum ];
}


STRING AnalysisAddIn::getImplementationName_Static()
{
    return STRFROMASCII( MY_IMPLNAME );
}


SEQ( STRING ) AnalysisAddIn::getSupportedServiceNames_Static()
{
    SEQ( STRING )   aRet(2);
    STRING*         pArray = aRet.getArray();
    pArray[0] = STRFROMASCII( ADDIN_SERVICE );
    pArray[1] = STRFROMASCII( MY_SERVICE );
    return aRet;
}


REF( uno::XInterface ) SAL_CALL AnalysisAddIn_CreateInstance(
        const uno::Reference< lang::XMultiServiceFactory >& xServiceFact )
{
    static uno::Reference< uno::XInterface > xInst = (cppu::OWeakObject*) new AnalysisAddIn( xServiceFact );
    return xInst;
}


// XServiceName

STRING SAL_CALL AnalysisAddIn::getServiceName() THROWDEF_RTE
{
    // name of specific AddIn service
    return STRFROMASCII( MY_SERVICE );
}


// XServiceInfo

STRING SAL_CALL AnalysisAddIn::getImplementationName() THROWDEF_RTE
{
    return getImplementationName_Static();
}


sal_Bool SAL_CALL AnalysisAddIn::supportsService( const STRING& aName ) THROWDEF_RTE
{
    return aName.compareToAscii( ADDIN_SERVICE ) == 0 || aName.compareToAscii( MY_SERVICE ) == 0;
}


SEQ( STRING ) SAL_CALL AnalysisAddIn::getSupportedServiceNames() THROWDEF_RTE
{
    return getSupportedServiceNames_Static();
}


// XLocalizable

void SAL_CALL AnalysisAddIn::setLocale( const lang::Locale& eLocale ) THROWDEF_RTE
{
    aFuncLoc = eLocale;

    InitData();     // change of locale invalidates resources!
}

lang::Locale SAL_CALL AnalysisAddIn::getLocale() THROWDEF_RTE
{
    return aFuncLoc;
}


// XAddIn

STRING SAL_CALL AnalysisAddIn::getProgrammaticFuntionName( const STRING& ) THROWDEF_RTE
{
    //  not used by calc
    //  (but should be implemented for other uses of the AddIn service)

    return STRING();
}


STRING SAL_CALL AnalysisAddIn::getDisplayFunctionName( const STRING& aProgrammaticName ) THROWDEF_RTE
{
    STRING          aRet;

    const FuncData* p = pFD->Get( aProgrammaticName );
    if( p )
    {
        aRet = GetDisplFuncStr( p->GetUINameID() );
        if( p->IsDouble() )
            aRet += STRFROMANSI( "_ADD" );
    }
    else
    {
        aRet = STRFROMANSI( "UNKNOWNFUNC_" );
        aRet += aProgrammaticName;
    }

    return aRet;
}


STRING SAL_CALL AnalysisAddIn::getFunctionDescription( const STRING& aProgrammaticName ) THROWDEF_RTE
{
    STRING          aRet;

    const FuncData* p = pFD->Get( aProgrammaticName );
    if( p )
        aRet = GetFuncDescrStr( p->GetDescrID(), 1 );

    return aRet;
}


STRING SAL_CALL AnalysisAddIn::getDisplayArgumentName( const STRING& aName, sal_Int32 nArg ) THROWDEF_RTE
{
    STRING          aRet;

    const FuncData* p = pFD->Get( aName );
    if( p && nArg <= 0xFFFF )
    {
        sal_uInt16  nStr = p->GetStrIndex( sal_uInt16( nArg ) );
        if( nStr )
            aRet = GetFuncDescrStr( p->GetDescrID(), nStr );
        else
            aRet = STRFROMANSI( "internal" );
    }

    return aRet;
}


STRING SAL_CALL AnalysisAddIn::getArgumentDescription( const STRING& aName, sal_Int32 nArg ) THROWDEF_RTE
{
    STRING          aRet;

    const FuncData* p = pFD->Get( aName );
    if( p && nArg <= 0xFFFF )
    {
        sal_uInt16  nStr = p->GetStrIndex( sal_uInt16( nArg ) );
        if( nStr )
            aRet = GetFuncDescrStr( p->GetDescrID(), nStr + 1 );
        else
            aRet = STRFROMANSI( "for internal use only" );
    }

    return aRet;
}


static const char*  pDefCatName = "Add-In";


STRING SAL_CALL AnalysisAddIn::getProgrammaticCategoryName( const STRING& aName ) THROWDEF_RTE
{
    //  return non-translated strings
//  return STRFROMASCII( "Add-In" );
    const FuncData*     p = pFD->Get( aName );
    STRING              aRet;
    if( p )
    {
        const sal_Char* pStr;

        switch( p->GetCategory() )
        {
            case FDCat_DateTime:    pStr = "Date&Time";         break;
            case FDCat_Finance:     pStr = "Financial";         break;
            case FDCat_Inf:         pStr = "Information";       break;
            case FDCat_Math:        pStr = "Mathematical";      break;
            case FDCat_Tech:        pStr = "Technical";         break;
            default:
                                    pStr = pDefCatName;         break;
        }

        aRet = STRFROMASCII( pStr );
    }
    else
        aRet = STRFROMASCII( pDefCatName );

    return aRet;
}


STRING SAL_CALL AnalysisAddIn::getDisplayCategoryName( const STRING& aProgrammaticFunctionName ) THROWDEF_RTE
{
    //  return translated strings, not used for predefined categories
//  return STRFROMASCII( "Add-In" );
    const FuncData*     p = pFD->Get( aProgrammaticFunctionName );
    STRING              aRet;
    if( p )
    {
        const sal_Char* pStr;

        switch( p->GetCategory() )
        {
            case FDCat_DateTime:    pStr = "Date&Time";         break;
            case FDCat_Finance:     pStr = "Financial";         break;
            case FDCat_Inf:         pStr = "Information";       break;
            case FDCat_Math:        pStr = "Mathematical";      break;
            case FDCat_Tech:        pStr = "Technical";         break;
            default:
                                    pStr = pDefCatName;         break;
        }

        aRet = STRFROMASCII( pStr );
    }
    else
        aRet = STRFROMASCII( pDefCatName );

    return aRet;
}


static const sal_Char*      pLang[] = { "de", "en" };
static const sal_Char*      pCoun[] = { "DE", "US" };
static const sal_uInt32     nNumOfLoc = sizeof( pLang ) / sizeof( sal_Char* );


void AnalysisAddIn::InitDefLocales( void )
{
    pDefLocales = new CSS::lang::Locale[ nNumOfLoc ];

    for( sal_uInt32 n = 0 ; n < nNumOfLoc ; n++ )
    {
        pDefLocales[ n ].Language = STRING::createFromAscii( pLang[ n ] );
        pDefLocales[ n ].Country = STRING::createFromAscii( pCoun[ n ] );
    }
}


inline const CSS::lang::Locale& AnalysisAddIn::GetLocale( sal_uInt32 nInd )
{
    if( !pDefLocales )
        InitDefLocales();

    if( nInd < sizeof( pLang ) )
        return pDefLocales[ nInd ];
    else
        return aFuncLoc;
}


SEQofLocName SAL_CALL AnalysisAddIn::getCompatibilityNames( const STRING& aProgrammaticName ) THROWDEF_RTE
{
    const FuncData*             p = pFD->Get( aProgrammaticName );

    if( !p )
        return SEQofLocName( 0 );

    const StringList&           r = p->GetCompNameList();
    sal_uInt32                  nCount = r.Count();

    SEQofLocName                aRet( nCount );

    CSS::sheet::LocalizedName*  pArray = aRet.getArray();

    for( sal_uInt32 n = 0 ; n < nCount ; n++ )
    {
        pArray[ n ] = CSS::sheet::LocalizedName( GetLocale( n ), *r.Get( n ) );
    }

    return aRet;
}


// XAnalysis

/**
 * Workday
 */

sal_Int32 SAL_CALL AnalysisAddIn::getWorkday( constREFXPS& xOptions,
    sal_Int32 nDate, sal_Int32 nDays, const ANY& aHDay ) THROWDEF_RTE_IAE
{
    if( !nDays )
        return nDate;

    sal_Int32                   nNullDate = GetNullDate( xOptions );

    SortedIndividualInt32List   aSrtLst;

    aSrtLst.InsertHolidayList( aAnyConv, xOptions, aHDay, nNullDate, sal_False );

    sal_Int32                   nActDate = nDate + nNullDate;

    if( nDays > 0 )
    {
        if( GetDayOfWeek( nActDate ) == 5 )
            // when starting on Saturday, assuming we're starting on Sunday to get the jump over the weekend
            nActDate++;

        while( nDays )
        {
            nActDate++;

            if( GetDayOfWeek( nActDate ) < 5 )
            {
                if( !aSrtLst.Find( nActDate ) )
                    nDays--;
            }
            else
                nActDate++;     // jump over weekend
        }
    }
    else
    {
        if( GetDayOfWeek( nActDate ) == 6 )
            // when starting on Sunday, assuming we're starting on Saturday to get the jump over the weekend
            nActDate--;

        while( nDays )
        {
            nActDate--;

            if( GetDayOfWeek( nActDate ) < 5 )
            {
                if( !aSrtLst.Find( nActDate ) )
                    nDays++;
            }
            else
                nActDate--;     // jump over weekend
        }
    }

    return nActDate - nNullDate;
}


/**
 * Yearfrac
 */

double SAL_CALL AnalysisAddIn::getYearfrac( constREFXPS& xOpt,
    sal_Int32 nStartDate, sal_Int32 nEndDate, const ANY& rMode ) THROWDEF_RTE_IAE
{
    double fRet = GetYearFrac( xOpt, nStartDate, nEndDate, getDateMode( xOpt, rMode ) );
    RETURN_FINITE( fRet );
}


sal_Int32 SAL_CALL AnalysisAddIn::getEdate( constREFXPS& xOpt, sal_Int32 nStartDate, sal_Int32 nMonths ) THROWDEF_RTE_IAE
{
    sal_Int32 nNullDate = GetNullDate( xOpt );
    ScaDate aDate( nNullDate, nStartDate, 5 );
    aDate.addMonths( nMonths );
    return aDate.getDate( nNullDate );
}


sal_Int32 SAL_CALL AnalysisAddIn::getWeeknum( constREFXPS& xOpt, sal_Int32 nDate, sal_Int32 nMode ) THROWDEF_RTE_IAE
{
    nDate += GetNullDate( xOpt );

    sal_uInt16  nDay, nMonth, nYear;
    DaysToDate( nDate, nDay, nMonth, nYear );

    sal_Int32   nFirstInYear = DateToDays( 1, 1, nYear );
    sal_uInt16  nFirstDayInYear = GetDayOfWeek( nFirstInYear );

    return ( nDate - nFirstInYear + ( ( nMode == 1 )? ( nFirstDayInYear + 1 ) % 7 : nFirstDayInYear ) ) / 7 + 1;
}


sal_Int32 SAL_CALL AnalysisAddIn::getEomonth( constREFXPS& xOpt, sal_Int32 nDate, sal_Int32 nMonths ) THROWDEF_RTE_IAE
{
    sal_Int32   nNullDate = GetNullDate( xOpt );
    nDate += nNullDate;
    sal_uInt16  nDay, nMonth, nYear;
    DaysToDate( nDate, nDay, nMonth, nYear );

    sal_Int32   nNewMonth = nMonth + nMonths;

    if( nNewMonth > 12 )
    {
        nYear = sal::static_int_cast<sal_uInt16>( nYear + ( nNewMonth / 12 ) );
        nNewMonth %= 12;
    }
    else if( nNewMonth < 1 )
    {
        nNewMonth = -nNewMonth;
        nYear = sal::static_int_cast<sal_uInt16>( nYear - ( nNewMonth / 12 ) );
        nYear--;
        nNewMonth %= 12;
        nNewMonth = 12 - nNewMonth;
    }

    return DateToDays( DaysInMonth( sal_uInt16( nNewMonth ), nYear ), sal_uInt16( nNewMonth ), nYear ) - nNullDate;
}


sal_Int32 SAL_CALL AnalysisAddIn::getNetworkdays( constREFXPS& xOpt,
        sal_Int32 nStartDate, sal_Int32 nEndDate, const ANY& aHDay ) THROWDEF_RTE_IAE
{
    sal_Int32                   nNullDate = GetNullDate( xOpt );

    SortedIndividualInt32List   aSrtLst;

    aSrtLst.InsertHolidayList( aAnyConv, xOpt, aHDay, nNullDate, sal_False );

    sal_Int32                   nActDate = nStartDate + nNullDate;
    sal_Int32                   nStopDate = nEndDate + nNullDate;
    sal_Int32                   nCnt = 0;

    if( nActDate <= nStopDate )
    {
        while( nActDate <= nStopDate )
        {
            if( GetDayOfWeek( nActDate ) < 5 && !aSrtLst.Find( nActDate ) )
                nCnt++;

            nActDate++;
        }
    }
    else
    {
        while( nActDate >= nStopDate )
        {
            if( GetDayOfWeek( nActDate ) < 5 && !aSrtLst.Find( nActDate ) )
                nCnt--;

            nActDate--;
        }
    }

    return nCnt;
}


sal_Int32 SAL_CALL AnalysisAddIn::getIseven( sal_Int32 nVal ) THROWDEF_RTE_IAE
{
    return ( nVal & 0x00000001 )? 0 : 1;
}


sal_Int32 SAL_CALL AnalysisAddIn::getIsodd( sal_Int32 nVal ) THROWDEF_RTE_IAE
{
    return ( nVal & 0x00000001 )? 1 : 0;
}

double SAL_CALL
AnalysisAddIn::getMultinomial( constREFXPS& xOpt, const SEQSEQ( sal_Int32 )& aVLst,
                               const SEQ( uno::Any )& aOptVLst ) THROWDEF_RTE_IAE
{
    ScaDoubleListGE0 aValList;

    aValList.Append( aVLst );
    aValList.Append( aAnyConv, xOpt, aOptVLst );

    if( aValList.Count() == 0 )
        return 0.0;

    sal_Int32 nZ = 0;
    double    fN = 1.0;

    for( const double *p = aValList.First(); p; p = aValList.Next() )
    {
        double fInt = (*p >= 0.0) ? rtl::math::approxFloor( *p ) : rtl::math::approxCeil( *p );
        if ( fInt < 0.0 || fInt > 170.0 )
            THROW_IAE;
        sal_Int32 n = static_cast< sal_Int32 >( fInt );
        if( n > 0 )
        {
            nZ += n;
            fN *= Fak( n );
        }
    }

    if( nZ > 170 )
        THROW_IAE;

    double fRet = Fak( nZ ) / fN;
    RETURN_FINITE( fRet );
}


double SAL_CALL AnalysisAddIn::getSeriessum( double fX, double fN, double fM, const SEQSEQ( double )& aCoeffList ) THROWDEF_RTE_IAE
{
    double                          fRet = 0.0;

    // #i32269# 0^0 is undefined, Excel returns #NUM! error
    if( fX == 0.0 && fN == 0 )
        THROW_RTE;

    if( fX != 0.0 )
    {
        sal_Int32       n1, n2;
        sal_Int32       nE1 = aCoeffList.getLength();
        sal_Int32       nE2;

        for( n1 = 0 ; n1 < nE1 ; n1++ )
        {
            const SEQ( double )&    rList = aCoeffList[ n1 ];
            nE2 = rList.getLength();
            const double*           pList = rList.getConstArray();

            for( n2 = 0 ; n2 < nE2 ; n2++ )
            {
                fRet += pList[ n2 ] * pow( fX, fN );

                fN += fM;
            }
        }
    }

    RETURN_FINITE( fRet );
}


double SAL_CALL AnalysisAddIn::getQuotient( double fNum, double fDenom ) THROWDEF_RTE_IAE
{
    double fRet;
    if( (fNum < 0) != (fDenom < 0) )
        fRet = ::rtl::math::approxCeil( fNum / fDenom );
    else
        fRet = ::rtl::math::approxFloor( fNum / fDenom );
    RETURN_FINITE( fRet );
}


double SAL_CALL AnalysisAddIn::getMround( double fNum, double fMult ) THROWDEF_RTE_IAE
{
    if( fMult == 0.0 )
        return fMult;

    double fRet = fMult * ::rtl::math::round( fNum / fMult );
    RETURN_FINITE( fRet );
}


double SAL_CALL AnalysisAddIn::getSqrtpi( double fNum ) THROWDEF_RTE_IAE
{
    double fRet = sqrt( fNum * PI );
    RETURN_FINITE( fRet );
}


double SAL_CALL AnalysisAddIn::getRandbetween( double fMin, double fMax ) THROWDEF_RTE_IAE
{
    fMin = ::rtl::math::round( fMin, 0, rtl_math_RoundingMode_Up );
    fMax = ::rtl::math::round( fMax, 0, rtl_math_RoundingMode_Up );
    if( fMin > fMax )
        THROW_IAE;

    // fMax -> range
    double fRet = fMax - fMin + 1.0;
    fRet *= rand();
    fRet /= (RAND_MAX + 1.0);
    fRet += fMin;
    fRet = floor( fRet );   // simple floor is sufficient here
    RETURN_FINITE( fRet );
}


double SAL_CALL AnalysisAddIn::getGcd( constREFXPS& xOpt, const SEQSEQ( double )& aVLst, const SEQ( uno::Any )& aOptVLst ) THROWDEF_RTE_IAE
{
    ScaDoubleListGT0 aValList;

    aValList.Append( aVLst );
    aValList.Append( aAnyConv, xOpt, aOptVLst );

    if( aValList.Count() == 0 )
        return 0.0;

    const double*   p = aValList.First();
    double          f = *p;

    p = aValList.Next();

    while( p )
    {
        f = GetGcd( *p, f );
        p = aValList.Next();
    }

    RETURN_FINITE( f );
}


double SAL_CALL AnalysisAddIn::getLcm( constREFXPS& xOpt, const SEQSEQ( double )& aVLst, const SEQ( uno::Any )& aOptVLst ) THROWDEF_RTE_IAE
{
    ScaDoubleListGE0 aValList;

    aValList.Append( aVLst );
    aValList.Append( aAnyConv, xOpt, aOptVLst );

    if( aValList.Count() == 0 )
        return 0.0;

    const double*   p = aValList.First();
    double          f = *p;

    if( f == 0.0 )
        return f;

    p = aValList.Next();

    while( p )
    {
        double      fTmp = *p;
        if( f == 0.0 )
            return f;
        else
            f = fTmp * f / GetGcd( fTmp, f );
        p = aValList.Next();
    }

    RETURN_FINITE( f );
}


double SAL_CALL AnalysisAddIn::getBesseli( double fNum, sal_Int32 nOrder ) THROWDEF_RTE_IAE_NCE
{
    double fRet = sca::analysis::BesselI( fNum, nOrder );
    RETURN_FINITE( fRet );
}


double SAL_CALL AnalysisAddIn::getBesselj( double fNum, sal_Int32 nOrder ) THROWDEF_RTE_IAE_NCE
{
    double fRet = sca::analysis::BesselJ( fNum, nOrder );
    RETURN_FINITE( fRet );
}


double SAL_CALL AnalysisAddIn::getBesselk( double fNum, sal_Int32 nOrder ) THROWDEF_RTE_IAE_NCE
{
    if( nOrder < 0 || fNum <= 0.0 )
        THROW_IAE;

    double fRet = sca::analysis::BesselK( fNum, nOrder );
    RETURN_FINITE( fRet );
}


double SAL_CALL AnalysisAddIn::getBessely( double fNum, sal_Int32 nOrder ) THROWDEF_RTE_IAE_NCE
{
    if( nOrder < 0 || fNum <= 0.0 )
        THROW_IAE;

    double fRet = sca::analysis::BesselY( fNum, nOrder );
    RETURN_FINITE( fRet );
}


const double    SCA_MAX2        = 511.0;            // min. val for binary numbers (9 bits + sign)
const double    SCA_MIN2        = -SCA_MAX2-1.0;    // min. val for binary numbers (9 bits + sign)
const double    SCA_MAX8        = 536870911.0;      // max. val for octal numbers (29 bits + sign)
const double    SCA_MIN8        = -SCA_MAX8-1.0;    // min. val for octal numbers (29 bits + sign)
const double    SCA_MAX16       = 549755813888.0;   // max. val for hexadecimal numbers (39 bits + sign)
const double    SCA_MIN16       = -SCA_MAX16-1.0;   // min. val for hexadecimal numbers (39 bits + sign)
const sal_Int32 SCA_MAXPLACES   = 10;               // max. number of places


STRING SAL_CALL AnalysisAddIn::getBin2Oct( constREFXPS& xOpt, const STRING& aNum, const ANY& rPlaces ) THROWDEF_RTE_IAE
{
    double fVal = ConvertToDec( aNum, 2, SCA_MAXPLACES );
    sal_Int32 nPlaces = 0;
    sal_Bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
    return ConvertFromDec( fVal, SCA_MIN8, SCA_MAX8, 8, nPlaces, SCA_MAXPLACES, bUsePlaces );
}


double SAL_CALL AnalysisAddIn::getBin2Dec( const STRING& aNum ) THROWDEF_RTE_IAE
{
    double fRet = ConvertToDec( aNum, 2, SCA_MAXPLACES );
    RETURN_FINITE( fRet );
}


STRING SAL_CALL AnalysisAddIn::getBin2Hex( constREFXPS& xOpt, const STRING& aNum, const ANY& rPlaces ) THROWDEF_RTE_IAE
{
    double fVal = ConvertToDec( aNum, 2, SCA_MAXPLACES );
    sal_Int32 nPlaces = 0;
    sal_Bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
    return ConvertFromDec( fVal, SCA_MIN16, SCA_MAX16, 16, nPlaces, SCA_MAXPLACES, bUsePlaces );
}


STRING SAL_CALL AnalysisAddIn::getOct2Bin( constREFXPS& xOpt, const STRING& aNum, const ANY& rPlaces ) THROWDEF_RTE_IAE
{
    double fVal = ConvertToDec( aNum, 8, SCA_MAXPLACES );
    sal_Int32 nPlaces = 0;
    sal_Bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
    return ConvertFromDec( fVal, SCA_MIN2, SCA_MAX2, 2, nPlaces, SCA_MAXPLACES, bUsePlaces );
}


double SAL_CALL AnalysisAddIn::getOct2Dec( const STRING& aNum ) THROWDEF_RTE_IAE
{
    double fRet = ConvertToDec( aNum, 8, SCA_MAXPLACES );
    RETURN_FINITE( fRet );
}


STRING SAL_CALL AnalysisAddIn::getOct2Hex( constREFXPS& xOpt, const STRING& aNum, const ANY& rPlaces ) THROWDEF_RTE_IAE
{
    double fVal = ConvertToDec( aNum, 8, SCA_MAXPLACES );
    sal_Int32 nPlaces = 0;
    sal_Bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
    return ConvertFromDec( fVal, SCA_MIN16, SCA_MAX16, 16, nPlaces, SCA_MAXPLACES, bUsePlaces );
}


STRING SAL_CALL AnalysisAddIn::getDec2Bin( constREFXPS& xOpt, sal_Int32 nNum, const ANY& rPlaces ) THROWDEF_RTE_IAE
{
    sal_Int32 nPlaces = 0;
    sal_Bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
    return ConvertFromDec( nNum, SCA_MIN2, SCA_MAX2, 2, nPlaces, SCA_MAXPLACES, bUsePlaces );
}


STRING SAL_CALL AnalysisAddIn::getDec2Oct( constREFXPS& xOpt, sal_Int32 nNum, const ANY& rPlaces ) THROWDEF_RTE_IAE
{
    sal_Int32 nPlaces = 0;
    sal_Bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
    return ConvertFromDec( nNum, SCA_MIN8, SCA_MAX8, 8, nPlaces, SCA_MAXPLACES, bUsePlaces );
}


STRING SAL_CALL AnalysisAddIn::getDec2Hex( constREFXPS& xOpt, double fNum, const ANY& rPlaces ) THROWDEF_RTE_IAE
{
    sal_Int32 nPlaces = 0;
    sal_Bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
    return ConvertFromDec( fNum, SCA_MIN16, SCA_MAX16, 16, nPlaces, SCA_MAXPLACES, bUsePlaces );
}


STRING SAL_CALL AnalysisAddIn::getHex2Bin( constREFXPS& xOpt, const STRING& aNum, const ANY& rPlaces ) THROWDEF_RTE_IAE
{
    double fVal = ConvertToDec( aNum, 16, SCA_MAXPLACES );
    sal_Int32 nPlaces = 0;
    sal_Bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
    return ConvertFromDec( fVal, SCA_MIN2, SCA_MAX2, 2, nPlaces, SCA_MAXPLACES, bUsePlaces );
}


double SAL_CALL AnalysisAddIn::getHex2Dec( const STRING& aNum ) THROWDEF_RTE_IAE
{
    double fRet = ConvertToDec( aNum, 16, SCA_MAXPLACES );
    RETURN_FINITE( fRet );
}


STRING SAL_CALL AnalysisAddIn::getHex2Oct( constREFXPS& xOpt, const STRING& aNum, const ANY& rPlaces ) THROWDEF_RTE_IAE
{
    double fVal = ConvertToDec( aNum, 16, SCA_MAXPLACES );
    sal_Int32 nPlaces = 0;
    sal_Bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
    return ConvertFromDec( fVal, SCA_MIN8, SCA_MAX8, 8, nPlaces, SCA_MAXPLACES, bUsePlaces );
}


sal_Int32 SAL_CALL AnalysisAddIn::getDelta( constREFXPS& xOpt, double fNum1, const ANY& rNum2 ) THROWDEF_RTE_IAE
{
    return fNum1 == aAnyConv.getDouble( xOpt, rNum2, 0.0 );
}


double SAL_CALL AnalysisAddIn::getErf( constREFXPS& xOpt, double fLL, const ANY& rUL ) THROWDEF_RTE_IAE
{
    double fUL, fRet;
    sal_Bool bContainsValue = aAnyConv.getDouble( fUL, xOpt, rUL );

    fRet = bContainsValue ? (Erf( fUL ) - Erf( fLL )) : Erf( fLL );
    RETURN_FINITE( fRet );
}


double SAL_CALL AnalysisAddIn::getErfc( double f ) THROWDEF_RTE_IAE
{
    double fRet = Erfc( f );
    RETURN_FINITE( fRet );
}


sal_Int32 SAL_CALL AnalysisAddIn::getGestep( constREFXPS& xOpt, double fNum, const ANY& rStep ) THROWDEF_RTE_IAE
{
    return fNum >= aAnyConv.getDouble( xOpt, rStep, 0.0 );
}


double SAL_CALL AnalysisAddIn::getFactdouble( sal_Int32 nNum ) THROWDEF_RTE_IAE
{
    double fRet = FactDouble( nNum );
    RETURN_FINITE( fRet );
}


double SAL_CALL AnalysisAddIn::getImabs( const STRING& aNum ) THROWDEF_RTE_IAE
{
    double fRet = Complex( aNum ).Abs();
    RETURN_FINITE( fRet );
}


double SAL_CALL AnalysisAddIn::getImaginary( const STRING& aNum ) THROWDEF_RTE_IAE
{
    double fRet = Complex( aNum ).Imag();
    RETURN_FINITE( fRet );
}


STRING SAL_CALL AnalysisAddIn::getImpower( const STRING& aNum, double f ) THROWDEF_RTE_IAE
{
    Complex     z( aNum );

    z.Power( f );

    return z.GetString();
}


double SAL_CALL AnalysisAddIn::getImargument( const STRING& aNum ) THROWDEF_RTE_IAE
{
    double fRet = Complex( aNum ).Arg();
    RETURN_FINITE( fRet );
}


STRING SAL_CALL AnalysisAddIn::getImcos( const STRING& aNum ) THROWDEF_RTE_IAE
{
    Complex     z( aNum );

    z.Cos();

    return z.GetString();
}


STRING SAL_CALL AnalysisAddIn::getImdiv( const STRING& aDivid, const STRING& aDivis ) THROWDEF_RTE_IAE
{
    Complex     z( aDivid );

    z.Div( Complex( aDivis ) );

    return z.GetString();
}


STRING SAL_CALL AnalysisAddIn::getImexp( const STRING& aNum ) THROWDEF_RTE_IAE
{
    Complex     z( aNum );

    z.Exp();

    return z.GetString();
}


STRING SAL_CALL AnalysisAddIn::getImconjugate( const STRING& aNum ) THROWDEF_RTE_IAE
{
    Complex     z( aNum );

    z.Conjugate();

    return z.GetString();
}


STRING SAL_CALL AnalysisAddIn::getImln( const STRING& aNum ) THROWDEF_RTE_IAE
{
    Complex     z( aNum );

    z.Ln();

    return z.GetString();
}


STRING SAL_CALL AnalysisAddIn::getImlog10( const STRING& aNum ) THROWDEF_RTE_IAE
{
    Complex     z( aNum );

    z.Log10();

    return z.GetString();
}


STRING SAL_CALL AnalysisAddIn::getImlog2( const STRING& aNum ) THROWDEF_RTE_IAE
{
    Complex     z( aNum );

    z.Log2();

    return z.GetString();
}


STRING SAL_CALL AnalysisAddIn::getImproduct( constREFXPS&, const SEQSEQ( STRING )& aNum1, const SEQ( uno::Any )& aNL ) THROWDEF_RTE_IAE
{
    ComplexList     z_list;

    z_list.Append( aNum1, AH_IgnoreEmpty );
    z_list.Append( aNL, AH_IgnoreEmpty );

    const Complex*  p = z_list.First();

    if( !p )
        return Complex( 0 ).GetString();

    Complex         z( *p );

    for( p = z_list.Next() ; p ; p = z_list.Next() )
        z.Mult( *p );

    return z.GetString();
}


double SAL_CALL AnalysisAddIn::getImreal( const STRING& aNum ) THROWDEF_RTE_IAE
{
    double fRet = Complex( aNum ).Real();
    RETURN_FINITE( fRet );
}


STRING SAL_CALL AnalysisAddIn::getImsin( const STRING& aNum ) THROWDEF_RTE_IAE
{
    Complex     z( aNum );

    z.Sin();

    return z.GetString();
}


STRING SAL_CALL AnalysisAddIn::getImsub( const STRING& aNum1, const STRING& aNum2 ) THROWDEF_RTE_IAE
{
    Complex     z( aNum1 );

    z.Sub( Complex( aNum2 ) );

    return z.GetString();
}


STRING SAL_CALL AnalysisAddIn::getImsum( constREFXPS&, const SEQSEQ( STRING )& aNum1, const SEQ( CSS::uno::Any )& aFollowingPars ) THROWDEF_RTE_IAE
{
    ComplexList     z_list;

    z_list.Append( aNum1, AH_IgnoreEmpty );
    z_list.Append( aFollowingPars, AH_IgnoreEmpty );

    const Complex*  p = z_list.First();

    if( !p )
        return Complex( 0 ).GetString();

    Complex         z( *p );

    for( p = z_list.Next() ; p ; p = z_list.Next() )
        z.Add( *p );

    return z.GetString();
}


STRING SAL_CALL AnalysisAddIn::getImsqrt( const STRING& aNum ) THROWDEF_RTE_IAE
{
    Complex     z( aNum );

    z.Sqrt();

    return z.GetString();
}


STRING SAL_CALL AnalysisAddIn::getComplex( double fR, double fI, const ANY& rSuff ) THROWDEF_RTE_IAE
{
    sal_Bool    bi;

    switch( rSuff.getValueTypeClass() )
    {
        case uno::TypeClass_VOID:
            bi = sal_True;
            break;
        case uno::TypeClass_STRING:
            {
            const STRING*   pSuff = ( const STRING* ) rSuff.getValue();
            bi = pSuff->compareToAscii( "i" ) == 0 || pSuff->getLength() == 0;
            if( !bi && pSuff->compareToAscii( "j" ) != 0 )
                THROW_IAE;
            }
            break;
        default:
            THROW_IAE;
    }

    return Complex( fR, fI, bi ? 'i' : 'j' ).GetString();
}


double SAL_CALL AnalysisAddIn::getConvert( double f, const STRING& aFU, const STRING& aTU ) THROWDEF_RTE_IAE
{
    if( !pCDL )
        pCDL = new ConvertDataList();

    double fRet = pCDL->Convert( f, aFU, aTU );
    RETURN_FINITE( fRet );
}


/* vim:set shiftwidth=4 softtabstop=4 expandtab: */