PropIDUtils.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // PropIDUtils.cpp
  2. #include "StdAfx.h"
  3. #include "PropIDUtils.h"
  4. #include "Common/IntToString.h"
  5. #include "Common/StringConvert.h"
  6. #include "Windows/FileFind.h"
  7. #include "Windows/PropVariantConversions.h"
  8. #include "../../PropID.h"
  9. using namespace NWindows;
  10. static UString ConvertUInt32ToString(UInt32 value)
  11. {
  12. wchar_t buffer[32];
  13. ConvertUInt64ToString(value, buffer);
  14. return buffer;
  15. }
  16. static void ConvertUInt32ToHex(UInt32 value, wchar_t *s)
  17. {
  18. for (int i = 0; i < 8; i++)
  19. {
  20. int t = value & 0xF;
  21. value >>= 4;
  22. s[7 - i] = (wchar_t)((t < 10) ? (L'0' + t) : (L'A' + (t - 10)));
  23. }
  24. s[8] = L'\0';
  25. }
  26. UString ConvertPropertyToString(const PROPVARIANT &propVariant, PROPID propID, bool full)
  27. {
  28. switch(propID)
  29. {
  30. case kpidCreationTime:
  31. case kpidLastWriteTime:
  32. case kpidLastAccessTime:
  33. {
  34. if (propVariant.vt != VT_FILETIME)
  35. return UString(); // It is error;
  36. FILETIME localFileTime;
  37. if (propVariant.filetime.dwHighDateTime == 0 &&
  38. propVariant.filetime.dwLowDateTime == 0)
  39. return UString();
  40. if (!::FileTimeToLocalFileTime(&propVariant.filetime, &localFileTime))
  41. return UString(); // It is error;
  42. return ConvertFileTimeToString(localFileTime, true, full);
  43. }
  44. case kpidCRC:
  45. {
  46. if(propVariant.vt != VT_UI4)
  47. break;
  48. wchar_t temp[12];
  49. ConvertUInt32ToHex(propVariant.ulVal, temp);
  50. return temp;
  51. }
  52. case kpidAttributes:
  53. {
  54. if(propVariant.vt != VT_UI4)
  55. break;
  56. UString result;
  57. UInt32 attributes = propVariant.ulVal;
  58. if (NFile::NFind::NAttributes::IsReadOnly(attributes)) result += L'R';
  59. if (NFile::NFind::NAttributes::IsHidden(attributes)) result += L'H';
  60. if (NFile::NFind::NAttributes::IsSystem(attributes)) result += L'S';
  61. if (NFile::NFind::NAttributes::IsDirectory(attributes)) result += L'D';
  62. if (NFile::NFind::NAttributes::IsArchived(attributes)) result += L'A';
  63. if (NFile::NFind::NAttributes::IsCompressed(attributes)) result += L'C';
  64. if (NFile::NFind::NAttributes::IsEncrypted(attributes)) result += L'E';
  65. return result;
  66. }
  67. case kpidDictionarySize:
  68. {
  69. if(propVariant.vt != VT_UI4)
  70. break;
  71. UInt32 size = propVariant.ulVal;
  72. if (size % (1 << 20) == 0)
  73. return ConvertUInt32ToString(size >> 20) + L"MB";
  74. if (size % (1 << 10) == 0)
  75. return ConvertUInt32ToString(size >> 10) + L"KB";
  76. return ConvertUInt32ToString(size);
  77. }
  78. }
  79. return ConvertPropVariantToString(propVariant);
  80. }