1// PropVariantConversions.cpp
2
3#include "StdAfx.h"
4
5#include "Common/IntToString.h"
6#include "Common/StringConvert.h"
7
8#include "Windows/Defs.h"
9
10#include "PropVariantConversions.h"
11
12static UString ConvertUInt64ToString(UInt64 value)
13{
14  wchar_t buffer[32];
15  ConvertUInt64ToString(value, buffer);
16  return buffer;
17}
18
19static UString ConvertInt64ToString(Int64 value)
20{
21  wchar_t buffer[32];
22  ConvertInt64ToString(value, buffer);
23  return buffer;
24}
25
26static char *UIntToStringSpec(char c, UInt32 value, char *s, int numPos)
27{
28  if (c != 0)
29    *s++ = c;
30  char temp[16];
31  int pos = 0;
32  do
33  {
34    temp[pos++] = (char)('0' + value % 10);
35    value /= 10;
36  }
37  while (value != 0);
38  int i;
39  for (i = 0; i < numPos - pos; i++)
40    *s++ = '0';
41  do
42    *s++ = temp[--pos];
43  while (pos > 0);
44  *s = '\0';
45  return s;
46}
47
48bool ConvertFileTimeToString(const FILETIME &ft, char *s, bool includeTime, bool includeSeconds)
49{
50  s[0] = '\0';
51  SYSTEMTIME st;
52  if (!BOOLToBool(FileTimeToSystemTime(&ft, &st)))
53    return false;
54  s = UIntToStringSpec(0, st.wYear, s, 4);
55  s = UIntToStringSpec('-', st.wMonth, s, 2);
56  s = UIntToStringSpec('-', st.wDay, s, 2);
57  if (includeTime)
58  {
59    s = UIntToStringSpec(' ', st.wHour, s, 2);
60    s = UIntToStringSpec(':', st.wMinute, s, 2);
61    if (includeSeconds)
62      UIntToStringSpec(':', st.wSecond, s, 2);
63  }
64  return true;
65}
66
67UString ConvertFileTimeToString(const FILETIME &ft, bool includeTime, bool includeSeconds)
68{
69  char s[32];
70  ConvertFileTimeToString(ft, s, includeTime, includeSeconds);
71  return GetUnicodeString(s);
72}
73
74
75UString ConvertPropVariantToString(const PROPVARIANT &prop)
76{
77  switch (prop.vt)
78  {
79    case VT_EMPTY: return UString();
80    case VT_BSTR: return prop.bstrVal;
81    case VT_UI1: return ConvertUInt64ToString(prop.bVal);
82    case VT_UI2: return ConvertUInt64ToString(prop.uiVal);
83    case VT_UI4: return ConvertUInt64ToString(prop.ulVal);
84    case VT_UI8: return ConvertUInt64ToString(prop.uhVal.QuadPart);
85    case VT_FILETIME: return ConvertFileTimeToString(prop.filetime, true, true);
86    // case VT_I1: return ConvertInt64ToString(prop.cVal);
87    case VT_I2: return ConvertInt64ToString(prop.iVal);
88    case VT_I4: return ConvertInt64ToString(prop.lVal);
89    case VT_I8: return ConvertInt64ToString(prop.hVal.QuadPart);
90    case VT_BOOL: return VARIANT_BOOLToBool(prop.boolVal) ? L"+" : L"-";
91    default: throw 150245;
92  }
93}
94
95UInt64 ConvertPropVariantToUInt64(const PROPVARIANT &prop)
96{
97  switch (prop.vt)
98  {
99    case VT_UI1: return prop.bVal;
100    case VT_UI2: return prop.uiVal;
101    case VT_UI4: return prop.ulVal;
102    case VT_UI8: return (UInt64)prop.uhVal.QuadPart;
103    default: throw 151199;
104  }
105}
106