-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathiafx.cpp
More file actions
126 lines (99 loc) · 3.47 KB
/
iafx.cpp
File metadata and controls
126 lines (99 loc) · 3.47 KB
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
123
124
125
126
/*
IL : Intlog Language
Object Oriented Prolog Project
Copyright (C) 1992-2021 - Ing. Capelli Carlo
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <sstream>
#include "iafx.h"
#include "qdata.h"
#include "parse.h"
#include "eng.h"
#ifdef _AFXDLL
#include "afxdllx.h"
static AFX_EXTENSION_MODULE NEAR extensionDLL = { 0, 0 };
#endif
static int runquery(IntlogExec *, Term, kstr_list *, Term*, unsigned);
#ifdef _AFXDLL
extern "C" int CALLBACK LibMain(HINSTANCE hInstance, WORD, WORD, LPSTR) {
AfxInitExtensionModule(extensionDLL, hInstance);
return 1;
}
#endif
/////////////////////////////////
// init DLL: if no engPtr given
// rely on default IO handlers
//
IIFTYPE(void) IAFX_Initialize() {
#ifdef _AFXDLL
// create a new CDynLinkLibrary
new CDynLinkLibrary(extensionDLL);
TRACE("IAFX_Initialize\n");
#endif
// force linkage
SetEngines(nullptr);
}
IIFTYPE(void) IAFX_Terminate() {
}
/////////////////////////////////////////////////////////////////
// attempt a parse and call
// if query succeed, copy vars values to v[] (up to nv elements)
// you must guarantee that nv >= 'num of vars in src'
//
IIFTYPE(int) IAFX_QueryString(const char *src, EngHandle h, Term **var_val, kstr_list *var_ids) {
int rc = -1; // if some error occurs
IntlogExec *ile = GetEngines()->HtoD(h);
if (ile != nullptr) {
// prepare input source stream
IntlogParser* parser = GetEngines()->get_parser();
istringstream srcstream(src);
// save status
parser->SetSource(&srcstream, "QueryString", var_ids);
// submit source line: if parse ok, query
if (parser->getinput() == IntlogParser::NewClause) {
Term tparsed = parser->parsed;
unsigned nv = var_ids->numel();
if (nv > 0)
*var_val = new Term[nv];
rc = runquery(ile, tparsed, var_ids, *var_val, nv);
tparsed.Destroy();
}
}
// some error occurred
return rc;
}
//////////////////////////////////////////
// run query with some interface handling
//
IIFTYPE(int) IAFX_QueryTerm(Term toQuery, EngHandle h, Term *v, kstr_list *vars) {
IntlogExec *ile = GetEngines()->HtoD(h);
return ile? runquery(ile, toQuery, vars, v, vars->numel()) : -1;
}
//////// LOCALs //////////
static int runquery(
IntlogExec *eng, Term tquery, kstr_list *var_ids,
Term *v, unsigned nv) {
for (unsigned j = 0; j < nv; j++)
v[j] = Term(f_NOTERM);
// save current status
ProofStatus ps(eng);
stkpos cvbase = ps.pdim();
Clause q(tquery, var_ids, eng->get_db());
int rc = eng->query(&q);
if (rc) // query succed: copy variables to caller
for (unsigned i = 0; i < nv; i++)
v[i] = eng->value(Var(i), cvbase);
q.no_image();
ps.restore();
return rc;
}