michael@0: // PropVariantConversions.cpp michael@0: michael@0: #include "StdAfx.h" michael@0: michael@0: #include michael@0: michael@0: #include "PropVariantConversions.h" michael@0: michael@0: #include "Windows/Defs.h" michael@0: michael@0: #include "Common/StringConvert.h" michael@0: #include "Common/IntToString.h" michael@0: michael@0: static UString ConvertUInt64ToString(UInt64 value) michael@0: { michael@0: wchar_t buffer[32]; michael@0: ConvertUInt64ToString(value, buffer); michael@0: return buffer; michael@0: } michael@0: michael@0: static UString ConvertInt64ToString(Int64 value) michael@0: { michael@0: wchar_t buffer[32]; michael@0: ConvertInt64ToString(value, buffer); michael@0: return buffer; michael@0: } michael@0: michael@0: /* michael@0: static void UIntToStringSpec(UInt32 value, char *s, int numPos) michael@0: { michael@0: char s2[32]; michael@0: ConvertUInt64ToString(value, s2); michael@0: int len = strlen(s2); michael@0: int i; michael@0: for (i = 0; i < numPos - len; i++) michael@0: s[i] = '0'; michael@0: for (int j = 0; j < len; j++, i++) michael@0: s[i] = s2[j]; michael@0: s[i] = '\0'; michael@0: } michael@0: */ michael@0: michael@0: bool ConvertFileTimeToString(const FILETIME &ft, char *s, bool includeTime, bool includeSeconds) michael@0: { michael@0: s[0] = '\0'; michael@0: SYSTEMTIME st; michael@0: if(!BOOLToBool(FileTimeToSystemTime(&ft, &st))) michael@0: return false; michael@0: /* michael@0: UIntToStringSpec(st.wYear, s, 4); michael@0: strcat(s, "-"); michael@0: UIntToStringSpec(st.wMonth, s + strlen(s), 2); michael@0: strcat(s, "-"); michael@0: UIntToStringSpec(st.wDay, s + strlen(s), 2); michael@0: if (includeTime) michael@0: { michael@0: strcat(s, " "); michael@0: UIntToStringSpec(st.wHour, s + strlen(s), 2); michael@0: strcat(s, ":"); michael@0: UIntToStringSpec(st.wMinute, s + strlen(s), 2); michael@0: if (includeSeconds) michael@0: { michael@0: strcat(s, ":"); michael@0: UIntToStringSpec(st.wSecond, s + strlen(s), 2); michael@0: } michael@0: } michael@0: */ michael@0: sprintf(s, "%04d-%02d-%02d", st.wYear, st.wMonth, st.wDay); michael@0: if (includeTime) michael@0: { michael@0: sprintf(s + strlen(s), " %02d:%02d", st.wHour, st.wMinute); michael@0: if (includeSeconds) michael@0: sprintf(s + strlen(s), ":%02d", st.wSecond); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: UString ConvertFileTimeToString(const FILETIME &fileTime, bool includeTime, bool includeSeconds) michael@0: { michael@0: char s[32]; michael@0: ConvertFileTimeToString(fileTime, s, includeTime, includeSeconds); michael@0: return GetUnicodeString(s); michael@0: } michael@0: michael@0: michael@0: UString ConvertPropVariantToString(const PROPVARIANT &propVariant) michael@0: { michael@0: switch (propVariant.vt) michael@0: { michael@0: case VT_EMPTY: michael@0: return UString(); michael@0: case VT_BSTR: michael@0: return propVariant.bstrVal; michael@0: case VT_UI1: michael@0: return ConvertUInt64ToString(propVariant.bVal); michael@0: case VT_UI2: michael@0: return ConvertUInt64ToString(propVariant.uiVal); michael@0: case VT_UI4: michael@0: return ConvertUInt64ToString(propVariant.ulVal); michael@0: case VT_UI8: michael@0: return ConvertUInt64ToString(propVariant.uhVal.QuadPart); michael@0: case VT_FILETIME: michael@0: return ConvertFileTimeToString(propVariant.filetime, true, true); michael@0: /* michael@0: case VT_I1: michael@0: return ConvertInt64ToString(propVariant.cVal); michael@0: */ michael@0: case VT_I2: michael@0: return ConvertInt64ToString(propVariant.iVal); michael@0: case VT_I4: michael@0: return ConvertInt64ToString(propVariant.lVal); michael@0: case VT_I8: michael@0: return ConvertInt64ToString(propVariant.hVal.QuadPart); michael@0: michael@0: case VT_BOOL: michael@0: return VARIANT_BOOLToBool(propVariant.boolVal) ? L"1" : L"0"; michael@0: default: michael@0: #ifndef _WIN32_WCE michael@0: throw 150245; michael@0: #else michael@0: return UString(); michael@0: #endif michael@0: } michael@0: } michael@0: michael@0: UInt64 ConvertPropVariantToUInt64(const PROPVARIANT &propVariant) michael@0: { michael@0: switch (propVariant.vt) michael@0: { michael@0: case VT_UI1: michael@0: return propVariant.bVal; michael@0: case VT_UI2: michael@0: return propVariant.uiVal; michael@0: case VT_UI4: michael@0: return propVariant.ulVal; michael@0: case VT_UI8: michael@0: return (UInt64)propVariant.uhVal.QuadPart; michael@0: default: michael@0: #ifndef _WIN32_WCE michael@0: throw 151199; michael@0: #else michael@0: return 0; michael@0: #endif michael@0: } michael@0: }