other-licenses/7zstub/src/Windows/PropVariantConversions.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 // PropVariantConversions.cpp
     3 #include "StdAfx.h"
     5 #include <stdio.h>
     7 #include "PropVariantConversions.h"
     9 #include "Windows/Defs.h"
    11 #include "Common/StringConvert.h"
    12 #include "Common/IntToString.h"
    14 static UString ConvertUInt64ToString(UInt64 value)
    15 {
    16   wchar_t buffer[32];
    17   ConvertUInt64ToString(value, buffer);
    18   return buffer;
    19 }
    21 static UString ConvertInt64ToString(Int64 value)
    22 {
    23   wchar_t buffer[32];
    24   ConvertInt64ToString(value, buffer);
    25   return buffer;
    26 }
    28 /*
    29 static void UIntToStringSpec(UInt32 value, char *s, int numPos)
    30 {
    31   char s2[32];
    32   ConvertUInt64ToString(value, s2);
    33   int len = strlen(s2);
    34   int i;
    35   for (i = 0; i < numPos - len; i++)
    36     s[i] = '0';
    37   for (int j = 0; j < len; j++, i++)
    38     s[i] = s2[j];
    39   s[i] = '\0';
    40 }
    41 */
    43 bool ConvertFileTimeToString(const FILETIME &ft, char *s, bool includeTime, bool includeSeconds)
    44 {
    45   s[0] = '\0';
    46   SYSTEMTIME st;
    47   if(!BOOLToBool(FileTimeToSystemTime(&ft, &st)))
    48     return false;
    49   /*
    50   UIntToStringSpec(st.wYear, s, 4);
    51   strcat(s, "-");
    52   UIntToStringSpec(st.wMonth, s + strlen(s), 2);
    53   strcat(s, "-");
    54   UIntToStringSpec(st.wDay, s + strlen(s), 2);
    55   if (includeTime)
    56   {
    57     strcat(s, " ");
    58     UIntToStringSpec(st.wHour, s + strlen(s), 2);
    59     strcat(s, ":");
    60     UIntToStringSpec(st.wMinute, s + strlen(s), 2);
    61     if (includeSeconds)
    62     {
    63       strcat(s, ":");
    64       UIntToStringSpec(st.wSecond, s + strlen(s), 2);
    65     }
    66   }
    67   */
    68   sprintf(s, "%04d-%02d-%02d", st.wYear, st.wMonth, st.wDay);
    69   if (includeTime)
    70   {
    71     sprintf(s + strlen(s), " %02d:%02d", st.wHour, st.wMinute);
    72     if (includeSeconds)
    73       sprintf(s + strlen(s), ":%02d", st.wSecond);
    74   }
    75   return true;
    76 }
    78 UString ConvertFileTimeToString(const FILETIME &fileTime, bool includeTime, bool includeSeconds)
    79 {
    80   char s[32];
    81   ConvertFileTimeToString(fileTime, s,  includeTime, includeSeconds);
    82   return GetUnicodeString(s);
    83 }
    86 UString ConvertPropVariantToString(const PROPVARIANT &propVariant)
    87 {
    88   switch (propVariant.vt)
    89   {
    90     case VT_EMPTY:
    91       return UString();
    92     case VT_BSTR:
    93       return propVariant.bstrVal;
    94     case VT_UI1:
    95       return ConvertUInt64ToString(propVariant.bVal);
    96     case VT_UI2:
    97       return ConvertUInt64ToString(propVariant.uiVal);
    98     case VT_UI4:
    99       return ConvertUInt64ToString(propVariant.ulVal);
   100     case VT_UI8:
   101       return ConvertUInt64ToString(propVariant.uhVal.QuadPart);
   102     case VT_FILETIME:
   103       return ConvertFileTimeToString(propVariant.filetime, true, true);
   104     /*
   105     case VT_I1:
   106       return ConvertInt64ToString(propVariant.cVal);
   107     */
   108     case VT_I2:
   109       return ConvertInt64ToString(propVariant.iVal);
   110     case VT_I4:
   111       return ConvertInt64ToString(propVariant.lVal);
   112     case VT_I8:
   113       return ConvertInt64ToString(propVariant.hVal.QuadPart);
   115     case VT_BOOL:
   116       return VARIANT_BOOLToBool(propVariant.boolVal) ? L"1" : L"0";
   117     default:
   118       #ifndef _WIN32_WCE
   119       throw 150245;
   120       #else
   121       return UString();
   122       #endif
   123   }
   124 }
   126 UInt64 ConvertPropVariantToUInt64(const PROPVARIANT &propVariant)
   127 {
   128   switch (propVariant.vt)
   129   {
   130     case VT_UI1:
   131       return propVariant.bVal;
   132     case VT_UI2:
   133       return propVariant.uiVal;
   134     case VT_UI4:
   135       return propVariant.ulVal;
   136     case VT_UI8:
   137       return (UInt64)propVariant.uhVal.QuadPart;
   138     default:
   139       #ifndef _WIN32_WCE
   140       throw 151199;
   141       #else
   142       return 0;
   143       #endif
   144   }
   145 }

mercurial