diff options
author | John Ankarström <john@ankarstrom.se> | 2022-08-21 00:47:54 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-08-21 00:47:54 +0200 |
commit | 593757d3d178e7f94376241a70ac29fe2bf94469 (patch) | |
tree | d5861e25f3707571d51bf5d33935c10ae8f2f9ab /c/test.cpp | |
parent | 3fa33fc6b16e066838f4db3e182776a04c5c7d26 (diff) | |
download | EpisodeBrowser-593757d3d178e7f94376241a70ac29fe2bf94469.tar.gz |
Rework remote data fetching.
Diffstat (limited to 'c/test.cpp')
-rw-r--r-- | c/test.cpp | 117 |
1 files changed, 11 insertions, 106 deletions
@@ -1,8 +1,4 @@ #include <windows.h> -#include <wininet.h> -#include <libxml/HTMLparser.h> -#include <libxml/HTMLtree.h> -#include <libxml/xpath.h> #include "data.h" #include "episodelistview.h" @@ -25,56 +21,6 @@ struct Test #define TEST(id) }; struct id : public Test { id() : Test(#id) #define FAIL(...) do { Sprintf(error, __VA_ARGS__); return; } while (0) -template <typename F> -struct Defer -{ - Defer(F dtor) : dtor(dtor) {} - ~Defer() { dtor(); } - F dtor; -}; - -#define DEFER(x) Defer APPLY(CAT, defer_, __COUNTER__) {[=](){x;}} - -struct InternetFile -{ - InternetFile(const wchar_t* url); - ~InternetFile(); - DWORD Read(void* buf, DWORD cb); - HINTERNET hi; - HINTERNET hiUrl; -}; - -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) { - InternetCloseHandle(hi); - throw Win32Error{}; - } -} - -InternetFile::~InternetFile() -{ - InternetCloseHandle(hiUrl); - InternetCloseHandle(hi); -} - -DWORD InternetFile::Read(void* buf, DWORD cb) -{ - DWORD cbRead; - if (InternetReadFile(hiUrl, buf, cb, &cbRead)) - return cbRead; - else - throw Win32Error{}; -} - TESTS { TEST(StrcpyWithSmallerDestination) @@ -252,7 +198,7 @@ TESTS //DeleteFile(L"tmp.dat"); } - TEST(DownloadData) + TEST(DownloadDataViaProlog) { WcharPtr title, wiki, date, source, hint; int i = 1053; @@ -276,60 +222,21 @@ TESTS g_cfg.cEp = i; } - TEST(XML) + TEST(Fetch) { - LIBXML_TEST_VERSION; - - InternetFile inf{L"https://www.detectiveconanworld.com/wiki/Anime"}; - char buf[1024]; - - htmlParserCtxtPtr ctxt = htmlCreatePushParserCtxt(nullptr, nullptr, - buf, sizeof(buf), "https://www.detectiveconanworld.com/wiki/Anime", - XML_CHAR_ENCODING_UTF8); - DEFER(xmlFreeParserCtxt(ctxt)); - - htmlCtxtUseOptions(ctxt, HTML_PARSE_RECOVER|HTML_PARSE_NOERROR|HTML_PARSE_NOWARNING); - - while (DWORD cbRead = inf.Read(&buf, sizeof(buf))) { - if (!htmlParseChunk(ctxt, buf, cbRead, 0)) - FAIL(xmlGetLastError()->message); - } - htmlParseChunk(ctxt, buf, 0, 1); /* Terminate. */ - - htmlDocPtr doc = ctxt->myDoc; - if (!doc) - FAIL(xmlGetLastError()->message); - DEFER(xmlFreeDoc(doc)); /* Needed? */ - - xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc); - if (!xpathCtx) - FAIL(xmlGetLastError()->message); - DEFER(xmlXPathFreeContext(xpathCtx)); - - xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression( - reinterpret_cast<const xmlChar*>("//tr/td[@style='background:#f2fde9;']"), - xpathCtx); - if (!xpathObj) - FAIL(xmlGetLastError()->message); - DEFER(xmlXPathFreeObject(xpathObj)); - - 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); - // } + extern FileView<ElvDataA> g_fvElv; + extern FileView<DlvDataA> g_fvDlv; + void FetchData(FileView<ElvDataA>& fvElv, FileView<DlvDataA>& fvDlv); + //FileView<ElvDataA> fvElv{L"testelv.dat", 1080}; + //FileView<DlvDataA> fvDlv{L"testdlv.dat", 1080}; + FetchData(g_fvElv, g_fvDlv); } }; int RunTests() { const Test tests[] = { - //StrcpyWithSmallerDestination{}, + StrcpyWithSmallerDestination{}, //EpisodeDataFromWeb{}, //EpisodeDataFromProlog{}, //IO{}, @@ -337,10 +244,8 @@ int RunTests() //SampleConfigurationToDisk{}, //MigrateCfg{} //MigrateDlvDataFromPrologToDisk{}, - //DownloadData{}, - XML{}, - //ImportElvData{}, - //ImportDlvData{}, + //DownloadDataViaProlog{}, + Fetch{}, }; printf("Results (%llu tests):\n", sizeof(tests)/sizeof(*tests)); |