aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--c/datalistview.c14
-rw-r--r--c/defs.h15
-rw-r--r--c/pl.c84
3 files changed, 73 insertions, 40 deletions
diff --git a/c/datalistview.c b/c/datalistview.c
index 5335b27..fbb46d0 100644
--- a/c/datalistview.c
+++ b/c/datalistview.c
@@ -46,17 +46,17 @@ DlvShowEpisode(int iEpisode)
lviKey.mask = LVIF_TEXT;
lviValue.mask = LVIF_TEXT;
- t = T(3);
- PI(t,iEpisode) return;
- q = Q("episode_data","episode_datum",3,t);
+ t = PL_new_term_refs(3);
+ if (!Plp(t,"I",iEpisode)) return;
+ q = PL_open_query(NULL, PL_Q_NORMAL,
+ PL_predicate("episode_datum", 3, "episode_data"), t);
- for (i = 0; Qn(q); i++) {
+ for (i = 0; PL_next_solution(q); i++) {
char *szKey;
char *szValue;
TCHAR *tszKey, *tszValue;
- GAC(t+1,&szKey) continue;
- GAC(t+2,&szValue) continue;
+ if (!Plg(t+1,"ss",&szKey,&szValue)) continue;
tszKey = TszFromSz(szKey, CP_UTF8);
if (!tszKey) continue;
@@ -78,7 +78,7 @@ DlvShowEpisode(int iEpisode)
c: free(tszKey);
}
- Qc(q);
+ PL_cut_query(q);
UpdateLayout();
lvfi.flags = LVFI_PARAM;
diff --git a/c/defs.h b/c/defs.h
index 2b4e0ba..8196a93 100644
--- a/c/defs.h
+++ b/c/defs.h
@@ -3,6 +3,7 @@
#include <windows.h>
#include <commctrl.h>
+#include <SWI-Prolog.h>
/* common.c */
TCHAR *TszFromSz(const char *, int);
@@ -32,6 +33,8 @@ void DlvShowEpisode(int);
/* pl.c */
int Pl(char *, char *, char *, ...);
+int Plp(term_t, char *, ...);
+int Plg(term_t, char *, ...);
/* defs.h */
#define DLVSIKEY 0
@@ -40,18 +43,6 @@ int Pl(char *, char *, char *, ...);
#define ELVSITITLE 1
#define ELVSIRATING 2
-#define P(m,p,a,t) if (!PL_call_predicate(NULL, PL_Q_NORMAL, \
- PL_predicate(p,a,m), t))
-#define Q(m,p,a,t) PL_open_query(NULL, PL_Q_NORMAL, PL_predicate(p,a,m), t)
-#define Qn(q) PL_next_solution(q)
-#define Qc(q) PL_cut_query(q)
-#define T(a) PL_new_term_refs(a)
-#define A(s) PL_new_atom(s)
-#define PA(t,x) if (!PL_put_atom(t,x))
-#define PI(t,x) if (!PL_put_integer(t,x))
-#define GI(t,x) if (!PL_get_integer(t,x))
-#define GAC(t,x) if (!PL_get_atom_chars(t,x))
-
inline int
Cmp(int a, int b)
{
diff --git a/c/pl.c b/c/pl.c
index 9a5baee..acb6ba3 100644
--- a/c/pl.c
+++ b/c/pl.c
@@ -1,26 +1,61 @@
#include <stdarg.h>
#include <string.h>
#include <SWI-Prolog.h>
+#include "defs.h"
+static int Plpv(term_t, char *, va_list);
+static int Plgv(term_t, char *, va_list);
+
+/* Call Prolog predicate once. */
int
Pl(char *szMod, char *szPred, char *szFmt, ...)
{
- int i, iArity;
+ int iArity;
term_t t;
va_list vl, vl2;
- va_start(vl, szFmt);
- va_copy(vl2, vl);
iArity = strlen(szFmt);
t = PL_new_term_refs(iArity);
+ va_start(vl, szFmt);
+ va_copy(vl2, vl);
+
+ if (!Plpv(t, szFmt, vl)) goto f;
+ if (!PL_call_predicate(NULL, PL_Q_NORMAL,
+ PL_predicate(szPred, iArity, szMod), t))
+ goto f;
+ if (!Plgv(t, szFmt, vl2)) goto f;
+
+ va_end(vl);
+ va_end(vl2);
+ return 1;
+f: va_end(vl);
+ va_end(vl2);
+ return 0;
+}
+
+/* Put known values in term. */
+int
+Plp(term_t t, char *szFmt, ...)
+{
+ int r;
+ va_list vl;
+ va_start(vl, szFmt);
+ r = Plpv(t, szFmt, vl);
+ va_end(vl);
+ return r;
+}
+int
+Plpv(term_t t, char *szFmt, va_list vl)
+{
+ int i;
for (i = 0; szFmt[i]; i++) {
switch (szFmt[i]) {
case 'I':
{
int x;
x = va_arg(vl, int);
- if (!PL_put_integer(t+i, x)) goto f;
+ if (!PL_put_integer(t+i, x)) return 0;
break;
}
case 'S':
@@ -29,7 +64,7 @@ Pl(char *szMod, char *szPred, char *szFmt, ...)
char *x;
x = va_arg(vl, char *);
a = PL_new_atom(x);
- if (!PL_put_atom(t+i, a)) goto f;
+ if (!PL_put_atom(t+i, a)) return 0;
break;
}
case 'i':
@@ -40,40 +75,47 @@ Pl(char *szMod, char *szPred, char *szFmt, ...)
break;
}
}
+ return 1;
+}
- if (!PL_call_predicate(NULL, PL_Q_NORMAL,
- PL_predicate(szPred, iArity, szMod), t))
- goto f;
-
+/* Get unknown values from term. */
+int
+Plg(term_t t, char *szFmt, ...)
+{
+ int r;
+ va_list vl;
+ va_start(vl, szFmt);
+ r = Plgv(t, szFmt, vl);
+ va_end(vl);
+ return r;
+}
+int
+Plgv(term_t t, char *szFmt, va_list vl)
+{
+ int i;
for (i = 0; szFmt[i]; i++) {
switch (szFmt[i]) {
case 'i':
{
int *lp;
- lp = va_arg(vl2, int *);
- if (!PL_get_integer(t+i, lp)) goto f;
+ lp = va_arg(vl, int *);
+ if (!PL_get_integer(t+i, lp)) return 0;
break;
}
case 's':
{
char **lp;
- lp = va_arg(vl2, char **);
- if (!PL_get_atom_chars(t+i, lp)) goto f;
+ lp = va_arg(vl, char **);
+ if (!PL_get_atom_chars(t+i, lp)) return 0;
break;
}
case 'I':
- va_arg(vl2, int);
+ va_arg(vl, int);
break;
case 'S':
- va_arg(vl2, char *);
+ va_arg(vl, char *);
break;
}
}
-
- va_end(vl);
- va_end(vl2);
return 1;
-f: va_end(vl);
- va_end(vl2);
- return 0;
}