Use new Gnulib function dtoastr() to format short, accurate real numbers.
[pspp-builds.git] / src / ui / syntax-gen.c
index e853c5692780463beed4a27887ffdda079dd93df..9221eae4d6a554c09e87275884ab496d35980ba8 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2008 Free Software Foundation, Inc.
+   Copyright (C) 2008, 2010 Free Software Foundation, Inc.
 
    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
 #include <ctype.h>
 #include <mbchar.h>
 
-#include <data/data-in.h>
-#include <data/data-out.h>
-#include <data/format.h>
-#include <data/value.h>
-#include <libpspp/assertion.h>
-#include <libpspp/message.h>
-#include <libpspp/str.h>
+#include "data/data-in.h"
+#include "data/data-out.h"
+#include "data/format.h"
+#include "data/value.h"
+#include "libpspp/assertion.h"
+#include "libpspp/cast.h"
+#include "libpspp/message.h"
+#include "libpspp/str.h"
+
+#include "gl/ftoastr.h"
 
 /* Appends to OUTPUT a pair of hex digits for each byte in IN. */
 static void
@@ -37,8 +40,8 @@ syntax_gen_hex_digits (struct string *output, struct substring in)
   for (i = 0; i < in.length; i++)
     {
       unsigned char c = in.string[i];
-      ds_put_char (output, "0123456789ABCDEF"[c >> 4]);
-      ds_put_char (output, "0123456789ABCDEF"[c & 0xf]);
+      ds_put_byte (output, "0123456789ABCDEF"[c >> 4]);
+      ds_put_byte (output, "0123456789ABCDEF"[c & 0xf]);
     }
 }
 
@@ -58,13 +61,13 @@ has_control_chars (struct substring in)
 static bool
 has_single_quote (struct substring str)
 {
-  return (SIZE_MAX != ss_find_char (str, '\''));
+  return (SIZE_MAX != ss_find_byte (str, '\''));
 }
 
 static bool
 has_double_quote (struct substring str)
 {
-  return (SIZE_MAX != ss_find_char (str, '"'));
+  return (SIZE_MAX != ss_find_byte (str, '"'));
 }
 
 /* Appends to OUTPUT valid PSPP syntax for a quoted string that
@@ -83,7 +86,7 @@ syntax_gen_string (struct string *output, struct substring in)
     {
       ds_put_cstr (output, "X'");
       syntax_gen_hex_digits (output, in);
-      ds_put_char (output, '\'');
+      ds_put_byte (output, '\'');
     }
   else
     {
@@ -98,15 +101,15 @@ syntax_gen_string (struct string *output, struct substring in)
       assert (is_basic ('\''));
 
       quote = has_double_quote (in) && !has_single_quote (in) ? '\'' : '"';
-      ds_put_char (output, quote);
+      ds_put_byte (output, quote);
       for (i = 0; i < in.length; i++)
         {
           char c = in.string[i];
           if (c == quote)
-            ds_put_char (output, quote);
-          ds_put_char (output, c);
+            ds_put_byte (output, quote);
+          ds_put_byte (output, c);
         }
-      ds_put_char (output, quote);
+      ds_put_byte (output, quote);
     }
 }
 
@@ -146,15 +149,18 @@ syntax_gen_number (struct string *output,
           & (FMT_CAT_DATE | FMT_CAT_TIME | FMT_CAT_DATE_COMPONENT)))
     {
       union value v_in, v_out;
-      char *s;
+      char *s, *error;
       bool ok;
 
       v_in.f = number;
-      s = data_out (&v_in, format);
-      msg_disable ();
-      ok = data_in (ss_cstr (s), LEGACY_NATIVE,
-                    format->type, false, 0, 0, &v_out, 0);
-      msg_enable ();
+      s = data_out (&v_in, "FIXME",  format);
+
+      /* FIXME: UTF8 encoded strings will fail here */
+      error = data_in (ss_cstr (s), LEGACY_NATIVE,
+                       format->type, &v_out, 0, NULL);
+      ok = error == NULL;
+      free (error);
+
       if (ok && v_out.f == number)
         {
           syntax_gen_string (output, ss_cstr (s));
@@ -168,18 +174,10 @@ syntax_gen_number (struct string *output,
     ds_put_cstr (output, "SYSMIS");
   else
     {
-      /* FIXME: This should consistently yield precisely the same
-         value as NUMBER on input, but its results for values
-         cannot be exactly represented in decimal are ugly: many
-         of them will have far more decimal digits than are
-         needed.  The free-format floating point output routine
-         from Steele and White, "How to Print Floating-Point
-         Numbers Accurately" is really what we want.  The MPFR
-         library has an implementation of this, or equivalent
-         functionality, in its mpfr_strtofr routine, but it would
-         not be nice to make PSPP depend on this.  Probably, we
-         should implement something equivalent to it. */
-      ds_put_format (output, "%.*g", DBL_DIG + 1, number);
+      char s[DBL_BUFSIZE_BOUND];
+
+      dtoastr (s, sizeof s, 0, 0, number);
+      ds_put_cstr (output, s);
     }
 }
 
@@ -196,7 +194,10 @@ syntax_gen_value (struct string *output, const union value *value, int width,
   if (width == 0)
     syntax_gen_number (output, value->f, format);
   else
-    syntax_gen_string (output, ss_buffer (value_str (value, width), width));
+    {
+      char *s = CHAR_CAST_BUG (char *, value_str (value, width));
+      syntax_gen_string (output, ss_buffer (s, width));
+    }
 }
 
 /* Appends <low> THRU <high> to OUTPUT.  If LOW is LOWEST, then
@@ -279,7 +280,7 @@ syntax_gen_pspp_valist (struct string *output, const char *format,
           }
 
         case '%':
-          ds_put_char (output, '%');
+          ds_put_byte (output, '%');
           break;
 
         default: