From 3add6b2f9e87258e06ecdf0120529a096e9ea8d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Sat, 20 Aug 2022 12:45:46 +0200 Subject: Test XML parsing. --- c/test.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 3 deletions(-) (limited to 'c/test.cpp') diff --git a/c/test.cpp b/c/test.cpp index 8d43a46..78f7017 100644 --- a/c/test.cpp +++ b/c/test.cpp @@ -1,5 +1,8 @@ #include #include +#include +#include +#include #include "data.h" #include "episodelistview.h" @@ -226,9 +229,10 @@ TESTS TEST(Internet) { HINTERNET hi, hiUrl; - unsigned char buf[2048]; + static unsigned char buf[8'388'608] = {0}; DWORD cbRead; + /* Download HTML. */ hi = InternetOpen(L"Episode Browser", INTERNET_OPEN_TYPE_DIRECT, nullptr, nullptr, /*INTERNET_FLAG_ASYNC*/0); @@ -240,10 +244,58 @@ TESTS if (!hiUrl) goto b; - if (!InternetReadFile(hiUrl, &buf, sizeof(buf), &cbRead)) - goto c; + cbRead = 1; + while (cbRead) + if (!InternetReadFile(hiUrl, &buf, sizeof(buf), &cbRead)) + goto c; + //printf("%s\n", buf); + + InternetCloseHandle(hiUrl); + InternetCloseHandle(hi); + + /* Parse HTML. */ + LIBXML_TEST_VERSION; + htmlDocPtr doc; + doc = htmlReadMemory(reinterpret_cast(buf), sizeof(buf), + "https://www.detectiveconanworld.com/wiki/Anime", nullptr, + HTML_PARSE_RECOVER|HTML_PARSE_NOERROR|HTML_PARSE_NOWARNING); + if (!doc) + goto z; + + xmlXPathContextPtr xpathCtx; + xmlXPathObjectPtr xpathObj; + + xpathCtx = xmlXPathNewContext(doc); + if (!xpathCtx) + goto y; + + xpathObj = xmlXPathEvalExpression( + reinterpret_cast("//tr"), + xpathCtx); + if (!xpathObj) + goto x; + + xmlNodeSetPtr nodes; + int cNodes; + nodes = xpathObj->nodesetval; + cNodes = nodes? nodes->nodeNr: 0; + + printf("%d nodes\n", cNodes); + for (int i = 0; i < cNodes; i++) { + xmlNodePtr node = nodes->nodeTab[i]; + printf("node \"%s\": type %d\n", node->name, node->type); + } + + xmlXPathFreeObject(xpathObj); + xmlXPathFreeContext(xpathCtx); + xmlFreeDoc(doc); return; + + x: xmlXPathFreeContext(xpathCtx); + y: xmlFreeDoc(doc); + z: FAIL(xmlGetLastError()->message); + c: InternetCloseHandle(hiUrl); b: InternetCloseHandle(hi); a: FAIL(Win32Error{}.what()); -- cgit v1.2.3