From 9c237e1537238ebe95289ed6df82f6d43c482944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Wed, 24 Aug 2022 02:56:26 +0200 Subject: Fix bug in ParsedDoc. The parser context wants its own buffer, apparently. (The documentation is a little ... sub-par.) --- c/data.cpp | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'c/data.cpp') diff --git a/c/data.cpp b/c/data.cpp index 7c0fcb7..7722cd0 100644 --- a/c/data.cpp +++ b/c/data.cpp @@ -25,37 +25,39 @@ using XmlXPathObjectPtr = Managed; +using InternetHandle = Managed; +static InternetHandle s_hi = InternetOpen(L"Episode Browser", INTERNET_OPEN_TYPE_DIRECT, nullptr, nullptr, 0); + struct ParsedDoc { - using InternetHandle = Managed; using HtmlParserCtxtPtr = Managed; using HtmlDocPtr = Managed; - InternetHandle hi; InternetHandle hiUrl; HtmlParserCtxtPtr ctxt; - char buf[1024]; + char bufI[1024]; + char bufX[1024]; ParsedDoc(const wchar_t* wszUrl, const char* szUrl) - : hi(InternetOpen(L"Episode Browser", INTERNET_OPEN_TYPE_DIRECT, nullptr, nullptr, 0)), - hiUrl(InternetOpenUrl(hi, wszUrl, nullptr, 0, INTERNET_FLAG_NO_UI, 0)), - ctxt(htmlCreatePushParserCtxt(nullptr, nullptr, buf, sizeof(buf), szUrl, XML_CHAR_ENCODING_UTF8)) + : hiUrl(InternetOpenUrl(s_hi, wszUrl, nullptr, 0, INTERNET_FLAG_NO_UI, 0)), + ctxt(htmlCreatePushParserCtxt(nullptr, nullptr, bufX, sizeof(bufX), szUrl, XML_CHAR_ENCODING_UTF8)) { htmlCtxtUseOptions(ctxt, HTML_PARSE_RECOVER|HTML_PARSE_NOERROR|HTML_PARSE_NOWARNING); BOOL r; DWORD cbRead; - while (r = InternetReadFile(hiUrl, buf, sizeof(buf), &cbRead), cbRead) { + while (r = InternetReadFile(hiUrl, bufI, sizeof(bufI), &cbRead), cbRead) { if (!r) throw InternetError(); - if (!htmlParseChunk(ctxt, buf, cbRead, 0)) + if (!htmlParseChunk(ctxt, bufI, cbRead, 0)) throw XmlError(); } - htmlParseChunk(ctxt, buf, 0, 1); /* Stop parsing. */ + htmlParseChunk(ctxt, bufI, 0, 1); /* Stop parsing. */ } operator htmlDocPtr() { return ctxt->myDoc; } }; + template bool WcharsFromXmlchars(wchar_t (&dst)[N], XmlCharPtr utf8) noexcept { -- cgit v1.2.3