From 2e8afa446f2444a8be5e6180bf90cfa48b6e02f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Wed, 17 Aug 2022 03:15:37 +0200 Subject: Add CfgA. --- c/data.h | 21 +++++++++++++++++++ c/datalistview.cpp | 3 +++ c/episodelistview.cpp | 29 +++++++++++++------------- c/episodelistview.h | 2 ++ c/layout.cpp | 6 ++++-- c/main.cpp | 57 +++++++++++++++++++-------------------------------- c/test.cpp | 39 ++++++++++++++++++++++++++++++++--- c/test.h | 7 ------- 8 files changed, 102 insertions(+), 62 deletions(-) (limited to 'c') diff --git a/c/data.h b/c/data.h index a93b256..dd443b9 100644 --- a/c/data.h +++ b/c/data.h @@ -37,6 +37,22 @@ struct DlvDataA wchar_t wiki[192] = {0}; }; +/* Configuration. */ +struct CfgA +{ + unsigned char version = 'a'; + unsigned char bViewWatched = 1; + unsigned char bViewTVOriginal = 1; + unsigned char pad = 0; + int iSortCol = 1; + int iFocus = 0; + int heightDlv = 0; + wchar_t limitScreenwriter[64] = {0}; + wchar_t root[MAX_PATH] = {0}; + wchar_t glob[64] = {0}; + wchar_t url[192] = {0}; +}; + /* Variable template for obtaining the version of a given struct. */ template constexpr inline unsigned char Version = T{}.version; @@ -111,6 +127,11 @@ struct FileView return view; } + inline T* operator ->() noexcept + { + return view; + } + HANDLE hf; HANDLE hm; T* view; diff --git a/c/datalistview.cpp b/c/datalistview.cpp index 531dc2c..f04b270 100644 --- a/c/datalistview.cpp +++ b/c/datalistview.cpp @@ -27,6 +27,9 @@ DataListView::DataListView(const HWND hWndParent) lvc.pszText = const_cast(L"Value"); lvc.cx = 500; ListView_InsertColumn(hWnd, DLVSIVALUE, &lvc); + + extern FileView g_fvCfg; + m_height = g_fvCfg.At(0).heightDlv; } int DataListView::Height() diff --git a/c/episodelistview.cpp b/c/episodelistview.cpp index be753a6..824b527 100644 --- a/c/episodelistview.cpp +++ b/c/episodelistview.cpp @@ -12,11 +12,9 @@ #include "util.h" #include "win.h" -constexpr size_t MAX_EPISODES = 8192; - EpisodeListView::EpisodeListView(const HWND hWndParent) : ListView(hWndParent, reinterpret_cast(IDC_EPISODELISTVIEW), 0), - m_fv(L"elvdata.dat", MAX_EPISODES) + m_fv(L"elvdata.dat", ELVMAX) { LVCOLUMN lvc; @@ -37,8 +35,8 @@ EpisodeListView::EpisodeListView(const HWND hWndParent) ListView_InsertColumn(hWnd, ELVSIRATING, &lvc); /* Get saved sort-by-column setting. */ - if (!Pl("cfg","get_sort",&m_iSortCol)) - m_iSortCol = 1; + extern FileView g_fvCfg; + m_iSortCol = g_fvCfg->iSortCol; } void EpisodeListView::EnsureFocusVisible() @@ -139,7 +137,8 @@ LRESULT EpisodeListView::HandleNotify(const LPARAM lParam) /* The sign of m_iSortCol decides the sort order. */ m_iSortCol = abs(m_iSortCol) == iCol? -m_iSortCol: iCol; - Pl("cfg","set_sort",m_iSortCol); + extern FileView g_fvCfg; + g_fvCfg->iSortCol = m_iSortCol; Sort(); ShowFocus(); return 0; @@ -233,9 +232,10 @@ void EpisodeListView::RestoreFocus() int i, iEpisode, iItem; LVFINDINFO lvfi; extern DataListView* const g_dlv; + extern FileView g_fvCfg; iItem = 0; - if (!Pl("cfg","get_focus",&iEpisode)) return; + iEpisode = g_fvCfg->iFocus; lvfi.flags = LVFI_PARAM; lvfi.lParam = iEpisode; @@ -261,9 +261,10 @@ s: g_dlv->ShowEpisode(iEpisode); void EpisodeListView::SaveFocus() { + extern FileView g_fvCfg; LVITEM lvi = {LVIF_PARAM, -1}; if (FindNextItem(&lvi, LVNI_FOCUSED)) - Pl("cfg","set_focus",lvi.lParam); + g_fvCfg->iFocus = lvi.lParam; } void EpisodeListView::SetTop(const int iItem) @@ -405,15 +406,15 @@ void EpisodeListView::Update() if (!e.siEp[0]) continue; - if (extern char g_limitScreenwriter[]; - g_limitScreenwriter[0] && !Pl("episode_data","episode_datum",iEp, - "Screenwriter",g_limitScreenwriter)) - continue; + extern FileView g_fvCfg; + //if (g_fvCfg->limitScreenwriter[0] && !Pl("episode_data","episode_datum",iEp, + // "Screenwriter",g_fvCfg->limitScreenwriter)) + // continue; - if (extern int g_bViewWatched; !g_bViewWatched && e.bWatched) + if (!g_fvCfg->bViewWatched && e.bWatched) continue; - if (extern int g_bViewTVOriginal; !g_bViewTVOriginal && e.bTVOriginal) + if (!g_fvCfg->bViewTVOriginal && e.bTVOriginal) continue; /* Insert item. */ diff --git a/c/episodelistview.h b/c/episodelistview.h index f58f3ca..ee5bc22 100644 --- a/c/episodelistview.h +++ b/c/episodelistview.h @@ -12,6 +12,8 @@ #define ELVSITITLE 1 #define ELVSIRATING 2 +#define ELVMAX 8192 + struct EpisodeListView : public ListView { EpisodeListView(HWND hWndParent); diff --git a/c/layout.cpp b/c/layout.cpp index 09c0958..653b675 100644 --- a/c/layout.cpp +++ b/c/layout.cpp @@ -69,11 +69,13 @@ void DlvDragger::Drag(const int, const int y) const void DlvDragger::Reset() const { g_dlv->SetHeight(0); - Pl("cfg","set_dlv_height",0); + extern FileView g_fvCfg; + g_fvCfg->heightDlv = 0; UpdateLayout(); } void DlvDragger::Done() const { - Pl("cfg","set_dlv_height",g_dlv->Height()); + extern FileView g_fvCfg; + g_fvCfg->heightDlv = g_dlv->Height(); } diff --git a/c/main.cpp b/c/main.cpp index 9a4bcf2..1b818c0 100644 --- a/c/main.cpp +++ b/c/main.cpp @@ -51,10 +51,8 @@ EpisodeListView* g_elv; /* Layout handlers. */ DlvDragger g_dragDlv; -/* View settings. */ -int g_bViewWatched = 1; -int g_bViewTVOriginal = 1; -char g_limitScreenwriter[64]; +/* File views. */ +FileView g_fvCfg{L"cfg.dat", 1}; /* Optional Windows functions. */ BOOL (*IsThemeActive)(); @@ -217,21 +215,6 @@ void InitializeMainWindow(const HWND hWnd) g_dlv = new DataListView(hWnd); g_elv = new EpisodeListView(hWnd); - /* Get saved view settings. */ - Pl("cfg","get_view_watched",&g_bViewWatched); - Pl("cfg","get_view_tv_original",&g_bViewTVOriginal); - { - Mark m; - char* s; - if (Pl("cfg","get_limit_screenwriter",&s)) - Strcpy(g_limitScreenwriter, s); - } - { - int dlvHeight = 0; - Pl("cfg","get_dlv_height",&dlvHeight); - g_dlv->SetHeight(dlvHeight); - } - /* The global main window handle must only be set AFTER * successful initialization. */ g_hWnd = hWnd; @@ -246,9 +229,12 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, SetFocus(g_elv->hWnd); /* Set menu item checkmarks according to saved settings. */ - CheckMenuItem(GetMenu(hWnd), IDM_VIEW_WATCHED, g_bViewWatched? MF_CHECKED: MF_UNCHECKED); - CheckMenuItem(GetMenu(hWnd), IDM_VIEW_TV_ORIGINAL, g_bViewTVOriginal? MF_CHECKED: MF_UNCHECKED); - CheckMenuItem(GetMenu(hWnd), IDM_VIEW_OTHERS, g_limitScreenwriter[0]? MF_UNCHECKED: MF_CHECKED); + CheckMenuItem(GetMenu(hWnd), IDM_VIEW_WATCHED, + g_fvCfg->bViewWatched? MF_CHECKED: MF_UNCHECKED); + CheckMenuItem(GetMenu(hWnd), IDM_VIEW_TV_ORIGINAL, + g_fvCfg->bViewTVOriginal? MF_CHECKED: MF_UNCHECKED); + CheckMenuItem(GetMenu(hWnd), IDM_VIEW_OTHERS, + g_fvCfg->limitScreenwriter[0]? MF_UNCHECKED: MF_CHECKED); return 0; case WM_CLOSE: @@ -340,13 +326,13 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, /*IDM_FILE_FETCH_DATA*/L"Fetch episode data from the web (may take a few seconds).", /*IDM_FILE_FETCH_SCREENWRITERS*/L"Fetch screenwriters from the web (may take a minute).", /*IDM_FILE_ABOUT*/L"Show information about Episode Browser.", - /*IDM_VIEW_WATCHED*/(g_bViewWatched? + /*IDM_VIEW_WATCHED*/(g_fvCfg->bViewWatched? L"Click to hide watched episodes.": L"Click to show watched episodes."), - /*IDM_VIEW_TV_ORIGINAL*/(g_bViewTVOriginal? + /*IDM_VIEW_TV_ORIGINAL*/(g_fvCfg->bViewTVOriginal? L"Click to hide TV original episodes.": L"Click to show TV original episodes."), - /*IDM_VIEW_OTHERS*/(g_limitScreenwriter? + /*IDM_VIEW_OTHERS*/(g_fvCfg->limitScreenwriter? L"Click to hide episodes by other screenwriters.": L"Click to show episodes by other screenwriters.") }; @@ -429,9 +415,9 @@ void HandleMainMenu(const HWND hWnd, const WORD command) break; case IDM_VIEW_WATCHED: - CheckMenuItem(GetMenu(hWnd), IDM_VIEW_WATCHED, g_bViewWatched? MF_UNCHECKED: MF_CHECKED); - g_bViewWatched = !g_bViewWatched; - Pl("cfg","set_view_watched",g_bViewWatched); + CheckMenuItem(GetMenu(hWnd), IDM_VIEW_WATCHED, + g_fvCfg->bViewWatched? MF_UNCHECKED: MF_CHECKED); + g_fvCfg->bViewWatched = !g_fvCfg->bViewWatched; g_elv->Update(); g_elv->EnsureFocusVisible(); /* TODO: Remember last valid focus. In case of @@ -439,28 +425,27 @@ void HandleMainMenu(const HWND hWnd, const WORD command) break; case IDM_VIEW_TV_ORIGINAL: - CheckMenuItem(GetMenu(hWnd), IDM_VIEW_TV_ORIGINAL, g_bViewTVOriginal? MF_UNCHECKED: MF_CHECKED); - g_bViewTVOriginal = !g_bViewTVOriginal; - Pl("cfg","set_view_tv_original",g_bViewTVOriginal); + CheckMenuItem(GetMenu(hWnd), IDM_VIEW_TV_ORIGINAL, + g_fvCfg->bViewTVOriginal? MF_UNCHECKED: MF_CHECKED); + g_fvCfg->bViewTVOriginal = !g_fvCfg->bViewTVOriginal; g_elv->Update(); g_elv->EnsureFocusVisible(); break; case IDM_VIEW_OTHERS: - if (g_limitScreenwriter[0]) { /* Show episodes by all screenwriters. */ + if (g_fvCfg->limitScreenwriter[0]) { /* Show episodes by all screenwriters. */ CheckMenuItem(GetMenu(hWnd), IDM_VIEW_OTHERS, MF_CHECKED); - g_limitScreenwriter[0] = 0; + g_fvCfg->limitScreenwriter[0] = 0; } else { /* Hide episodes by other screenwriters than current. */ Mark m; - char* s; + WcharPtr s; LVITEM lvi = {LVIF_PARAM, -1}; if (g_elv->FindNextItem(&lvi, LVNI_FOCUSED) && Pl("episode_data","episode_datum",lvi.lParam,"Screenwriter",&s)) { - Strcpy(g_limitScreenwriter, s); + Wcscpy(g_fvCfg->limitScreenwriter, s); CheckMenuItem(GetMenu(hWnd), IDM_VIEW_OTHERS, MF_UNCHECKED); } } - Pl("cfg","set_limit_screenwriter",g_limitScreenwriter); g_elv->Update(); g_elv->EnsureFocusVisible(); break; diff --git a/c/test.cpp b/c/test.cpp index 84e0631..b8cf20a 100644 --- a/c/test.cpp +++ b/c/test.cpp @@ -1,10 +1,15 @@ #include "data.h" -#include "test.h" +#include "episodelistview.h" #include "pl.h" #include "util.h" #include "win.h" -Test::Test(const char* name_) : name(name_) {} +struct Test +{ + const char* name = {0}; + char error[64] = {0}; + Test(const char* name) : name(name) {} +}; #define CAT(a, b) a##b #define APPLY(a, ...) a(__VA_ARGS__) @@ -53,11 +58,16 @@ TESTS ElvDataA e1_0, e2_0; FromProlog(6, e1_0); FromProlog(10, e2_0); + + /* Write two ElvDataA structs to disk. */ { FileView fv{L"tmp.dat", 2}; memcpy(fv+0, &e1_0, sizeof(e1_0)); memcpy(fv+1, &e2_0, sizeof(e2_0)); } + + /* Read first ElvDataA struct from disk using + * ElvDataA-typed FileView. */ { FileView fv{L"tmp.dat", 2}; const ElvDataA& e1 = *fv; @@ -76,6 +86,9 @@ TESTS if (wcscmp(e1_0.title, e1.title) != 0) FAIL("title is different"); } + + /* Read second ElvDataA struct from disk using + * unsigned char-typed FileView. */ { FileView fv{L"tmp.dat", 2*sizeof(ElvDataA)}; ElvDataA& e2 = *reinterpret_cast(&fv.At(sizeof(ElvDataA))); @@ -102,7 +115,7 @@ TESTS return; { - FileView fv{L"tmp.dat", 8192}; + FileView fv{L"tmp.dat", ELVMAX}; ElvDataA* p = fv; for (int iEp = 1; iEp <= cEp; iEp++) { @@ -120,6 +133,25 @@ TESTS } //DeleteFile(L"tmp.dat"); } + + TEST(SampleConfigurationToDisk) + { + CfgA cfg_0; + { + FileView fv{L"tmp.dat", 1}; + Wcscpy(cfg_0.url, L"https://animixplay.to/v1/detective-conan/ep"); + memcpy(fv, &cfg_0, sizeof(cfg_0)); + } + { + FileView fv{L"tmp.dat", 1}; + const CfgA& cfg = fv.At(0); + if (cfg_0.bViewWatched != cfg.bViewWatched) + FAIL("bViewWatched is different"); + if (wcscmp(cfg_0.url, cfg.url) != 0) + FAIL("url is not correct"); + } + //DeleteFile(L"tmp.dat"); + } }; int RunTests() @@ -130,6 +162,7 @@ int RunTests() EpisodeDataFromProlog{}, IO{}, //MigrateElvDataFromPrologToDisk{}, + SampleConfigurationToDisk{}, }; printf("Results (%llu tests):\n", sizeof(tests)/sizeof(*tests)); diff --git a/c/test.h b/c/test.h index 5565b36..50ae9e6 100644 --- a/c/test.h +++ b/c/test.h @@ -1,13 +1,6 @@ #ifndef TEST_H #define TEST_H -struct Test -{ - const char* name = {0}; - char error[64] = {0}; - Test(const char* name); -}; - int RunTests(); #endif -- cgit v1.2.3