PropVariantConversions.cpp 3.3 KB

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