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