Coverage Report

Created: 2026-03-12 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libetonyek/src/lib/contexts/IWORKFormulaElement.cpp
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/*
3
 * This file is part of the libetonyek 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 "IWORKFormulaElement.h"
11
12
#include "libetonyek_xml.h"
13
#include "IWORKDictionary.h"
14
#include "IWORKFormula.h"
15
#include "IWORKRefContext.h"
16
#include "IWORKStringElement.h"
17
#include "IWORKToken.h"
18
#include "IWORKTypes.h"
19
20
namespace libetonyek
21
{
22
23
namespace
24
{
25
class FmElement : public IWORKXMLEmptyContextBase
26
{
27
public:
28
  explicit FmElement(IWORKXMLParserState &state);
29
30
private:
31
  IWORKXMLContextPtr_t element(int name) override;
32
};
33
34
FmElement::FmElement(IWORKXMLParserState &state)
35
0
  : IWORKXMLEmptyContextBase(state)
36
0
{
37
0
}
38
39
IWORKXMLContextPtr_t FmElement::element(int /*name*/)
40
0
{
41
  // TODO: sfa:pair as child
42
0
  static bool first=true;
43
0
  if (first)
44
0
  {
45
0
    ETONYEK_DEBUG_MSG(("FmElement::element: found some elements, ignored\n"));
46
0
    first=false;
47
0
  }
48
49
0
  return IWORKXMLContextPtr_t();
50
0
}
51
}
52
53
namespace
54
{
55
class HostCellIdElement : public IWORKXMLEmptyContextBase
56
{
57
public:
58
  explicit HostCellIdElement(IWORKXMLParserState &state);
59
60
private:
61
  void attribute(int name, const char *value) override;
62
  void endOfElement() override;
63
64
  boost::optional<unsigned> m_col, m_row;
65
};
66
67
HostCellIdElement::HostCellIdElement(IWORKXMLParserState &state)
68
0
  : IWORKXMLEmptyContextBase(state)
69
0
  , m_col()
70
0
  , m_row()
71
0
{
72
0
}
73
74
void HostCellIdElement::attribute(const int name, const char *const value)
75
0
{
76
0
  switch (name)
77
0
  {
78
0
  case IWORKToken::column | IWORKToken::NS_URI_SF:
79
0
    m_col = int_cast(value);
80
0
    break;
81
0
  case IWORKToken::row | IWORKToken::NS_URI_SF:
82
0
    m_row = int_cast(value);
83
0
    break;
84
0
  default :
85
0
    IWORKXMLEmptyContextBase::attribute(name, value);
86
0
  }
87
0
}
88
89
void HostCellIdElement::endOfElement()
90
0
{
91
0
  if (!m_col || !m_row)
92
0
  {
93
0
    ETONYEK_DEBUG_MSG(("HostCellIdElement::endOfElement: can not find cell position\n"));
94
0
    return;
95
0
  }
96
0
  getState().m_tableData->m_formulaHC = 256*get(m_row)+get(m_col);
97
0
}
98
}
99
100
namespace
101
{
102
class CellAddressElement : public IWORKXMLEmptyContextBase
103
{
104
public:
105
  explicit CellAddressElement(IWORKXMLParserState &state);
106
107
private:
108
  void attribute(int name, const char *value) override;
109
  void endOfElement() override;
110
111
  boost::optional<unsigned> m_x, m_y;
112
};
113
114
CellAddressElement::CellAddressElement(IWORKXMLParserState &state)
115
0
  : IWORKXMLEmptyContextBase(state)
116
0
  , m_x()
117
0
  , m_y()
118
0
{
119
0
}
120
121
void CellAddressElement::attribute(const int name, const char *const value)
122
0
{
123
0
  switch (name)
124
0
  {
125
0
  case IWORKToken::x_coordinate | IWORKToken::NS_URI_SF:
126
0
    m_x = int_cast(value);
127
0
    break;
128
0
  case IWORKToken::y_coordinate | IWORKToken::NS_URI_SF:
129
0
    m_y = int_cast(value);
130
0
    break;
131
0
  default :
132
0
    IWORKXMLEmptyContextBase::attribute(name, value);
133
0
  }
134
0
}
135
136
void CellAddressElement::endOfElement()
137
0
{
138
0
  if (!m_x || !m_y)
139
0
  {
140
0
    ETONYEK_DEBUG_MSG(("CellAddressElement::endOfElement: can not find cell position\n"));
141
0
    return;
142
0
  }
143
0
  getState().m_tableData->m_formulaHC = 256*get(m_y)+get(m_x);
144
0
}
145
}
146
147
// FoElement
148
IWORKFoElement::IWORKFoElement(IWORKXMLParserState &state)
149
0
  : IWORKXMLEmptyContextBase(state)
150
0
  , m_id()
151
0
  , m_formula()
152
0
  , m_hc()
153
0
{
154
0
}
155
156
const boost::optional<ID_t> &IWORKFoElement::getId() const
157
0
{
158
0
  return m_id;
159
0
}
160
161
void IWORKFoElement::attribute(const int name, const char *const value)
162
0
{
163
0
  switch (name)
164
0
  {
165
0
  case IWORKToken::NS_URI_SFA | IWORKToken::ID:
166
0
    m_id = value;
167
0
    break;
168
0
  case IWORKToken::fs | IWORKToken::NS_URI_SF :
169
0
    m_formula=value;
170
0
    break;
171
0
  case IWORKToken::hc | IWORKToken::NS_URI_SF :
172
0
    m_hc=int_cast(value);
173
0
    break;
174
0
  case IWORKToken::ht | IWORKToken::NS_URI_SF : // big string
175
0
    break;
176
0
  default :
177
0
    IWORKXMLEmptyContextBase::attribute(name, value);
178
0
    break;
179
0
  }
180
0
}
181
182
IWORKXMLContextPtr_t IWORKFoElement::element(int name)
183
0
{
184
0
  switch (name)
185
0
  {
186
0
  case IWORKToken::fm | IWORKToken::NS_URI_SF :
187
0
    return std::make_shared<FmElement>(getState());
188
0
    break;
189
0
  default:
190
0
    break;
191
0
  }
192
193
0
  ETONYEK_DEBUG_MSG(("IWORKFoElement::element: find unknown element %d\n", name));
194
0
  return IWORKXMLContextPtr_t();
195
0
}
196
197
void IWORKFoElement::endOfElement()
198
0
{
199
0
  if (!m_formula) return;
200
201
0
  IWORKFormulaPtr_t formula(new IWORKFormula(m_hc));
202
0
  if (!formula->parse(get(m_formula)))
203
0
    return;
204
0
  getState().m_tableData->m_formula = formula;
205
0
  getState().m_tableData->m_formulaHC = m_hc;
206
0
  if (getId())
207
0
    getState().getDictionary().m_formulas[get(getId())]=formula;
208
0
}
209
210
// OfElement
211
IWORKOfElement::IWORKOfElement(IWORKXMLParserState &state)
212
0
  : IWORKXMLEmptyContextBase(state)
213
0
  , m_ref()
214
0
  , m_hc()
215
0
{
216
0
}
217
218
void IWORKOfElement::attribute(const int name, const char *const value)
219
0
{
220
0
  switch (name)
221
0
  {
222
0
  case IWORKToken::hc | IWORKToken::NS_URI_SF :
223
0
    m_hc=int_cast(value);
224
0
    break;
225
0
  default :
226
0
    IWORKXMLEmptyContextBase::attribute(name, value);
227
0
    break;
228
0
  }
229
0
}
230
231
IWORKXMLContextPtr_t IWORKOfElement::element(int name)
232
0
{
233
0
  switch (name)
234
0
  {
235
0
  case IWORKToken::fm | IWORKToken::NS_URI_SF :
236
0
    return std::make_shared<FmElement>(getState());
237
0
    break;
238
0
  case IWORKToken::mf_ref | IWORKToken::NS_URI_SF :
239
0
    return std::make_shared<IWORKRefContext>(getState(), m_ref);
240
0
  default:
241
0
    break;
242
0
  }
243
244
0
  ETONYEK_DEBUG_MSG(("IWORKOfElement::element: find unknown element %d\n", name));
245
0
  return IWORKXMLContextPtr_t();
246
0
}
247
248
void IWORKOfElement::endOfElement()
249
0
{
250
0
  if (!m_ref)
251
0
  {
252
0
    ETONYEK_DEBUG_MSG(("IWORKOfElement::endOfElement: can not find the ref\n"));
253
0
    return;
254
0
  }
255
0
  const IWORKFormulaMap_t::const_iterator it = getState().getDictionary().m_formulas.find(get(m_ref));
256
0
  if (it==getState().getDictionary().m_formulas.end())
257
0
  {
258
0
    ETONYEK_DEBUG_MSG(("IWORKOfElement::endOfElement: can not find the ref %s\n", get(m_ref).c_str()));
259
0
    return;
260
0
  }
261
0
  getState().m_tableData->m_formula = it->second;
262
0
  getState().m_tableData->m_formulaHC = m_hc;
263
0
}
264
265
// formula
266
IWORKFormulaElement::IWORKFormulaElement(IWORKXMLParserState &state)
267
0
  : IWORKXMLEmptyContextBase(state)
268
0
  , m_id()
269
0
  , m_formula()
270
0
  , m_hc()
271
0
{
272
0
}
273
274
const boost::optional<ID_t> &IWORKFormulaElement::getId() const
275
0
{
276
0
  return m_id;
277
0
}
278
279
void IWORKFormulaElement::attribute(const int name, const char *const value)
280
0
{
281
0
  switch (name)
282
0
  {
283
0
  case IWORKToken::NS_URI_SFA | IWORKToken::ID:
284
0
    m_id = value;
285
0
    break;
286
0
  default :
287
0
    IWORKXMLEmptyContextBase::attribute(name, value);
288
0
    break;
289
0
  }
290
0
}
291
292
IWORKXMLContextPtr_t IWORKFormulaElement::element(int name)
293
0
{
294
0
  switch (name)
295
0
  {
296
0
  case IWORKToken::formula_string | IWORKToken::NS_URI_SF :
297
0
    return std::make_shared<IWORKStringElement>(getState(), m_formula);
298
0
  case IWORKToken::host_cell_ID | IWORKToken::NS_URI_SF :
299
0
    return std::make_shared<HostCellIdElement>(getState());
300
0
  case IWORKToken::host_table_ID | IWORKToken::NS_URI_SF :
301
0
    break;
302
0
  default:
303
0
    ETONYEK_DEBUG_MSG(("IWORKFormulaElement::element: find unknown element %d\n", name));
304
0
  }
305
306
0
  return IWORKXMLContextPtr_t();
307
0
}
308
309
void IWORKFormulaElement::endOfElement()
310
0
{
311
0
  if (!m_formula) return;
312
313
0
  IWORKFormulaPtr_t formula(new IWORKFormula(getState().m_tableData->m_formulaHC));
314
0
  if (!formula->parse(get(m_formula)))
315
0
    return;
316
0
  getState().m_tableData->m_formula = formula;
317
0
  if (getId())
318
0
    getState().getDictionary().m_formulas[get(getId())]=formula;
319
0
}
320
321
// TableCellFormula
322
IWORKTableCellFormulaElement::IWORKTableCellFormulaElement(IWORKXMLParserState &state)
323
0
  : IWORKXMLEmptyContextBase(state)
324
0
  , m_id()
325
0
  , m_formula()
326
0
  , m_tableId()
327
0
{
328
0
}
329
330
const boost::optional<ID_t> &IWORKTableCellFormulaElement::getId() const
331
0
{
332
0
  return m_id;
333
0
}
334
335
void IWORKTableCellFormulaElement::attribute(const int name, const char *const value)
336
0
{
337
0
  switch (name)
338
0
  {
339
0
  case IWORKToken::NS_URI_SFA | IWORKToken::ID:
340
0
    m_id = value;
341
0
    break;
342
0
  default :
343
0
    IWORKXMLEmptyContextBase::attribute(name, value);
344
0
    break;
345
0
  }
346
0
}
347
348
IWORKXMLContextPtr_t IWORKTableCellFormulaElement::element(int name)
349
0
{
350
0
  switch (name)
351
0
  {
352
0
  case IWORKToken::formula_string | IWORKToken::NS_URI_SF :
353
0
    return std::make_shared<IWORKStringElement>(getState(), m_formula);
354
0
  case IWORKToken::cell_address | IWORKToken::NS_URI_SF :
355
0
    return std::make_shared<CellAddressElement>(getState());
356
0
  case IWORKToken::host_table_ID | IWORKToken::NS_URI_SF :
357
0
    return std::make_shared<IWORKStringElement>(getState(), m_tableId);
358
0
  default:
359
0
    ETONYEK_DEBUG_MSG(("IWORKTableCellFormulaElement::element: find unknown element %d\n", name));
360
0
  }
361
362
0
  return IWORKXMLContextPtr_t();
363
0
}
364
365
void IWORKTableCellFormulaElement::endOfElement()
366
0
{
367
0
  if (!m_formula) return;
368
369
0
  IWORKFormulaPtr_t formula(new IWORKFormula(getState().m_tableData->m_formulaHC));
370
0
  if (!formula->parse(get(m_formula)))
371
0
    return;
372
0
  getState().m_tableData->m_formula = formula;
373
0
  if (getId())
374
0
    getState().getDictionary().m_formulas[get(getId())]=formula;
375
0
}
376
}
377
378
/* vim:set shiftwidth=2 softtabstop=2 expandtab: */