1370b324cSopenharmony_ci// PropVariantConv.cpp
2370b324cSopenharmony_ci
3370b324cSopenharmony_ci#include "StdAfx.h"
4370b324cSopenharmony_ci
5370b324cSopenharmony_ci#include "../Common/IntToString.h"
6370b324cSopenharmony_ci
7370b324cSopenharmony_ci#include "Defs.h"
8370b324cSopenharmony_ci#include "PropVariantConv.h"
9370b324cSopenharmony_ci
10370b324cSopenharmony_ci#define UINT_TO_STR_2(c, val) { s[0] = (c); s[1] = (char)('0' + (val) / 10); s[2] = (char)('0' + (val) % 10); s += 3; }
11370b324cSopenharmony_ci
12370b324cSopenharmony_cibool ConvertUtcFileTimeToString2(const FILETIME &utc, unsigned ns100, char *s, int level) throw()
13370b324cSopenharmony_ci{
14370b324cSopenharmony_ci  *s = 0;
15370b324cSopenharmony_ci  FILETIME ft;
16370b324cSopenharmony_ci  if (!FileTimeToLocalFileTime(&utc, &ft))
17370b324cSopenharmony_ci    return false;
18370b324cSopenharmony_ci
19370b324cSopenharmony_ci  SYSTEMTIME st;
20370b324cSopenharmony_ci  if (!BOOLToBool(FileTimeToSystemTime(&ft, &st)))
21370b324cSopenharmony_ci  {
22370b324cSopenharmony_ci    // win10 : that function doesn't work, if bit 63 of 64-bit FILETIME is set.
23370b324cSopenharmony_ci    return false;
24370b324cSopenharmony_ci  }
25370b324cSopenharmony_ci
26370b324cSopenharmony_ci  {
27370b324cSopenharmony_ci    unsigned val = st.wYear;
28370b324cSopenharmony_ci    if (val >= 10000)
29370b324cSopenharmony_ci    {
30370b324cSopenharmony_ci      *s++ = (char)('0' + val / 10000);
31370b324cSopenharmony_ci      val %= 10000;
32370b324cSopenharmony_ci    }
33370b324cSopenharmony_ci    s[3] = (char)('0' + val % 10); val /= 10;
34370b324cSopenharmony_ci    s[2] = (char)('0' + val % 10); val /= 10;
35370b324cSopenharmony_ci    s[1] = (char)('0' + val % 10);
36370b324cSopenharmony_ci    s[0] = (char)('0' + val / 10);
37370b324cSopenharmony_ci    s += 4;
38370b324cSopenharmony_ci  }
39370b324cSopenharmony_ci  UINT_TO_STR_2('-', st.wMonth)
40370b324cSopenharmony_ci  UINT_TO_STR_2('-', st.wDay)
41370b324cSopenharmony_ci
42370b324cSopenharmony_ci  if (level > kTimestampPrintLevel_DAY)
43370b324cSopenharmony_ci  {
44370b324cSopenharmony_ci    UINT_TO_STR_2(' ', st.wHour)
45370b324cSopenharmony_ci    UINT_TO_STR_2(':', st.wMinute)
46370b324cSopenharmony_ci
47370b324cSopenharmony_ci    if (level >= kTimestampPrintLevel_SEC)
48370b324cSopenharmony_ci    {
49370b324cSopenharmony_ci      UINT_TO_STR_2(':', st.wSecond)
50370b324cSopenharmony_ci
51370b324cSopenharmony_ci      if (level > kTimestampPrintLevel_SEC)
52370b324cSopenharmony_ci      {
53370b324cSopenharmony_ci        *s++ = '.';
54370b324cSopenharmony_ci        /*
55370b324cSopenharmony_ci        {
56370b324cSopenharmony_ci          unsigned val = st.wMilliseconds;
57370b324cSopenharmony_ci          s[2] = (char)('0' + val % 10); val /= 10;
58370b324cSopenharmony_ci          s[1] = (char)('0' + val % 10);
59370b324cSopenharmony_ci          s[0] = (char)('0' + val / 10);
60370b324cSopenharmony_ci          s += 3;
61370b324cSopenharmony_ci        }
62370b324cSopenharmony_ci        *s++ = ' ';
63370b324cSopenharmony_ci        */
64370b324cSopenharmony_ci
65370b324cSopenharmony_ci        {
66370b324cSopenharmony_ci          unsigned numDigits = 7;
67370b324cSopenharmony_ci          UInt32 val = (UInt32)((((UInt64)ft.dwHighDateTime << 32) + ft.dwLowDateTime) % 10000000);
68370b324cSopenharmony_ci          for (unsigned i = numDigits; i != 0;)
69370b324cSopenharmony_ci          {
70370b324cSopenharmony_ci            i--;
71370b324cSopenharmony_ci            s[i] = (char)('0' + val % 10); val /= 10;
72370b324cSopenharmony_ci          }
73370b324cSopenharmony_ci          if (numDigits > (unsigned)level)
74370b324cSopenharmony_ci            numDigits = (unsigned)level;
75370b324cSopenharmony_ci          s += numDigits;
76370b324cSopenharmony_ci        }
77370b324cSopenharmony_ci        if (level >= kTimestampPrintLevel_NTFS + 1)
78370b324cSopenharmony_ci        {
79370b324cSopenharmony_ci          *s++ = (char)('0' + (ns100 / 10));
80370b324cSopenharmony_ci          if (level >= kTimestampPrintLevel_NTFS + 2)
81370b324cSopenharmony_ci            *s++ = (char)('0' + (ns100 % 10));
82370b324cSopenharmony_ci        }
83370b324cSopenharmony_ci      }
84370b324cSopenharmony_ci    }
85370b324cSopenharmony_ci  }
86370b324cSopenharmony_ci
87370b324cSopenharmony_ci  *s = 0;
88370b324cSopenharmony_ci  return true;
89370b324cSopenharmony_ci}
90370b324cSopenharmony_ci
91370b324cSopenharmony_ci
92370b324cSopenharmony_cibool ConvertUtcFileTimeToString(const FILETIME &utc, char *s, int level) throw()
93370b324cSopenharmony_ci{
94370b324cSopenharmony_ci  return ConvertUtcFileTimeToString2(utc, 0, s, level);
95370b324cSopenharmony_ci}
96370b324cSopenharmony_ci
97370b324cSopenharmony_cibool ConvertUtcFileTimeToString2(const FILETIME &ft, unsigned ns100, wchar_t *dest, int level) throw()
98370b324cSopenharmony_ci{
99370b324cSopenharmony_ci  char s[32];
100370b324cSopenharmony_ci  bool res = ConvertUtcFileTimeToString2(ft, ns100, s, level);
101370b324cSopenharmony_ci  for (unsigned i = 0;; i++)
102370b324cSopenharmony_ci  {
103370b324cSopenharmony_ci    Byte c = (Byte)s[i];
104370b324cSopenharmony_ci    dest[i] = c;
105370b324cSopenharmony_ci    if (c == 0)
106370b324cSopenharmony_ci      break;
107370b324cSopenharmony_ci  }
108370b324cSopenharmony_ci  return res;
109370b324cSopenharmony_ci}
110370b324cSopenharmony_ci
111370b324cSopenharmony_cibool ConvertUtcFileTimeToString(const FILETIME &ft, wchar_t *dest, int level) throw()
112370b324cSopenharmony_ci{
113370b324cSopenharmony_ci  char s[32];
114370b324cSopenharmony_ci  bool res = ConvertUtcFileTimeToString(ft, s, level);
115370b324cSopenharmony_ci  for (unsigned i = 0;; i++)
116370b324cSopenharmony_ci  {
117370b324cSopenharmony_ci    Byte c = (Byte)s[i];
118370b324cSopenharmony_ci    dest[i] = c;
119370b324cSopenharmony_ci    if (c == 0)
120370b324cSopenharmony_ci      break;
121370b324cSopenharmony_ci  }
122370b324cSopenharmony_ci  return res;
123370b324cSopenharmony_ci}
124370b324cSopenharmony_ci
125370b324cSopenharmony_ci
126370b324cSopenharmony_civoid ConvertPropVariantToShortString(const PROPVARIANT &prop, char *dest) throw()
127370b324cSopenharmony_ci{
128370b324cSopenharmony_ci  *dest = 0;
129370b324cSopenharmony_ci  switch (prop.vt)
130370b324cSopenharmony_ci  {
131370b324cSopenharmony_ci    case VT_EMPTY: return;
132370b324cSopenharmony_ci    case VT_BSTR: dest[0] = '?'; dest[1] = 0; return;
133370b324cSopenharmony_ci    case VT_UI1: ConvertUInt32ToString(prop.bVal, dest); return;
134370b324cSopenharmony_ci    case VT_UI2: ConvertUInt32ToString(prop.uiVal, dest); return;
135370b324cSopenharmony_ci    case VT_UI4: ConvertUInt32ToString(prop.ulVal, dest); return;
136370b324cSopenharmony_ci    case VT_UI8: ConvertUInt64ToString(prop.uhVal.QuadPart, dest); return;
137370b324cSopenharmony_ci    case VT_FILETIME:
138370b324cSopenharmony_ci    {
139370b324cSopenharmony_ci      // const unsigned prec = prop.wReserved1;
140370b324cSopenharmony_ci      int level = 0;
141370b324cSopenharmony_ci      /*
142370b324cSopenharmony_ci      if (prec == 0)
143370b324cSopenharmony_ci        level = 7;
144370b324cSopenharmony_ci      else if (prec > 16 && prec <= 16 + 9)
145370b324cSopenharmony_ci        level = prec - 16;
146370b324cSopenharmony_ci      */
147370b324cSopenharmony_ci      ConvertUtcFileTimeToString(prop.filetime, dest, level);
148370b324cSopenharmony_ci      return;
149370b324cSopenharmony_ci    }
150370b324cSopenharmony_ci    // case VT_I1: return ConvertInt64ToString(prop.cVal, dest); return;
151370b324cSopenharmony_ci    case VT_I2: ConvertInt64ToString(prop.iVal, dest); return;
152370b324cSopenharmony_ci    case VT_I4: ConvertInt64ToString(prop.lVal, dest); return;
153370b324cSopenharmony_ci    case VT_I8: ConvertInt64ToString(prop.hVal.QuadPart, dest); return;
154370b324cSopenharmony_ci    case VT_BOOL: dest[0] = VARIANT_BOOLToBool(prop.boolVal) ? '+' : '-'; dest[1] = 0; return;
155370b324cSopenharmony_ci    default: dest[0] = '?'; dest[1] = ':'; ConvertUInt64ToString(prop.vt, dest + 2);
156370b324cSopenharmony_ci  }
157370b324cSopenharmony_ci}
158370b324cSopenharmony_ci
159370b324cSopenharmony_civoid ConvertPropVariantToShortString(const PROPVARIANT &prop, wchar_t *dest) throw()
160370b324cSopenharmony_ci{
161370b324cSopenharmony_ci  *dest = 0;
162370b324cSopenharmony_ci  switch (prop.vt)
163370b324cSopenharmony_ci  {
164370b324cSopenharmony_ci    case VT_EMPTY: return;
165370b324cSopenharmony_ci    case VT_BSTR: dest[0] = '?'; dest[1] = 0; return;
166370b324cSopenharmony_ci    case VT_UI1: ConvertUInt32ToString(prop.bVal, dest); return;
167370b324cSopenharmony_ci    case VT_UI2: ConvertUInt32ToString(prop.uiVal, dest); return;
168370b324cSopenharmony_ci    case VT_UI4: ConvertUInt32ToString(prop.ulVal, dest); return;
169370b324cSopenharmony_ci    case VT_UI8: ConvertUInt64ToString(prop.uhVal.QuadPart, dest); return;
170370b324cSopenharmony_ci    case VT_FILETIME:
171370b324cSopenharmony_ci    {
172370b324cSopenharmony_ci      // const unsigned prec = prop.wReserved1;
173370b324cSopenharmony_ci      int level = 0;
174370b324cSopenharmony_ci      /*
175370b324cSopenharmony_ci      if (prec == 0)
176370b324cSopenharmony_ci        level = 7;
177370b324cSopenharmony_ci      else if (prec > 16 && prec <= 16 + 9)
178370b324cSopenharmony_ci        level = prec - 16;
179370b324cSopenharmony_ci      */
180370b324cSopenharmony_ci      ConvertUtcFileTimeToString(prop.filetime, dest, level);
181370b324cSopenharmony_ci      return;
182370b324cSopenharmony_ci    }
183370b324cSopenharmony_ci    // case VT_I1: return ConvertInt64ToString(prop.cVal, dest); return;
184370b324cSopenharmony_ci    case VT_I2: ConvertInt64ToString(prop.iVal, dest); return;
185370b324cSopenharmony_ci    case VT_I4: ConvertInt64ToString(prop.lVal, dest); return;
186370b324cSopenharmony_ci    case VT_I8: ConvertInt64ToString(prop.hVal.QuadPart, dest); return;
187370b324cSopenharmony_ci    case VT_BOOL: dest[0] = VARIANT_BOOLToBool(prop.boolVal) ? (wchar_t)'+' : (wchar_t)'-'; dest[1] = 0; return;
188370b324cSopenharmony_ci    default: dest[0] = '?'; dest[1] = ':'; ConvertUInt32ToString(prop.vt, dest + 2);
189370b324cSopenharmony_ci  }
190370b324cSopenharmony_ci}
191