aboutsummaryrefslogtreecommitdiff
path: root/c/ext.cpp
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-08-23 01:43:09 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-08-23 01:43:09 +0200
commit3962b1bdfb2a8a2e3a5ff4f4e51a61b0c44f2e6b (patch)
tree1d7ee76b4f2bfb3f205f6cacb39afdbd68062545 /c/ext.cpp
parent74b9361ccc77bcbb6b8188ad5914d6b26530d26a (diff)
downloadEpisodeBrowser-3962b1bdfb2a8a2e3a5ff4f4e51a61b0c44f2e6b.tar.gz
Add Managed (generic RAII type).
Diffstat (limited to 'c/ext.cpp')
-rw-r--r--c/ext.cpp23
1 files changed, 8 insertions, 15 deletions
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<HANDLE, FindClose, Win32Error, -1>;
+
/* 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;
}