Next: , Previous: , Up: 2015   [Contents][Index]

Review of extensions for Vicare (part 5)

Posted on June 10, 2015

More reviews of Vicare’s extensions. Having formally abandoned Sourceforge and Gitorious, the code is on Github and Bitbucket.


Expat is an old but still working XML parser in the form of a C language library. Vicare/Expat is an interface for Vicare. I have updated it to Vicare Scheme version ‘0.4’ and created a Bitbucket mirror; the current head of Vicare/Expat’s master branch should work with the current head of Vicare Scheme’s master branch.

The API of Vicare/Expat is more low level than the one defined by other Vicare extensions: the “objects” of the API are raw pointers referencing data structures managed by C language code; there are no wrapper Scheme structures. This means more care must be taken when writing code with this package.

There are plenty of examples in Vicare/Expat’s documentation, but still here is one:

(import (vicare)
  (vicare xml expat)
  (prefix (only (vicare ffi)

(define (document->events doc)

  (define (main doc)
      (define parser
      (define start
	    (XML_StartElementHandler start-callback)
	   ( start))))
      (define end
	    (XML_EndElementHandler end-callback)
	   ( end))))
      (define cdata
	    (XML_CharacterDataHandler cdata-callback)
	   ( cdata))))
      (define comment
	    (XML_CommentHandler comment-callback)
	   ( comment))))
      (XML_SetElementHandler       parser start end)
      (XML_SetCharacterDataHandler parser cdata)
      (XML_SetCommentHandler       parser comment)
      (XML_Parse parser (string->utf8 doc) #f #t)
      (flush-output-port (current-output-port))))

  (define (start-callback data element attributes)
    (let ((element    (cstring->string element))
	  (attributes (argv->strings attributes)))
      (pretty-print (list 'start element attributes))))

  (define (end-callback data element)
    (let ((element (cstring->string element)))
      (pretty-print (list 'end element))))

  (define (cdata-callback data buf.ptr buf.len)
    (let ((text (cstring->string buf.ptr buf.len)))
      (pretty-print (list 'cdata text))))

  (define (comment-callback data cstr)
    (let ((text (cstring->string cstr)))
      (pretty-print (list 'comment text))))

  (main doc))

  "<!-- this is a test document -->\
  <thing colour=\"yellow\">\

which prints:

(comment " this is a test document ")
(start "stuff" ())
(start "thing" ("colour" "yellow"))
(start "alpha" ())
(cdata "one")
(end "alpha")
(start "beta" ())
(cdata "two")
(end "beta")
(end "thing")
(start "thing" ())
(start "alpha" ())
(cdata "123")
(end "alpha")
(start "beta" ())
(cdata "456")
(end "beta")
(end "thing")
(end "stuff")

Next: , Previous: , Up: 2015   [Contents][Index]