Re: [xml] GNOME VFS and LibXML



On ÐÑÐ, 2004-05-27 at 17:04 +0200, dodji seketeli org wrote:

You can read the documentation at http://xmlsoft.org/xmlio.html, and define
your own IO handler callbacks that make  use of GNOME VFS (for instance).

I was contemplating doing that for my project but I haven't had time to
properly do it yet. So if you could come out with a code snippet that shows
how to plug gnome vfs at xmlio level, it would be great ;)

Looks like I got it. It's quite simple. I used local documentation (from libxml2-doc debian package), so I 
can't give exact links to webpages (but
debian documentation is actually a copy of developer's part of libXML website, so look at filenames).

---

#include <glib.h>
#include <libgnomevfs/gnome-vfs.h>
#include <libxml/xmlIO.h>

/* A simple fwrite-like wrapper for gnome_vfs_write
   This is needed because xmlOutputWriteCallback must take only three parameters and return the number of 
bytes written or -1 on error (see
   /usr/share/doc/libxml2-doc/html/html/libxml-xmlIO.html#xmlOutputWriteCallback), and gnome_vfs_write take 
four parameters (fourth is the
   number of written bytes) and returns GnomeVFSResult.
*/
int gnomevfs_mywrite(GnomeVFSHandle *handle, const gchar *buf, gint len) {
        GnomeVFSFileSize written;

        if (gnome_vfs_write(handle, buf, len, &written) == GNOME_VFS_OK)
                return (int)written; /* GnomeVFSFileSize is a unsigned long long blah blah blah */
        else
                return -1;
};


/* Output buffer constructor (see /usr/share/doc/libxml2-doc/html/xmlio.html)
*/
xmlOutputBufferPtr gnomevfs_ouput(GnomeVFSHandle *handle) {
        xmlOutputBufferPtr ret;

        xmlRegisterDefaultOutputCallbacks();                                    /* Load default output 
functions */

        if (handle == NULL) return NULL;                                        /* Just check */
        if ((ret = xmlAllocOutputBuffer(NULL)) != NULL) {                       /* Creating an output buffer 
(see documentation) */
                ret->context = handle;                                          /* A context to use with 
callbacks */
                ret->writecallback = (xmlOutputWriteCallback)gnomevfs_mywrite;  /* A write function (look 
wrapper comment above) */
                ret->closecallback = (xmlOutputCloseCallback)gnome_vfs_close;   /* Close function (no 
wrapper, because gnome_vfs_close take only one parameter - handler (context) */
        }

        return ret;
};

/* Simple saving function
*/
void save_file(gchar *filename) {
        GnomeVFSHandle *handle;
        GnomeVFSResult result;
        xmlOutputBufferPtr output;
        xmlDocPtr doc;

        result = gnome_vfs_create(&handle, filename, GNOME_VFS_OPEN_WRITE, TRUE, S_IREAD|S_IWRITE);     /* 
Creating new file */
        if (result == GNOME_VFS_OK) {
                output = gnomevfs_ouput(handle);                /* Creating our handler */
                doc = xmlNewDoc(NULL);                          /* This will create an empty document */
                xmlSaveFormatFileTo(output, doc, NULL, 2);      /* Write our empty document (only xml header) 
to our output buffer */
        }
};

---

-- 
Dan Korostelev <dan ats energo ru>




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]