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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * 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/formula_estimate.h"
#include "../xlslib/globalrec.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;
 
estimated_formula_result_t::estimated_formula_result_t(CGlobalRecords& gRecords) :
    value_type(ESTVAL_UNKNOWN),
    always_calc(0),
    calc_on_load(0),
    m_GlobalRecords(gRecords)
{
/*
 *   union
 *   {
 *       bool b;
 *       signed32_t i;
 *       double f;
 *       u16string *s;
 *       errcode_t e;
 *   } value;
 *   enum
 *   {
 *       ESTVAL_UNKNOWN = 0,
 *       ESTVAL_BOOLEAN,
 *       ESTVAL_INTEGER,
 *       ESTVAL_FLOATINGPOINT,
 *       ESTVAL_STRING,
 *       ESTVAL_ERRORCODE,
 *   } value_type;
 *
 *   bool always_calc : 1;
 *   bool calc_on_load: 1;
 */
    memset(&value, 0, sizeof(value));
}
 
estimated_formula_result_t::~estimated_formula_result_t()
{
    clear_value(ESTVAL_UNKNOWN);
}
 
estimated_formula_result_t& estimated_formula_result_t::operator =(const estimated_formula_result_t &src)
{
    (void)src; // stop warning
    throw std::string("Should never have invoked the estimated_formula_result_t copy operator!");
}
 
void estimated_formula_result_t::clear_value(estval_type_t incoming_type)
{
    if (value_type == ESTVAL_STRING && incoming_type != ESTVAL_STRING) {
        delete value.s;
    } else
    if (value_type != ESTVAL_STRING && incoming_type == ESTVAL_STRING) {
        value.s = new u16string;
    }
    value_type = incoming_type;
}
 
void estimated_formula_result_t::SetCalcOnLoad(void)
{
    calc_on_load = 1;
}
 
void estimated_formula_result_t::SetCalcAlways(void)
{
    always_calc = 1;
}
 
unsigned16_t estimated_formula_result_t::GetOptionFlags(void) const
{
    return (unsigned16_t)(calc_on_load << 1 | always_calc);
}
 
bool estimated_formula_result_t::SetBoolean(bool v)
{
    clear_value(ESTVAL_BOOLEAN);
    return value.b = v;
}
 
signed32_t estimated_formula_result_t::SetInteger(signed32_t v)
{
    clear_value(ESTVAL_INTEGER);
    return value.i = v;
}
 
double estimated_formula_result_t::SetFloatingPoint(double v)
{
    clear_value(ESTVAL_FLOATINGPOINT);
    return value.f = v;
}
 
const u16string& estimated_formula_result_t::SetText(const std::string& v)
{
    clear_value(ESTVAL_STRING);
    m_GlobalRecords.char2str16(v, *value.s);
    return *value.s;
}
 
const u16string& estimated_formula_result_t::SetText(const ustring& v)
{
    clear_value(ESTVAL_STRING);
    m_GlobalRecords.wide2str16(v, *value.s);
    return *value.s;
}
 
#ifndef __FRAMEWORK__
const u16string& estimated_formula_result_t::SetText(const u16string& v)
{
    clear_value(ESTVAL_STRING);
    return *value.s = v;
}
 
#endif
errcode_t estimated_formula_result_t::SetErrorCode(errcode_t v)
{
    clear_value(ESTVAL_ERRORCODE);
    return value.e = v;
}
 
unsigned64_t estimated_formula_result_t::GetEncodedValue(void) const
{
    unsigned64_t rv;
 
    switch (value_type)    {
    default:
    case ESTVAL_UNKNOWN:
        XL_ASSERTS("Should never get here!");
 
    case ESTVAL_BOOLEAN:
        rv = 0xFFFF000000000001ULL;
        rv |= ((unsigned32_t)value.b) << 16;
        break;
 
    case ESTVAL_INTEGER:
        rv = CUnit::EncodeFP2I64(value.i);
        break;
 
    case ESTVAL_FLOATINGPOINT:
        rv = CUnit::EncodeFP2I64(value.f);
        break;
 
    case ESTVAL_STRING:
        rv = 0xFFFF000000000000ULL;
        break;
 
    case ESTVAL_ERRORCODE:
        rv = 0xFFFF000000000002ULL;
        rv |= ((unsigned32_t)value.e) << 16;
        break;
    }
    return rv;
}
 
bool estimated_formula_result_t::EncodedValueIsString(void) const
{
    return value_type == ESTVAL_STRING;
}
 
const u16string* estimated_formula_result_t::GetStringValue(void) const
{
    if (value_type == ESTVAL_STRING) {
        return value.s;
    }
    return NULL;
}