Re: Loading database from string (C API)
Carlo Capelli <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <CABty9wz3JhF7nO-3a8v4BsRfHTuLjfCTGa+hTQ6vgAS-wBd89Q@mail.gmail.com> |
2013/12/6 Edd Barrett <[email protected]> > Hi, > > I am writing a VM which embeds SWI-prolog using the C interface (I am > actually calling the SWI C interface via Python-CFFI, but it is all the > same). > > I need to be able to parse Prolog source code (in a C string) into the > Prolog > database. Looking at the FFI documentation [1] I don't see an obvious way > to do > this. I guess one way is to write the Prolog source code to a file and > construct a query to consult/1. > > Maybe it is possible to write the code to a memory file and use load_file/2 > to load from a stream, but that seems convoluted too. > > Is there a better way? > > I think you could benefit from using the C++ interface: for instance, here is how I load code from memory (Qt resources, mostly) bool SwiPrologEngine::in_thread::named_load(QString n, QString t, bool silent_yn) { try { PlTerm cs, s, opts; if ( atom_codes(A(t), cs) && open_chars_stream(cs, s)) { PlTail l(opts); l.append(stream(s)); if (silent_yn) l.append(silent(A("true"))); l.close(); bool rc = load_files(A(n), opts); close(s); return rc; } } catch(PlException ex) { qDebug() << t2w(ex); } return false; ... I get such a natural interface with a bit of syntax sugar, found in PREDICATE.h <https://github.com/CapelliC/pqConsole/blob/master/PREDICATE.h> Just declare as visible to working scope what symbols you need to access structure1(stream) structure1(silent) predicate2(atom_codes) predicate2(open_chars_stream) predicate2(load_files) predicate1(current_module) predicate1(close) bye Carlo Thanks > > P.S. Typo in documentation: > "security riscs" -> "security risks" > http://www.swi-prolog.org/pldoc/man?section=memory-files > > [1] http://www.swi-prolog.org/pldoc/man?section=foreign > -- > Best Regards > Edd Barrett > > http://www.theunixzoo.co.uk > _______________________________________________ > SWI-Prolog mailing list > [email protected] > https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog > -------------- next part -------------- HTML attachment scrubbed and removed