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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "formula/formulahelper.hxx"
#include <unotools/charclass.hxx>
#include <unotools/syslocale.hxx>
namespace formula
{
namespace
{
//============================================================================
class OEmptyFunctionDescription : public IFunctionDescription
{
public:
OEmptyFunctionDescription(){}
virtual ~OEmptyFunctionDescription(){}
virtual ::rtl::OUString getFunctionName() const { return ::rtl::OUString(); }
virtual const IFunctionCategory* getCategory() const { return NULL; }
virtual ::rtl::OUString getDescription() const { return ::rtl::OUString(); }
virtual xub_StrLen getSuppressedArgumentCount() const { return 0; }
virtual ::rtl::OUString getFormula(const ::std::vector< ::rtl::OUString >& ) const { return ::rtl::OUString(); }
virtual void fillVisibleArgumentMapping(::std::vector<sal_uInt16>& ) const {}
virtual void initArgumentInfo() const {}
virtual ::rtl::OUString getSignature() const { return ::rtl::OUString(); }
virtual rtl::OString getHelpId() const { return ""; }
virtual sal_uInt32 getParameterCount() const { return 0; }
virtual ::rtl::OUString getParameterName(sal_uInt32 ) const { return ::rtl::OUString(); }
virtual ::rtl::OUString getParameterDescription(sal_uInt32 ) const { return ::rtl::OUString(); }
virtual bool isParameterOptional(sal_uInt32 ) const { return sal_False; }
};
}
//===================================================================
// class FormulaHelper - static Method
//===================================================================
#define FUNC_NOTFOUND 0xffff
FormulaHelper::FormulaHelper(const IFunctionManager* _pFunctionManager)
:m_pSysLocale(new SvtSysLocale)
,m_pFunctionManager(_pFunctionManager)
,open(_pFunctionManager->getSingleToken(IFunctionManager::eOk))
,close(_pFunctionManager->getSingleToken(IFunctionManager::eClose))
,sep(_pFunctionManager->getSingleToken(IFunctionManager::eSep))
,arrayOpen(_pFunctionManager->getSingleToken(IFunctionManager::eArrayOpen))
,arrayClose(_pFunctionManager->getSingleToken(IFunctionManager::eArrayClose))
{
m_pCharClass = m_pSysLocale->GetCharClassPtr();
}
sal_Bool FormulaHelper::GetNextFunc( const String& rFormula,
sal_Bool bBack,
xub_StrLen& rFStart, // Input and output
xub_StrLen* pFEnd, // = NULL
const IFunctionDescription** ppFDesc, // = NULL
::std::vector< ::rtl::OUString>* pArgs ) const // = NULL
{
xub_StrLen nOldStart = rFStart;
String aFname;
rFStart = GetFunctionStart( rFormula, rFStart, bBack, ppFDesc ? &aFname : NULL );
sal_Bool bFound = ( rFStart != FUNC_NOTFOUND );
if ( bFound )
{
if ( pFEnd )
*pFEnd = GetFunctionEnd( rFormula, rFStart );
if ( ppFDesc )
{
*ppFDesc = NULL;
const ::rtl::OUString sTemp( aFname );
const sal_uInt32 nCategoryCount = m_pFunctionManager->getCount();
for(sal_uInt32 j= 0; j < nCategoryCount && !*ppFDesc; ++j)
{
const IFunctionCategory* pCategory = m_pFunctionManager->getCategory(j);
const sal_uInt32 nCount = pCategory->getCount();
for(sal_uInt32 i = 0 ; i < nCount; ++i)
{
const IFunctionDescription* pCurrent = pCategory->getFunction(i);
if ( pCurrent->getFunctionName().equalsIgnoreAsciiCase(sTemp) )
{
*ppFDesc = pCurrent;
break;
}
}// for(sal_uInt32 i = 0 ; i < nCount; ++i)
}
if ( *ppFDesc && pArgs )
{
GetArgStrings( *pArgs,rFormula, rFStart, static_cast<sal_uInt16>((*ppFDesc)->getParameterCount() ));
}
else
{
static OEmptyFunctionDescription s_aFunctionDescription;
*ppFDesc = &s_aFunctionDescription;
}
}
}
else
rFStart = nOldStart;
return bFound;
}
//------------------------------------------------------------------------
void FormulaHelper::FillArgStrings( const String& rFormula,
xub_StrLen nFuncPos,
sal_uInt16 nArgs,
::std::vector< ::rtl::OUString >& _rArgs ) const
{
xub_StrLen nStart = 0;
xub_StrLen nEnd = 0;
sal_uInt16 i;
sal_Bool bLast = sal_False;
for ( i=0; i<nArgs && !bLast; i++ )
{
nStart = GetArgStart( rFormula, nFuncPos, i );
if ( i+1<nArgs ) // last argument?
{
nEnd = GetArgStart( rFormula, nFuncPos, i+1 );
if ( nEnd != nStart )
_rArgs.push_back(rFormula.Copy( nStart, nEnd-1-nStart ));
else
_rArgs.push_back(String()), bLast = sal_True;
}
else
{
nEnd = GetFunctionEnd( rFormula, nFuncPos )-1;
if ( nStart < nEnd )
_rArgs.push_back( rFormula.Copy( nStart, nEnd-nStart ) );
else
_rArgs.push_back(String());
}
}
if ( bLast )
for ( ; i<nArgs; i++ )
_rArgs.push_back(String());
}
//------------------------------------------------------------------------
void FormulaHelper::GetArgStrings( ::std::vector< ::rtl::OUString >& _rArgs
,const String& rFormula,
xub_StrLen nFuncPos,
sal_uInt16 nArgs ) const
{
if (nArgs)
{
FillArgStrings( rFormula, nFuncPos, nArgs, _rArgs );
}
}
//------------------------------------------------------------------------
inline sal_Bool IsFormulaText( const CharClass* _pCharClass,const String& rStr, xub_StrLen nPos )
{
if( _pCharClass->isLetterNumeric( rStr, nPos ) )
return sal_True;
else
{ // In internationalized versions function names may contain a dot
// and in every version also an underscore... ;-)
sal_Unicode c = rStr.GetChar(nPos);
return c == '.' || c == '_';
}
}
xub_StrLen FormulaHelper::GetFunctionStart( const String& rFormula,
xub_StrLen nStart,
sal_Bool bBack,
String* pFuncName ) const
{
xub_StrLen nStrLen = rFormula.Len();
if ( nStrLen < nStart )
return nStart;
xub_StrLen nFStart = FUNC_NOTFOUND;
xub_StrLen nParPos = nStart;
sal_Bool bRepeat, bFound;
do
{
bFound = sal_False;
bRepeat = sal_False;
if ( bBack )
{
while ( !bFound && (nParPos > 0) )
{
if ( rFormula.GetChar(nParPos) == '"' )
{
nParPos--;
while ( (nParPos > 0) && rFormula.GetChar(nParPos) != '"' )
nParPos--;
if (nParPos > 0)
nParPos--;
}
else if ( (bFound = ( rFormula.GetChar(nParPos) == '(' ) ) == sal_False )
nParPos--;
}
}
else
{
while ( !bFound && (nParPos < nStrLen) )
{
if ( rFormula.GetChar(nParPos) == '"' )
{
nParPos++;
while ( (nParPos < nStrLen) && rFormula.GetChar(nParPos) != '"' )
nParPos++;
nParPos++;
}
else if ( (bFound = ( rFormula.GetChar(nParPos) == '(' ) ) == sal_False )
nParPos++;
}
}
if ( bFound && (nParPos > 0) )
{
nFStart = nParPos-1;
while ( (nFStart > 0) && IsFormulaText(m_pCharClass, rFormula, nFStart ))
nFStart--;
}
nFStart++;
if ( bFound )
{
if ( IsFormulaText( m_pCharClass,rFormula, nFStart ) )
{
// Function found
if ( pFuncName )
*pFuncName = rFormula.Copy( nFStart, nParPos-nFStart );
}
else // Brackets without function -> keep searching
{
bRepeat = sal_True;
if ( !bBack )
nParPos++;
else if (nParPos > 0)
nParPos--;
else
bRepeat = sal_False;
}
}
else // No brackets found
{
nFStart = FUNC_NOTFOUND;
if ( pFuncName )
pFuncName->Erase();
}
}
while(bRepeat);
return nFStart;
}
//------------------------------------------------------------------------
xub_StrLen FormulaHelper::GetFunctionEnd( const String& rStr, xub_StrLen nStart ) const
{
xub_StrLen nStrLen = rStr.Len();
if ( nStrLen < nStart )
return nStart;
short nParCount = 0;
bool bInArray = false;
sal_Bool bFound = sal_False;
while ( !bFound && (nStart < nStrLen) )
{
sal_Unicode c = rStr.GetChar(nStart);
if ( c == '"' )
{
nStart++;
while ( (nStart < nStrLen) && rStr.GetChar(nStart) != '"' )
nStart++;
}
else if ( c == open )
nParCount++;
else if ( c == close )
{
nParCount--;
if ( nParCount == 0 )
bFound = sal_True;
else if ( nParCount < 0 )
{
bFound = sal_True;
nStart--; // read one too far
}
}
else if ( c == arrayOpen )
{
bInArray = true;
}
else if ( c == arrayClose )
{
bInArray = false;
}
else if ( c == sep )
{
if ( !bInArray && nParCount == 0 )
{
bFound = sal_True;
nStart--; // read one too far
}
}
nStart++; // Set behind found position
}
return nStart;
}
//------------------------------------------------------------------
xub_StrLen FormulaHelper::GetArgStart( const String& rStr, xub_StrLen nStart, sal_uInt16 nArg ) const
{
xub_StrLen nStrLen = rStr.Len();
if ( nStrLen < nStart )
return nStart;
short nParCount = 0;
bool bInArray = false;
sal_Bool bFound = sal_False;
while ( !bFound && (nStart < nStrLen) )
{
sal_Unicode c = rStr.GetChar(nStart);
if ( c == '"' )
{
nStart++;
while ( (nStart < nStrLen) && rStr.GetChar(nStart) != '"' )
nStart++;
}
else if ( c == open )
{
bFound = ( nArg == 0 );
nParCount++;
}
else if ( c == close )
{
nParCount--;
bFound = ( nParCount == 0 );
}
else if ( c == arrayOpen )
{
bInArray = true;
}
else if ( c == arrayClose )
{
bInArray = false;
}
else if ( c == sep )
{
if ( !bInArray && nParCount == 1 )
{
nArg--;
bFound = ( nArg == 0 );
}
}
nStart++;
}
return nStart;
}
// =============================================================================
} // formula
// =============================================================================
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|