Path_WIN32U.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // Path_WIN32U.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Path_WIN32U.cpp#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Filesystem
  8. // Module: Path
  9. //
  10. // Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/Path_WIN32U.h"
  16. #include "Poco/Environment_WIN32.h"
  17. #include "Poco/UnicodeConverter.h"
  18. #include "Poco/Buffer.h"
  19. #include "Poco/Exception.h"
  20. #include "Poco/UnWindows.h"
  21. namespace Poco {
  22. std::string PathImpl::currentImpl()
  23. {
  24. std::string result;
  25. DWORD len = GetCurrentDirectoryW(0, NULL);
  26. if (len > 0)
  27. {
  28. Buffer<wchar_t> buffer(len);
  29. DWORD n = GetCurrentDirectoryW(len, buffer.begin());
  30. if (n > 0 && n <= len)
  31. {
  32. UnicodeConverter::toUTF8(buffer.begin(), result);
  33. if (result[result.size() - 1] != '\\')
  34. result.append("\\");
  35. return result;
  36. }
  37. }
  38. throw SystemException("Cannot get current directory");
  39. }
  40. std::string PathImpl::systemImpl()
  41. {
  42. Buffer<wchar_t> buffer(MAX_PATH_LEN);
  43. DWORD n = GetSystemDirectoryW(buffer.begin(), static_cast<DWORD>(buffer.size()));
  44. if (n > 0)
  45. {
  46. n = GetLongPathNameW(buffer.begin(), buffer.begin(), static_cast<DWORD>(buffer.size()));
  47. if (n <= 0) throw SystemException("Cannot get system directory long path name");
  48. std::string result;
  49. UnicodeConverter::toUTF8(buffer.begin(), result);
  50. if (result[result.size() - 1] != '\\') result.append("\\");
  51. return result;
  52. }
  53. throw SystemException("Cannot get temporary directory path");
  54. }
  55. std::string PathImpl::homeImpl()
  56. {
  57. std::string result;
  58. if (EnvironmentImpl::hasImpl("USERPROFILE"))
  59. {
  60. result = EnvironmentImpl::getImpl("USERPROFILE");
  61. }
  62. else if (EnvironmentImpl::hasImpl("HOMEDRIVE") && EnvironmentImpl::hasImpl("HOMEPATH"))
  63. {
  64. result = EnvironmentImpl::getImpl("HOMEDRIVE");
  65. result.append(EnvironmentImpl::getImpl("HOMEPATH"));
  66. }
  67. else
  68. {
  69. result = systemImpl();
  70. }
  71. std::string::size_type n = result.size();
  72. if (n > 0 && result[n - 1] != '\\')
  73. result.append("\\");
  74. return result;
  75. }
  76. std::string PathImpl::tempImpl()
  77. {
  78. Buffer<wchar_t> buffer(MAX_PATH_LEN);
  79. DWORD n = GetTempPathW(static_cast<DWORD>(buffer.size()), buffer.begin());
  80. if (n > 0)
  81. {
  82. n = GetLongPathNameW(buffer.begin(), buffer.begin(), static_cast<DWORD>(buffer.size()));
  83. if (n <= 0) throw SystemException("Cannot get temporary directory long path name");
  84. std::string result;
  85. UnicodeConverter::toUTF8(buffer.begin(), result);
  86. if (result[result.size() - 1] != '\\')
  87. result.append("\\");
  88. return result;
  89. }
  90. throw SystemException("Cannot get temporary directory path");
  91. }
  92. std::string PathImpl::nullImpl()
  93. {
  94. return "NUL:";
  95. }
  96. std::string PathImpl::expandImpl(const std::string& path)
  97. {
  98. std::wstring upath;
  99. UnicodeConverter::toUTF16(path, upath);
  100. Buffer<wchar_t> buffer(MAX_PATH_LEN);
  101. DWORD n = ExpandEnvironmentStringsW(upath.c_str(), buffer.begin(), static_cast<DWORD>(buffer.size()));
  102. if (n > 0 && n < buffer.size() - 1)
  103. {
  104. buffer[n + 1] = 0;
  105. std::string result;
  106. UnicodeConverter::toUTF8(buffer.begin(), result);
  107. return result;
  108. }
  109. else return path;
  110. }
  111. void PathImpl::listRootsImpl(std::vector<std::string>& roots)
  112. {
  113. roots.clear();
  114. wchar_t buffer[128];
  115. DWORD n = GetLogicalDriveStringsW(sizeof(buffer)/sizeof(wchar_t) - 1, buffer);
  116. wchar_t* it = buffer;
  117. wchar_t* end = buffer + (n > sizeof(buffer) ? sizeof(buffer) : n);
  118. while (it < end)
  119. {
  120. std::wstring udev;
  121. while (it < end && *it) udev += *it++;
  122. std::string dev;
  123. UnicodeConverter::toUTF8(udev, dev);
  124. roots.push_back(dev);
  125. ++it;
  126. }
  127. }
  128. } // namespace Poco