Change many %g format specifiers to %.*g with precision DBL_DIG + 1.
[pspp] / src / ui / gui / recode-dialog.c
index 05984dd44b368af9684b167fd66b0420ff0881f4..4ef8ae216d88f3a9825bf3555a5b0d81cbc36221 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2007, 2009, 2010, 2011, 2012  Free Software Foundation
+   Copyright (C) 2007, 2009, 2010, 2011, 2012, 2014  Free Software Foundation
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -30,6 +30,7 @@
 
 #include <gtk/gtk.h>
 
+#include <float.h>
 #include <xalloc.h>
 #include <ui/gui/psppire-data-window.h>
 #include <ui/gui/dialog-common.h>
@@ -104,7 +105,7 @@ new_value_to_string (const GValue *src, GValue *dest)
     {
     case NV_NUMERIC:
       {
-       gchar *text = g_strdup_printf ("%g", nv->v.v);
+       gchar *text = g_strdup_printf ("%.*g", DBL_DIG + 1, nv->v.v);
        g_value_set_string (dest, text);
        g_free (text);
       }
@@ -314,7 +315,7 @@ recode_different_dialog (PsppireDataWindow *de)
 static gchar *
 num_to_string (gdouble x)
 {
-  return g_strdup_printf ("%g", x);
+  return g_strdup_printf ("%.*g", DBL_DIG + 1, x);
 }
 
 /* Callback which gets called when a new row is selected
@@ -951,31 +952,26 @@ run_old_and_new_dialog (struct recode_dialog *rd)
 
 /* Generate a syntax fragment for NV and append it to STR */
 static void
-new_value_append_syntax (GString *str, const struct new_value *nv)
+new_value_append_syntax (struct string *dds, const struct new_value *nv)
 {
   switch (nv->type)
     {
     case NV_NUMERIC:
-      g_string_append_printf (str, "%g", nv->v.v);
+      ds_put_c_format (dds, "%.*g", DBL_DIG + 1, nv->v.v);
       break;
     case NV_STRING:
-      {
-       struct string ds = DS_EMPTY_INITIALIZER;
-       syntax_gen_string (&ds, ss_cstr (nv->v.s));
-       g_string_append (str, ds_cstr (&ds));
-       ds_destroy (&ds);
-      }
+      syntax_gen_string (dds, ss_cstr (nv->v.s));
       break;
     case NV_COPY:
-      g_string_append (str, "COPY");
+      ds_put_cstr (dds, "COPY");
       break;
     case NV_SYSMIS:
-      g_string_append (str, "SYSMIS");
+      ds_put_cstr (dds, "SYSMIS");
       break;
     default:
       /* Shouldn't ever happen */
       g_warning ("Invalid type in new recode value");
-      g_string_append (str, "???");
+      ds_put_cstr (dds, "???");
       break;
     }
 }
@@ -987,8 +983,10 @@ generate_syntax (const struct recode_dialog *rd)
   gboolean ok;
   GtkTreeIter iter;
   gchar *text;
+  struct string dds;
+
+  ds_init_empty (&dds);
 
-  GString *str = g_string_sized_new (100);
 
   /* Declare new string variables if applicable */
   if ( rd->different &&
@@ -1002,24 +1000,24 @@ generate_syntax (const struct recode_dialog *rd)
       g_hash_table_iter_init (&iter, rd->varmap);
       while (g_hash_table_iter_next (&iter, (void**) &var, (void**) &nlp))
        {
-         g_string_append (str, "\nSTRING ");
-         g_string_append (str, nlp->name);
-         g_string_append_printf (str, " (A%d).",
+         ds_put_cstr (&dds, "\nSTRING ");
+         ds_put_cstr (&dds, nlp->name);
+         ds_put_c_format (&dds, " (A%d).",
                                  (int)
                                  gtk_spin_button_get_value (GTK_SPIN_BUTTON (rd->width_entry) )
                                  );
        }
     }
 
-  g_string_append (str, "\nRECODE ");
+  ds_put_cstr (&dds, "\nRECODE ");
 
-  psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->variable_treeview), 0, str);
+  psppire_var_view_append_names_str (PSPPIRE_VAR_VIEW (rd->variable_treeview), 0, &dds);
 
-  g_string_append (str, "\n\t");
+  ds_put_cstr (&dds, "\n\t");
 
   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->convert_button)))
     {
-      g_string_append (str, "(CONVERT) ");
+      ds_put_cstr (&dds, "(CONVERT) ");
     }
 
   for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (rd->value_map),
@@ -1040,13 +1038,13 @@ generate_syntax (const struct recode_dialog *rd)
       ov = g_value_get_boxed (&ov_value);
       nv = g_value_get_boxed (&nv_value);
 
-      g_string_append (str, "(");
+      ds_put_cstr (&dds, "(");
 
-      old_value_append_syntax (str, ov);
-      g_string_append (str, " = ");
-      new_value_append_syntax (str, nv);
+      old_value_append_syntax (&dds, ov);
+      ds_put_cstr (&dds, " = ");
+      new_value_append_syntax (&dds, nv);
 
-      g_string_append (str, ") ");
+      ds_put_cstr (&dds, ") ");
       g_value_unset (&ov_value);
       g_value_unset (&nv_value);
     }
@@ -1056,7 +1054,7 @@ generate_syntax (const struct recode_dialog *rd)
     {
 
       GtkTreeIter iter;
-      g_string_append (str, "\n\tINTO ");
+      ds_put_cstr (&dds, "\n\tINTO ");
 
       for (ok = psppire_var_view_get_iter_first (PSPPIRE_VAR_VIEW (rd->variable_treeview), &iter);
           ok;
@@ -1067,12 +1065,12 @@ generate_syntax (const struct recode_dialog *rd)
 
            nlp = g_hash_table_lookup (rd->varmap, var);
            
-           g_string_append (str, nlp->name);
-           g_string_append (str, " ");
+           ds_put_cstr (&dds, nlp->name);
+           ds_put_cstr (&dds, " ");
          }
     }
 
-  g_string_append (str, ".");
+  ds_put_cstr (&dds, ".");
 
   /* If applicable, set labels for the new variables. */
   if ( rd->different )
@@ -1090,7 +1088,7 @@ generate_syntax (const struct recode_dialog *rd)
              struct string sl;
              ds_init_empty (&sl);
              syntax_gen_string (&sl, ss_cstr (nlp->label));
-             g_string_append_printf (str, "\nVARIABLE LABELS %s %s.",
+             ds_put_c_format (&dds, "\nVARIABLE LABELS %s %s.",
                                      nlp->name, ds_cstr (&sl));
 
              ds_destroy (&sl);
@@ -1098,12 +1096,12 @@ generate_syntax (const struct recode_dialog *rd)
        }
     }
 
-  g_string_append (str, "\nEXECUTE.\n");
+  ds_put_cstr (&dds, "\nEXECUTE.\n");
 
 
-  text = str->str;
+  text = ds_steal_cstr (&dds);
 
-  g_string_free (str, FALSE);
+  ds_destroy (&dds);
 
   return text;
 }