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