-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFormat.cpp
232 lines (183 loc) · 5.57 KB
/
Format.cpp
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
////////////////////////////////////////////////////////////////////////////////
//! \file Format.cpp
//! \brief Helper functions for parsing and formatting WMI values.
//! \author Chris Oldwood
#include "Common.hpp"
#include "Format.hpp"
#include <Core/StringUtils.hpp>
#include <WMI/DateTime.hpp>
#include <WCL/VariantVector.hpp>
#include <Core/AnsiWide.hpp>
#include <malloc.h.>
static tstring formatIntegerValue(const WCL::Variant& value, bool applyFormatting);
////////////////////////////////////////////////////////////////////////////////
//! Get the string used to separate groups of digits in a number.
static tstring getGroupSeparator()
{
int count = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SMONTHOUSANDSEP, nullptr, 0);
tchar* buffer = static_cast<tchar*>(_alloca(Core::numBytes<tchar>(count+1)));
buffer[0] = TXT('\0');
if (::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SMONTHOUSANDSEP, buffer, count+1) == 0)
return TXT(",");
return buffer;
}
////////////////////////////////////////////////////////////////////////////////
//! Try and convert a string into a datetime. The format of a WMI datetime is:-
//! YYYYMMDDHHMMSS.FFFFFF+TZO e.g. 20101008181758.546000+060
bool tryConvertDateTime(const tstring& value, tstring& datetime)
{
CDateTime parsed;
tstring offset;
if (!WMI::tryParseDateTime(value, parsed, offset))
return false;
datetime = Core::fmt(TXT("%s %s"), parsed.ToString().c_str(), offset.c_str());
return true;
}
////////////////////////////////////////////////////////////////////////////////
//! Try and convert a string into a 64-bit integer. WMI appears to return sint64
//! and uint64 values as strings (VT_BSTR).
bool tryConvert64BitInteger(const tstring& value, tstring& integer)
{
tstring::const_iterator it = value.begin();
tstring::const_iterator end = value.end();
bool isSigned = false;
if (it == end)
return false;
if (*it == TXT('-'))
{
isSigned = true;
++it;
}
while (it != end)
{
if (!isdigit(*it))
return false;
++it;
}
if (isSigned)
integer = formatIntegerValue(WCL::Variant(Core::parse<int64>(value)), true);
else
integer = formatIntegerValue(WCL::Variant(Core::parse<uint64>(value)), true);
return true;
}
////////////////////////////////////////////////////////////////////////////////
//! Format an empty/null value.
static tstring formatEmptyValue(const WCL::Variant& value, bool applyFormatting)
{
tstring result;
if (applyFormatting)
{
ASSERT((value.type() == VT_EMPTY) || (value.type() == VT_NULL));
if (value.type() == VT_EMPTY)
{
result = TXT("<empty>");
}
else if (value.type() == VT_NULL)
{
result = TXT("<null>");
}
}
return result;
}
////////////////////////////////////////////////////////////////////////////////
//! Format a string value. If enabled it will look for strings that appear to be
//! WMI style datetimes and reformat them as a normal datetime.
static tstring formatStringValue(const WCL::Variant& value, bool applyFormatting)
{
tstring result = value.format();
if (applyFormatting)
{
tstring converted;
if (tryConvertDateTime(result, converted))
{
result = converted;
}
else if (tryConvert64BitInteger(result, converted))
{
result = converted;
}
}
return result;
}
////////////////////////////////////////////////////////////////////////////////
//! Format an integer value.
static tstring formatIntegerValue(const WCL::Variant& value, bool applyFormatting)
{
tstring result;
if (applyFormatting)
{
typedef tstring::reverse_iterator rev_iter;
typedef tstring::const_reverse_iterator c_rev_iter;
const tstring rawResult = value.format();
const size_t numDigits = (rawResult[0] != TXT('-')) ? rawResult.length() : (rawResult.length()-1);
const tstring separator = getGroupSeparator();
const size_t numSeps = (numDigits-1) / 3;
const size_t total = rawResult.length() + (numSeps * separator.length());
result = tstring(total, TXT(' '));
c_rev_iter it = rawResult.rbegin();
c_rev_iter end = rawResult.rend();
size_t digits = 0;
size_t seps = 0;
rev_iter output = result.rbegin();
while (it != end)
{
*output++ = *it++;
if ( ((++digits % 3) == 0) && (seps++ != numSeps) )
output = std::copy(separator.rbegin(), separator.rend(), output);
}
}
else
{
result = value.format();
}
return result;
}
////////////////////////////////////////////////////////////////////////////////
//! Format a VARIANT type value.
tstring formatValue(const WCL::Variant& value, bool applyFormatting)
{
tstring result;
VARTYPE type = value.type();
if ( (type == VT_EMPTY) || (type == VT_NULL))
{
result = formatEmptyValue(value, applyFormatting);
}
else if (type == VT_BSTR)
{
result = formatStringValue(value, applyFormatting);
}
else if ( (type == VT_I1 ) || (type == VT_I2 ) || (type == VT_I4 ) || (type == VT_I8 )
|| (type == VT_UI1) || (type == VT_UI2) || (type == VT_UI4) || (type == VT_UI8) )
{
result = formatIntegerValue(value, applyFormatting);
}
else
{
if (value.isArray())
{
VARTYPE valueType = value.valueType();
if (valueType == VT_BSTR)
{
typedef WCL::VariantVector<BSTR>::const_iterator c_iter;
SAFEARRAY* safeArray = V_ARRAY(&value);
WCL::VariantVector<BSTR> array(safeArray, VT_BSTR, false);
for (c_iter it = array.begin(); it != array.end(); ++it)
{
if (it != array.begin())
result += TXT(',');
result += W2T(*it);
}
}
else
{
result = Core::fmt(TXT("<array of %s>"), WCL::Variant::formatType(valueType));
}
}
else
{
if (!value.tryFormat(result))
result = TXT("<conversion failed>");
}
}
return result;
}