From d7bb04a09c7f04b0c3a8580d6a9ccd6313656fcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Mon, 22 Aug 2022 03:17:54 +0200 Subject: Add InternetError exception. --- c/data.cpp | 69 +++++++++++++++++++++++++++++++------------------------------- 1 file changed, 34 insertions(+), 35 deletions(-) (limited to 'c/data.cpp') diff --git a/c/data.cpp b/c/data.cpp index 771e6fb..e260209 100644 --- a/c/data.cpp +++ b/c/data.cpp @@ -6,46 +6,45 @@ #include #include "data.h" +#include "win.h" struct InternetFile { - InternetFile(const wchar_t* url); - ~InternetFile(); - DWORD Read(void* buf, DWORD cb); - HINTERNET hi; - HINTERNET hiUrl; -}; + InternetFile(const wchar_t* url) + { + hi = InternetOpen(L"Episode Browser", + INTERNET_OPEN_TYPE_DIRECT, nullptr, nullptr, + /*INTERNET_FLAG_ASYNC*/0); + if (!hi) + throw Win32Error{}; + + hiUrl = InternetOpenUrl(hi, url, + nullptr, 0, INTERNET_FLAG_NO_UI, 0); + if (!hiUrl) { + DWORD e = GetLastError(); + InternetCloseHandle(hi); + throw InternetError(e); + } + } -InternetFile::InternetFile(const wchar_t* url) -{ - hi = InternetOpen(L"Episode Browser", - INTERNET_OPEN_TYPE_DIRECT, nullptr, nullptr, - /*INTERNET_FLAG_ASYNC*/0); - if (!hi) - throw Win32Error{}; - - hiUrl = InternetOpenUrl(hi, url, - nullptr, 0, INTERNET_FLAG_NO_UI, 0); - if (!hiUrl) { + ~InternetFile() + { + InternetCloseHandle(hiUrl); InternetCloseHandle(hi); - throw Win32Error{}; } -} -InternetFile::~InternetFile() -{ - InternetCloseHandle(hiUrl); - InternetCloseHandle(hi); -} + DWORD Read(void* buf, DWORD cb) + { + DWORD cbRead; + if (InternetReadFile(hiUrl, buf, cb, &cbRead)) + return cbRead; + else + throw InternetError{}; + } -DWORD InternetFile::Read(void* buf, DWORD cb) -{ - DWORD cbRead; - if (InternetReadFile(hiUrl, buf, cb, &cbRead)) - return cbRead; - else - throw Win32Error{}; -} + HINTERNET hi; + HINTERNET hiUrl; +}; template struct XmlPtr @@ -98,8 +97,8 @@ void FetchData() { LIBXML_TEST_VERSION; - InternetFile inf{L"https://www.detectiveconanworld.com/wiki/Anime"}; - //InternetFile inf{L"file://C:/Users/John/Desktop/dcw.html"}; + InternetFile inf(L"https://www.detectiveconanworld.com/wiki/Anime"); + //InternetFile inf(L"file://C:/Users/John/Desktop/dcw.html"); char buf[1024]; HtmlParserCtxtPtr ctxt = htmlCreatePushParserCtxt(nullptr, nullptr, @@ -167,6 +166,6 @@ void WaitFetchData(bool* bDone) noexcept *bDone = true; } catch (...) { *bDone = true; - ShowException(L"Remote data could not be fetched due to %s: %s", L"Error", MB_ICONWARNING); + ShowException(L"Remote data could not be fetched due to an error: %s", L"Error", MB_ICONWARNING); } } -- cgit v1.2.3