[gxml] examples/c: actually add .c files this time



commit 8fc8a1d55427087a8f7442bf9b1a067caf8bad04
Author: Richard Schwarting <aquarichy gmail com>
Date:   Sun Jul 28 01:30:11 2013 -0400

    examples/c: actually add .c files this time

 examples/c/create_document.c             |   59 ++++++++++++++++++++++++++++++
 examples/c/create_document_from_file.c   |   22 +++++++++++
 examples/c/create_document_from_path.c   |   16 ++++++++
 examples/c/create_document_from_string.c |   20 ++++++++++
 examples/c/create_document_minimal.c     |   55 ++++++++++++++++++++++++++++
 examples/c/save_document_to_path.c       |   14 +++++++
 examples/c/save_document_to_stream.c     |   20 ++++++++++
 7 files changed, 206 insertions(+), 0 deletions(-)
---
diff --git a/examples/c/create_document.c b/examples/c/create_document.c
new file mode 100644
index 0000000..7020029
--- /dev/null
+++ b/examples/c/create_document.c
@@ -0,0 +1,59 @@
+#include <gxml/gxml.h>
+#include <stdio.h>
+
+int main () {
+  GXmlDocument *doc;
+  GXmlElement *root;
+  GXmlElement *owner;
+  GXmlElement *books;
+  GXmlElement *book;
+  int i;
+  char *str;
+
+  char *authors[] = { "John Green", "Jane Austen", "J.D. Salinger" };
+  char *titles[] = { "The Fault in Our Stars", "Pride & Prejudice", "Nine Stories" };
+
+  doc = gxml_document_new ();
+
+  // Add a root node
+  root = gxml_document_create_element (doc, "Bookshelf");
+
+  gxml_node_append_child (GXML_NODE (doc), GXML_NODE (root));
+  // if we tried to add a second one, it would fail :), and a g_warning would be printed
+
+  // Add an owner node
+  owner = gxml_document_create_element (doc, "Owner");
+  gxml_node_append_child (GXML_NODE (root), GXML_NODE (owner));
+  gxml_element_set_attribute (owner, "fullname", "John Green");
+  // TODO: need to figure out what sort of errors these would return,
+  // want the devhelp pages to describe meaningful possible errors
+
+  // Add a collection of books
+  books = gxml_document_create_element (doc, "Books");
+  gxml_node_append_child (GXML_NODE (root), GXML_NODE (books));
+
+  for (i = 0; i < sizeof (authors) / sizeof (char*); i++) {
+    book = gxml_document_create_element (doc, "Book");
+    gxml_element_set_attribute (book, "author", authors[i]);
+    gxml_element_set_attribute (book, "title", titles[i]);
+    gxml_node_append_child (GXML_NODE (books), GXML_NODE (book));
+  }
+
+  // TODO: want a "gxml_node_from_string ()"; make sure document ownership transfer properly
+
+  str = gxml_node_to_string (GXML_NODE (doc), TRUE, 2);
+  printf ("%s:\n%s\n", __FILE__, str);
+  g_free (str);
+
+  g_object_unref (doc); 
+  g_object_unref (root); // TODO: figure out what happens to root if you deallocate doc?! */
+  // TODO: perhaps see whether we can make it that all the nodes that are returned are weak references
+  g_object_unref (owner);
+  g_object_unref (books);
+  g_object_unref (book);
+  /* In util/ there's a small programme that tells you when ownership
+     is transfered (that is, you are given a return value that you own
+     and must free. */
+
+  return 0;
+}
diff --git a/examples/c/create_document_from_file.c b/examples/c/create_document_from_file.c
new file mode 100644
index 0000000..86d99ec
--- /dev/null
+++ b/examples/c/create_document_from_file.c
@@ -0,0 +1,22 @@
+#include <gxml/gxml.h>
+#include <stdio.h>
+
+int main () {
+  GXmlDocument *doc;
+  GFile *file;
+  char *str;
+  GCancellable *can = g_cancellable_new ();
+
+  file = g_file_new_for_path ("bookshelf.xml");
+  doc = gxml_document_new_from_gfile (file, can); // TODO: want to change this function name to "from file"
+
+  str = gxml_node_to_string (GXML_NODE (doc), TRUE, 2);
+  printf ("%s:\n%s\n", __FILE__, str);
+  g_free (str);
+
+  g_object_unref (doc);
+  // TODO: how do you free a GFile?  g_object_unref ()? 
+  
+
+  return 0;
+}
diff --git a/examples/c/create_document_from_path.c b/examples/c/create_document_from_path.c
new file mode 100644
index 0000000..a869d39
--- /dev/null
+++ b/examples/c/create_document_from_path.c
@@ -0,0 +1,16 @@
+#include <gxml/gxml.h>
+#include <stdio.h>
+
+int main () {
+  GXmlDocument *doc;
+  char *str;
+
+  doc = gxml_document_new_from_path ("bookshelf.xml"); 
+  str = gxml_node_to_string (GXML_NODE (doc), TRUE, 2);
+  printf ("%s:\n%s\n", __FILE__, str);
+  g_free (str);
+
+  g_object_unref (doc);
+
+  return 0;
+}
diff --git a/examples/c/create_document_from_string.c b/examples/c/create_document_from_string.c
new file mode 100644
index 0000000..c9b6a80
--- /dev/null
+++ b/examples/c/create_document_from_string.c
@@ -0,0 +1,20 @@
+#include <gxml/gxml.h>
+#include <stdio.h>
+
+int main () {
+  char *xml;
+  char *str;
+  GXmlDocument *doc;
+
+  xml = "<?xml version=\"1.0\"?><Bookshelf><Owner fullname=\"John Green\"/><Books><Book author=\"John 
Green\" title=\"The Fault in Our Stars\"/><Book author=\"Jane Austen\" title=\"Pride &amp; Prejudice\"/><Book 
author=\"J.D. Salinger\" title=\"Nine Stories\"/></Books></Bookshelf>";
+
+  doc = gxml_document_new_from_string (xml);
+
+  str = gxml_node_to_string (GXML_NODE (doc), TRUE, 2);
+  printf ("%s:\n%s\n", __FILE__, str);
+  g_free (str);
+
+  g_object_unref (doc);
+
+  return 0;
+}
diff --git a/examples/c/create_document_minimal.c b/examples/c/create_document_minimal.c
new file mode 100644
index 0000000..b418320
--- /dev/null
+++ b/examples/c/create_document_minimal.c
@@ -0,0 +1,55 @@
+#include <gxml/gxml.h>
+#include <stdio.h>
+
+
+int main () {
+  GXmlDocument *doc;
+  GXmlElement *root;
+  GXmlElement *owner;
+  GXmlElement *books;
+  GXmlElement *book;
+  int i;
+  char *str;
+
+  char *authors[] = { "John Green", "Jane Austen", "J.D. Salinger" };
+  char *titles[] = { "The Fault in Our Stars", "Pride & Prejudice", "Nine Stories" };
+
+  doc = gxml_document_new ();
+
+  // Add a root node
+  root = gxml_document_create_element (doc, "Bookshelf");
+  gxml_node_append_child (GXML_NODE (doc), GXML_NODE (root));
+  // if we tried to add a second one, it would fail and a g_warning would be printed :)
+
+  // Add an owner node
+  owner = gxml_document_create_element (doc, "Owner");
+  gxml_node_append_child (GXML_NODE (root), GXML_NODE (owner));
+  gxml_element_set_attribute (owner, "fullname", "John Green");
+  // TODO: need to figure out what sort of errors these would return,
+  // want the devhelp pages to describe meaningful possible errors
+
+  // Add a collection of books
+  books = gxml_document_create_element (doc, "Books");
+  gxml_node_append_child (GXML_NODE (root), GXML_NODE (books));
+  for (i = 0; i < sizeof (authors) / sizeof (char*); i++) {
+    book = gxml_document_create_element (doc, "Book");
+    gxml_element_set_attribute (book, "author", authors[i]);
+    gxml_element_set_attribute (book, "title", titles[i]);
+    gxml_node_append_child (GXML_NODE (books), GXML_NODE (book));
+  }
+
+  str = gxml_node_to_string (GXML_NODE (doc), TRUE, 2);
+  printf ("%s:\n%s\n", __FILE__, str);
+  g_free (str);
+
+  g_object_unref (doc);
+  g_object_unref (root);
+  g_object_unref (owner);
+  g_object_unref (books);
+  g_object_unref (book);
+
+  // TODO: how do we clean them up?
+
+  return 0;
+}
+
diff --git a/examples/c/save_document_to_path.c b/examples/c/save_document_to_path.c
new file mode 100644
index 0000000..e16eddc
--- /dev/null
+++ b/examples/c/save_document_to_path.c
@@ -0,0 +1,14 @@
+#include <gxml/gxml.h>
+#include <stdio.h>
+
+int main () {
+  GXmlDocument *doc;
+
+  doc = gxml_document_new_from_path ("bookshelf.xml");
+
+  gxml_document_save_to_path (doc, "bookshelf2.xml");
+
+  g_object_unref (doc);
+
+  return 0;
+}
diff --git a/examples/c/save_document_to_stream.c b/examples/c/save_document_to_stream.c
new file mode 100644
index 0000000..873cf9e
--- /dev/null
+++ b/examples/c/save_document_to_stream.c
@@ -0,0 +1,20 @@
+#include <gxml/gxml.h>
+#include <stdio.h>
+
+int main () {
+  GXmlDocument *doc;
+  GFile *outfile;
+  GFileOutputStream *outstream;
+  GCancellable *can = g_cancellable_new ();
+
+  doc = gxml_document_new_from_path ("bookshelf.xml");
+
+  outfile = g_file_new_for_path ("bookshelf3.xml");
+  outstream = g_file_replace (outfile, NULL, FALSE, G_FILE_CREATE_REPLACE_DESTINATION, can, NULL);
+  gxml_document_save_to_stream (doc, G_OUTPUT_STREAM (outstream), can);
+
+  g_object_unref (doc);
+  // TODO: figure out how to close/unref outstream and outfile
+
+  return 0;
+}


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