aboutsummaryrefslogtreecommitdiff
path: root/win.c
blob: 660275d8a033dfd12692266b214a352d04879155 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <windows.h>
#include <SWI-Prolog.h>
#include <stdio.h>

static int Attach(void);
static void ShowEpisode(int);
static void UpdateList(void);

/*
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    PSTR lpCmdLine, INT nCmdShow)
*/
int main(int argc, char *argv[])
{
	char *rgArgs[2];

	rgArgs[0] = "episode_browser";
	rgArgs[1] = NULL;

	if (!PL_initialise(1, rgArgs))
		PL_halt(1);

	Attach();
	//UpdateList();
	ShowEpisode(400);

	PL_halt(0);
}

/* Attach persistent databases. */
int Attach()
{
	int r;
	term_t t;

	t = PL_new_term_refs(2);
	r = PL_call_predicate(NULL, PL_Q_NORMAL,
		PL_predicate("attach",  0, "track_episodes"),
		t);
	if (!r) return r;

	r = PL_call_predicate(NULL, PL_Q_NORMAL,
		PL_predicate("attach",  0, "episode_data"),
		t);
	if (!r) return r;

	return 1;
}

/* Update episode list. */
void UpdateList()
{
	term_t t;
	qid_t q;

	t = PL_new_term_refs(2);
	PL_call_predicate(NULL, PL_Q_NORMAL,
		PL_predicate("update_tracked_episodes",  0, "track_episode"),
		t);

	q = PL_open_query(NULL, PL_Q_NORMAL,
		PL_predicate("episode_file", 2, "local_episodes"),
		t);

	while (PL_next_solution(q)) {
		char *sFile;
		int nEpisode;

		if (!PL_get_integer(t+0, &nEpisode))
			goto close;
		if (!PL_get_atom_chars(t+1, &sFile))
			goto close;
		printf("%d: %s\n", nEpisode, sFile);
	}

close:
	PL_close_query(q);		/* Or PL_cut_query? */
}

/* Show episode data. */
void ShowEpisode(int nEpisode)
{
	int r;
	term_t t;

	t = PL_new_term_refs(3);
	if(!PL_put_integer(t+0, nEpisode))
		return;

	r = PL_call_predicate(NULL, PL_Q_NORMAL,
		PL_predicate("lookup_episode_local",  3, "episode_data"),
		t);
	if (!r) return;

	/* The episode data is a list of unary compounds,
	 * whose name is the key and whose value is the value. */

	{
		term_t tHead, tList;

		tHead = PL_new_term_ref();
		tList = PL_copy_term_ref(t+2);

		while (PL_get_list(tList, tHead, tList)) {
			atom_t aKey;
			const char *sKey;
			char *sValue;
			term_t tValue;
			size_t iArity;

			if (!PL_get_name_arity(tHead, &aKey, &iArity))
				continue;
			if (!PL_get_arg(1, tHead, tValue))
				continue;
			sKey = PL_atom_chars(aKey);
			if(!PL_get_atom_chars(tValue, &sValue))
				continue;

			printf("%s/%d: %s\n", sKey, iArity, sValue);
		}
	}
}