Ejemplo de uso de gmarkup.
authordanigm <danigm>
Mon, 15 Feb 2010 22:15:15 +0000 (22:15 +0000)
committerdanigm <danigm@aa302d1f-72b1-406a-902b-8593ca2b2744>
Mon, 15 Feb 2010 22:15:15 +0000 (22:15 +0000)
Ejemplo del parser de xml simple de glib copiado de
http://blogs.gnome.org/tthurman/2008/02/14/gmarkup/ para implementar
el guardado y cargado de ficheros .tbo.

Ticket #1763

git-svn-id: https://forja.rediris.es/svn/cusl4-tbo@43 aa302d1f-72b1-406a-902b-8593ca2b2744

.gitignore
test/xml/gmarkup.c [new file with mode: 0644]
test/xml/simple.xml [new file with mode: 0644]

index 57050e8..d432950 100644 (file)
@@ -3,3 +3,33 @@
 Makefile
 Makefile.in
 *~
+2010-01-16-screenshot.png
+ABOUT-NLS
+aclocal.m4
+autom4te.cache/
+build/
+config.h
+config.h.in
+config.status
+configure
+libtool
+m4/
+package/
+po/ChangeLog
+po/Makefile.in.in
+po/Makevars.template
+po/POTFILES
+po/Rules-quot
+po/boldquot.sed
+po/en@boldquot.header
+po/en@quot.header
+po/es.gmo
+po/insert-header.sin
+po/quot.sed
+po/remove-potcdate.sin
+po/stamp-it
+po/tbo.pot
+src/.deps/
+src/tbo
+st
+stamp-h1
diff --git a/test/xml/gmarkup.c b/test/xml/gmarkup.c
new file mode 100644 (file)
index 0000000..c946047
--- /dev/null
@@ -0,0 +1,90 @@
+#include <glib.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+gchar *current_animal_noise = NULL;
+
+/* The handler functions. */
+
+void start_element (GMarkupParseContext *context,
+    const gchar         *element_name,
+    const gchar        **attribute_names,
+    const gchar        **attribute_values,
+    gpointer             user_data,
+    GError             **error) {
+
+  const gchar **name_cursor = attribute_names;
+  const gchar **value_cursor = attribute_values;
+
+  while (*name_cursor) {
+    if (strcmp (*name_cursor, "noise") == 0)
+      current_animal_noise = g_strdup (*value_cursor);
+
+    name_cursor++;
+    value_cursor++;
+  }
+}
+
+void text(GMarkupParseContext *context,
+    const gchar         *text,
+    gsize                text_len,
+    gpointer             user_data,
+    GError             **error)
+{
+  /* Note that "text" is not a regular C string: it is
+   * not null-terminated. This is the reason for the
+   * unusual %*s format below.
+   */
+  if (current_animal_noise)
+    printf("I am a %*s and I go %s. Can you do it?\n",
+        text_len, text, current_animal_noise);
+}
+
+void end_element (GMarkupParseContext *context,
+    const gchar         *element_name,
+    gpointer             user_data,
+    GError             **error)
+{
+  if (current_animal_noise)
+    {
+      g_free (current_animal_noise);
+      current_animal_noise = NULL;
+    }
+}
+
+/* The list of what handler does what. */
+static GMarkupParser parser = {
+  start_element,
+  end_element,
+  text,
+  NULL,
+  NULL
+};
+
+/* Code to grab the file into memory and parse it. */
+int main() {
+  char *text;
+  gsize length;
+  GMarkupParseContext *context = g_markup_parse_context_new (
+      &parser,
+      0,
+      NULL,
+      NULL);
+
+  /* seriously crummy error checking */
+
+  if (g_file_get_contents ("simple.xml", &text, &length, NULL) == FALSE) {
+    printf("Couldn't load XML\n");
+    exit(255);
+  }
+
+  if (g_markup_parse_context_parse (context, text, length, NULL) == FALSE) {
+    printf("Parse failed\n");
+    exit(255);
+  }
+
+  g_free(text);
+  g_markup_parse_context_free (context);
+}
+/* EOF */
diff --git a/test/xml/simple.xml b/test/xml/simple.xml
new file mode 100644 (file)
index 0000000..368875c
--- /dev/null
@@ -0,0 +1,6 @@
+<zoo>
+  <animal noise="roar">lion</animal>
+  <animal noise="sniffle">bunny</animal>
+  <animal noise="lol">cat</animal>
+  <keeper/>
+</zoo>