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
|
/* -*- 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 "funcdesc.hxx"
#include "addincol.hxx"
#include "appoptio.hxx"
#include "callform.hxx"
#include "compiler.hxx"
#include "global.hxx"
#include "sc.hrc"
#include "scmod.hxx"
#include "scresid.hxx"
#include <rtl/ustring.hxx>
#include <rtl/ustrbuf.hxx>
#include <tools/rcid.h>
#include <tools/resid.hxx>
#include <unotools/collatorwrapper.hxx>
#include <numeric>
#include <boost/scoped_ptr.hpp>
class ScFuncRes : public Resource
{
public:
ScFuncRes( ResId&, ScFuncDesc*, bool & rbSuppressed );
private:
sal_uInt16 GetNum();
};
class ScResourcePublisher : public Resource
{
private:
void FreeResource() { Resource::FreeResource(); }
public:
ScResourcePublisher( const ScResId& rId ) : Resource( rId ) {}
~ScResourcePublisher() { FreeResource(); }
bool IsAvailableRes( const ResId& rId ) const
{ return Resource::IsAvailableRes( rId ); }
};
//========================================================================
// class ScFuncDesc:
//========================================================================
ScFuncDesc::ScFuncDesc() :
pFuncName (NULL),
pFuncDesc (NULL),
ppDefArgNames (NULL),
ppDefArgDescs (NULL),
pDefArgFlags (NULL),
nFIndex (0),
nCategory (0),
nArgCount (0),
bIncomplete (false),
bHasSuppressedArgs(false)
{}
ScFuncDesc::~ScFuncDesc()
{
Clear();
}
void ScFuncDesc::Clear()
{
sal_uInt16 nArgs = nArgCount;
if (nArgs >= VAR_ARGS) nArgs -= VAR_ARGS-1;
if (nArgs)
{
for (sal_uInt16 i=0; i<nArgs; i++ )
{
delete ppDefArgNames[i];
delete ppDefArgDescs[i];
}
delete [] ppDefArgNames;
delete [] ppDefArgDescs;
delete [] pDefArgFlags;
}
nArgCount = 0;
ppDefArgNames = NULL;
ppDefArgDescs = NULL;
pDefArgFlags = NULL;
delete pFuncName;
pFuncName = NULL;
delete pFuncDesc;
pFuncDesc = NULL;
nFIndex = 0;
nCategory = 0;
sHelpId = rtl::OString();
bIncomplete = false;
bHasSuppressedArgs = false;
}
::rtl::OUString ScFuncDesc::GetParamList() const
{
::rtl::OUString sep(ScCompiler::GetNativeSymbol(ocSep));
::rtl::OUStringBuffer aSig;
if ( nArgCount > 0 )
{
if ( nArgCount < VAR_ARGS )
{
sal_uInt16 nLastSuppressed = nArgCount;
sal_uInt16 nLastAdded = nArgCount;
for ( sal_uInt16 i=0; i<nArgCount; i++ )
{
if (pDefArgFlags[i].bSuppress)
nLastSuppressed = i;
else
{
nLastAdded = i;
aSig.append(*(ppDefArgNames[i]));
if ( i != nArgCount-1 )
{
aSig.append(sep);
aSig.appendAscii( " " );
}
}
}
// If only suppressed parameters follow the last added parameter,
// remove one "; "
if (nLastSuppressed < nArgCount && nLastAdded < nLastSuppressed &&
aSig.getLength() >= 2)
aSig.setLength(aSig.getLength() - 2);
}
else
{
sal_uInt16 nFix = nArgCount - VAR_ARGS;
for ( sal_uInt16 nArg = 0; nArg < nFix; nArg++ )
{
if (!pDefArgFlags[nArg].bSuppress)
{
aSig.append(*(ppDefArgNames[nArg]));
aSig.append(sep);
aSig.appendAscii( " " );
}
}
/* NOTE: Currently there are no suppressed var args parameters. If
* there were, we'd have to cope with it here and above for the fix
* parameters. For now parameters are always added, so no special
* treatment of a trailing "; " necessary. */
aSig.append(*(ppDefArgNames[nFix]));
aSig.append(sal_Unicode('1'));
aSig.append(sep);
aSig.append(sal_Unicode(' '));
aSig.append(*(ppDefArgNames[nFix]));
aSig.append(sal_Unicode('2'));
aSig.append(sep);
aSig.appendAscii(" ... ");
}
}
return aSig.makeStringAndClear();
}
::rtl::OUString ScFuncDesc::getSignature() const
{
::rtl::OUStringBuffer aSig;
if(pFuncName)
{
aSig.append(*pFuncName);
::rtl::OUString aParamList = GetParamList();
if( aParamList.getLength() )
{
aSig.appendAscii( "( " );
aSig.append(aParamList);
// U+00A0 (NBSP) prevents automatic line break
aSig.append( static_cast< sal_Unicode >(0xA0) );
aSig.appendAscii( ")" );
}
else
aSig.appendAscii( "()" );
}
return aSig.makeStringAndClear();
}
::rtl::OUString ScFuncDesc::getFormula( const ::std::vector< ::rtl::OUString >& _aArguments ) const
{
::rtl::OUString sep = ScCompiler::GetNativeSymbol(ocSep);
::rtl::OUStringBuffer aFormula;
if(pFuncName)
{
aFormula.append( *pFuncName );
aFormula.appendAscii( "(" );
::std::vector< ::rtl::OUString >::const_iterator aIter = _aArguments.begin();
::std::vector< ::rtl::OUString >::const_iterator aEnd = _aArguments.end();
if ( nArgCount > 0 && aIter != aEnd )
{
bool bLastArg = ( aIter->getLength() == 0 );
while( aIter != aEnd && !bLastArg )
{
aFormula.append( *(aIter) );
if ( aIter != (aEnd-1) )
{
bLastArg = !( (aIter+1)->getLength() > 0 );
if ( !bLastArg )
aFormula.append( sep );
}
++aIter;
}
}
aFormula.appendAscii( ")" );
}
return aFormula.makeStringAndClear();
}
sal_uInt16 ScFuncDesc::GetSuppressedArgCount() const
{
if (!bHasSuppressedArgs || !pDefArgFlags)
return nArgCount;
sal_uInt16 nArgs = nArgCount;
if (nArgs >= VAR_ARGS)
nArgs -= VAR_ARGS - 1;
sal_uInt16 nCount = nArgs;
for (sal_uInt16 i=0; i < nArgs; ++i)
{
if (pDefArgFlags[i].bSuppress)
--nCount;
}
if (nArgCount >= VAR_ARGS)
nCount += VAR_ARGS - 1;
return nCount;
}
::rtl::OUString ScFuncDesc::getFunctionName() const
{
::rtl::OUString sRet;
if ( pFuncName )
sRet = *pFuncName;
return sRet;
}
const formula::IFunctionCategory* ScFuncDesc::getCategory() const
{
return ScGlobal::GetStarCalcFunctionMgr()->getCategory(nCategory);
}
::rtl::OUString ScFuncDesc::getDescription() const
{
::rtl::OUString sRet;
if ( pFuncDesc )
sRet = *pFuncDesc;
return sRet;
}
xub_StrLen ScFuncDesc::getSuppressedArgumentCount() const
{
return GetSuppressedArgCount();
}
void ScFuncDesc::fillVisibleArgumentMapping(::std::vector<sal_uInt16>& _rArguments) const
{
if (!bHasSuppressedArgs || !pDefArgFlags)
{
_rArguments.resize( nArgCount);
::std::vector<sal_uInt16>::iterator iter = _rArguments.begin();
sal_uInt16 value = 0;
while (iter != _rArguments.end())
*iter++ = value++;
}
_rArguments.reserve( nArgCount);
sal_uInt16 nArgs = nArgCount;
if (nArgs >= VAR_ARGS)
nArgs -= VAR_ARGS - 1;
for (sal_uInt16 i=0; i < nArgs; ++i)
{
if (!pDefArgFlags[i].bSuppress)
_rArguments.push_back(i);
}
}
void ScFuncDesc::initArgumentInfo() const
{
// get the full argument description
// (add-in has to be instantiated to get the type information)
if ( bIncomplete && pFuncName )
{
ScUnoAddInCollection& rAddIns = *ScGlobal::GetAddInCollection();
::rtl::OUString aIntName(rAddIns.FindFunction( *pFuncName, true )); // pFuncName is upper-case
if ( aIntName.getLength() )
{
// GetFuncData with bComplete=true loads the component and updates
// the global function list if needed.
rAddIns.GetFuncData( aIntName, true );
}
if ( bIncomplete )
{
OSL_FAIL( "couldn't initialize add-in function" );
const_cast<ScFuncDesc*>(this)->bIncomplete = false; // even if there was an error, don't try again
}
}
}
::rtl::OString ScFuncDesc::getHelpId() const
{
return sHelpId;
}
sal_uInt32 ScFuncDesc::getParameterCount() const
{
return nArgCount;
}
::rtl::OUString ScFuncDesc::getParameterName(sal_uInt32 _nPos) const
{
return *(ppDefArgNames[_nPos]);
}
::rtl::OUString ScFuncDesc::getParameterDescription(sal_uInt32 _nPos) const
{
return *(ppDefArgDescs[_nPos]);
}
bool ScFuncDesc::isParameterOptional(sal_uInt32 _nPos) const
{
return pDefArgFlags[_nPos].bOptional;
}
bool ScFuncDesc::compareByName(const ScFuncDesc* a, const ScFuncDesc* b)
{
return (ScGlobal::GetCaseCollator()->compareString(*a->pFuncName, *b->pFuncName ) == COMPARE_LESS);
}
//===================================================================
// class ScFunctionList:
//===================================================================
ScFunctionList::ScFunctionList() :
nMaxFuncNameLen ( 0 )
{
ScFuncDesc* pDesc = NULL;
xub_StrLen nStrLen = 0;
FuncCollection* pFuncColl;
::std::list<ScFuncDesc*> tmpFuncList;
sal_uInt16 nDescBlock[] =
{
RID_SC_FUNCTION_DESCRIPTIONS1,
RID_SC_FUNCTION_DESCRIPTIONS2
};
for (sal_uInt16 k = 0; k < SAL_N_ELEMENTS(nDescBlock); ++k)
{
SAL_WNODEPRECATED_DECLARATIONS_PUSH
::std::auto_ptr<ScResourcePublisher> pBlock( new ScResourcePublisher( ScResId( nDescBlock[k] ) ) );
SAL_WNODEPRECATED_DECLARATIONS_POP
// Browse for all possible OpCodes. This is not the fastest method, but
// otherwise the sub resources within the resource blocks and the
// resource blocks themselfs would had to be ordered according to
// OpCodes, which is utopian..
for (sal_uInt16 i = 0; i <= SC_OPCODE_LAST_OPCODE_ID; ++i)
{
ScResId aRes(i);
aRes.SetRT(RSC_RESOURCE);
// Sub resource of OpCode available?
if (pBlock->IsAvailableRes(aRes))
{
pDesc = new ScFuncDesc;
bool bSuppressed = false;
ScFuncRes aSubRes( aRes, pDesc, bSuppressed);
// Instead of dealing with this exceptional case at 1001 places
// we simply don't add an entirely suppressed function to the
// list and delete it.
if (bSuppressed)
delete pDesc;
else
{
pDesc->nFIndex = i;
tmpFuncList.push_back(pDesc);
nStrLen = (*(pDesc->pFuncName)).getLength();
if (nStrLen > nMaxFuncNameLen)
nMaxFuncNameLen = nStrLen;
}
}
}
}
sal_uInt16 nNextId = SC_OPCODE_LAST_OPCODE_ID + 1; // FuncID for AddIn functions
// Interpretation of AddIn list
::rtl::OUString aDefArgNameValue = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("value"));
::rtl::OUString aDefArgNameString = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("string"));
::rtl::OUString aDefArgNameValues = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("values"));
::rtl::OUString aDefArgNameStrings = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("strings"));
::rtl::OUString aDefArgNameCells = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("cells"));
::rtl::OUString aDefArgNameNone = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("none"));
::rtl::OUString aDefArgDescValue = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("a value"));
::rtl::OUString aDefArgDescString = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("a string"));
::rtl::OUString aDefArgDescValues = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("array of values"));
::rtl::OUString aDefArgDescStrings = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("array of strings"));
::rtl::OUString aDefArgDescCells = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("range of cells"));
::rtl::OUString aDefArgDescNone = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("none"));
::rtl::OUString aArgName, aArgDesc;
pFuncColl = ScGlobal::GetFuncCollection();
for (sal_uInt16 i = 0; i < pFuncColl->GetCount(); ++i)
{
pDesc = new ScFuncDesc;
FuncData *pAddInFuncData = (FuncData*)pFuncColl->At(i);
sal_uInt16 nArgs = pAddInFuncData->GetParamCount() - 1;
pAddInFuncData->getParamDesc( aArgName, aArgDesc, 0 );
pDesc->nFIndex = nNextId++; // ??? OpCode vergeben
pDesc->nCategory = ID_FUNCTION_GRP_ADDINS;
pDesc->pFuncName = new ::rtl::OUString(pAddInFuncData->GetInternalName());
pDesc->pFuncName->toAsciiUpperCase();
::rtl::OUStringBuffer aBuf(aArgDesc);
aBuf.append(sal_Unicode('\n'));
aBuf.appendAscii("( AddIn: ");
aBuf.append(pAddInFuncData->GetModuleName());
aBuf.appendAscii(" )");
pDesc->pFuncDesc = new ::rtl::OUString(aBuf.makeStringAndClear());
pDesc->nArgCount = nArgs;
if (nArgs)
{
pDesc->pDefArgFlags = new ScFuncDesc::ParameterFlags[nArgs];
pDesc->ppDefArgNames = new ::rtl::OUString*[nArgs];
pDesc->ppDefArgDescs = new ::rtl::OUString*[nArgs];
for (sal_uInt16 j = 0; j < nArgs; ++j)
{
pDesc->pDefArgFlags[j].bOptional = false;
pDesc->pDefArgFlags[j].bSuppress = false;
pAddInFuncData->getParamDesc( aArgName, aArgDesc, j+1 );
if ( aArgName.getLength() )
pDesc->ppDefArgNames[j] = new ::rtl::OUString( aArgName );
else
{
switch (pAddInFuncData->GetParamType(j+1))
{
case PTR_DOUBLE:
pDesc->ppDefArgNames[j] = new ::rtl::OUString( aDefArgNameValue );
break;
case PTR_STRING:
pDesc->ppDefArgNames[j] = new ::rtl::OUString( aDefArgNameString );
break;
case PTR_DOUBLE_ARR:
pDesc->ppDefArgNames[j] = new ::rtl::OUString( aDefArgNameValues );
break;
case PTR_STRING_ARR:
pDesc->ppDefArgNames[j] = new ::rtl::OUString( aDefArgNameStrings );
break;
case PTR_CELL_ARR:
pDesc->ppDefArgNames[j] = new ::rtl::OUString( aDefArgNameCells );
break;
default:
pDesc->ppDefArgNames[j] = new ::rtl::OUString( aDefArgNameNone );
break;
}
}
if ( aArgDesc.getLength() )
pDesc->ppDefArgDescs[j] = new ::rtl::OUString( aArgDesc );
else
{
switch (pAddInFuncData->GetParamType(j+1))
{
case PTR_DOUBLE:
pDesc->ppDefArgDescs[j] = new ::rtl::OUString( aDefArgDescValue );
break;
case PTR_STRING:
pDesc->ppDefArgDescs[j] = new ::rtl::OUString( aDefArgDescString );
break;
case PTR_DOUBLE_ARR:
pDesc->ppDefArgDescs[j] = new ::rtl::OUString( aDefArgDescValues );
break;
case PTR_STRING_ARR:
pDesc->ppDefArgDescs[j] = new ::rtl::OUString( aDefArgDescStrings );
break;
case PTR_CELL_ARR:
pDesc->ppDefArgDescs[j] = new ::rtl::OUString( aDefArgDescCells );
break;
default:
pDesc->ppDefArgDescs[j] = new ::rtl::OUString( aDefArgDescNone );
break;
}
}
}
}
tmpFuncList.push_back(pDesc);
nStrLen = (*(pDesc->pFuncName)).getLength();
if ( nStrLen > nMaxFuncNameLen)
nMaxFuncNameLen = nStrLen;
}
// StarOne AddIns
ScUnoAddInCollection* pUnoAddIns = ScGlobal::GetAddInCollection();
long nUnoCount = pUnoAddIns->GetFuncCount();
for (long nFunc=0; nFunc<nUnoCount; nFunc++)
{
pDesc = new ScFuncDesc;
pDesc->nFIndex = nNextId++;
if ( pUnoAddIns->FillFunctionDesc( nFunc, *pDesc ) )
{
tmpFuncList.push_back(pDesc);
nStrLen = (*(pDesc->pFuncName)).getLength();
if (nStrLen > nMaxFuncNameLen)
nMaxFuncNameLen = nStrLen;
}
else
delete pDesc;
}
//Move list to vector for better random access performance
::std::vector<const ScFuncDesc*> tmp(tmpFuncList.begin(), tmpFuncList.end());
tmpFuncList.clear();
aFunctionList.swap(tmp);
//Initialize iterator
aFunctionListIter = aFunctionList.end();
}
ScFunctionList::~ScFunctionList()
{
const ScFuncDesc* pDesc = First();
while (pDesc)
{
delete pDesc;
pDesc = Next();
}
}
const ScFuncDesc* ScFunctionList::First()
{
const ScFuncDesc* pDesc = NULL;
aFunctionListIter = aFunctionList.begin();
if(aFunctionListIter != aFunctionList.end())
pDesc = *aFunctionListIter;
return pDesc;
}
const ScFuncDesc* ScFunctionList::Next()
{
const ScFuncDesc* pDesc = NULL;
if(aFunctionListIter != aFunctionList.end())
{
if((++aFunctionListIter) != aFunctionList.end())
pDesc = *aFunctionListIter;
}
return pDesc;
}
const ScFuncDesc* ScFunctionList::GetFunction( sal_uInt32 nIndex ) const
{
const ScFuncDesc* pDesc = NULL;
if(nIndex < aFunctionList.size())
pDesc = aFunctionList.at(nIndex);
return pDesc;
}
//===================================================================
// class ScFunctionCategory:
//===================================================================
sal_uInt32 ScFunctionCategory::getCount() const
{
return m_pCategory->size();
}
const formula::IFunctionManager* ScFunctionCategory::getFunctionManager() const
{
return m_pMgr;
}
::rtl::OUString ScFunctionCategory::getName() const
{
if ( !m_sName.getLength() )
m_sName = ScFunctionMgr::GetCategoryName(m_nCategory+1);
return m_sName;
}
const formula::IFunctionDescription* ScFunctionCategory::getFunction(sal_uInt32 _nPos) const
{
const ScFuncDesc* pDesc = NULL;
if(_nPos < m_pCategory->size())
pDesc = m_pCategory->at(_nPos);
return pDesc;
}
sal_uInt32 ScFunctionCategory::getNumber() const
{
return m_nCategory;
}
//========================================================================
// class ScFunctionMgr:
//========================================================================
ScFunctionMgr::ScFunctionMgr() :
pFuncList( ScGlobal::GetStarCalcFunctionList() )
{
OSL_ENSURE( pFuncList, "Functionlist not found." );
sal_uInt32 catCount[MAX_FUNCCAT] = {0};
aCatLists[0] = new ::std::vector<const ScFuncDesc*>();
aCatLists[0]->reserve(pFuncList->GetCount());
// Retrieve all functions, store in cumulative ("All") category, and count
// number of functions in each category
for(const ScFuncDesc* pDesc = pFuncList->First(); pDesc; pDesc = pFuncList->Next())
{
OSL_ENSURE((pDesc->nCategory) < MAX_FUNCCAT, "Unknown category");
if ((pDesc->nCategory) < MAX_FUNCCAT)
++catCount[pDesc->nCategory];
aCatLists[0]->push_back(pDesc);
}
// Sort functions in cumulative category by name
::std::sort(aCatLists[0]->begin(), aCatLists[0]->end(), ScFuncDesc::compareByName);
// Allocate correct amount of space for categories
for (sal_uInt16 i = 1; i < MAX_FUNCCAT; ++i)
{
aCatLists[i] = new ::std::vector<const ScFuncDesc*>();
aCatLists[i]->reserve(catCount[i]);
}
// Fill categories with the corresponding functions (still sorted by name)
for(::std::vector<const ScFuncDesc*>::iterator iter = aCatLists[0]->begin(); iter!=aCatLists[0]->end(); ++iter)
{
if (((*iter)->nCategory) < MAX_FUNCCAT)
aCatLists[(*iter)->nCategory]->push_back(*iter);
}
// Initialize iterators
pCurCatListIter = aCatLists[0]->end();
pCurCatListEnd = aCatLists[0]->end();
}
ScFunctionMgr::~ScFunctionMgr()
{
for (sal_uInt16 i = 0; i < MAX_FUNCCAT; ++i)
delete aCatLists[i];
}
const ScFuncDesc* ScFunctionMgr::Get( const ::rtl::OUString& rFName ) const
{
const ScFuncDesc* pDesc = NULL;
if (rFName.getLength() <= pFuncList->GetMaxFuncNameLen())
{
::boost::scoped_ptr<ScFuncDesc> dummy(new ScFuncDesc);
dummy->pFuncName = new ::rtl::OUString(rFName);
::std::vector<const ScFuncDesc*>::iterator lower =
::std::lower_bound(aCatLists[0]->begin(), aCatLists[0]->end(),
static_cast<const ScFuncDesc*>(dummy.get()), ScFuncDesc::compareByName);
if(rFName.equalsIgnoreAsciiCase(*(*lower)->pFuncName))
pDesc = *lower;
}
return pDesc;
}
const ScFuncDesc* ScFunctionMgr::Get( sal_uInt16 nFIndex ) const
{
const ScFuncDesc* pDesc;
for (pDesc = First(0); pDesc; pDesc = Next())
if (pDesc->nFIndex == nFIndex)
break;
return pDesc;
}
const ScFuncDesc* ScFunctionMgr::First( sal_uInt16 nCategory ) const
{
OSL_ENSURE( nCategory < MAX_FUNCCAT, "Unbekannte Kategorie" );
const ScFuncDesc* pDesc = NULL;
if ( nCategory < MAX_FUNCCAT )
{
pCurCatListIter = aCatLists[nCategory]->begin();
pCurCatListEnd = aCatLists[nCategory]->end();
pDesc = *pCurCatListIter;
}
else
{
pCurCatListIter = aCatLists[0]->end();
pCurCatListEnd = aCatLists[0]->end();
}
return pDesc;
}
const ScFuncDesc* ScFunctionMgr::Next() const
{
const ScFuncDesc* pDesc = NULL;
if ( pCurCatListIter != pCurCatListEnd )
{
if ( (++pCurCatListIter) != pCurCatListEnd )
{
pDesc = *pCurCatListIter;
}
}
return pDesc;
}
sal_uInt32 ScFunctionMgr::getCount() const
{
return MAX_FUNCCAT - 1;
}
const formula::IFunctionCategory* ScFunctionMgr::getCategory(sal_uInt32 nCategory) const
{
formula::IFunctionCategory* pRet = NULL;
if ( nCategory < (MAX_FUNCCAT-1) )
{
pRet = new ScFunctionCategory(const_cast<ScFunctionMgr*>(this),aCatLists[nCategory+1],nCategory); // aCatLists[0] is "all"
}
return pRet;
}
const formula::IFunctionDescription* ScFunctionMgr::getFunctionByName(const ::rtl::OUString& _sFunctionName) const
{
return Get(_sFunctionName);
}
void ScFunctionMgr::fillLastRecentlyUsedFunctions(::std::vector< const formula::IFunctionDescription*>& _rLastRUFunctions) const
{
const ScAppOptions& rAppOpt = SC_MOD()->GetAppOptions();
sal_uInt16 nLRUFuncCount = Min( rAppOpt.GetLRUFuncListCount(), (sal_uInt16)LRU_MAX );
sal_uInt16* pLRUListIds = rAppOpt.GetLRUFuncList();
if ( pLRUListIds )
{
for (sal_uInt16 i = 0; i < nLRUFuncCount; ++i)
_rLastRUFunctions.push_back( Get( pLRUListIds[i] ) );
}
}
::rtl::OUString ScFunctionMgr::GetCategoryName(sal_uInt32 _nCategoryNumber )
{
if ( _nCategoryNumber > SC_FUNCGROUP_COUNT )
{
OSL_FAIL("Invalid category number!");
return ::rtl::OUString();
}
SAL_WNODEPRECATED_DECLARATIONS_PUSH
::std::auto_ptr<ScResourcePublisher> pCategories( new ScResourcePublisher( ScResId( RID_FUNCTION_CATEGORIES ) ) );
SAL_WNODEPRECATED_DECLARATIONS_POP
return ResId::toString(ScResId(static_cast<sal_uInt16>(_nCategoryNumber)));
}
sal_Unicode ScFunctionMgr::getSingleToken(const formula::IFunctionManager::EToken _eToken) const
{
switch(_eToken)
{
case eOk:
return ScCompiler::GetNativeSymbol(ocOpen).GetChar(0);
case eClose:
return ScCompiler::GetNativeSymbol(ocClose).GetChar(0);
case eSep:
return ScCompiler::GetNativeSymbol(ocSep).GetChar(0);
case eArrayOpen:
return ScCompiler::GetNativeSymbol(ocArrayOpen).GetChar(0);
case eArrayClose:
return ScCompiler::GetNativeSymbol(ocArrayClose).GetChar(0);
}
return 0;
}
//========================================================================
// class ScFuncRes:
//========================================================================
ScFuncRes::ScFuncRes( ResId &aRes, ScFuncDesc* pDesc, bool & rbSuppressed )
: Resource(aRes)
{
rbSuppressed = (bool)GetNum();
pDesc->nCategory = GetNum();
pDesc->sHelpId = ReadByteStringRes();
pDesc->nArgCount = GetNum();
sal_uInt16 nArgs = pDesc->nArgCount;
if (nArgs >= VAR_ARGS)
nArgs -= VAR_ARGS - 1;
if (nArgs)
{
pDesc->pDefArgFlags = new ScFuncDesc::ParameterFlags[nArgs];
for (sal_uInt16 i = 0; i < nArgs; ++i)
{
pDesc->pDefArgFlags[i].bOptional = (bool)GetNum();
}
}
// Need to read the value from the resource even if nArgs==0 to advance the
// resource position pointer, so this can't be in the if(nArgs) block above.
sal_uInt16 nSuppressed = GetNum();
if (nSuppressed)
{
if (nSuppressed > nArgs)
{
OSL_TRACE( "ScFuncRes: suppressed parameters count mismatch on OpCode %u: suppressed %d > params %d",
aRes.GetId(), (int)nSuppressed, (int)nArgs);
nSuppressed = nArgs; // sanitize
}
for (sal_uInt16 i = 0; i < nSuppressed; ++i)
{
sal_uInt16 nParam = GetNum();
if (nParam < nArgs)
{
if (pDesc->nArgCount >= VAR_ARGS && nParam == nArgs-1)
{
OSL_TRACE( "ScFuncRes: VAR_ARGS parameters can't be suppressed, on OpCode %u: param %d == arg %d-1",
aRes.GetId(), (int)nParam, (int)nArgs);
}
else
{
pDesc->pDefArgFlags[nParam].bSuppress = true;
pDesc->bHasSuppressedArgs = true;
}
}
else
{
OSL_TRACE( "ScFuncRes: suppressed parameter exceeds count on OpCode %u: param %d >= args %d",
aRes.GetId(), (int)nParam, (int)nArgs);
}
}
}
pDesc->pFuncName = new ::rtl::OUString( ScCompiler::GetNativeSymbol( static_cast<OpCode>( aRes.GetId())));
pDesc->pFuncDesc = new ::rtl::OUString( ResId::toString(ScResId(1)));
if (nArgs)
{
pDesc->ppDefArgNames = new ::rtl::OUString*[nArgs];
pDesc->ppDefArgDescs = new ::rtl::OUString*[nArgs];
for (sal_uInt16 i = 0; i < nArgs; ++i)
{
pDesc->ppDefArgNames[i] = new ::rtl::OUString(ResId::toString(ScResId(2*(i+1) )));
pDesc->ppDefArgDescs[i] = new ::rtl::OUString(ResId::toString(ScResId(2*(i+1)+1)));
}
}
FreeResource();
}
sal_uInt16 ScFuncRes::GetNum()
{
return ReadShortRes();
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|