diff options
author | John Ankarström <john@ankarstrom.se> | 2022-08-17 03:15:37 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-08-17 03:15:37 +0200 |
commit | 2e8afa446f2444a8be5e6180bf90cfa48b6e02f0 (patch) | |
tree | 2ad81de172861419f16da8d4dd53074f80bd455d /c | |
parent | bd857d24443b8c8f5d2f58047c3f8ac5f058acea (diff) | |
download | EpisodeBrowser-2e8afa446f2444a8be5e6180bf90cfa48b6e02f0.tar.gz |
Add CfgA.
Diffstat (limited to 'c')
-rw-r--r-- | c/data.h | 21 | ||||
-rw-r--r-- | c/datalistview.cpp | 3 | ||||
-rw-r--r-- | c/episodelistview.cpp | 29 | ||||
-rw-r--r-- | c/episodelistview.h | 2 | ||||
-rw-r--r-- | c/layout.cpp | 6 | ||||
-rw-r--r-- | c/main.cpp | 57 | ||||
-rw-r--r-- | c/test.cpp | 39 | ||||
-rw-r--r-- | c/test.h | 7 |
8 files changed, 102 insertions, 62 deletions
@@ -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 <typename T> 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<wchar_t*>(L"Value"); lvc.cx = 500; ListView_InsertColumn(hWnd, DLVSIVALUE, &lvc); + + extern FileView<CfgA> 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<HMENU>(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<CfgA> 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<CfgA> 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<CfgA> 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<CfgA> 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<CfgA> 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<CfgA> g_fvCfg; + g_fvCfg->heightDlv = 0; UpdateLayout(); } void DlvDragger::Done() const { - Pl("cfg","set_dlv_height",g_dlv->Height()); + extern FileView<CfgA> g_fvCfg; + g_fvCfg->heightDlv = g_dlv->Height(); } @@ -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<CfgA> 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; @@ -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<ElvDataA> 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<ElvDataA> 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<unsigned char> fv{L"tmp.dat", 2*sizeof(ElvDataA)}; ElvDataA& e2 = *reinterpret_cast<ElvDataA*>(&fv.At(sizeof(ElvDataA))); @@ -102,7 +115,7 @@ TESTS return; { - FileView<ElvDataA> fv{L"tmp.dat", 8192}; + FileView<ElvDataA> 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<CfgA> 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<CfgA> 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)); @@ -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 |