From a5452151816f2ea33352a62162df809cc06b81dc Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sat, 15 Jun 2019 09:15:13 +0200 Subject: [PATCH] FormElementSelect: changed storage data type to preserve options order. --- src/formelementselect.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/formelementselect.c b/src/formelementselect.c index 1a4dd74..3cb9d0e 100644 --- a/src/formelementselect.c +++ b/src/formelementselect.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015-2017 Andrea Zagli + * Copyright (C) 2015-2019 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -45,7 +45,7 @@ static void zak_form_cgi_form_element_select_finalize (GObject *gobject); typedef struct _ZakFormCgiFormElementSelectPrivate ZakFormCgiFormElementSelectPrivate; struct _ZakFormCgiFormElementSelectPrivate { - GHashTable *ht_options; + GPtrArray *ar_options; }; G_DEFINE_TYPE (ZakFormCgiFormElementSelect, zak_form_cgi_form_element_select, ZAK_FORM_CGI_TYPE_FORM_ELEMENT) @@ -74,7 +74,7 @@ zak_form_cgi_form_element_select_init (ZakFormCgiFormElementSelect *zak_form_cgi { ZakFormCgiFormElementSelectPrivate *priv = ZAK_FORM_CGI_FORM_ELEMENT_SELECT_GET_PRIVATE (zak_form_cgi_form_element_select); - priv->ht_options = g_hash_table_new (g_str_hash, g_str_equal); + priv->ar_options = g_ptr_array_new (); } /** @@ -101,7 +101,7 @@ ZakFormCgiFormElement */ ZakFormCgiFormElement *zak_form_cgi_form_element_select_new_attrs (const gchar *id, - ...) + ...) { va_list ap; @@ -141,11 +141,12 @@ zak_form_cgi_form_element_select_add_option (ZakFormCgiFormElementSelect *elemen ht_attrs = zak_cgi_commons_valist_to_ghashtable (ap); if (ht_attrs == NULL) { - g_hash_table_new (g_str_hash, g_str_equal); + ht_attrs = g_hash_table_new (g_str_hash, g_str_equal); } g_hash_table_replace (ht_attrs, "zak-cgi-content", g_strdup (content)); + g_hash_table_replace (ht_attrs, "value", g_strdup (value)); - g_hash_table_replace (priv->ht_options, g_strdup (value), ht_attrs); + g_ptr_array_add (priv->ar_options, ht_attrs); } void @@ -200,9 +201,8 @@ static gchar { gchar *ret; - GHashTableIter iter; - gpointer key; - gpointer value; + guint i; + GString *options; GHashTable *ht_options_attrs; @@ -220,15 +220,14 @@ static gchar /* list options */ options = g_string_new ("\n"); - g_hash_table_iter_init (&iter, priv->ht_options); - while (g_hash_table_iter_next (&iter, &key, &value)) + + for (i = 0; i < priv->ar_options->len; i++) { - ht_options_attrs = (GHashTable *)value; - g_hash_table_replace (ht_options_attrs, "value", g_strdup ((gchar *)key)); + ht_options_attrs = (GHashTable *)g_ptr_array_index (priv->ar_options, i); if (gval != NULL) { - if (g_strcmp0 (gval, (gchar *)key) == 0) + if (g_strcmp0 (gval, (gchar *)g_hash_table_lookup (ht_options_attrs, "value")) == 0) { g_hash_table_replace (ht_options_attrs, "selected", g_strdup ("selected")); } -- 2.49.0