Path_WIN32.cpp 2.7 KB

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