Re: Linkage with C++ programs

[email protected] Wed, 19 Jan 2005 19:44:11 -0800 (PST)
Newsgroups gmane.lisp.scheme.ssax-sxml
Message-ID <[email protected]>
Hello!

> I have another doubt - is is possible to call this code snippet ( i.e
> converitng xml to sxml ) from a c / c++ programs ? Plz give some simple
> example to link ssax lib and other programming languages.

Yes: one can call C (or C++, Fortran, etc) function from Scheme, and a
(compiled) Scheme function from C++. It is usually easier if the main
program is in Scheme. That main program can invoke as many foreign
functions as needed. Here's a simple example of parsing an XML file
and passing all character data to an external function -- in our case,
a two-argument `subset' of printf. If you wish to invoke a C++
function, you need to declare it as 
	``extern "C" <function-prototype>''
in your C++ code. The compiler is Bigloo 2.4b.

(module convert

  ; includes. If you use a SSAX distribution for Bigloo, you don't
  ; need them
  (include "myenv-bigloo.scm")
  (include "char-encoding.scm")
  (include "util.scm")
  (include "srfi-13-local.scm")
  (include "input-parse.scm")
  (include "look-for-str.scm")
  (include "catch-error.scm")
  (include "SSAX-expanded.scm")
  (include "SXML-tree-trans.scm")

  ; FFI declaration
  ; A `subset' of printf with two string arguments
  ; (C string: string)
  ; Please see the chapter on C interface in Bigloo documentation
  (extern (macro prn::int (::string ::string) "printf"))

  (main main))



(define (handle sxml)
  (pre-post-order
    sxml
    `((*default* . ,(lambda _ #f))
      (*text* . ,(lambda (tag str) 
		   (prn "text> %s\n" str))))))

(define (main argv)
  (if (not (= 2 (length argv)))
      (exit 4))     ; at least one argument, besides argv[0], is expected
  (handle
   (call-with-input-file (cadr argv)
     (lambda (port) (ssax:xml->sxml port '()))))
)


	Cheers,
	Oleg


-------------------------------------------------------
This SF.Net email is sponsored by: IntelliVIEW -- Interactive Reporting
Tool for open source databases. Create drag-&-drop reports. Save time
by over 75%! Publish reports on the web. Export to DOC, XLS, RTF, etc.
Download a FREE copy at http://www.intelliview.com/go/osdn_nl