-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXlUnitSupport.cpp
More file actions
454 lines (363 loc) · 12.2 KB
/
XlUnitSupport.cpp
File metadata and controls
454 lines (363 loc) · 12.2 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#define _CRT_SECURE_NO_WARNINGS
#include "ExcelUtils.h"
#include "ExcelUtilsX.h"
#include "XlUnitSupport.h"
#include "WinUtils.h"
#include "Converter.h"
#include "Oleacc.h"
#include "comutil.h"
#include "XLCALL.H"
namespace XlUnitSupport {
struct VariantHolder {
VARIANT v;
bool initialized = false;
VariantHolder(bool init = true) {
VariantInit(&this->v);
this->initialized = true;
}
~VariantHolder() {
if (this->initialized) VariantClear(&this->v);
}
};
bool IsNumber(ExcelUtils::VariantString & vs) {
if (vs.IsError()) return false;
if (vs.AvailableString() && ::IsNumber(vs.GetString())) return true;
return false;
}
template <bool strict, bool ignoreFromCellAndGetCallerRange = false, int N>
VARIANT GetUnitFromInputs(char(&c)[N], VARIANT & vFromCell, VARIANT * vUnit = nullptr) {
VariantHolder tmp(false);
VARIANT * vPtr = nullptr;
ExcelUtils::VariantString vs_unit;
bool cond = false;
if (!vUnit) {
cond = true;
} else {
if (vUnit->vt == VT_DISPATCH) {
tmp.v = ExcelUtils::GetDispatchProperty<ExcelUtils::g_valueName>(*vUnit);
tmp.initialized = true;
vPtr = &tmp.v;
} else {
vPtr = vUnit;
}
if (strict) {
cond = ExcelUtils::IsMissing(*vPtr);
} else {
cond = ExcelUtils::IsError(*vPtr) || ExcelUtils::IsNumeric(*vPtr);
}
}
const wchar_t * vs_unit_ptr;
if (cond) {
VARIANT numFormat;
if (ignoreFromCellAndGetCallerRange) {
auto callerRange = GetCallerRange();
numFormat = ExcelUtils::GetRangeNumberFormat(callerRange);
VariantClear(&callerRange);
} else {
if ((vFromCell.vt & VT_TYPEMASK) == VT_DISPATCH) {
numFormat = ExcelUtils::GetRangeNumberFormat(vFromCell);
} else {
numFormat.vt = VT_BSTR;
numFormat.bstrVal = NULL;
}
}
if (numFormat.vt == VT_BSTR) {
WCharPointer str = numFormat.bstrVal;
//RetrieveStr<L'"', L' '>(str);
RetrieveStr<L'"'>(str);
WCharPointer str2 = RetrieveStr<L'"', L' '>(str);
str2.Trim();
vs_unit_ptr = str2;
vs_unit.TakeOwnership(numFormat);
} else if (numFormat.vt == VT_NULL) {
vs_unit_ptr = nullptr;
} else {
VariantClear(&numFormat);
if (ExcelUtils::IsError(numFormat)) return numFormat;
return ExcelUtils::MakeVariant_Error(VARIANT_ERROR_NA);
}
} else {
vs_unit = *vPtr;
if (vs_unit.AvailableString()) {
vs_unit_ptr = vs_unit.GetString();
} else {
if (vs_unit.IsError()) return vs_unit.GetVariant();
return ExcelUtils::MakeVariant_Error(VARIANT_ERROR_NA);
}
}
if (vs_unit_ptr) sprintf_s(c, "%S", vs_unit_ptr);
else c[0] = 0;
return ExcelUtils::MakeVariant_Empty();
}
LPDISPATCH GetXlAppPtr() {
static bool done;
static VariantHolder xlAppGuard;
LPDISPATCH retVal = nullptr;
if (done) {
retVal = xlAppGuard.v.pdispVal;
} else {
XLOPER12 xRes;
auto r = Excel12(xlGetHwnd, &xRes, 0);
if (r == xlretSuccess) {
if (xRes.xltype == xltypeInt) {
auto h = (HWND)xRes.val.w;
auto hDesk = FindWindowExA(h, NULL, "XLDESK", NULL);
auto hXl = FindWindowExA(hDesk, NULL, "EXCEL7", NULL);
if (hXl) {
LPDISPATCH xlPtr;
auto ridd = __uuidof(IDispatch);
auto hr = AccessibleObjectFromWindow(hXl, OBJID_NATIVEOM, ridd, (void**)&xlPtr);
if (SUCCEEDED(hr)) {
BSTR name = L"Application";
DISPID dispid;
DISPPARAMS dispparamsNoArgs = { NULL, NULL, 0, 0 };
hr = xlPtr->GetIDsOfNames(IID_NULL, &name, 1, NULL, &dispid);
if (SUCCEEDED(hr)) {
auto&& xlApp = xlAppGuard.v;
hr = xlPtr->Invoke(dispid, IID_NULL, NULL, DISPATCH_PROPERTYGET, &dispparamsNoArgs, &xlApp, NULL, NULL);
if (SUCCEEDED(hr)) {
if (xlApp.vt == VT_DISPATCH) {
done = true;
retVal = xlApp.pdispVal;
}
}
}
xlPtr->Release();
}
}
Excel12(xlFree, 0, 1, &xRes);
}
}
}
return retVal;
}
VARIANT GetCallerRange() {
VARIANT retVal;
VariantInit(&retVal);
if (auto xlApp = GetXlAppPtr()) {
static DISPID dispid;
static bool once;
static HRESULT once_hr;
BSTR name = L"Caller";
if (!once || FAILED(once_hr)) {
once = true;
once_hr = xlApp->GetIDsOfNames(IID_NULL, &name, 1, NULL, &dispid);
}
if (SUCCEEDED(once_hr)) {
VARIANT xlRange;
VariantInit(&xlRange);
DISPPARAMS dispparamsNoArgs = { NULL, NULL, 0, 0 };
auto hr = xlApp->Invoke(dispid, IID_NULL, NULL, DISPATCH_PROPERTYGET, &dispparamsNoArgs, &xlRange, NULL, NULL);
if (SUCCEEDED(hr)) {
if (xlRange.vt == VT_DISPATCH) {
retVal = xlRange;
}
}
}
}
return retVal;
}
VARIANT ConvertFromSI(VARIANT & v, VARIANT & v_To, bool strict) {
VARIANT vConv = ExcelUtils::ConvertToDoubleOrErrorOrStringError(v);
if (!ExcelUtils::IsDouble(vConv)) return vConv;
auto val = vConv.dblVal;
char unit[500];
auto ok = GetUnitFromInputs<true, true>(unit, v, &v_To);
if (ExcelUtils::IsError(ok)) return ok;
if (unit[0] == 0) return ExcelUtils::MakeVariant_Double(val);
static UnitConverter converter;
auto convertedValue = converter.To(unit, strict).FromSI().Convert(val);
if (auto error = converter.GetError()) {
return ExcelUtils::MakeVariant_String(xFormat(L"# %S #", error));
} else {
return ExcelUtils::MakeVariant_Double(convertedValue);
}
}
VARIANT ConvertToSI(VARIANT & v, VARIANT & v_From, bool strict) {
VARIANT vConv = ExcelUtils::ConvertToDoubleOrErrorOrStringError(v);
if (vConv.vt != VT_R8) return vConv;
auto val = vConv.dblVal;
char unit[500];
auto ok = GetUnitFromInputs<true>(unit, v, &v_From);
if (ExcelUtils::IsError(ok)) return ok;
if (unit[0] == 0) return ExcelUtils::MakeVariant_Double(val);
static UnitConverter converter;
auto convertedValue = converter.From(unit, strict != 0).ToSI().Convert(val);
if (auto error = converter.GetError()) {
return ExcelUtils::MakeVariant_String(xFormat(L"# %S #", error));
} else {
return ExcelUtils::MakeVariant_Double(convertedValue);
}
}
VARIANT GetType(VARIANT & v_Unit) {
char unit[500];
auto ok = GetUnitFromInputs<false>(unit, v_Unit, &v_Unit);
if (ExcelUtils::IsError(ok)) return ok;
static UnitConverterCore converter;
auto pv = converter.ToSI(ValueAndUnit(1, unit));
if (pv.error) {
return ExcelUtils::MakeVariant_String(L"# unrecognized unit #");
} else {
DimensionTypesEx dimTypes;
auto dimInfo = dimTypes.GetDimensionName(pv.dim);
if (dimInfo.type) {
return ExcelUtils::MakeVariant_String(xFormat(L"%S", dimInfo.type));
} else {
return ExcelUtils::MakeVariant_String(nullptr);
}
}
}
VARIANT GetInterpretation(VARIANT & v_Unit) {
char unit[500];
auto ok = GetUnitFromInputs<false>(unit, v_Unit, &v_Unit);
if (ExcelUtils::IsError(ok)) return ok;
static UnitConverterCore converter;
auto pv = converter.ToSI(ValueAndUnit(1, unit));
if (pv.error) {
return ExcelUtils::MakeVariant_String(L"# unrecognized unit #");
} else {
return ExcelUtils::MakeVariant_String(xFormat(L"%S", pv.orig_unitStrFullNamePlural));
}
}
VARIANT GetDimensions(VARIANT & v_Unit) {
char unit[500];
auto ok = GetUnitFromInputs<false>(unit, v_Unit, &v_Unit);
if (ExcelUtils::IsError(ok)) return ok;
static UnitConverterCore converter;
auto pv = converter.ToSI(ValueAndUnit(1, unit));
if (pv.error) {
return ExcelUtils::MakeVariant_String(L"# unrecognized unit #");
} else {
return ExcelUtils::MakeVariant_String(xFormat(L"%S", pv.dim.ToString()));
}
}
VARIANT Convert(
VARIANT & v,
VARIANT & v_From,
VARIANT & v_To,
bool strict,
Convert_MissingBehavior missingBehavior_from,
Convert_MissingBehavior missingBehavior_to)
{
VARIANT vConv = ExcelUtils::ConvertToDoubleOrErrorOrStringError(v);
if (vConv.vt != VT_R8) return vConv;
auto val = vConv.dblVal;
VARIANT ok;
char unit_from[500];
ok = GetUnitFromInputs<true>(unit_from, v, &v_From);
if (ExcelUtils::IsError(ok)) return ok;
char unit_to[500];
ok = GetUnitFromInputs<true, true>(unit_to, v, &v_To);
if (ExcelUtils::IsError(ok)) return ok;
static UnitConverter converter;
double convertedValue;
bool missing_to = unit_to[0] == 0;
bool missing_from = unit_from[0] == 0;
if (!missing_to && !missing_from) {
do_full_conversion:
convertedValue = converter.SetUnits(unit_from, unit_to, strict != 0).Convert(val);
} else {
if (missing_to && missing_from) {
/* no conversion */
return ExcelUtils::MakeVariant_Double(val);
} else if (missing_to) {
if (missingBehavior_to == XlUnitSupport::missing_is_adimensional) {
goto do_full_conversion;
} else if (missingBehavior_to == XlUnitSupport::missing_is_SI) {
convertedValue = converter.From(unit_from, strict != 0).ToSI().Convert(val);
} else if (missingBehavior_to == XlUnitSupport::missing_is_specified_unit) {
/* no conversion */
return ExcelUtils::MakeVariant_Double(val);
} else {
MY_ASSERT(false);
}
} else if (missing_from) {
if (missingBehavior_from == XlUnitSupport::missing_is_adimensional) {
goto do_full_conversion;
} else if (missingBehavior_from == XlUnitSupport::missing_is_SI) {
convertedValue = converter.To(unit_to, strict).FromSI().Convert(val);
} else if (missingBehavior_from == XlUnitSupport::missing_is_specified_unit) {
/* no conversion */
return ExcelUtils::MakeVariant_Double(val);
} else {
MY_ASSERT(false);
}
} else {
MY_ASSERT(false);
}
}
if (auto error = converter.GetError()) {
return ExcelUtils::MakeVariant_String(xFormat(L"%S", error));
} else {
return ExcelUtils::MakeVariant_Double(convertedValue);
}
}
/* must clear retVal if not returned */
VARIANT ConvertCheck(VARIANT & retVal, const wchar_t * unit, bool noThrow) {
if (ExcelUtils::IsString(retVal)) {
if (retVal.bstrVal && retVal.bstrVal[0] == L'#') {
if (noThrow) {
return retVal;
}
else {
auto ex = std::runtime_error(xFormat("%S", retVal.bstrVal));
VariantClear(&retVal);
throw ex;
}
}
auto type = XlUnitSupport::GetType(ExcelUtils::MakeVariant_FakeString(unit));
if (noThrow) {
auto retVal2 = ExcelUtils::MakeVariant_String((const wchar_t*)xFormat(L"# Wrong unit for %s: %s #", type.bstrVal, retVal.bstrVal));
VariantClear(&type);
VariantClear(&retVal);
return retVal2;
}
else {
auto ex = std::runtime_error(xFormat("Wrong unit for %S: %S", type.bstrVal, retVal.bstrVal));
VariantClear(&type);
VariantClear(&retVal);
throw ex;
}
}
if (!ExcelUtils::IsDouble(retVal)) {
auto type = XlUnitSupport::GetType(ExcelUtils::MakeVariant_FakeString(unit));
if (noThrow) {
auto retVal2 = ExcelUtils::MakeVariant_String((const wchar_t*)xFormat(L"# Wrong input for %s #", type.bstrVal));
VariantClear(&type);
VariantClear(&retVal);
return retVal2;
}
else {
auto ex = std::runtime_error(xFormat("Wrong input for %S", type.bstrVal));
VariantClear(&type);
VariantClear(&retVal);
throw ex;
}
}
return retVal;
}
VARIANT ConvertInputCore(VARIANT & v, const wchar_t * unit, bool noThrow) {
auto retVal = Convert(v, ExcelUtils::MakeVariant_Missing(), ExcelUtils::MakeVariant_FakeString(unit), false, XlUnitSupport::missing_is_SI);
return ConvertCheck(retVal, unit, noThrow);
}
//VARIANT ConvertOutputCore(VARIANT & v, const wchar_t * unit, bool noThrow) {
// auto retVal = XlUnitSupport::Convert(v, ExcelUtils::MakeVariant_FakeString(unit), ExcelUtils::MakeVariant_Missing(), false, XlUnitSupport::missing_is_adimensional, XlUnitSupport::missing_is_specified_unit);
// return ConvertCheck(retVal, unit, noThrow);
//}
double ConvertInput(VARIANT & v, const wchar_t * unit) {
return ConvertInputCore(v, unit, false).dblVal;
}
//double ConvertOutput(VARIANT & v, const wchar_t * unit) {
// return ConvertOutputCore(v, unit, false).dblVal;
//}
VARIANT ConvertInput_noThrow(VARIANT & v, const wchar_t * unit) {
return ConvertInputCore(v, unit, true);
}
//VARIANT ConvertOutput_noThrow(VARIANT & v, const wchar_t * unit) {
// return ConvertOutputCore(v, unit, true);
//}
VARIANT ConvertOutput(VARIANT & v, const wchar_t * unit) {
auto retVal = XlUnitSupport::Convert(v, ExcelUtils::MakeVariant_FakeString(unit), ExcelUtils::MakeVariant_Missing(), false, XlUnitSupport::missing_is_adimensional, XlUnitSupport::missing_is_specified_unit);
return ConvertCheck(retVal, unit, true);
}
}