aboutsummaryrefslogtreecommitdiff
path: root/c/data.h
blob: 2cc4c14ca70eed6d6dfe4e4744f03fb021b890c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifndef DATA_H
#define DATA_H

#include "pl.h"
#include "util.h"
#include "wcharptr.h"

/* ElvDataA and DlvDataA are written as-is to disk; note the careful
 * alignment. As such, they should be regarded as immutable. If the
 * format needs to be changed in the future, then new structs called
 * ElvDataB and DlvDataB should be added. */

struct ElvDataA
{
	unsigned char version = 'a';
	unsigned char rating = 0;
	unsigned char bWatched = 0;
	unsigned char bTVOriginal = 0;
	wchar_t sRating[4] = {0};
	wchar_t siEp[6] = {0};
	wchar_t title[128] = {0};
};

struct DlvDataA
{
	wchar_t date[32] = {0};
	wchar_t source[48] = {0};
	wchar_t screenwriter[48] = {0};
	wchar_t hint[128] = {0};
	wchar_t wiki[192] = {0};
};

struct FileView
{
	FileView(const wchar_t* filename, size_t cb);
	~FileView();
	inline operator unsigned char*() { return view; }
	inline operator ElvDataA*() { return reinterpret_cast<ElvDataA*>(view); }
	HANDLE hf;
	HANDLE hm;
	unsigned char* view;
	/* TODO: Handle exceptions on read and write... */
};

void Write(unsigned char* buf, const ElvDataA& e);
ElvDataA* Read(unsigned char* buf);

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;

	if (Pl("episode_data","episode_rating",iEp,reinterpret_cast<int*>(&e.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