1 // PropVariantConversions.cpp
2 
3 #include "StdAfx.h"
4 
5 #include "PropVariantConversions.h"
6 
7 #include "Windows/Defs.h"
8 
9 #include "Common/StringConvert.h"
10 #include "Common/IntToString.h"
11 
ConvertUInt64ToString(UInt64 value)12 static UString ConvertUInt64ToString(UInt64 value)
13 {
14   wchar_t buffer[32];
15   ConvertUInt64ToString(value, buffer);
16   return buffer;
17 }
18 
ConvertInt64ToString(Int64 value)19 static UString ConvertInt64ToString(Int64 value)
20 {
21   wchar_t buffer[32];
22   ConvertInt64ToString(value, buffer);
23   return buffer;
24 }
25 
UIntToStringSpec(char c,UInt32 value,char * s,int numPos)26 static 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 
ConvertFileTimeToString(const FILETIME & ft,char * s,bool includeTime,bool includeSeconds)48 bool ConvertFileTimeToString(const FILETIME &ft, char *s, bool includeTime, bool includeSeconds)
49 {
50 #ifdef _WIN32
51   s[0] = '\0';
52   SYSTEMTIME st;
53   if (!BOOLToBool(FileTimeToSystemTime(&ft, &st)))
54     return false;
55   s = UIntToStringSpec(0, st.wYear, s, 4);
56   s = UIntToStringSpec('-', st.wMonth, s, 2);
57   s = UIntToStringSpec('-', st.wDay, s, 2);
58   if (includeTime)
59   {
60     s = UIntToStringSpec(' ', st.wHour, s, 2);
61     s = UIntToStringSpec(':', st.wMinute, s, 2);
62     if (includeSeconds)
63       UIntToStringSpec(':', st.wSecond, s, 2);
64   }
65   /*
66   sprintf(s, "%04d-%02d-%02d", st.wYear, st.wMonth, st.wDay);
67   if (includeTime)
68   {
69     sprintf(s + strlen(s), " %02d:%02d", st.wHour, st.wMinute);
70     if (includeSeconds)
71       sprintf(s + strlen(s), ":%02d", st.wSecond);
72   }
73   */
74 #else
75   BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *Time, DWORD *Seconds );
76 
77   FILETIME filetime;
78   LocalFileTimeToFileTime(&ft, &filetime);
79 
80   LARGE_INTEGER  ltime;
81 
82   ltime.QuadPart = filetime.dwHighDateTime;
83   ltime.QuadPart = (ltime.QuadPart << 32) | filetime.dwLowDateTime;
84 
85   DWORD dw;
86   RtlTimeToSecondsSince1970(&ltime, &dw );
87   time_t timep = (time_t)dw;
88 
89   struct tm * date = localtime(&timep);
90 
91   sprintf(s, "%04d-%02d-%02d", date->tm_year+1900, date->tm_mon+1,date->tm_mday);
92   if (includeTime)
93   {
94     sprintf(s + strlen(s), " %02d:%02d", date->tm_hour,date->tm_min);
95     if (includeSeconds)
96       sprintf(s + strlen(s), ":%02d", date->tm_sec);
97   }
98 #endif
99   return true;
100 }
101 
ConvertFileTimeToString(const FILETIME & fileTime,bool includeTime,bool includeSeconds)102 UString ConvertFileTimeToString(const FILETIME &fileTime, bool includeTime, bool includeSeconds)
103 {
104   char s[32];
105   ConvertFileTimeToString(fileTime, s,  includeTime, includeSeconds);
106   return GetUnicodeString(s);
107 }
108 
109 
ConvertPropVariantToString(const PROPVARIANT & prop)110 UString ConvertPropVariantToString(const PROPVARIANT &prop)
111 {
112   switch (prop.vt)
113   {
114     case VT_EMPTY: return UString();
115     case VT_BSTR: return prop.bstrVal;
116     case VT_UI1: return ConvertUInt64ToString(prop.bVal);
117     case VT_UI2: return ConvertUInt64ToString(prop.uiVal);
118     case VT_UI4: return ConvertUInt64ToString(prop.ulVal);
119     case VT_UI8: return ConvertUInt64ToString(prop.uhVal.QuadPart);
120     case VT_FILETIME: return ConvertFileTimeToString(prop.filetime, true, true);
121     // case VT_I1: return ConvertInt64ToString(prop.cVal);
122     case VT_I2: return ConvertInt64ToString(prop.iVal);
123     case VT_I4: return ConvertInt64ToString(prop.lVal);
124     case VT_I8: return ConvertInt64ToString(prop.hVal.QuadPart);
125     case VT_BOOL: return VARIANT_BOOLToBool(prop.boolVal) ? L"+" : L"-";
126     default:
127       #ifndef _WIN32_WCE
128       throw 150245;
129       #else
130       return UString();
131       #endif
132   }
133 }
134 
ConvertPropVariantToUInt64(const PROPVARIANT & prop)135 UInt64 ConvertPropVariantToUInt64(const PROPVARIANT &prop)
136 {
137   switch (prop.vt)
138   {
139     case VT_UI1: return prop.bVal;
140     case VT_UI2: return prop.uiVal;
141     case VT_UI4: return prop.ulVal;
142     case VT_UI8: return (UInt64)prop.uhVal.QuadPart;
143     default:
144       #ifndef _WIN32_WCE
145       throw 151199;
146       #else
147       return 0;
148       #endif
149   }
150 }
151