From 3962b1bdfb2a8a2e3a5ff4f4e51a61b0c44f2e6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Tue, 23 Aug 2022 01:43:09 +0200 Subject: Add Managed (generic RAII type). --- c/ext.cpp | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) (limited to 'c/ext.cpp') diff --git a/c/ext.cpp b/c/ext.cpp index f485dce..ee2c943 100644 --- a/c/ext.cpp +++ b/c/ext.cpp @@ -76,6 +76,8 @@ static inline bool MatchFileName(wchar_t (&file)[MAX_PATH], const wchar_t* const static bool FindMatchingFile(wchar_t (&file)[MAX_PATH], const wchar_t* const root, const wchar_t* const siEp, const int level = 0) { + using FindHandle = Managed; + /* Don't recurse too much. */ if (level > 3) return false; @@ -84,9 +86,7 @@ static bool FindMatchingFile(wchar_t (&file)[MAX_PATH], const wchar_t* const roo Swprintf(pat, L"%s\\*", root); WIN32_FIND_DATA fdata; - HANDLE hf = FindFirstFile(pat, &fdata); - if (hf == INVALID_HANDLE_VALUE) - throw Win32Error().what(); + FindHandle h = FindFirstFile(pat, &fdata); do { if (fdata.cFileName[0] == L'.') @@ -95,25 +95,18 @@ static bool FindMatchingFile(wchar_t (&file)[MAX_PATH], const wchar_t* const roo /* Recurse into directory. */ wchar_t root2[MAX_PATH]; Swprintf(root2, L"%s\\%s", root, fdata.cFileName); - try { - if (FindMatchingFile(file, root2, siEp, level+1)) - return true; - } catch (...) { - FindClose(hf); - throw; - } + if (FindMatchingFile(file, root2, siEp, level+1)) + return true; } else /* Try to match file name. */ if (MatchFileName(fdata.cFileName, siEp)) { Swprintf(file, L"%s\\%s", root, fdata.cFileName); return true; } - } while (FindNextFile(hf, &fdata)); + } while (FindNextFile(h, &fdata)); - DWORD e = GetLastError(); - FindClose(hf); - if (e != ERROR_NO_MORE_FILES) - throw Win32Error(e); + if (GetLastError() != ERROR_NO_MORE_FILES) + throw Win32Error(); return false; } -- cgit v1.2.3