diff options
Diffstat (limited to 'c')
-rw-r--r-- | c/data.h | 52 | ||||
-rw-r--r-- | c/datalistview.cpp | 2 | ||||
-rw-r--r-- | c/episodelistview.cpp | 8 | ||||
-rw-r--r-- | c/main.cpp | 14 | ||||
-rw-r--r-- | c/pl.cpp | 93 | ||||
-rw-r--r-- | c/pl.h | 124 | ||||
-rw-r--r-- | c/test.cpp | 64 | ||||
-rw-r--r-- | c/util.h | 1 | ||||
-rw-r--r-- | c/wcharptr.cpp | 64 | ||||
-rw-r--r-- | c/wcharptr.h | 33 | ||||
-rw-r--r-- | c/win.cpp | 25 | ||||
-rw-r--r-- | c/win.h | 3 |
12 files changed, 24 insertions, 459 deletions
@@ -4,9 +4,7 @@ #include <stdexcept> #include <windows.h> -#include "pl.h" #include "util.h" -#include "wcharptr.h" #include "win.h" /* Fetch data from the web. */ @@ -172,54 +170,4 @@ struct FileView } }; -inline int FromWeb(const int iEp, ElvDataA& e, DlvDataA& d) noexcept -{ - WcharPtr title, wiki, date, source, hint; - const int r = Pl("episode_data","fetch_episode_data",iEp,&title,&wiki,&date,&source,&hint); - if (title) Wcscpy(e.title, title); - if (wiki) Wcscpy(d.wiki, wiki); - if (date) Wcscpy(d.date, date); - if (source) Wcscpy(d.source, source); - if (hint) Wcscpy(d.hint, hint); - return r; -} - -inline bool FromProlog(const int iEp, ElvDataA& e) noexcept -{ - if (WcharPtr title; Pl("episode_data","episode_title",iEp,&title)) - Wcscpy(e.title, title); - else - return false; - - int rating; - if (Pl("episode_data","episode_rating",iEp,&rating)) { - e.rating = rating; - Swprintf(e.sRating, L"%d", e.rating); - } - - if (Pl("episode_data","tv_original",iEp)) - e.bTVOriginal = true; - - if (Pl("track_episodes","watched",iEp)) - e.bWatched = true; - - Swprintf(e.siEp, L"%d", iEp); - - return true; -} - -inline void FromProlog(const int iEp, DlvDataA& d) noexcept -{ - if (WcharPtr wiki; Pl("episode_data","episode_wiki",iEp,&wiki)) - Wcscpy(d.wiki, wiki); - if (WcharPtr screenwriter; Pl("episode_data","episode_datum",iEp,"Screenwriter",&screenwriter)) - Wcscpy(d.screenwriter, screenwriter); - if (WcharPtr date; Pl("episode_data","episode_datum",iEp,"Date",&date)) - Wcscpy(d.date, date); - if (WcharPtr source; Pl("episode_data","episode_datum",iEp,"Source",&source)) - Wcscpy(d.source, source); - if (WcharPtr hint; Pl("episode_data","episode_datum",iEp,"Hint",&hint)) - Wcscpy(d.hint, hint); -} - #endif diff --git a/c/datalistview.cpp b/c/datalistview.cpp index a71a270..ec9592d 100644 --- a/c/datalistview.cpp +++ b/c/datalistview.cpp @@ -2,7 +2,6 @@ #include <stdio.h> #include <windows.h> #include <commctrl.h> -#include <SWI-Prolog.h> #include "resource.h" #include "data.h" @@ -10,7 +9,6 @@ #include "episodelistview.h" #include "listview.h" #include "layout.h" -#include "wcharptr.h" extern CfgA& g_cfg; extern FileView<DlvDataA> g_fvDlv; diff --git a/c/episodelistview.cpp b/c/episodelistview.cpp index a76717a..72a8618 100644 --- a/c/episodelistview.cpp +++ b/c/episodelistview.cpp @@ -2,7 +2,6 @@ #include <vector> #include <windows.h> #include <commctrl.h> -#include <SWI-Prolog.h> #include "resource.h" #include "data.h" @@ -10,7 +9,6 @@ #include "episodelistview.h" #include "ext.h" #include "listview.h" -#include "pl.h" #include "util.h" #include "win.h" @@ -358,9 +356,6 @@ int CALLBACK EpisodeListView::SortProc(const LPARAM iItem1, const LPARAM iItem2, void EpisodeListView::Update() { - if (!Pl("episode_data","ensure_episode_data")) - return; - /* Save scrolling position. */ int iEpTop = 0; { @@ -371,9 +366,8 @@ void EpisodeListView::Update() /* Save selected episodes. */ int iItemMark; - static std::vector<int> vEpSel; + std::vector<int> vEpSel; { - vEpSel.clear(); LVITEM lvi = {LVIF_PARAM, -1}; while (FindNextItem(&lvi, LVNI_SELECTED)) vEpSel.push_back(lvi.lParam); @@ -2,7 +2,6 @@ #include <stdexcept> #include <windows.h> #include <commctrl.h> -#include <SWI-Prolog.h> #include <libxml/xmlversion.h> #include "debug.h" @@ -10,7 +9,6 @@ #include "datalistview.h" #include "episodelistview.h" #include "layout.h" -#include "pl.h" #include "test.h" #include "util.h" @@ -86,13 +84,6 @@ int WINAPI WinMain(const HINSTANCE hInstance, const HINSTANCE, char* const, cons setbuf(stdout, nullptr); LIBXML_TEST_VERSION; - /* Initialize Prolog. */ - const char* argv[] = {"EpisodeBrowser", nullptr}; - if (!PL_initialise(1, const_cast<char**>(argv))) - throw std::runtime_error("Could not initialize Prolog."); - if (!Pl("track_episodes","attach") || !Pl("episode_data","attach")) - throw std::runtime_error("Could not attach databases."); - INITCOMMONCONTROLSEX icc; icc.dwSize = sizeof(icc); icc.dwICC = ICC_WIN95_CLASSES; @@ -134,7 +125,7 @@ int WINAPI WinMain(const HINSTANCE hInstance, const HINSTANCE, char* const, cons ShowWindow(hWnd, nCmdShow); /* Populate episode list view. */ - Pl("track_episodes","update_tracked_episodes"); + /* TODO: Update tracked episodes. */ g_elv->Update(); g_elv->RestoreFocus(); @@ -149,7 +140,6 @@ int WINAPI WinMain(const HINSTANCE hInstance, const HINSTANCE, char* const, cons DispatchMessage(&msg); } - PL_halt(0); return 0; } @@ -274,7 +264,7 @@ LRESULT CALLBACK HandleMsg(const HWND hWnd, const UINT uMsg, const WPARAM wParam g_hWndFocus = GetFocus(); else { SetFocus(g_hWndFocus); - Pl("track_episodes","update_tracked_episodes"); + /* TODO: Update tracked episodes. */ g_elv->Redraw(); } return 0; diff --git a/c/pl.cpp b/c/pl.cpp deleted file mode 100644 index bab2f31..0000000 --- a/c/pl.cpp +++ /dev/null @@ -1,93 +0,0 @@ -#include <windows.h> -#include <SWI-Prolog.h> - -#include "pl.h" - -Frame::Frame() -{ - m_f = PL_open_foreign_frame(); -} - -Frame::~Frame() -{ - PL_close_foreign_frame(m_f); -} - -void Frame::Close() -{ - PL_close_foreign_frame(m_f); -} - -void Frame::Discard() -{ - PL_discard_foreign_frame(m_f); -} - -void Frame::Rewind() -{ - PL_rewind_foreign_frame(m_f); -} - -Mark::Mark() -{ - PL_mark_string_buffers(&m_m); -} - -Mark::~Mark() -{ - PL_release_string_buffers_from_mark(m_m); -} - -Query::Query(const module_t ctx, const predicate_t p, const term_t t0) -{ - m_q = PL_open_query(ctx, PL_Q_CATCH_EXCEPTION, p, t0); -} - -Query::~Query() -{ - PL_cut_query(m_q); -} - -int Query::Cut(std::nothrow_t) -{ - return PL_cut_query(m_q); -} -int Query::Cut() -{ - if (PL_cut_query(m_q)) return 1; - if (const term_t t = PL_exception(m_q)) throw t; - return 0; -} - -int Query::Close(std::nothrow_t) -{ - return PL_close_query(m_q); -} - -int Query::Close() -{ - if (PL_close_query(m_q)) return 1; - if (const term_t t = PL_exception(m_q)) throw t; - return 0; -} - -int Query::NextSolution(std::nothrow_t) -{ - return PL_next_solution(m_q); -} - -int Query::NextSolution() -{ - if (PL_next_solution(m_q)) return 1; - if (const term_t t = PL_exception(m_q)) throw t; - return 0; -} - -WcharPtr PlString(const term_t t, const int flags) -{ - char* s; - if (PL_get_chars(t, &s, flags)) - return {WcharPtr::FromNarrow(s)}; - else - return {}; -} @@ -1,124 +0,0 @@ -#ifndef PL_H -#define PL_H - -#include <string> -#include <windows.h> -#include <SWI-Prolog.h> - -#include "wcharptr.h" - -/* Convert Prolog term to wide characters. */ -WcharPtr PlString(const term_t t, const int flags = CVT_WRITE); - -/* Wrapper for opening and closing Prolog foreign frames. */ -struct Frame -{ - Frame(); - ~Frame(); - void Close(); - /* Close foreign frame AND invalidate associated data. */ - void Discard(); - void Rewind(); -private: - fid_t m_f; -}; - -/* Wrapper for marking and releasing strings on the Prolog stack. */ -struct Mark -{ - Mark(); - ~Mark(); -private: - buf_mark_t m_m; -}; - -/* Wrapper for opening, solving and cutting Prolog query. */ -struct Query -{ - Query(module_t ctx, predicate_t p, term_t t0); - ~Query(); - int Cut(); - int Cut(std::nothrow_t); - /* Cut query AND invalidate associated data. */ - int Close(); - int Close(std::nothrow_t); - int NextSolution(); - int NextSolution(std::nothrow_t); -private: - qid_t m_q; -}; - -/* Polymorphic aliases for PL_put_*, PL_get_*. */ -inline int PlPut(term_t t, int x) { return PL_put_integer(t, x); } -inline int PlPut(term_t t, long x) { return PL_put_integer(t, x); } -inline int PlPut(term_t t, long long x) { return PL_put_int64(t, x); } -inline int PlPut(term_t t, atom_t x) { return PL_put_atom(t, x); } -inline int PlPut(term_t t, char* x) { return PL_put_atom_chars(t, x); } -inline int PlPut(term_t t, const char* x) { return PL_put_atom_chars(t, x); } -inline int PlPut(term_t, int*) { return -1; } -inline int PlPut(term_t, long*) { return -1; } -inline int PlPut(term_t, long long*) { return -1; } -inline int PlPut(term_t, atom_t*) { return -1; } -inline int PlPut(term_t, char**) { return -1; } -inline int PlPut(term_t, WcharPtr*) { return -1; } - -inline int PlGet(term_t, int) { return -1; } -inline int PlGet(term_t, long) { return -1; } -inline int PlGet(term_t, long long) { return -1; } -inline int PlGet(term_t, atom_t) { return -1; } -inline int PlGet(term_t, char*) { return -1; } -inline int PlGet(term_t, const char*) { return -1; } -inline int PlGet(term_t t, int* x) { return PL_get_integer(t, x); } -inline int PlGet(term_t t, long* x) { return PL_get_long(t, x); } -inline int PlGet(term_t t, long long* x) { return PL_get_int64(t, x); } -inline int PlGet(term_t t, atom_t* x) { return PL_get_atom(t, x); } -inline int PlGet(term_t t, char** x) { return PL_get_atom_chars(t, x); } -inline int PlGet(term_t t, WcharPtr* x) -{ - Mark m; - char* s; - if (!PlGet(t, &s)) return 0; - *x = WcharPtr::FromNarrow(s); - return 1; -} - -/* Put in or get from a term reference an arbitrary number of values, - * returning false if any value could not be put/got. */ -template <typename = void> -inline bool PlPutN(term_t) { return true; } -template <typename T, typename... U> -inline bool PlPutN(term_t t, T x, U... xs) { return PlPut(t, x) && PlPutN(t+1, xs...); } - -template <typename = void> -inline bool PlGetN(term_t) { return true; } -template <typename T, typename... U> -inline bool PlGetN(term_t t, T x, U... xs) { return PlGet(t, x) && PlGetN(t+1, xs...); } - -/* Call Prolog predicate. */ -template <bool Except = false, typename... T> -int Pl(const char* const mod, const char* const pred, T... xs) -{ - Frame f; - const term_t t = PL_new_term_refs(sizeof...(T)); - - if (!PlPutN(t, xs...)) return 0; - Query q(nullptr, PL_predicate(pred, sizeof...(T), mod), t); - - if constexpr (Except) { - if (!q.NextSolution()) return 0; - } else { - if (!q.NextSolution(std::nothrow)) return 0; - } - - if (!PlGetN(t, xs...)) return 0; - return 1; -} - -/* Call Prolog predicate, propagating Prolog exceptions. */ -template <typename... T> -int Plx(const char* const mod, const char* const pred, T... xs) -{ - return Pl<true>(mod, pred, xs...); -} - -#endif @@ -4,7 +4,6 @@ #include "data.h" #include "episodelistview.h" #include "ext.h" -#include "pl.h" #include "util.h" #include "win.h" @@ -36,9 +35,9 @@ TESTS TEST(IO) { - ElvDataA e1_0, e2_0; - FromProlog(6, e1_0); - FromProlog(10, e2_0); + extern FileView<ElvDataA> g_fvElv; + ElvDataA& e1_0 = g_fvElv.At(5); + ElvDataA& e2_0 = g_fvElv.At(9); /* Write two ElvDataA structs to disk. */ { @@ -89,63 +88,10 @@ TESTS //DeleteFile(L"tmp.dat"); } - TEST(MigrateElvDataFromPrologToDisk) - { - int cEp; - if (!Pl("episode_data","episode_count",&cEp)) - return; - - { - FileView<ElvDataA> fv(L"tmp.dat", g_cfg.cEp+128u); - ElvDataA* p = fv; - - for (int iEp = 1; iEp <= cEp; iEp++) { - ElvDataA e; - FromProlog(iEp, e); - memcpy(p, &e, sizeof(e)); - p++; - } - } - { - FileView<ElvDataA> fv(L"tmp.dat", g_cfg.cEp+128u); - ElvDataA& e = fv.At(9); - if (wcscmp(e.title, L"Pro Soccer Player Blackmail Case") != 0) - FAIL("title is not correct"); - } - //DeleteFile(L"tmp.dat"); - } - - TEST(MigrateDlvDataFromPrologToDisk) - { - int cEp; - if (!Pl("episode_data","episode_count",&cEp)) - return; - - { - FileView<DlvDataA> fv(L"tmp.dat", g_cfg.cEp+128u); - DlvDataA* p = fv; - - for (int iEp = 1; iEp <= cEp; iEp++) { - DlvDataA d; - FromProlog(iEp, d); - memcpy(p, &d, sizeof(d)); - p++; - } - } - { - FileView<DlvDataA> fv(L"tmp.dat", g_cfg.cEp+128u); - DlvDataA& e = fv.At(9); - if (wcscmp(e.date, L"March 11, 1996") != 0) - FAIL("date is not correct"); - } - //DeleteFile(L"tmp.dat"); - } - // TEST(MigrateCfg) // { -// FileView<CfgA> fva(L"cfga.dat", 1); // FileView<CfgB> fvb = FileView<CfgB>::Initialized(L"cfgb.dat", 1); -// CfgA* a = fva+0; +// CfgA* a = &g_cfg; // CfgB* b = fvb+0; // #define CPY(member) b->member = a->member; @@ -172,8 +118,6 @@ int RunTests() const Test tests[] = { StrcpyWithSmallerDestination(), //IO(), - //MigrateElvDataFromPrologToDisk(), - //MigrateDlvDataFromPrologToDisk(), //MigrateCfg(), }; @@ -4,7 +4,6 @@ #include <cstring> #include <memory> #include <string> -#include <SWI-Prolog.h> #include <windows.h> #define CONCAT_IMPL(a, b) a##b diff --git a/c/wcharptr.cpp b/c/wcharptr.cpp deleted file mode 100644 index f659767..0000000 --- a/c/wcharptr.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include <utility> -#include <windows.h> - -#include "wcharptr.h" -#include "win.h" - -WcharPtr WcharPtr::FromNarrow(const char* const src, const int cp) -{ - int cchNarrow = strlen(src)+1; - int cchWide = MultiByteToWideChar(cp, 0, src, cchNarrow, nullptr, 0); - wchar_t* dst = new wchar_t[cchWide]; - if (!MultiByteToWideChar(cp, 0, src, cchNarrow, dst, cchWide)) { - delete dst; - throw Win32Error(); - } - return dst; -} - -WcharPtr WcharPtr::Copy(const wchar_t* const src) -{ - const int cch = wcslen(src)+1; - wchar_t* dst = new wchar_t[cch]; - memcpy(dst, src, cch*sizeof(wchar_t)); - return dst; -} - -WcharPtr::WcharPtr() noexcept {} - -WcharPtr::~WcharPtr() noexcept -{ - delete m_p; -} - -WcharPtr::operator wchar_t*() noexcept -{ - return m_p; -} - -WcharPtr::WcharPtr(wchar_t* const s) noexcept : m_p(s) {} - -WcharPtr& WcharPtr::operator=(wchar_t* const s) noexcept -{ - if (m_p != s) { - delete m_p; - m_p = s; - } - return *this; -} - -WcharPtr::WcharPtr(WcharPtr&& other) noexcept - : m_p(std::exchange(other.m_p, nullptr)) {} - -WcharPtr& WcharPtr::operator=(WcharPtr&& other) noexcept -{ - std::swap(m_p, other.m_p); - return *this; -} - -wchar_t* WcharPtr::Release() noexcept -{ - wchar_t* const p = m_p; - m_p = nullptr; - return p; -} diff --git a/c/wcharptr.h b/c/wcharptr.h deleted file mode 100644 index 2267017..0000000 --- a/c/wcharptr.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef WCHARPTR_H -#define WCHARPTR_H - -#include <windows.h> - -/* WcharPtr: Simple wrapper for wide C strings. */ -struct WcharPtr -{ - /* Named copy constructors (expensive). */ - static WcharPtr FromNarrow(const char* buf, int cp = CP_UTF8); - static WcharPtr Copy(const wchar_t* s); - - /* Non-explicit copies are disabled. */ - WcharPtr(WcharPtr& other) = delete; - WcharPtr& operator=(WcharPtr& other) = delete; - - WcharPtr() noexcept; - ~WcharPtr(); - operator wchar_t*() noexcept; - - WcharPtr(wchar_t* s) noexcept; - WcharPtr& operator=(wchar_t* s) noexcept; - - WcharPtr(WcharPtr&& other) noexcept; - WcharPtr& operator=(WcharPtr&& other) noexcept; - - /* Return pointer, releasing ownership. */ - wchar_t *Release() noexcept; -private: - wchar_t* m_p = nullptr; -}; - -#endif @@ -1,13 +1,21 @@ #include <utility> #include <string> -#include <SWI-Prolog.h> +#include <string_view> #include <windows.h> #include <wininet.h> -#include "pl.h" #include "util.h" #include "win.h" -#include "wcharptr.h" + +std::wstring WideFromNarrow(const std::string_view src, const int cp) +{ + int cchNarrow = src.length()+1; + int cchWide = MultiByteToWideChar(cp, 0, src.data(), cchNarrow, nullptr, 0); + std::wstring dst(cchWide, 0); + if (!MultiByteToWideChar(cp, 0, src.data(), cchNarrow, dst.data(), cchWide)) + throw Win32Error(); + return dst; +} void WithNextWindow(void (*proc)(HWND)) { @@ -87,19 +95,14 @@ void ShowException(const wchar_t* const fmt, const wchar_t* const title, const U { try { std::rethrow_exception(std::current_exception()); - } catch (const term_t& t) { - WcharPtr what = PlString(t); - std::wstring msg(wcslen(fmt)+wcslen(what), 0); - Swprintf(msg, fmt, static_cast<wchar_t*>(what)); - EBMessageBox(msg, title, uType); } catch (const WideException& e) { std::wstring msg(wcslen(fmt)+wcslen(e.What()), 0); Swprintf(msg, fmt, e.What()); EBMessageBox(msg, title, uType); } catch (const std::exception& e) { - auto what = WcharPtr::FromNarrow(e.what()); - std::wstring msg(wcslen(fmt)+wcslen(what), 0); - Swprintf(msg, fmt, static_cast<wchar_t*>(what)); + std::wstring what = WideFromNarrow(e.what()); + std::wstring msg(wcslen(fmt)+what.length(), 0); + Swprintf(msg, fmt, what.c_str()); EBMessageBox(msg, title, uType); } catch (...) { const wchar_t* what = L"an unknown error occurred"; @@ -6,6 +6,9 @@ #include <windows.h> #include <commctrl.h> +/* Convert narrow to wide string. */ +std::wstring WideFromNarrow(const std::string_view src, const int cp = CP_UTF8); + /* Run given procedure at creation of next window. */ void WithNextWindow(void (*proc)(HWND)); |