LibreOffice Module sc (master)  1
calcconfig.cxx
Go to the documentation of this file.
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 <ostream>
11 #include <set>
12 
14 #include <formula/grammar.hxx>
15 #include <formula/opcode.hxx>
16 #include <rtl/ustring.hxx>
17 #include <sal/log.hxx>
18 #include <sfx2/objsh.hxx>
19 #include <unotools/configmgr.hxx>
20 
21 #include <calcconfig.hxx>
22 #include <compiler.hxx>
23 #include <docsh.hxx>
24 
26 #include <com/sun/star/datatransfer/XTransferable2.hpp>
27 
29 
31 {
32  static rtl::Reference<ConfigurationListener> xListener(new ConfigurationListener("/org.openoffice.Office.Common/Misc"));
33  return xListener;
34 }
35 
37 {
38  static rtl::Reference<ConfigurationListener> xListener(new ConfigurationListener("/org.openoffice.Office.Calc/Formula/Calculation"));
39  return xListener;
40 }
41 
43 {
44  const char* env = getenv( "SC_FORCE_CALCULATION" );
45  if( env != nullptr )
46  {
47  if( strcmp( env, "opencl" ) == 0 )
48  {
49  SAL_INFO("sc.core.formulagroup", "Forcing calculations to use OpenCL");
51  }
52  if( strcmp( env, "threads" ) == 0 )
53  {
54  SAL_INFO("sc.core.formulagroup", "Forcing calculations to use threads");
56  }
57  if( strcmp( env, "core" ) == 0 )
58  {
59  SAL_INFO("sc.core.formulagroup", "Forcing calculations to use core");
60  return ForceCalculationCore;
61  }
62  SAL_WARN("sc.core.formulagroup", "Unrecognized value of SC_FORCE_CALCULATION");
63  abort();
64  }
65  return ForceCalculationNone;
66 }
67 
69 {
71  return type;
72 }
73 
75 {
77  return false;
79  if( force != ForceCalculationNone )
80  return force == ForceCalculationOpenCL;
81  static comphelper::ConfigurationListenerProperty<bool> gOpenCLEnabled(getMiscListener(), "UseOpenCL");
82  return gOpenCLEnabled.get();
83 }
84 
86 {
88  return false;
90  if( force != ForceCalculationNone )
91  return force == ForceCalculationThreads;
92  static comphelper::ConfigurationListenerProperty<bool> gThreadingEnabled(getFormulaCalculationListener(), "UseThreadedCalculationForFormulaGroups");
93  return gThreadingEnabled.get();
94 }
95 
97  meStringRefAddressSyntax(formula::FormulaGrammar::CONV_UNSPECIFIED),
98  meStringConversion(StringConversion::LOCALE), // old LibreOffice behavior
99  mbEmptyStringAsZero(false),
100  mbHasStringRefSyntax(false)
101 {
103 
104  // SAL _DEBUG(__FILE__ ":" << __LINE__ << ": ScCalcConfig::ScCalcConfig(): " << *this);
105 }
106 
108 {
109  // Keep in order of opcode value, is that clearest? (Random order,
110  // at least, would make no sense at all.)
111  static const OpCodeSet pDefaultOpenCLSubsetOpCodes(new o3tl::sorted_vector<OpCode>({
112  ocAdd,
113  ocSub,
114  ocNegSub,
115  ocMul,
116  ocDiv,
117  ocPow,
118  ocRandom,
119  ocSin,
120  ocCos,
121  ocTan,
122  ocArcTan,
123  ocExp,
124  ocLn,
125  ocSqrt,
127  ocSNormInv,
128  ocRound,
129  ocPower,
130  ocSumProduct,
131  ocMin,
132  ocMax,
133  ocSum,
134  ocProduct,
135  ocAverage,
136  ocCount,
137  ocVar,
138  ocNormDist,
139  ocVLookup,
140  ocCorrel,
141  ocCovar,
142  ocPearson,
143  ocSlope,
144  ocSumIfs}));
145 
146  // Note that these defaults better be kept in sync with those in
147  // officecfg/registry/schema/org/openoffice/Office/Calc.xcs.
148  // Crazy.
149  mbOpenCLSubsetOnly = true;
150  mbOpenCLAutoSelect = true;
152  mpOpenCLSubsetOpCodes = pDefaultOpenCLSubsetOpCodes;
153 }
154 
156 {
157  *this = ScCalcConfig();
158 }
159 
161 {
162  // String conversion options are per document.
165  // INDIRECT ref syntax is per document.
168 }
169 
171 {
172  meStringRefAddressSyntax = eConv;
173  mbHasStringRefSyntax = true;
174 }
175 
177 {
187 }
188 
190 {
191  return !operator==(r);
192 }
193 
195 {
196  OUStringBuffer result(256);
197  formula::FormulaCompiler aCompiler;
198  formula::FormulaCompiler::OpCodeMapPtr pOpCodeMap(aCompiler.GetOpCodeMap(css::sheet::FormulaLanguage::ENGLISH));
199 
200  for (auto i = rOpCodes->begin(); i != rOpCodes->end(); ++i)
201  {
202  if (i != rOpCodes->begin())
203  result.append(';');
204  result.append(pOpCodeMap->getSymbol(*i));
205  }
206 
207  return result.toString();
208 }
209 
211 {
212  ScCalcConfig::OpCodeSet result = std::make_shared<o3tl::sorted_vector< OpCode >>();
213  formula::FormulaCompiler aCompiler;
214  formula::FormulaCompiler::OpCodeMapPtr pOpCodeMap(aCompiler.GetOpCodeMap(css::sheet::FormulaLanguage::ENGLISH));
215 
216  const formula::OpCodeHashMap& rHashMap(pOpCodeMap->getHashMap());
217 
218  sal_Int32 fromIndex(0);
219  sal_Int32 semicolon;
220  OUString s(rOpCodes + ";");
221 
222  while ((semicolon = s.indexOf(';', fromIndex)) >= 0)
223  {
224  if (semicolon > fromIndex)
225  {
226  OUString element(s.copy(fromIndex, semicolon - fromIndex));
227  sal_Int32 n = element.toInt32();
228  if (n > 0 || (n == 0 && element == "0"))
229  result->insert(static_cast<OpCode>(n));
230  else
231  {
232  auto opcode(rHashMap.find(element));
233  if (opcode != rHashMap.end())
234  result->insert(opcode->second);
235  else
236  SAL_WARN("sc.opencl", "Unrecognized OpCode " << element << " in OpCode set string");
237  }
238  }
239  fromIndex = semicolon+1;
240  }
241  // HACK: Both unary and binary minus have the same string but different opcodes.
242  if( result->find( ocSub ) != result->end())
243  result->insert( ocNegSub );
244  return result;
245 }
246 
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ocSqrt
void setOpenCLConfigToDefault()
Definition: calcconfig.cxx:107
static rtl::Reference< ConfigurationListener > const & getMiscListener()
Definition: calcconfig.cxx:30
static rtl::Reference< ConfigurationListener > const & getFormulaCalculationListener()
Definition: calcconfig.cxx:36
ocVar
ocCos
OUString maOpenCLDevice
Definition: calcconfig.hxx:64
sal_Int64 n
ocExp
ocProduct
static ForceCalculationType getForceCalculationType()
Definition: calcconfig.cxx:68
ocPearson
OUString ScOpCodeSetToSymbolicString(const ScCalcConfig::OpCodeSet &rOpCodes)
Definition: calcconfig.cxx:194
ocCount
ocMin
ocNegSub
ocRound
ScCalcConfig::OpCodeSet ScStringToOpCodeSet(const OUString &rOpCodes)
Definition: calcconfig.cxx:210
static bool IsFuzzing()
ocSumProduct
ocSNormInv
std::shared_ptr< o3tl::sorted_vector< OpCode > > OpCodeSet
Definition: calcconfig.hxx:67
bool mbOpenCLSubsetOnly
Definition: calcconfig.hxx:62
StringConversion meStringConversion
Definition: calcconfig.hxx:54
ocCorrel
OpCodeMapPtr GetOpCodeMap(const sal_Int32 nLanguage) const
void MergeDocumentSpecific(const ScCalcConfig &r)
Definition: calcconfig.cxx:160
ocSumIfs
ocPower
int i
OpCodeSet mpOpenCLSubsetOpCodes
Definition: calcconfig.hxx:69
ocMul
ocSub
ocSlope
bool operator!=(const ScCalcConfig &r) const
Definition: calcconfig.cxx:189
ocMax
ocSin
ocVLookup
formula::FormulaGrammar::AddressConvention meStringRefAddressSyntax
Definition: calcconfig.hxx:53
bool mbOpenCLAutoSelect
Definition: calcconfig.hxx:63
static bool isOpenCLEnabled()
Definition: calcconfig.cxx:74
Configuration options for formula interpreter.
Definition: calcconfig.hxx:43
sal_Int32 mnOpenCLMinimumFormulaGroupSize
Definition: calcconfig.hxx:65
#define LOCALE
Definition: vbaformat.cxx:57
ocAdd
bool operator==(const ScCalcConfig &r) const
Definition: calcconfig.cxx:176
ocArcTan
ocAverage
bool mbHasStringRefSyntax
Definition: calcconfig.hxx:56
ocRandom
#define SAL_INFO(area, stream)
ocLn
ocStdNormDist
ocDiv
ForceCalculationType
Definition: calcconfig.hxx:32
ocPow
Any result
ResultType type
#define SAL_WARN(area, stream)
ocNormDist
std::shared_ptr< const OpCodeMap > OpCodeMapPtr
static ForceCalculationType forceCalculationTypeInit()
Definition: calcconfig.cxx:42
static bool isThreadingEnabled()
Definition: calcconfig.cxx:85
ocCovar
bool mbEmptyStringAsZero
Definition: calcconfig.hxx:55
ocTan
void SetStringRefSyntax(formula::FormulaGrammar::AddressConvention eConv)
Definition: calcconfig.cxx:170
ocSum
std::unordered_map< OUString, OpCode > OpCodeHashMap