1 // PropVariantConvert.cpp
2
3 #include "StdAfx.h"
4
5 #include "../Common/IntToString.h"
6
7 #include "Defs.h"
8 #include "PropVariantConv.h"
9
10 #define UINT_TO_STR_2(c, val) { s[0] = (c); s[1] = (char)('0' + (val) / 10); s[2] = (char)('0' + (val) % 10); s += 3; }
11
ConvertUtcFileTimeToString(const FILETIME & utc,char * s,int level)12 bool ConvertUtcFileTimeToString(const FILETIME &utc, char *s, int level) throw()
13 {
14 *s = 0;
15 FILETIME ft;
16 if (!FileTimeToLocalFileTime(&utc, &ft))
17 return false;
18
19 SYSTEMTIME st;
20 if (!BOOLToBool(FileTimeToSystemTime(&ft, &st)))
21 return false;
22
23 {
24 unsigned val = st.wYear;
25 if (val >= 10000)
26 {
27 *s++ = (char)('0' + val / 10000);
28 val %= 10000;
29 }
30 s[3] = (char)('0' + val % 10); val /= 10;
31 s[2] = (char)('0' + val % 10); val /= 10;
32 s[1] = (char)('0' + val % 10);
33 s[0] = (char)('0' + val / 10);
34 s += 4;
35 }
36 UINT_TO_STR_2('-', st.wMonth);
37 UINT_TO_STR_2('-', st.wDay);
38
39 if (level > kTimestampPrintLevel_DAY)
40 {
41 UINT_TO_STR_2(' ', st.wHour);
42 UINT_TO_STR_2(':', st.wMinute);
43
44 if (level >= kTimestampPrintLevel_SEC)
45 {
46 UINT_TO_STR_2(':', st.wSecond);
47
48 if (level > kTimestampPrintLevel_SEC)
49 {
50 *s++ = '.';
51 /*
52 {
53 unsigned val = st.wMilliseconds;
54 s[2] = (char)('0' + val % 10); val /= 10;
55 s[1] = (char)('0' + val % 10);
56 s[0] = (char)('0' + val / 10);
57 s += 3;
58 }
59 *s++ = ' ';
60 */
61
62 {
63 unsigned numDigits = 7;
64 UInt32 val = (UInt32)((((UInt64)ft.dwHighDateTime << 32) + ft.dwLowDateTime) % 10000000);
65 for (unsigned i = numDigits; i != 0;)
66 {
67 i--;
68 s[i] = (char)('0' + val % 10); val /= 10;
69 }
70 if (numDigits > (unsigned)level)
71 numDigits = (unsigned)level;
72 s += numDigits;
73 }
74 }
75 }
76 }
77
78 *s = 0;
79 return true;
80 }
81
82
ConvertUtcFileTimeToString(const FILETIME & ft,wchar_t * dest,int level)83 bool ConvertUtcFileTimeToString(const FILETIME &ft, wchar_t *dest, int level) throw()
84 {
85 char s[32];
86 bool res = ConvertUtcFileTimeToString(ft, s, level);
87 for (unsigned i = 0;; i++)
88 {
89 unsigned char c = s[i];
90 dest[i] = c;
91 if (c == 0)
92 break;
93 }
94 return res;
95 }
96
97
ConvertPropVariantToShortString(const PROPVARIANT & prop,char * dest)98 void ConvertPropVariantToShortString(const PROPVARIANT &prop, char *dest) throw()
99 {
100 *dest = 0;
101 switch (prop.vt)
102 {
103 case VT_EMPTY: return;
104 case VT_BSTR: dest[0] = '?'; dest[1] = 0; return;
105 case VT_UI1: ConvertUInt32ToString(prop.bVal, dest); return;
106 case VT_UI2: ConvertUInt32ToString(prop.uiVal, dest); return;
107 case VT_UI4: ConvertUInt32ToString(prop.ulVal, dest); return;
108 case VT_UI8: ConvertUInt64ToString(prop.uhVal.QuadPart, dest); return;
109 case VT_FILETIME: ConvertUtcFileTimeToString(prop.filetime, dest); return;
110 // case VT_I1: return ConvertInt64ToString(prop.cVal, dest); return;
111 case VT_I2: ConvertInt64ToString(prop.iVal, dest); return;
112 case VT_I4: ConvertInt64ToString(prop.lVal, dest); return;
113 case VT_I8: ConvertInt64ToString(prop.hVal.QuadPart, dest); return;
114 case VT_BOOL: dest[0] = VARIANT_BOOLToBool(prop.boolVal) ? '+' : '-'; dest[1] = 0; return;
115 default: dest[0] = '?'; dest[1] = ':'; ConvertUInt64ToString(prop.vt, dest + 2);
116 }
117 }
118
ConvertPropVariantToShortString(const PROPVARIANT & prop,wchar_t * dest)119 void ConvertPropVariantToShortString(const PROPVARIANT &prop, wchar_t *dest) throw()
120 {
121 *dest = 0;
122 switch (prop.vt)
123 {
124 case VT_EMPTY: return;
125 case VT_BSTR: dest[0] = '?'; dest[1] = 0; return;
126 case VT_UI1: ConvertUInt32ToString(prop.bVal, dest); return;
127 case VT_UI2: ConvertUInt32ToString(prop.uiVal, dest); return;
128 case VT_UI4: ConvertUInt32ToString(prop.ulVal, dest); return;
129 case VT_UI8: ConvertUInt64ToString(prop.uhVal.QuadPart, dest); return;
130 case VT_FILETIME: ConvertUtcFileTimeToString(prop.filetime, dest); return;
131 // case VT_I1: return ConvertInt64ToString(prop.cVal, dest); return;
132 case VT_I2: ConvertInt64ToString(prop.iVal, dest); return;
133 case VT_I4: ConvertInt64ToString(prop.lVal, dest); return;
134 case VT_I8: ConvertInt64ToString(prop.hVal.QuadPart, dest); return;
135 case VT_BOOL: dest[0] = VARIANT_BOOLToBool(prop.boolVal) ? (wchar_t)'+' : (wchar_t)'-'; dest[1] = 0; return;
136 default: dest[0] = '?'; dest[1] = ':'; ConvertUInt32ToString(prop.vt, dest + 2);
137 }
138 }
139