admin
2023-03-07 8b06b1cbf112d55307ea8a6efe711db4e7506d89
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * This file is part of xlslib -- A multiplatform, C/C++ library
 * for dynamic generation of Excel(TM) files.
 *
 * Copyright 2010-2013 Ger Hobbelt All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY David Hoerl ''AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David Hoerl OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
#include "../xlslib/record.h"
#include "../xlslib/formula.h"
#include "../xlslib/globalrec.h"
#include "../xlslib/datast.h"
#include "../xlslib/cell.h"
#include "../xlslib/rectypes.h"
#include "../xlslib/formula_cell.h"
#include "../xlslib/formula_expr.h"
#include "../xlslib/formula_estimate.h"
 
#ifdef __BCPLUSPLUS__
#include <malloc.h>
// malloc.h needed for calloc. RLN 111208
#include <memory.h>
// memory.h needed for memset. RLN 111215
// These may be needed for other compilers as well.
#endif
 
using namespace xlslib_core;
using namespace xlslib_strings;
 
/* For information on the XLS binary format, see
 * http://msdn.microsoft.com/en-us/library/cc313154(v=office.12).aspx
 *
 * The section on formulas is entitled "Microsoft Excel Formulas"
 */
 
formula_cell_t::formula_cell_t(CGlobalRecords& gRecords, unsigned32_t rowval, unsigned32_t colval, expression_node_t* ast_val, bool a_formula, bool autodes, xf_t* pxfval) :
    cell_t(gRecords, rowval, colval, pxfval),
    ast(ast_val),
    auto_destruct_expression_tree(autodes),
    array_formula(a_formula),
    stack(NULL)
{
    XL_ASSERT(ast_val);
 
#if !defined (HAVE_PRAGMA_PACK) && !defined (HAVE_PRAGMA_PACK_PUSH_POP)
    XL_ASSERTS("Must Have Pragma Pack to use formulas");
#endif
}
 
formula_cell_t::formula_cell_t(CGlobalRecords& gRecords, unsigned32_t rowval, unsigned32_t colval, 
        formula_t *stack_val, bool a_formula, xf_t* pxfval) :
    cell_t(gRecords, rowval, colval, pxfval),
    ast(NULL),
    auto_destruct_expression_tree(true),
    array_formula(a_formula),
    stack(stack_val)
{
    XL_ASSERT(stack_val);
 
#if !defined (HAVE_PRAGMA_PACK) && !defined (HAVE_PRAGMA_PACK_PUSH_POP)
    XL_ASSERTS("Must Have Pragma Pack to use formulas");
#endif
}
 
formula_cell_t::~formula_cell_t()
{
    if (ast && auto_destruct_expression_tree) {
        ast->DestroyAST();
    }
}
 
void formula_cell_t::GetResultEstimate(estimated_formula_result_t &dst) const
{
    if (ast) {
        ast->GetResultEstimate(dst);
    } else if (stack) {
        stack->GetResultEstimate(dst);
    }
}
 
size_t formula_cell_t::GetSize(void) const
{
    estimated_formula_result_t estimate(m_GlobalRecords);
    const expression_node_t *expr = GetAST();
    XL_ASSERT(expr != NULL || stack != NULL);
    size_t len = 4+2+2+2+8+2+4+2;
    if (expr) {
        len += expr->GetSize();
        //printf("ESize: %ld\n", expr->GetSize());
    } else if (stack) {
        len += stack->GetSize();
        //printf("SSize: %ld\n", stack->GetSize());
    }
    GetResultEstimate(estimate);
    // TODO: Estimate size does not seem to work very well - quite low
    if (estimate.EncodedValueIsString()) {
        // FORMULA BIFF8 is immediately followed by a STRING BIFF8 record!
        const u16string* str = estimate.GetStringValue();
 
        XL_ASSERT(str);
        len += 4 + str->length() * (CGlobalRecords::IsASCII(*str) ? sizeof(unsigned8_t) : sizeof(unsigned16_t));
    }
    //printf("Size: %ld\n", len);
    return len;
}
 
CUnit* formula_cell_t::GetData(CDataStorage &datastore) const
{
    return datastore.MakeCFormula(*this);   // NOTE: this pointer HAS to be deleted elsewhere.
}
 
void formula_cell_t::DumpData(CUnit &dst) const
{
    if (ast) {
        formula_t *fs = new formula_t(m_GlobalRecords, this->GetWorksheet());
        ast->DumpData(*fs, true); // rgce dump, length_of_parsed_expr
        fs->DumpData(dst);
        delete fs;
    } else if (stack) {
        stack->DumpData(dst);
    }
}
 
/*
 *********************************
 *  CFormula class implementation
 *********************************
 */
 
CFormula::CFormula(CDataStorage &datastore, const formula_cell_t& expr) :
    CRecord(datastore)
{
    size_t basepos = 0;
 
    SetRecordType(RECTYPE_FORMULA);  // followed by the RECTYPE_STRING record when the formula evaluates to a string!
    AddValue16((unsigned16_t)expr.GetRow());
    AddValue16((unsigned16_t)expr.GetCol());
    AddValue16(expr.GetXFIndex());
 
    estimated_formula_result_t estimate(expr.GetGlobalRecords());
    expr.GetResultEstimate(estimate);
    AddValue64(estimate.GetEncodedValue());    // current_value_of_formula
    AddValue16(estimate.GetOptionFlags());    // flags: grbit
 
    AddValue32(0); // chn
 
    if(expr.IsArrayFormula()) {
        AddValue16(1+2+2);                            // len
        AddValue8(1);                                // tExpr
        AddValue16((unsigned16_t)expr.GetRow());
        AddValue16((unsigned16_t)expr.GetCol());
        SetRecordLength(GetDataSize()-RECORD_HEADER_SIZE);
 
        // fake it by appending it at the tail of the current record!
        basepos = GetDataSize();
 
        AddValue16(RECTYPE_ARRAY);  // followed by the RECTYPE_STRING record when the formula evaluates to a string!
        AddValue16(0);                // placeholder for len
 
        AddValue16((unsigned16_t)expr.GetRow());
        AddValue16((unsigned16_t)expr.GetRow());
        AddValue8((unsigned8_t)expr.GetCol());
        AddValue8((unsigned8_t)expr.GetCol());
        AddValue16(estimate.GetOptionFlags()); // flags: grbit
 
        AddValue32(0); // chn
    }
 
    size_t len_position = GetDataSize();
    AddValue16(0 /* expr.GetSize() */ ); // length_of_parsed_expr
    
    expr.DumpData(*this);
    size_t end = GetDataSize();
    SetValueAt16((unsigned16_t)(end - len_position - 2), (unsigned)len_position);    // go back and set real value for token length
 
    SetValueAt16((unsigned16_t)(GetDataSize() - basepos - RECORD_HEADER_SIZE), (unsigned)basepos + 2);    // SetRecordLength on either FORMULA or the ARRAY
 
    if (estimate.EncodedValueIsString()) {
        // FORMULA BIFF8 is immediately followed by a STRING BIFF8 record!
        //
        // fake it by appending it at the tail of the current record!
        basepos = GetDataSize();
 
        AddValue16(RECTYPE_STRING);
        AddValue16(0);                // placeholder for len
 
        const u16string* str = estimate.GetStringValue();
 
        XL_ASSERT(str);
        XL_ASSERT(str->length() < 256); // dbg
        AddUnicodeString(*str, LEN2_FLAGS_UNICODE);
 
        SetValueAt16((unsigned16_t)(GetDataSize() - basepos - RECORD_HEADER_SIZE), (unsigned)basepos + 2); // SetRecordLength
    }
}
 
CFormula::~CFormula()
{
}