#include #include #include 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); } } }