From: John Darrington Date: Fri, 24 Jul 2009 17:23:43 +0000 (+0200) Subject: Change signature of text_to_value. X-Git-Tag: build37~44 X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?p=pspp-builds.git;a=commitdiff_plain;h=d8659cda7d193dd3e6e035cd0b1b40493d360f0b Change signature of text_to_value. Now, text_to_value initialises the value, and expects the caller to destroy it. This fixes some gui bugs where strange things happened with long string value labels and missing values. --- diff --git a/src/ui/gui/find-dialog.c b/src/ui/gui/find-dialog.c index faeb8f64..37d8ebad 100644 --- a/src/ui/gui/find-dialog.c +++ b/src/ui/gui/find-dialog.c @@ -585,8 +585,6 @@ cmptr_value_destroy (struct comparator *cmptr) static struct comparator * value_comparator_create (const struct variable *var, const PsppireDict *dict, const char *target) { - const struct fmt_spec *fmt; - int width ; struct value_comparator *vc = xzalloc (sizeof (*vc)); struct comparator *cmptr = (struct comparator *) vc; @@ -596,12 +594,7 @@ value_comparator_create (const struct variable *var, const PsppireDict *dict, co cmptr->destroy = cmptr_value_destroy; cmptr->dict = dict; - width = var_get_width (var); - fmt = var_get_write_format (var); - - value_init (&vc->pattern, width); - - text_to_value (target, &vc->pattern, dict, *var_get_write_format (var) ); + text_to_value (target, dict, var, &vc->pattern); return cmptr; } diff --git a/src/ui/gui/helper.c b/src/ui/gui/helper.c index ff750b25..b09a2f58 100644 --- a/src/ui/gui/helper.c +++ b/src/ui/gui/helper.c @@ -60,17 +60,27 @@ value_to_text (union value v, const PsppireDict *dict, struct fmt_spec format) } +/* Converts TEXT to a value. -gboolean -text_to_value (const gchar *text, union value *v, + VAL will be initialised and filled by this function. + It is the caller's responsibility to destroy VAL when no longer needed. + VAR and DICT must be the variable and dictionary with which VAL + is associated. + + On success, VAL is returned, NULL otherwise. +*/ +union value * +text_to_value (const gchar *text, const PsppireDict *dict, - struct fmt_spec format) + const struct variable *var, + union value *val) { - bool ok; + const struct fmt_spec *format = var_get_print_format (var); + int width = var_get_width (var); - if ( format.type != FMT_A) + if ( format->type != FMT_A) { - if ( ! text ) return FALSE; + if ( ! text ) return NULL; { const gchar *s = text; @@ -81,17 +91,18 @@ text_to_value (const gchar *text, union value *v, s++; } - if ( !*s) return FALSE; + if ( !*s) return NULL; } } + value_init (val, width); msg_disable (); - ok = data_in (ss_cstr (text), UTF8, format.type, 0, 0, 0, + data_in (ss_cstr (text), UTF8, format->type, 0, 0, 0, dict->dict, - v, fmt_var_width (&format)); + val, width); msg_enable (); - return ok; + return val; } diff --git a/src/ui/gui/helper.h b/src/ui/gui/helper.h index f6c084d4..dfdd8932 100644 --- a/src/ui/gui/helper.h +++ b/src/ui/gui/helper.h @@ -37,9 +37,11 @@ struct fmt_spec; gchar * value_to_text (union value v, const PsppireDict *dict, struct fmt_spec format); -gboolean text_to_value (const gchar *text, union value *v, - const PsppireDict *dict, - struct fmt_spec format); +union value * +text_to_value (const gchar *text, + const PsppireDict *dict, + const struct variable *var, + union value *); GObject *get_object_assert (GtkBuilder *builder, const gchar *name, GType type); GtkAction * get_action_assert (GtkBuilder *builder, const gchar *name); diff --git a/src/ui/gui/missing-val-dialog.c b/src/ui/gui/missing-val-dialog.c index 16a7f6f3..7e04b854 100644 --- a/src/ui/gui/missing-val-dialog.c +++ b/src/ui/gui/missing-val-dialog.c @@ -80,8 +80,6 @@ missing_val_dialog_accept (GtkWidget *w, gpointer data) { struct missing_val_dialog *dialog = data; - const struct fmt_spec *write_spec = var_get_write_format (dialog->pv); - if ( gtk_toggle_button_get_active (dialog->button_discrete)) { gint nvals = 0; @@ -100,8 +98,7 @@ missing_val_dialog_accept (GtkWidget *w, gpointer data) continue; } - if ( text_to_value (text, &v, - dialog->dict, *write_spec)) + if ( text_to_value (text, dialog->dict, dialog->pv, &v)) { nvals++; mv_add_value (&dialog->mvl, &v); @@ -109,6 +106,7 @@ missing_val_dialog_accept (GtkWidget *w, gpointer data) else badvals++; g_free (text); + value_destroy (&v, var_get_width (dialog->pv)); } if ( nvals == 0 || badvals > 0 ) { @@ -127,14 +125,16 @@ missing_val_dialog_accept (GtkWidget *w, gpointer data) const gchar *low_text = gtk_entry_get_text (GTK_ENTRY (dialog->low)); const gchar *high_text = gtk_entry_get_text (GTK_ENTRY (dialog->high)); - if ( text_to_value (low_text, &low_val, dialog->dict, *write_spec) + if ( text_to_value (low_text, dialog->dict, dialog->pv, &low_val) && - text_to_value (high_text, &high_val, dialog->dict, *write_spec) ) + text_to_value (high_text, dialog->dict, dialog->pv, &high_val)) { if ( low_val.f > high_val.f ) { err_dialog (_("Incorrect range specification"), GTK_WINDOW (dialog->window)); + value_destroy (&low_val, var_get_width (dialog->pv)); + value_destroy (&high_val, var_get_width (dialog->pv)); return ; } } @@ -142,6 +142,8 @@ missing_val_dialog_accept (GtkWidget *w, gpointer data) { err_dialog (_("Incorrect range specification"), GTK_WINDOW (dialog->window)); + value_destroy (&low_val, var_get_width (dialog->pv)); + value_destroy (&high_val, var_get_width (dialog->pv)); return; } @@ -151,19 +153,25 @@ missing_val_dialog_accept (GtkWidget *w, gpointer data) mv_clear (&dialog->mvl); mv_add_range (&dialog->mvl, low_val.f, high_val.f); + value_destroy (&low_val, var_get_width (dialog->pv)); + value_destroy (&high_val, var_get_width (dialog->pv)); + if ( discrete_text && strlen (g_strstrip (discrete_text)) > 0 ) { union value discrete_val; - if ( !text_to_value (discrete_text, &discrete_val, + if ( !text_to_value (discrete_text, dialog->dict, - *write_spec)) + dialog->pv, + &discrete_val)) { err_dialog (_("Incorrect value for variable type"), GTK_WINDOW (dialog->window) ); g_free (discrete_text); + value_destroy (&discrete_val, var_get_width (dialog->pv)); return; } mv_add_value (&dialog->mvl, &discrete_val); + value_destroy (&discrete_val, var_get_width (dialog->pv)); } g_free (discrete_text); } diff --git a/src/ui/gui/val-labs-dialog.c b/src/ui/gui/val-labs-dialog.c index 765be7fd..5b226eb6 100644 --- a/src/ui/gui/val-labs-dialog.c +++ b/src/ui/gui/val-labs-dialog.c @@ -71,10 +71,10 @@ on_label_entry_change (GtkEntry *entry, gpointer data) text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry)); - text_to_value (text, &v, + text_to_value (text, dialog->var_store->dict, - *var_get_write_format (dialog->pv)); - + dialog->pv, + &v); if (val_labs_find (dialog->labs, &v)) { @@ -86,6 +86,8 @@ on_label_entry_change (GtkEntry *entry, gpointer data) gtk_widget_set_sensitive (dialog->change_button, FALSE); gtk_widget_set_sensitive (dialog->add_button, TRUE); } + + value_destroy (&v, var_get_width (dialog->pv)); } @@ -142,9 +144,10 @@ on_value_entry_change (GtkEntry *entry, gpointer data) const gchar *text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry)); union value v; - text_to_value (text, &v, + text_to_value (text, dialog->var_store->dict, - *var_get_write_format (dialog->pv)); + dialog->pv, + &v); g_signal_handler_block (GTK_ENTRY (dialog->label_entry), @@ -168,6 +171,8 @@ on_value_entry_change (GtkEntry *entry, gpointer data) g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry), dialog->change_handler_id); + + value_destroy (&v, var_get_width (dialog->pv)); } @@ -267,9 +272,10 @@ on_change (GtkWidget *w, gpointer data) union value v; - text_to_value (val_text, &v, + text_to_value (val_text, dialog->var_store->dict, - *var_get_write_format (dialog->pv)); + dialog->pv, + &v); val_labs_replace (dialog->labs, &v, gtk_entry_get_text (GTK_ENTRY (dialog->label_entry))); @@ -278,6 +284,8 @@ on_change (GtkWidget *w, gpointer data) repopulate_dialog (dialog); gtk_widget_grab_focus (dialog->value_entry); + + value_destroy (&v, var_get_width (dialog->pv)); } /* Callback which occurs when the "Add" button is clicked */ @@ -290,20 +298,22 @@ on_add (GtkWidget *w, gpointer data) const gchar *text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry)); - text_to_value (text, &v, + text_to_value (text, dialog->var_store->dict, - *var_get_write_format (dialog->pv)); - + dialog->pv, + &v); - if ( ! val_labs_add (dialog->labs, &v, - gtk_entry_get_text - ( GTK_ENTRY (dialog->label_entry)) ) ) - return ; + if (val_labs_add (dialog->labs, &v, + gtk_entry_get_text + ( GTK_ENTRY (dialog->label_entry)) ) ) + { + gtk_widget_set_sensitive (dialog->add_button, FALSE); - gtk_widget_set_sensitive (dialog->add_button, FALSE); + repopulate_dialog (dialog); + gtk_widget_grab_focus (dialog->value_entry); + } - repopulate_dialog (dialog); - gtk_widget_grab_focus (dialog->value_entry); + value_destroy (&v, var_get_width (dialog->pv)); } /* Callback which occurs when the "Remove" button is clicked */