From: Ben Pfaff Date: Sun, 10 Dec 2006 04:03:34 +0000 (+0000) Subject: New function fmt_dollar_template based on pspp-dev discussion. X-Git-Tag: v0.6.0~655 X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5bacd5c053e285ccfba287d2b28079f1d1c49cc9;p=pspp-builds.git New function fmt_dollar_template based on pspp-dev discussion. --- diff --git a/src/data/ChangeLog b/src/data/ChangeLog index e4306829..44cd03ee 100644 --- a/src/data/ChangeLog +++ b/src/data/ChangeLog @@ -1,3 +1,8 @@ +Sat Dec 9 20:02:25 2006 Ben Pfaff + + * format.c (fmt_dollar_template): New function, based on + dollar_format_template from var-type-dialog.c. + Sat Dec 9 18:05:59 2006 Ben Pfaff * data-out.c (output_scientific): Fix bad assumption that "buf" is diff --git a/src/data/format.c b/src/data/format.c index 60c1d0a9..e2b163a5 100644 --- a/src/data/format.c +++ b/src/data/format.c @@ -587,6 +587,34 @@ fmt_date_template (enum fmt_type type) NOT_REACHED (); } } + +/* Returns a string of the form "$#,###.##" according to FMT, + which must be of type FMT_DOLLAR. The caller must free the + string. */ +char * +fmt_dollar_template (const struct fmt_spec *fmt) +{ + struct string s = DS_EMPTY_INITIALIZER; + int c; + + ds_put_char (&s, '$'); + for (c = MAX (fmt->w - fmt->d - 1, 0); c > 0; ) + { + ds_put_char (&s, '#'); + if (--c % 4 == 0 && c > 0) + { + ds_put_char (&s, fmt_grouping_char (fmt->type)); + --c; + } + } + if (fmt->d > 0) + { + ds_put_char (&s, '.'); + ds_put_char_multiple (&s, '#', fmt->d); + } + + return ds_cstr (&s); +} /* Returns true if TYPE is a valid format type, false otherwise. */ diff --git a/src/data/format.h b/src/data/format.h index 089f1ed3..51484a3b 100644 --- a/src/data/format.h +++ b/src/data/format.h @@ -116,6 +116,7 @@ bool fmt_from_io (int io, enum fmt_type *); bool fmt_usable_for_input (enum fmt_type) PURE_FUNCTION; const char *fmt_date_template (enum fmt_type) PURE_FUNCTION; +char *fmt_dollar_template (const struct fmt_spec *); /* Maximum length of prefix or suffix string in struct fmt_number_style. */ diff --git a/src/ui/gui/ChangeLog b/src/ui/gui/ChangeLog index 0b8aee88..1fbddc3e 100644 --- a/src/ui/gui/ChangeLog +++ b/src/ui/gui/ChangeLog @@ -1,3 +1,9 @@ +Sat Dec 9 20:03:04 2006 Ben Pfaff + + * var-type-dialog.c (var_type_dialog_create): Use + fmt_dollar_template from data/format.c. + (dollar_format_template) Removed. + Sat Dec 9 07:19:53 WST 2006 John Darrington * syntax-editor.c : New file. diff --git a/src/ui/gui/var-type-dialog.c b/src/ui/gui/var-type-dialog.c index a4b66cc2..d4262872 100644 --- a/src/ui/gui/var-type-dialog.c +++ b/src/ui/gui/var-type-dialog.c @@ -212,44 +212,6 @@ static gint on_var_type_ok_clicked(GtkWidget *w, gpointer data); #define LEN 20 -/* return a string of the form "$#,###.##" according to FMT. - FMT must be of type FMT_DOLLAR - */ -static const gchar * -dollar_format_template(const struct fmt_spec *fmt) -{ - static gchar buf[LEN]; - g_assert( fmt->type == FMT_DOLLAR); - - { - gint c ; - gint int_part = fmt->w - fmt->d; - if ( fmt->d > 0 ) --int_part; - g_assert(int_part > 0); - - g_strlcpy(buf, "$", LEN); - - c = int_part - 1; - while(c > 0) - { - g_strlcat(buf, "#", LEN); - if(--c % 4 == 0 && c > 0 ) - { - g_strlcat(buf, ",", LEN); - --c; - } - } - if ( fmt->d > 0 ) - { - g_strlcat(buf, ".", LEN); - for ( c = 0 ; c < fmt->d ; ++c ) - g_strlcat(buf, "#", LEN); - } - } - - return buf; -} - static void add_to_group(GtkWidget *w, gpointer data) { @@ -504,11 +466,13 @@ var_type_dialog_create(GladeXML *xml) for ( i = 0 ; i < sizeof(dollar_format)/sizeof(dollar_format[0]) ; ++i ) { + char *template = fmt_dollar_template (&dollar_format[i]); gtk_list_store_append (list_store, &iter); gtk_list_store_set (list_store, &iter, - 0, dollar_format_template(&dollar_format[i]), + 0, template, 1, &dollar_format[i], -1); + free (template); } gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->dollar_treeview),