xmpedit

GTK+ editor for XMP metadata embedded in images
git clone https://code.djc.id.au/git/xmpedit/
commit 73cacecbfe295f0dced1d8d0a544d0aa9a203f3e
parent 0f970b261f52d6c4096fc7351e3af3e86ea90b50
Author: Dan Callaghan <djc@djc.id.au>
Date:   Mon,  6 Sep 2010 20:19:50 +1000

better glib-ish bindings for genx, including error checking

Diffstat:
Mbuild | 5+++--
Msrc/RDF_Writer.vala | 61+++++++++++++++++++------------------------------------------
Msrc/genx-glib.c | 90+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Asrc/genx-glib.h | 24++++++++++++++++++++++++
Mvapi/genx.vapi | 53++++++++++++++++++-----------------------------------
5 files changed, 152 insertions(+), 81 deletions(-)
diff --git a/build b/build
@@ -20,8 +20,9 @@ def files_under(path):
     return result
 
 def compile_vala(sources, target, pkgs=[], defs=[], vapidirs=[]):
-    invoke(['valac', '--save-temps', '-d', 'target', '-o', target] +
-           ['--Xcc=-Ilib/genx'] + # XXX generalise
+    invoke(['valac', '-g', '--save-temps', '-d', 'target', '-o', target] +
+           ['--Xcc=-g', '--Xcc=-Wall', '--Xcc=-O'] +
+           ['--Xcc=-Isrc', '--Xcc=-Ilib/genx'] + # XXX generalise
            ['--pkg=%s' % pkg for pkg in pkgs] +
            ['--define=%s' % define for define in defs] +
            ['--vapidir=%s' % vapidir for vapidir in vapidirs] +
diff --git a/src/RDF_Writer.vala b/src/RDF_Writer.vala
@@ -20,30 +20,19 @@ private class Writer {
      * only includes statements reachable from start_node!
      */
     public void write(URIRef start_node) {
-        Genx.Status status;
-        status = genx_writer.start_doc(output);
-        assert(status == Genx.Status.SUCCESS);
-        unowned Genx.Namespace rdf_ns = genx_writer.declare_namespace(RDF_NS, "rdf", out status);
-        assert(status == Genx.Status.SUCCESS);
-        unowned Genx.Element rdf_el = genx_writer.declare_element(rdf_ns, "RDF", out status);
-        assert(status == Genx.Status.SUCCESS);
+        genx_writer.start_doc(output);
+        unowned Genx.Namespace rdf_ns = genx_writer.declare_namespace(RDF_NS, "rdf");
+        unowned Genx.Element rdf_el = genx_writer.declare_element(rdf_ns, "RDF");
         
-        rdf_description_el = genx_writer.declare_element(rdf_ns, "Description", out status);
-        assert(status == Genx.Status.SUCCESS);
-        rdf_about_attr = genx_writer.declare_attribute(rdf_ns, "about", out status);
-        assert(status == Genx.Status.SUCCESS);
-        unowned Genx.Namespace xml_ns = genx_writer.declare_namespace(XML_NS, "xml", out status);
-        assert(status == Genx.Status.SUCCESS);
-        xml_lang_attr = genx_writer.declare_attribute(xml_ns, "lang", out status);
-        assert(status == Genx.Status.SUCCESS);
+        rdf_description_el = genx_writer.declare_element(rdf_ns, "Description");
+        rdf_about_attr = genx_writer.declare_attribute(rdf_ns, "about");
+        unowned Genx.Namespace xml_ns = genx_writer.declare_namespace(XML_NS, "xml");
+        xml_lang_attr = genx_writer.declare_attribute(xml_ns, "lang");
         
-        status = rdf_el.start();
-        assert(status == Genx.Status.SUCCESS);
+        rdf_el.start();
         write_resource(start_node);
-        status = genx_writer.end_element();
-        assert(status == Genx.Status.SUCCESS);
-        status = genx_writer.end_doc();
-        assert(status == Genx.Status.SUCCESS);
+        genx_writer.end_element();
+        genx_writer.end_document();
     }
     
     /* for efficiency */
@@ -52,41 +41,31 @@ private class Writer {
     private unowned Genx.Attribute xml_lang_attr;
     
     private void write_resource(URIRef node) {
-        Genx.Status status;
-        status = rdf_description_el.start();
-        assert(status == Genx.Status.SUCCESS);
-        status = rdf_about_attr.add(node.uri);
-        assert(status == Genx.Status.SUCCESS);
+        rdf_description_el.start();
+        rdf_about_attr.add(node.uri);
         foreach (var statement in graph.find_matching_statements(node, null, null)) {
             write_property(statement.predicate, statement.object);
         }
-        status = genx_writer.end_element();
-        assert(status == Genx.Status.SUCCESS);
+        genx_writer.end_element();
     }
     
     private void write_property(URIRef predicate, Node object) {
         unowned Genx.Namespace ns;
         string local_name;
         split_uri(predicate.uri, out ns, out local_name);
-        Genx.Status status;
         unowned Genx.Element property_el; // XXX reuse
-        property_el = genx_writer.declare_element(ns, local_name, out status);
-        assert(status == Genx.Status.SUCCESS);
-        status = property_el.start();
-        assert(status == Genx.Status.SUCCESS);
+        property_el = genx_writer.declare_element(ns, local_name);
+        property_el.start();
         if (object is PlainLiteral) {
             PlainLiteral literal = (PlainLiteral) object;
             if (literal.lang != null) {
-                status = xml_lang_attr.add(literal.lang);
-                assert(status == Genx.Status.SUCCESS);
+                xml_lang_attr.add(literal.lang);
             }
-            status = genx_writer.add_text(literal.lexical_value);
-            assert(status == Genx.Status.SUCCESS);
+            genx_writer.add_text(literal.lexical_value);
         } else {
             assert_not_reached();
         }
-        status = genx_writer.end_element();
-        assert(status == Genx.Status.SUCCESS);
+        genx_writer.end_element();
     }
     
     private void split_uri(string uri,
@@ -94,9 +73,7 @@ private class Writer {
         var last_slash = uri.pointer_to_offset(uri.rchr(-1, '/')); // XXX crude
         var ns_uri = uri.substring(0, last_slash + 1);
         var ns_prefix = is_wellknown_ns(ns_uri);
-        Genx.Status status;
-        ns = genx_writer.declare_namespace(ns_uri, ns_prefix, out status);
-        assert(status == Genx.Status.SUCCESS);
+        ns = genx_writer.declare_namespace(ns_uri, ns_prefix);
         local_name = uri.substring(last_slash + 1);
     }
     
diff --git a/src/genx-glib.c b/src/genx-glib.c
@@ -2,6 +2,29 @@
 #include <glib.h>
 #include <genx.h>
 
+G_STATIC_ASSERT(sizeof(char) == sizeof(gchar));
+
+genxNamespace genx_writer_declare_namespace(genxWriter w, const char *uri, const char *prefix) {
+    genxStatus status;
+    genxNamespace retval = genxDeclareNamespace(w, (constUtf8) uri, (constUtf8) prefix, &status);
+    g_return_val_if_fail(status == GENX_SUCCESS, NULL);
+    return retval;
+}
+
+genxElement genx_writer_declare_element(genxWriter w, genxNamespace ns, const char *type) {
+    genxStatus status;
+    genxElement retval = genxDeclareElement(w, ns, (constUtf8) type, &status);
+    g_return_val_if_fail(status == GENX_SUCCESS, NULL);
+    return retval;
+}
+
+genxAttribute genx_writer_declare_attribute(genxWriter w, genxNamespace ns, const char *name) {
+    genxStatus status;
+    genxAttribute retval = genxDeclareAttribute(w, ns, (constUtf8) name, &status);
+    g_return_val_if_fail(status == GENX_SUCCESS, NULL);
+    return retval;
+}
+
 static genxStatus _genx_send_gstring(void *userData, constUtf8 s) {
     g_string_append((GString *)userData, (const gchar *)s);
     return GENX_SUCCESS;
@@ -22,8 +45,71 @@ static genxSender _genx_sender_gstring = {
     _genx_flush_gstring
 };
 
-genxStatus _genx_start_doc_gstring(genxWriter w, GString *gs) {
+void genx_writer_start_doc_gstring(genxWriter w, GString *gs) {
     // XXX bad, we should hold a proper ref to gs here
     genxSetUserData(w, (void *)gs);
-    genxStartDocSender(w, &_genx_sender_gstring);
+    genxStatus status = genxStartDocSender(w, &_genx_sender_gstring);
+    g_return_if_fail(status == GENX_SUCCESS);
+}
+
+void genx_writer_end_document(genxWriter w) {
+    genxStatus status = genxEndDocument(w);
+    g_return_if_fail(status == GENX_SUCCESS);
+}
+
+void genx_writer_comment(genxWriter w, const char *text) {
+    genxStatus status = genxComment(w, (constUtf8) text);
+    g_return_if_fail(status == GENX_SUCCESS);
+}
+
+void genx_writer_pi(genxWriter w, const char *target, const char *text) {
+    genxStatus status = genxPI(w, (constUtf8) target, (constUtf8) text);
+    g_return_if_fail(status == GENX_SUCCESS);
+}
+
+void genx_writer_start_element_literal(genxWriter w, const char *xmlns, const char *type) {
+    genxStatus status = genxStartElementLiteral(w, (constUtf8) xmlns, (constUtf8) type);
+    g_return_if_fail(status == GENX_SUCCESS);
+}
+
+void genx_element_start(genxElement e) {
+    genxStatus status = genxStartElement(e);
+    g_return_if_fail(status == GENX_SUCCESS);
+}
+
+void genx_writer_add_attribute_literal(genxWriter w, const char *xmlns,
+        const char *name, const char *value) {
+    genxStatus status = genxAddAttributeLiteral(w,
+            (constUtf8) xmlns, (constUtf8) name, (constUtf8) value);
+    g_return_if_fail(status == GENX_SUCCESS);
+}
+
+void genx_attribute_add(genxAttribute a, const char *value) {
+    genxStatus status = genxAddAttribute(a, (constUtf8) value);
+    g_return_if_fail(status == GENX_SUCCESS);
+}
+
+void genx_namespace_add(genxNamespace ns, char *prefix) {
+    genxStatus status = genxAddNamespace(ns, (utf8) prefix);
+    g_return_if_fail(status == GENX_SUCCESS);
+}
+
+void genx_writer_unset_default_namespace(genxWriter w) {
+    genxStatus status = genxUnsetDefaultNamespace(w);
+    g_return_if_fail(status == GENX_SUCCESS);
+}
+
+void genx_writer_end_element(genxWriter w) {
+    genxStatus status = genxEndElement(w);
+    g_return_if_fail(status == GENX_SUCCESS);
+}
+
+void genx_writer_add_text(genxWriter w, const char *start) {
+    genxStatus status = genxAddText(w, (constUtf8) start);
+    g_return_if_fail(status == GENX_SUCCESS);
+}
+
+void genx_writer_add_character(genxWriter w, int c) {
+    genxStatus status = genxAddCharacter(w, c);
+    g_return_if_fail(status == GENX_SUCCESS);
 }
diff --git a/src/genx-glib.h b/src/genx-glib.h
@@ -0,0 +1,24 @@
+
+#ifndef _GENX_GLIB_H
+#define _GENX_GLIB_H
+
+#include <genx.h>
+
+genxNamespace genx_writer_declare_namespace(genxWriter w, const char *uri, const char *prefix);
+genxElement genx_writer_declare_element(genxWriter w, genxNamespace ns, const char *type);
+genxAttribute genx_writer_declare_attribute(genxWriter w, genxNamespace ns, const char *name);
+void genx_writer_start_doc_gstring(genxWriter w, GString *gs);
+void genx_writer_end_document(genxWriter w);
+void genx_writer_comment(genxWriter w, const char *text);
+void genx_writer_pi(genxWriter w, const char *target, const char *text);
+void genx_writer_start_element_literal(genxWriter w, const char *xmlns, const char *type);
+void genx_element_start(genxElement e);
+void genx_writer_add_attribute_literal(genxWriter w, const char *xmlns,
+        const char *name, const char *value);
+void genx_attribute_add(genxAttribute a, const char *value);
+void genx_namespace_add(genxNamespace ns, char *prefix);
+void genx_writer_end_element(genxWriter w);
+void genx_writer_add_text(genxWriter w, const char *start);
+void genx_writer_add_character(genxWriter w, int c);
+
+#endif /* inclusion guard */
diff --git a/vapi/genx.vapi b/vapi/genx.vapi
@@ -1,4 +1,4 @@
-[CCode (cheader_filename = "genx.h")]
+[CCode (cheader_filename = "genx-glib.h")]
 namespace Genx {
 
     [CCode (cname = "genxStatus", cprefix = "GENX_")]
@@ -33,11 +33,9 @@ namespace Genx {
         [CCode (has_target = 0)]
         public delegate void DeallocCallback(void* user_data, void* data);
         
-        [CCode (cname = "_genx_default_alloc")]
         private static void* default_alloc(void* user_data, int bytes) {
             return GLib.malloc(bytes);
         }
-        [CCode (cname = "_genx_default_dealloc")]
         private static void default_dealloc(void* user_data, void* data) {
             GLib.free(data);
         }
@@ -48,44 +46,32 @@ namespace Genx {
                 DeallocCallback dealloc = default_dealloc,
                 void* user_data = null);
         
-        [CCode (cname = "genxDeclareNamespace")]
-        public unowned Namespace declare_namespace(string uri, string prefix, out Status status);
+        public unowned Namespace declare_namespace(string uri, string prefix);
         
-        [CCode (cname = "genxDeclareElement")]
-        public unowned Element declare_element(Namespace ns, string type, out Status status);
+        public unowned Element declare_element(Namespace ns, string type);
         
-        [CCode (cname = "genxDeclareAttribute")]
-        public unowned Attribute declare_attribute(Namespace ns, string name, out Status status);
+        public unowned Attribute declare_attribute(Namespace ns, string name);
         
-        [CCode (cname = "_genx_start_doc_gstring")]
-        public Status start_doc(GLib.StringBuilder output);
+        [CCode (cname = "genx_writer_start_doc_gstring")]
+        public void start_doc(GLib.StringBuilder output);
         
-        [CCode (cname = "genxEndDocument")]
-        public Status end_doc();
+        public void end_document();
         
-        [CCode (cname = "genxComment")]
-        public Status comment(string text);
+        public void comment(string text);
         
-        [CCode (cname = "genxPI")]
-        public Status pi(string target, string text);
+        public void pi(string target, string text);
         
-        [CCode (cname = "genxStartElementLiteral")]
-        public Status start_element_literal(string xmlns, string type);
+        public void start_element_literal(string xmlns, string type);
         
-        [CCode (cname = "genxAddAttributeLiteral")]
-        public Status add_attribute_literal(string xmlns, string name, string value);
+        public void add_attribute_literal(string xmlns, string name, string value);
         
-        [CCode (cname = "genxUnsetDefaultNamespace")]
-        public Status unset_default_namespace();
+        public void unset_default_namespace();
         
-        [CCode (cname = "genxEndElement")]
-        public Status end_element();
+        public void end_element();
         
-        [CCode (cname = "genxAddText")]
-        public Status add_text(string text);
+        public void add_text(string text);
         
-        [CCode (cname = "genxAddCharacter")]
-        public Status add_character(int character);
+        public void add_character(int character);
     
     }
     
@@ -93,8 +79,7 @@ namespace Genx {
     [CCode (cname = "struct genxNamespace_rec", free_function = "NAMESPACE_HAS_NO_PUBLIC_FREE")]
     public class Namespace {
     
-        [CCode (cname = "genxAddNamespace")]
-        public Status add(string? prefix = null);
+        public void add(string? prefix = null);
     
     }
     
@@ -102,8 +87,7 @@ namespace Genx {
     [CCode (cname = "struct genxElement_rec", free_function = "ELEMENT_HAS_NO_PUBLIC_FREE")]
     public class Element {
     
-        [CCode (cname = "genxStartElement")]
-        public Status start();
+        public void start();
     
     }
         
@@ -111,8 +95,7 @@ namespace Genx {
     [CCode (cname = "struct genxAttribute_rec", free_function = "ATTRIBUTE_HAS_NO_PUBLIC_FREE")]
     public class Attribute {
     
-        [CCode (cname = "genxAddAttribute")]
-        public Status add(string value);
+        public void add(string value);
     
     }