diff options
author | John Ankarström <john@ankarstrom.se> | 2022-08-22 03:17:54 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-08-22 03:21:52 +0200 |
commit | d7bb04a09c7f04b0c3a8580d6a9ccd6313656fcf (patch) | |
tree | 46f91cf658a39015d821d19935b6f99867ddbfed /c/data.cpp | |
parent | ba0750ff48bcf97ef99602fe27322fa706a93b6b (diff) | |
download | EpisodeBrowser-d7bb04a09c7f04b0c3a8580d6a9ccd6313656fcf.tar.gz |
Add InternetError exception.
Diffstat (limited to 'c/data.cpp')
-rw-r--r-- | c/data.cpp | 69 |
1 files changed, 34 insertions, 35 deletions
@@ -6,46 +6,45 @@ #include <libxml/xpath.h> #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 <auto F, typename T> 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); } } |