From 593757d3d178e7f94376241a70ac29fe2bf94469 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Sun, 21 Aug 2022 00:47:54 +0200 Subject: Rework remote data fetching. --- c/test.cpp | 117 ++++++------------------------------------------------------- 1 file changed, 11 insertions(+), 106 deletions(-) (limited to 'c/test.cpp') diff --git a/c/test.cpp b/c/test.cpp index b125a20..5bd8958 100644 --- a/c/test.cpp +++ b/c/test.cpp @@ -1,8 +1,4 @@ #include -#include -#include -#include -#include #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 -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("//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 g_fvElv; + extern FileView g_fvDlv; + void FetchData(FileView& fvElv, FileView& fvDlv); + //FileView fvElv{L"testelv.dat", 1080}; + //FileView 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)); -- cgit v1.2.3