diff options
author | John Ankarström <john@ankarstrom.se> | 2022-08-20 12:45:46 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-08-20 12:45:46 +0200 |
commit | 3add6b2f9e87258e06ecdf0120529a096e9ea8d3 (patch) | |
tree | 8d8cbd11eb91c80e1a9375c7164ebe016b26d028 /c/test.cpp | |
parent | c501708dcd7a87e632c3db95bb4ffadadd847eb2 (diff) | |
download | EpisodeBrowser-3add6b2f9e87258e06ecdf0120529a096e9ea8d3.tar.gz |
Test XML parsing.
Diffstat (limited to 'c/test.cpp')
-rw-r--r-- | c/test.cpp | 58 |
1 files changed, 55 insertions, 3 deletions
@@ -1,5 +1,8 @@ #include <windows.h> #include <wininet.h> +#include <libxml/HTMLparser.h> +#include <libxml/HTMLtree.h> +#include <libxml/xpath.h> #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<const char*>(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<const xmlChar*>("//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()); |