[template-glib] examples: simplify examples using expand_string()



commit 19375cda3e30cb92184fc8a9347c2d66accac97c
Author: Christian Hergert <chergert redhat com>
Date:   Sun Jan 17 22:29:56 2016 -0800

    examples: simplify examples using expand_string()

 examples/simple.c  |   25 ++++++-------------------
 examples/simple.js |    3 ++-
 examples/simple.py |    6 ++----
 3 files changed, 10 insertions(+), 24 deletions(-)
---
diff --git a/examples/simple.c b/examples/simple.c
index 72f0ac8..395f415 100644
--- a/examples/simple.c
+++ b/examples/simple.c
@@ -9,10 +9,8 @@ main (gint   argc,
   g_autoptr(TmplScope) scope = NULL;
   g_autoptr(TmplTemplate) tmpl = NULL;
   g_autoptr(GError) error = NULL;
-  g_autoptr(GOutputStream) stream = NULL;
   TmplSymbol *symbol = NULL;
-  const gchar *output;
-  gchar zero = 0;
+  gchar *str;
 
   /*
    * First we need to create and parse our template.
@@ -38,28 +36,17 @@ main (gint   argc,
   tmpl_symbol_assign_string (symbol, "My Title");
 
   /*
-   * Now lets expand the template into a memory stream. We could also
-   * just use a unix output stream using STDOUT, but this is more portable
-   * if people want to get this running on non-UNIX systems.
+   * Expand the template based on our scope. You can also expand into a
+   * GOutputStream, instead of a string.
    */
-  stream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
-  if (!tmpl_template_expand (tmpl, stream, scope, NULL, &error))
+  if (!(str = tmpl_template_expand_string (tmpl, scope, &error)))
     {
       g_printerr ("%s\n", error->message);
       return EXIT_FAILURE;
     }
 
-  /*
-   * Because we are converting this to a C String, we need to add a trailing
-   * null byte.
-   */
-  g_output_stream_write (stream, &zero, 1, NULL, NULL);
-
-  /*
-   * Okay, finally we can print this to stdout.
-   */
-  output = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (stream));
-  g_print ("%s\n", output);
+  g_print ("%s\n", str);
+  g_free (str);
 
   /*
    * All our state gets cleaned up thanks to g_autoptr()!
diff --git a/examples/simple.js b/examples/simple.js
index 4a37488..26964c0 100644
--- a/examples/simple.js
+++ b/examples/simple.js
@@ -19,4 +19,5 @@ title.assign_string("Example Title");
 let stream = Gio.UnixOutputStream.new (0, false);
 
 // Expand the template into stream
-tmpl.expand(stream, scope, null);
+let expanded = tmpl.expand_string(scope);
+log(expanded);
diff --git a/examples/simple.py b/examples/simple.py
index 87fbcfe..06e4bf7 100644
--- a/examples/simple.py
+++ b/examples/simple.py
@@ -19,8 +19,6 @@ scope = Template.Scope.new()
 title = scope.get('title')
 title.assign_string('Example Title')
 
-# Write to stdout
-stream = Gio.UnixOutputStream.new(0, False)
-
 # Expand the template into stream
-tmpl.expand(stream, scope, None)
+expanded = tmpl.expand_string(scope)
+print(expanded)


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