From def5c50960eea4c112647f88361a3ae7155901a8 Mon Sep 17 00:00:00 2001 From: egor-tensin Date: Tue, 4 Jul 2023 00:48:00 +0000 Subject: =?UTF-8?q?Deploying=20to=20gh-pages=20from=20@=20egor-tensin/wina?= =?UTF-8?q?pi-common@0c196cbe8b4927c78c02b2c7312fc69a507db845=20?= =?UTF-8?q?=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- path_8cpp_source.html | 135 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 path_8cpp_source.html (limited to 'path_8cpp_source.html') diff --git a/path_8cpp_source.html b/path_8cpp_source.html new file mode 100644 index 0000000..5cd7a6c --- /dev/null +++ b/path_8cpp_source.html @@ -0,0 +1,135 @@ + + + + + + + +winapi_common: src/path.cpp Source File + + + + + + + + + +
+
+ + + + + + +
+
winapi_common +
+
+
+ + + + + + + + +
+
+ + +
+ +
+ + +
+
+
+
path.cpp
+
+
+
1 // Copyright (c) 2020 Egor Tensin <Egor.Tensin@gmail.com>
+
2 // This file is part of the "winapi-common" project.
+
3 // For details, see https://github.com/egor-tensin/winapi-common.
+
4 // Distributed under the MIT License.
+
5 
+
6 #include <winapi/error.hpp>
+
7 #include <winapi/path.hpp>
+
8 #include <winapi/utf8.hpp>
+
9 
+
10 #include <windows.h>
+
11 
+
12 #include <cstddef>
+
13 #include <limits>
+
14 #include <stdexcept>
+
15 #include <string>
+
16 #include <vector>
+
17 
+
18 namespace winapi {
+
19 namespace {
+
20 
+
21 std::wstring do_canonicalize(const std::wstring& path) {
+
22  static constexpr std::size_t init_buffer_size = MAX_PATH;
+
23  static_assert(init_buffer_size > 0, "init_buffer_size must be positive");
+
24 
+
25  std::vector<wchar_t> buffer;
+
26  buffer.resize(init_buffer_size);
+
27 
+
28  while (true) {
+
29  if (buffer.size() > std::numeric_limits<DWORD>::max())
+
30  throw std::range_error{"Path buffer is too large"};
+
31  const auto nch = ::GetFullPathNameW(
+
32  path.c_str(), static_cast<DWORD>(buffer.size()), buffer.data(), NULL);
+
33 
+
34  if (nch == 0) {
+
35  throw error::windows(GetLastError(), "GetFullPathNameW");
+
36  }
+
37 
+
38  if (nch < buffer.size()) {
+
39  return {buffer.data(), nch};
+
40  }
+
41 
+
42  if (nch > buffer.size()) {
+
43  buffer.resize(2 * buffer.size());
+
44  }
+
45  }
+
46 }
+
47 
+
48 } // namespace
+
49 
+
50 CanonicalPath::CanonicalPath(const std::string& path) : m_path(canonicalize(path)) {}
+
51 
+
52 std::string CanonicalPath::canonicalize(const std::string& path) {
+
53  return narrow(do_canonicalize(widen(path)));
+
54 }
+
55 
+
56 } // namespace winapi
+
Make std::system_error work with GetLastError().
+
+ + + + -- cgit v1.2.3