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 :
10 : #include "simpleformulacalc.hxx"
11 : #include "document.hxx"
12 : #include "tokenarray.hxx"
13 : #include "interpre.hxx"
14 : #include "compiler.hxx"
15 :
16 :
17 0 : ScSimpleFormulaCalculator::ScSimpleFormulaCalculator( ScDocument* pDoc, const ScAddress& rAddr,
18 : const OUString& rFormula, formula::FormulaGrammar::Grammar eGram ):
19 : mbCalculated(false),
20 : maAddr(rAddr),
21 0 : mpDoc(pDoc)
22 : {
23 : // compile already here
24 0 : ScCompiler aComp(pDoc, rAddr);
25 0 : aComp.SetGrammar(eGram);
26 0 : mpCode.reset(aComp.CompileString(rFormula));
27 0 : if(!mpCode->GetCodeError() && mpCode->GetLen())
28 0 : aComp.CompileTokenArray();
29 0 : }
30 :
31 0 : ScSimpleFormulaCalculator::~ScSimpleFormulaCalculator()
32 : {
33 0 : }
34 :
35 0 : void ScSimpleFormulaCalculator::Calculate()
36 : {
37 0 : if(mbCalculated)
38 0 : return;
39 :
40 0 : mbCalculated = true;
41 0 : ScInterpreter aInt(NULL, mpDoc, maAddr, *mpCode.get());
42 0 : aInt.Interpret();
43 :
44 0 : mnFormatType = aInt.GetRetFormatType();
45 0 : mnFormatIndex = aInt.GetRetFormatIndex();
46 0 : maResult.SetToken(aInt.GetResultToken().get());
47 : }
48 :
49 0 : bool ScSimpleFormulaCalculator::IsValue()
50 : {
51 0 : Calculate();
52 :
53 0 : return maResult.IsValue();
54 : }
55 :
56 0 : sal_uInt16 ScSimpleFormulaCalculator::GetErrCode()
57 : {
58 0 : Calculate();
59 :
60 0 : sal_uInt16 nErr = mpCode->GetCodeError();
61 0 : if (nErr)
62 0 : return nErr;
63 0 : return maResult.GetResultError();
64 : }
65 :
66 :
67 0 : double ScSimpleFormulaCalculator::GetValue()
68 : {
69 0 : Calculate();
70 :
71 0 : if ((!mpCode->GetCodeError() || mpCode->GetCodeError() == errDoubleRef) &&
72 0 : !maResult.GetResultError())
73 0 : return maResult.GetDouble();
74 :
75 0 : return 0.0;
76 : }
77 :
78 0 : OUString ScSimpleFormulaCalculator::GetString()
79 : {
80 0 : Calculate();
81 :
82 0 : if ((!mpCode->GetCodeError() || mpCode->GetCodeError() == errDoubleRef) &&
83 0 : !maResult.GetResultError())
84 0 : return maResult.GetString();
85 :
86 0 : return OUString();
87 : }
88 :
89 0 : bool ScSimpleFormulaCalculator::HasColRowName()
90 : {
91 0 : mpCode->Reset();
92 0 : return mpCode->GetNextColRowName() != NULL;
93 : }
94 :
95 0 : ScTokenArray* ScSimpleFormulaCalculator::GetCode()
96 : {
97 0 : return mpCode.get();
98 93 : }
99 :
100 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|