[seed] Add seed_value_to_format to API, converts an array of SeedValue based on a format string. Also add C



commit c45bfb66be620844ef40890f914df24628a6d412
Author: Robert Carr <racarr svn gnome org>
Date:   Tue May 26 15:42:08 2009 -0400

    Add seed_value_to_format to API, converts an array of SeedValue based on a format string. Also add C test and docs
---
 doc/reference/seed-sections.txt |    1 +
 libseed/seed-api.c              |   90 +++++++++++++++++++++++++++++++++++++++
 libseed/seed.h                  |    4 ++
 tests/c/api-types.c             |   11 +++++
 4 files changed, 106 insertions(+), 0 deletions(-)

diff --git a/doc/reference/seed-sections.txt b/doc/reference/seed-sections.txt
index 034b1d8..d6ce92b 100644
--- a/doc/reference/seed-sections.txt
+++ b/doc/reference/seed-sections.txt
@@ -87,6 +87,7 @@ seed_value_from_object
 seed_value_to_filename
 seed_value_from_filename
 seed_value_get_type
+seed_value_to_format
 </SECTION>
 <SECTION>
 <TITLE>SeedClosure</TITLE>
diff --git a/libseed/seed-api.c b/libseed/seed-api.c
index 92d066e..63ebb74 100644
--- a/libseed/seed-api.c
+++ b/libseed/seed-api.c
@@ -16,6 +16,7 @@
  */
 
 #include "seed-private.h"
+#include <stdarg.h>
 
 /**
  * seed_value_protect:
@@ -729,3 +730,92 @@ seed_make_function (JSContextRef ctx,
   
   return oref;
 }
+
+/**
+ * seed_value_to_format:
+ * @ctx: A valid #SeedContext
+ * @format: Format string to use.
+ * @exception: Location to store an exception.
+ * @values: The values to convert.
+ * @Varargs: A %NULL-terminated list of locations to store the results of conversion. 
+ *
+ * A convenience API for converting multiple values at once, the format string
+ * is composed of single characters specifying types, for example:
+ * i: gint
+ * u: guint
+ * o: GObject *
+ * s: gchar *
+ * f: gdouble
+ * c: gchar
+ *
+ * and a valid format string could be "iuo". 
+ *
+ * This function may be in particular useful in converting arguments 
+ * in a #SeedFunctionCallback.
+ * Return value: Whether conversion was successful. 
+ */
+gboolean
+seed_value_to_format (JSContextRef ctx,
+		      const gchar *format,
+		      JSValueRef *values,
+		      JSValueRef *exception,
+		      ...)
+{
+  va_list argp;
+  const gchar *c;
+  guint i = 0;
+  
+  c = format;
+  
+  va_start (argp, exception);
+  
+  for (c = format; *c; c++)
+    {
+      JSValueRef val = values[i];
+      gpointer p = va_arg (argp, gpointer);
+      
+      if (!val || !p)
+	{
+	  va_end (argp);
+	  return FALSE;
+	}
+      switch (*c)
+	{
+	case 'i':
+	  {
+	    *((gint *)p) = seed_value_to_int (ctx, val, exception);
+	    break;
+	  }
+	case 'u':
+	  {
+	    *((guint *)p) = seed_value_to_uint (ctx, val, exception);
+	    break;
+	  }
+	case 's':
+	  {
+	    *((gchar **)p) = seed_value_to_string (ctx, val, exception);
+	    break;
+	  }
+	case 'f':
+	  {
+	    *((gdouble *)p) = seed_value_to_int (ctx, val, exception);
+	    break;
+	  }
+	case 'o':
+	  {
+	    *((GObject **)p) = seed_value_to_object (ctx, val, exception);
+	    break;
+	  }
+	case 'c':
+	  {
+	    *((gchar *)c) = seed_value_to_char (ctx, val, exception);
+	    break;
+	  }
+	}
+      i++;
+    }
+  
+  va_end (argp);
+  return TRUE;
+}
+		      
diff --git a/libseed/seed.h b/libseed/seed.h
index f1e09c9..e59fcbf 100644
--- a/libseed/seed.h
+++ b/libseed/seed.h
@@ -418,5 +418,9 @@ seed_object_get_prototype (SeedContext ctx, SeedObject obj);
 gboolean
 seed_object_is_of_class (SeedContext ctx, SeedObject obj, SeedClass class);
 
+gboolean
+seed_value_to_format (SeedContext,
+		      const gchar *format,
+		      ...);
 
 #endif
diff --git a/tests/c/api-types.c b/tests/c/api-types.c
index 9cc0e03..0b45f3b 100644
--- a/tests/c/api-types.c
+++ b/tests/c/api-types.c
@@ -138,4 +138,15 @@ void basic_types(TestSimpleFixture * fixture, gconstpointer _data)
 
 	g_assert(strncmp(filename_test_in, filename_test_out,
 					 strlen(filename_test_in)) == 0);
+	SeedValue si[2];
+	si[0] = seed_value_from_string (fixture->context, "Hi", NULL);
+	si[1] = seed_value_from_int (fixture->context, 1, NULL);
+	gint ni;
+	gchar *ns;
+	
+	seed_value_to_format (fixture->context, "si", si, NULL, &ns, &ni, NULL);
+	g_assert (ni == 1);
+	g_assert (!strcmp(ns, "Hi"));
+
+	
 }



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