]> saetta.ns0.it Git - libzakcgi/commitdiff
Changed private ZakCgiTag::valist_to_gptrarray in ::valist_to_ghashtable.
authorAndrea Zagli <azagli@libero.it>
Mon, 24 Aug 2015 17:30:56 +0000 (19:30 +0200)
committerAndrea Zagli <azagli@libero.it>
Mon, 24 Aug 2015 17:30:56 +0000 (19:30 +0200)
src/tag.c

index 779fd32617c6f775bcc6c183987786cc28bc8cab..32be9eae7b4eb41683c8a36f1185c530e5a4788f 100644 (file)
--- a/src/tag.c
+++ b/src/tag.c
 
 #include "tag.h"
 
-static GPtrArray
-*zak_cgi_tag_valist_to_gptrarray (va_list ap)
+static GHashTable
+*zak_cgi_tag_valist_to_ghashtable (va_list ap)
 {
-       GPtrArray *ret;
+       GHashTable *ret;
 
        gchar *attr;
+       gchar *attr_val;
 
-       ret = g_ptr_array_new ();
+       ret = g_hash_table_new (g_str_hash, g_str_equal);
 
        do
                {
                        attr = va_arg (ap, gchar *);
                        if (attr != NULL)
                                {
-                                       g_ptr_array_add (ret, g_strdup (attr));
+                                       attr_val = va_arg (ap, gchar *);
+                                       if (attr_val != NULL)
+                                               {
+                                                       g_hash_table_insert (ret, g_strdup (attr), g_strdup (attr_val));
+                                               }
+                                       else
+                                               {
+                                                       break;
+                                               }
                                }
                        else
                                {
                                        break;
                                }
                } while (TRUE);
+
        va_end (ap);
 
        return ret;
@@ -51,19 +61,18 @@ static GPtrArray
 static gchar
 *zak_cgi_tag_tag_attrs (const gchar *name,
                                                const gchar *id,
-                                               GPtrArray *ar_attrs)
+                                               GHashTable *ht_attrs)
 {
        gchar *ret;
        GString *str;
 
-       guint i;
-       guint l;
+       GHashTableIter iter;
 
        gboolean with_name;
        gboolean with_content;
 
-       gchar *attr;
-       gchar *attr_val;
+       gpointer key;
+       gpointer val;
 
        gchar *content;
 
@@ -84,46 +93,29 @@ static gchar
                                                                        id);
                }
 
-       l = ar_attrs->len;
-       for (i = 0; i < l; i++)
+       g_hash_table_iter_init (&iter, ht_attrs);
+       while (g_hash_table_iter_next (&iter, &key, &val))
                {
-                       attr = g_ptr_array_index (ar_attrs, i);
-                       if (attr != NULL)
+                       if (g_strcmp0 ((gchar *)key, "content") == 0)
                                {
-                                       attr_val = g_ptr_array_index (ar_attrs, ++i);
-                                       if (attr_val != NULL)
-                                               {
-                                                       if (g_strcmp0 (attr, "content") == 0)
-                                                               {
-                                                                       with_content = TRUE;
-                                                                       if (content != NULL)
-                                                                               {
-                                                                                       g_free (content);
-                                                                               }
-                                                                       content = g_strdup (attr_val);
-                                                               }
-                                                       else
-                                                               {
-                                                                       if (g_strcmp0 (attr, "name") == 0)
-                                                                               {
-                                                                                       with_name = TRUE;
-                                                                               }
-
-                                                                       g_string_append_printf (str,
-                                                                                                                       " %s=\"%s\"",
-                                                                                                                       attr,
-                                                                                                                       attr_val);
-                                                               }
-                                               }
-                                       else
+                                       with_content = TRUE;
+                                       if (content != NULL)
                                                {
-                                                       g_free (attr);
-                                                       break;
+                                                       g_free (content);
                                                }
+                                       content = g_strdup ((gchar *)val);
                                }
                        else
                                {
-                                       break;
+                                       if (g_strcmp0 ((gchar *)key, "name") == 0)
+                                               {
+                                                       with_name = TRUE;
+                                               }
+
+                                       g_string_append_printf (str,
+                                                                                       " %s=\"%s\"",
+                                                                                       (gchar *)key,
+                                                                                       (gchar *)val);
                                }
                }
 
@@ -170,7 +162,7 @@ gchar
 
        va_start (ap, id);
 
-       return zak_cgi_tag_tag_attrs (name, id, zak_cgi_tag_valist_to_gptrarray (ap));
+       return zak_cgi_tag_tag_attrs (name, id, zak_cgi_tag_valist_to_ghashtable (ap));
 }
 
 /**
@@ -187,7 +179,7 @@ gchar
 
        va_start (ap, id);
 
-       return zak_cgi_tag_tag_attrs ("img", id, zak_cgi_tag_valist_to_gptrarray (ap));
+       return zak_cgi_tag_tag_attrs ("img", id, zak_cgi_tag_valist_to_ghashtable (ap));
 }
 
 /**
@@ -200,16 +192,15 @@ gchar
 *zak_cgi_tag_text (const gchar *id,
                                   ...)
 {
-       GPtrArray *ar;
+       GHashTable *ht;
        va_list ap;
 
        va_start (ap, id);
 
-       ar = zak_cgi_tag_valist_to_gptrarray (ap);
-       g_ptr_array_add (ar, "type");
-       g_ptr_array_add (ar, "text");
+       ht = zak_cgi_tag_valist_to_ghashtable (ap);
+       g_hash_table_insert (ht, "type", "text");
 
-       return zak_cgi_tag_tag_attrs ("input", id, ar);
+       return zak_cgi_tag_tag_attrs ("input", id, ht);
 }
 
 /**
@@ -222,16 +213,15 @@ gchar
 *zak_cgi_tag_file (const gchar *id,
                                   ...)
 {
-       GPtrArray *ar;
+       GHashTable *ht;
        va_list ap;
 
        va_start (ap, id);
 
-       ar = zak_cgi_tag_valist_to_gptrarray (ap);
-       g_ptr_array_add (ar, "type");
-       g_ptr_array_add (ar, "file");
+       ht = zak_cgi_tag_valist_to_ghashtable (ap);
+       g_hash_table_insert (ht, "type", "file");
 
-       return zak_cgi_tag_tag_attrs ("input", id, ar);
+       return zak_cgi_tag_tag_attrs ("input", id, ht);
 }
 
 /**
@@ -244,14 +234,13 @@ gchar
 *zak_cgi_tag_submit (const gchar *id,
                                         ...)
 {
-       GPtrArray *ar;
+       GHashTable *ht;
        va_list ap;
 
        va_start (ap, id);
 
-       ar = zak_cgi_tag_valist_to_gptrarray (ap);
-       g_ptr_array_add (ar, "type");
-       g_ptr_array_add (ar, "submit");
+       ht = zak_cgi_tag_valist_to_ghashtable (ap);
+       g_hash_table_insert (ht, "type", "submit");
 
-       return zak_cgi_tag_tag_attrs ("input", id, ar);
+       return zak_cgi_tag_tag_attrs ("input", id, ht);
 }