1ed83bcbc9b3f1221634600ae89c30505fee3cda
[pspp-builds.git] / src / data / data-out.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2009, 2011 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "data-out.h"
20
21 #include <ctype.h>
22 #include <float.h>
23 #include <math.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <time.h>
27
28 #include <data/calendar.h>
29 #include <data/format.h>
30 #include <data/settings.h>
31 #include <data/value.h>
32
33 #include <libpspp/assertion.h>
34 #include <libpspp/float-format.h>
35 #include <libpspp/integer-format.h>
36 #include <libpspp/message.h>
37 #include <libpspp/misc.h>
38 #include <libpspp/str.h>
39 #include <libpspp/pool.h>
40 #include <libpspp/i18n.h>
41
42 #include "minmax.h"
43
44 #include "gettext.h"
45 #define _(msgid) gettext (msgid)
46 \f
47 /* A representation of a number that can be quickly rounded to
48    any desired number of decimal places (up to a specified
49    maximum). */
50 struct rounder
51   {
52     char string[64];    /* Magnitude of number with excess precision. */
53     int integer_digits; /* Number of digits before decimal point. */
54     int leading_nines;  /* Number of `9's or `.'s at start of string. */
55     int leading_zeros;  /* Number of `0's or `.'s at start of string. */
56     bool negative;      /* Is the number negative? */
57   };
58
59 static void rounder_init (struct rounder *, double number, int max_decimals);
60 static int rounder_width (const struct rounder *, int decimals,
61                           int *integer_digits, bool *negative);
62 static void rounder_format (const struct rounder *, int decimals,
63                             char *output);
64 \f
65 typedef void data_out_converter_func (const union value *,
66                                       const struct fmt_spec *,
67                                       char *);
68 #define FMT(NAME, METHOD, IMIN, OMIN, IO, CATEGORY) \
69         static data_out_converter_func output_##METHOD;
70 #include "format.def"
71
72 static bool output_decimal (const struct rounder *, const struct fmt_spec *,
73                             bool require_affixes, char *);
74 static bool output_scientific (double, const struct fmt_spec *,
75                                bool require_affixes, char *);
76
77 static double power10 (int) PURE_FUNCTION;
78 static double power256 (int) PURE_FUNCTION;
79
80 static void output_infinite (double, const struct fmt_spec *, char *);
81 static void output_missing (const struct fmt_spec *, char *);
82 static void output_overflow (const struct fmt_spec *, char *);
83 static bool output_bcd_integer (double, int digits, char *);
84 static void output_binary_integer (uint64_t, int bytes, enum integer_format,
85                                    char *);
86 static void output_hex (const void *, size_t bytes, char *);
87 \f
88
89 static data_out_converter_func *const converters[FMT_NUMBER_OF_FORMATS] =
90     {
91 #define FMT(NAME, METHOD, IMIN, OMIN, IO, CATEGORY) output_##METHOD,
92 #include "format.def"
93     };
94
95 /* Similar to data_out. Additionally recodes the output from
96    native form into the given legacy character ENCODING.
97    OUTPUT must be provided by the caller and must be at least
98    FORMAT->w long. No null terminator is appended to OUTPUT.
99 */
100 void
101 data_out_legacy (const union value *input, const char *encoding,
102                  const struct fmt_spec *format, char *output)
103 {
104   assert (fmt_check_output (format));
105
106   converters[format->type] (input, format, output);
107   if (0 != strcmp (encoding, C_ENCODING)
108       && fmt_get_category (format->type) != FMT_CAT_BINARY)
109     {
110       char *s  = recode_string (encoding, C_ENCODING, output, format->w );
111       memcpy (output, s, format->w);
112       free (s);
113     }
114 }
115
116 /* Converts the INPUT value into a UTF8 encoded string, according
117    to format specification FORMAT. 
118
119    VALUE must be the correct width for FORMAT, that is, its
120    width must be fmt_var_width(FORMAT).
121
122    ENCODING must be the encoding of INPUT.  Normally this can
123    be obtained by calling dict_get_encoding on the dictionary
124    with which INPUT is associated.
125
126    The return value is dynamically allocated, and must be freed
127    by the caller.  If POOL is non-null, then the return value is
128    allocated on that pool.
129 */
130 char *
131 data_out_pool (const union value *input, const char *encoding,
132                const struct fmt_spec *format, struct pool *pool)
133 {
134   const struct fmt_number_style *style = settings_get_style (format->type);
135   char *output;
136   char *t ;
137   assert (fmt_check_output (format));
138
139   output = xmalloc (format->w + style->extra_bytes + 1);
140
141   converters[format->type] (input, format, output);
142
143   t =  recode_string_pool (UTF8, encoding, output, format->w, pool);
144   free (output);
145   return t;
146 }
147
148 char *
149 data_out (const union value *input, const char *encoding, const struct fmt_spec *format)
150 {
151   return data_out_pool (input, encoding, format, NULL);
152 }
153
154 \f
155 /* Main conversion functions. */
156
157 /* Outputs F, COMMA, DOT, DOLLAR, PCT, E, CCA, CCB, CCC, CCD, and
158    CCE formats. */
159 static void
160 output_number (const union value *input, const struct fmt_spec *format,
161                char *output)
162 {
163   double number = input->f;
164
165   if (number == SYSMIS)
166     output_missing (format, output);
167   else if (!isfinite (number))
168     output_infinite (number, format, output);
169   else
170     {
171       if (format->type != FMT_E && fabs (number) < 1.5 * power10 (format->w))
172         {
173           struct rounder r;
174           rounder_init (&r, number, format->d);
175
176           if (output_decimal (&r, format, true, output)
177               || output_scientific (number, format, true, output)
178               || output_decimal (&r, format, false, output))
179             return;
180         }
181
182       if (!output_scientific (number, format, false, output))
183         output_overflow (format, output);
184     }
185 }
186
187 /* Outputs N format. */
188 static void
189 output_N (const union value *input, const struct fmt_spec *format,
190           char *output)
191 {
192   double number = input->f * power10 (format->d);
193   if (input->f == SYSMIS || number < 0)
194     output_missing (format, output);
195   else
196     {
197       char buf[128];
198       number = fabs (round (number));
199       if (number < power10 (format->w)
200           && sprintf (buf, "%0*.0f", format->w, number) == format->w)
201         memcpy (output, buf, format->w);
202       else
203         output_overflow (format, output);
204     }
205
206   output[format->w] = '\0';
207 }
208
209 /* Outputs Z format. */
210 static void
211 output_Z (const union value *input, const struct fmt_spec *format,
212           char *output)
213 {
214   double number = input->f * power10 (format->d);
215   char buf[128];
216   if (input->f == SYSMIS)
217     output_missing (format, output);
218   else if (fabs (number) < power10 (format->w)
219            && sprintf (buf, "%0*.0f", format->w,
220                        fabs (round (number))) == format->w)
221     {
222       if (number < 0 && strspn (buf, "0") < format->w)
223         {
224           char *p = &buf[format->w - 1];
225           *p = "}JKLMNOPQR"[*p - '0'];
226         }
227       memcpy (output, buf, format->w);
228       output[format->w] = '\0';
229     }
230   else
231     output_overflow (format, output);
232 }
233
234 /* Outputs P format. */
235 static void
236 output_P (const union value *input, const struct fmt_spec *format,
237           char *output)
238 {
239   if (output_bcd_integer (fabs (input->f * power10 (format->d)),
240                           format->w * 2 - 1, output)
241       && input->f < 0.0)
242     output[format->w - 1] |= 0xd;
243   else
244     output[format->w - 1] |= 0xf;
245 }
246
247 /* Outputs PK format. */
248 static void
249 output_PK (const union value *input, const struct fmt_spec *format,
250            char *output)
251 {
252   output_bcd_integer (input->f * power10 (format->d), format->w * 2, output);
253 }
254
255 /* Outputs IB format. */
256 static void
257 output_IB (const union value *input, const struct fmt_spec *format,
258            char *output)
259 {
260   double number = round (input->f * power10 (format->d));
261   if (input->f == SYSMIS
262       || number >= power256 (format->w) / 2 - 1
263       || number < -power256 (format->w) / 2)
264     memset (output, 0, format->w);
265   else
266     {
267       uint64_t integer = fabs (number);
268       if (number < 0)
269         integer = -integer;
270       output_binary_integer (integer, format->w,
271                              settings_get_output_integer_format (),
272                              output);
273     }
274
275   output[format->w] = '\0';
276 }
277
278 /* Outputs PIB format. */
279 static void
280 output_PIB (const union value *input, const struct fmt_spec *format,
281             char *output)
282 {
283   double number = round (input->f * power10 (format->d));
284   if (input->f == SYSMIS
285       || number < 0 || number >= power256 (format->w))
286     memset (output, 0, format->w);
287   else
288     output_binary_integer (number, format->w,
289                            settings_get_output_integer_format (), output);
290
291   output[format->w] = '\0';
292 }
293
294 /* Outputs PIBHEX format. */
295 static void
296 output_PIBHEX (const union value *input, const struct fmt_spec *format,
297                char *output)
298 {
299   double number = round (input->f);
300   if (input->f == SYSMIS)
301     output_missing (format, output);
302   else if (input->f < 0 || number >= power256 (format->w / 2))
303     output_overflow (format, output);
304   else
305     {
306       char tmp[8];
307       output_binary_integer (number, format->w / 2, INTEGER_MSB_FIRST, tmp);
308       output_hex (tmp, format->w / 2, output);
309     }
310
311 }
312
313 /* Outputs RB format. */
314 static void
315 output_RB (const union value *input, const struct fmt_spec *format,
316            char *output)
317 {
318   double d = input->f;
319   memcpy (output, &d, format->w);
320
321   output[format->w] = '\0';
322 }
323
324 /* Outputs RBHEX format. */
325 static void
326 output_RBHEX (const union value *input, const struct fmt_spec *format,
327               char *output)
328 {
329   double d = input->f;
330
331   output_hex (&d, format->w / 2, output);
332 }
333
334 /* Outputs DATE, ADATE, EDATE, JDATE, SDATE, QYR, MOYR, WKYR,
335    DATETIME, TIME, and DTIME formats. */
336 static void
337 output_date (const union value *input, const struct fmt_spec *format,
338              char *output)
339 {
340   double number = input->f;
341   int year, month, day, yday;
342
343   const char *template = fmt_date_template (format->type);
344   size_t template_width = strlen (template);
345   int excess_width = format->w - template_width;
346
347   char tmp[64];
348   char *p = tmp;
349
350   assert (format->w >= template_width);
351   if (number == SYSMIS)
352     goto missing;
353
354   if (fmt_get_category (format->type) == FMT_CAT_DATE)
355     {
356       if (number <= 0)
357         goto missing;
358       calendar_offset_to_gregorian (number / 60. / 60. / 24.,
359                                     &year, &month, &day, &yday);
360       number = fmod (number, 60. * 60. * 24.);
361     }
362   else
363     year = month = day = yday = 0;
364
365   while (*template != '\0')
366     {
367       int ch = *template;
368       int count = 1;
369       while (template[count] == ch)
370         count++;
371       template += count;
372
373       switch (ch)
374         {
375         case 'd':
376           if (count < 3)
377             p += sprintf (p, "%02d", day);
378           else
379             p += sprintf (p, "%03d", yday);
380           break;
381         case 'm':
382           if (count < 3)
383             p += sprintf (p, "%02d", month);
384           else
385             {
386               static const char *const months[12] =
387                 {
388                   "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
389                   "JUL", "AUG", "SEP", "OCT", "NOV", "DEC",
390                 };
391               p = stpcpy (p, months[month - 1]);
392             }
393           break;
394         case 'y':
395           if (count >= 4 || excess_width >= 2)
396             {
397               if (year <= 9999)
398                 p += sprintf (p, "%04d", year);
399               else if (format->type == FMT_DATETIME)
400                 p = stpcpy (p, "****");
401               else
402                 goto overflow;
403             }
404           else
405             {
406               int epoch =  settings_get_epoch ();
407               int offset = year - epoch;
408               if (offset < 0 || offset > 99)
409                 goto overflow;
410               p += sprintf (p, "%02d", abs (year) % 100);
411             }
412           break;
413         case 'q':
414           p += sprintf (p, "%d", (month - 1) / 3 + 1);
415           break;
416         case 'w':
417           p += sprintf (p, "%2d", (yday - 1) / 7 + 1);
418           break;
419         case 'D':
420           if (number < 0)
421             *p++ = '-';
422           number = fabs (number);
423           p += sprintf (p, "%*.0f", count, floor (number / 60. / 60. / 24.));
424           number = fmod (number, 60. * 60. * 24.);
425           break;
426         case 'H':
427           if (number < 0)
428             *p++ = '-';
429           number = fabs (number);
430           p += sprintf (p, "%0*.0f", count, floor (number / 60. / 60.));
431           number = fmod (number, 60. * 60.);
432           break;
433         case 'M':
434           p += sprintf (p, "%02d", (int) floor (number / 60.));
435           number = fmod (number, 60.);
436           excess_width = format->w - (p - tmp);
437           if (excess_width < 0)
438             goto overflow;
439           if (excess_width == 3 || excess_width == 4
440               || (excess_width >= 5 && format->d == 0))
441             p += sprintf (p, ":%02d", (int) number);
442           else if (excess_width >= 5)
443             {
444               int d = MIN (format->d, excess_width - 4);
445               int w = d + 3;
446               sprintf (p, ":%0*.*f", w, d, number);
447               if (settings_get_decimal_char (FMT_F) != '.')
448                 {
449                   char *cp = strchr (p, '.');
450                   if (cp != NULL)
451                     *cp = settings_get_decimal_char (FMT_F);
452                 }
453               p += strlen (p);
454             }
455           break;
456         case 'X':
457           *p++ = ' ';
458           break;
459         default:
460           assert (count == 1);
461           *p++ = ch;
462           break;
463         }
464     }
465
466   buf_copy_lpad (output, format->w, tmp, p - tmp, ' ');
467   output[format->w] = '\0';
468   return;
469
470  overflow:
471   output_overflow (format, output);
472   return;
473
474  missing:
475   output_missing (format, output);
476   return;
477 }
478
479 /* Outputs WKDAY format. */
480 static void
481 output_WKDAY (const union value *input, const struct fmt_spec *format,
482               char *output)
483 {
484   static const char *const weekdays[7] =
485     {
486       "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY",
487       "THURSDAY", "FRIDAY", "SATURDAY",
488     };
489
490   if (input->f >= 1 && input->f < 8)
491     {
492       buf_copy_str_rpad (output, format->w,
493                          weekdays[(int) input->f - 1], ' ');
494       output[format->w] = '\0';
495     }
496   else
497     {
498       if (input->f != SYSMIS)
499         msg (ME, _("Weekday number %f is not between 1 and 7."), input->f);
500       output_missing (format, output);
501     }
502
503 }
504
505 /* Outputs MONTH format. */
506 static void
507 output_MONTH (const union value *input, const struct fmt_spec *format,
508               char *output)
509 {
510   static const char *const months[12] =
511     {
512       "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
513       "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER",
514     };
515
516   if (input->f >= 1 && input->f < 13)
517     {
518       buf_copy_str_rpad (output, format->w, months[(int) input->f - 1], ' ');
519       output[format->w] = '\0';
520     }
521   else
522     {
523       if (input->f != SYSMIS)
524         msg (ME, _("Month number %f is not between 1 and 12."), input->f);
525       output_missing (format, output);
526     }
527
528 }
529
530 /* Outputs A format. */
531 static void
532 output_A (const union value *input, const struct fmt_spec *format,
533           char *output)
534 {
535   memcpy (output, value_str (input, format->w), format->w);
536   output[format->w] = '\0';
537 }
538
539 /* Outputs AHEX format. */
540 static void
541 output_AHEX (const union value *input, const struct fmt_spec *format,
542              char *output)
543 {
544   output_hex (value_str (input, format->w), format->w / 2, output);
545 }
546 \f
547 /* Decimal and scientific formatting. */
548
549 /* If REQUEST plus the current *WIDTH fits within MAX_WIDTH,
550    increments *WIDTH by REQUEST and return true.
551    Otherwise returns false without changing *WIDTH. */
552 static bool
553 allocate_space (int request, int max_width, int *width)
554 {
555   assert (*width <= max_width);
556   if (request + *width <= max_width)
557     {
558       *width += request;
559       return true;
560     }
561   else
562     return false;
563 }
564
565 /* Tries to compose the number represented by R, in the style of
566    FORMAT, into OUTPUT.  Returns true if successful, false on
567    failure, which occurs if FORMAT's width is too narrow.  If
568    REQUIRE_AFFIXES is true, then the prefix and suffix specified
569    by FORMAT's style must be included; otherwise, they may be
570    omitted to make the number fit. */
571 static bool
572 output_decimal (const struct rounder *r, const struct fmt_spec *format,
573                 bool require_affixes, char *output)
574 {
575   const struct fmt_number_style *style =
576     settings_get_style (format->type);
577
578   int decimals;
579
580   for (decimals = format->d; decimals >= 0; decimals--)
581     {
582       /* Formatted version of magnitude of NUMBER. */
583       char magnitude[64];
584
585       /* Number of digits in MAGNITUDE's integer and fractional parts. */
586       int integer_digits;
587
588       /* Amount of space within the field width already claimed.
589          Initially this is the width of MAGNITUDE, then it is reduced
590          in stages as space is allocated to prefixes and suffixes and
591          grouping characters. */
592       int width;
593
594       /* Include various decorations? */
595       bool add_neg_prefix;
596       bool add_affixes;
597       bool add_grouping;
598
599       /* Position in output. */
600       char *p;
601
602       /* Make sure there's room for the number's magnitude, plus
603          the negative suffix, plus (if negative) the negative
604          prefix. */
605       width = rounder_width (r, decimals, &integer_digits, &add_neg_prefix);
606       width += style->neg_suffix.width;
607       if (add_neg_prefix)
608         width += style->neg_prefix.width;
609       if (width > format->w)
610         continue;
611
612       /* If there's room for the prefix and suffix, allocate
613          space.  If the affixes are required, but there's no
614          space, give up. */
615       add_affixes = allocate_space (fmt_affix_width (style),
616                                     format->w, &width);
617       if (!add_affixes && require_affixes)
618         continue;
619
620       /* Check whether we should include grouping characters.
621          We need room for a complete set or we don't insert any at all.
622          We don't include grouping characters if decimal places were
623          requested but they were all dropped. */
624       add_grouping = (style->grouping != 0
625                       && integer_digits > 3
626                       && (format->d == 0 || decimals > 0)
627                       && allocate_space ((integer_digits - 1) / 3,
628                                          format->w, &width));
629
630       /* Format the number's magnitude. */
631       rounder_format (r, decimals, magnitude);
632
633       /* Assemble number. */
634       p = output;
635       if (format->w > width)
636         p = mempset (p, ' ', format->w - width);
637       if (add_neg_prefix)
638         p = stpcpy (p, style->neg_prefix.s);
639       if (add_affixes)
640         p = stpcpy (p, style->prefix.s);
641       if (!add_grouping)
642         p = mempcpy (p, magnitude, integer_digits);
643       else
644         {
645           int i;
646           for (i = 0; i < integer_digits; i++)
647             {
648               if (i > 0 && (integer_digits - i) % 3 == 0)
649                 *p++ = style->grouping;
650               *p++ = magnitude[i];
651             }
652         }
653       if (decimals > 0)
654         {
655           *p++ = style->decimal;
656           p = mempcpy (p, &magnitude[integer_digits + 1], decimals);
657         }
658       if (add_affixes)
659         p = stpcpy (p, style->suffix.s);
660       if (add_neg_prefix)
661         p = stpcpy (p, style->neg_suffix.s);
662       else
663         p = mempset (p, ' ', style->neg_suffix.width);
664
665       assert (p >= output + format->w);
666       assert (p <= output + format->w + style->extra_bytes);
667       *p = '\0';
668
669       return true;
670     }
671   return false;
672 }
673
674 /* Formats NUMBER into OUTPUT in scientific notation according to
675    the style of the format specified in FORMAT. */
676 static bool
677 output_scientific (double number, const struct fmt_spec *format,
678                    bool require_affixes, char *output)
679 {
680   const struct fmt_number_style *style =
681     settings_get_style (format->type);
682   int width;
683   int fraction_width;
684   bool add_affixes;
685   char buf[64], *p;
686
687   /* Allocate minimum required space. */
688   width = 6 + style->neg_suffix.width;
689   if (number < 0)
690     width += style->neg_prefix.width;
691   if (width > format->w)
692     return false;
693
694   /* Check for room for prefix and suffix. */
695   add_affixes = allocate_space (fmt_affix_width (style), format->w, &width);
696   if (require_affixes && !add_affixes)
697     return false;
698
699   /* Figure out number of characters we can use for the fraction,
700      if any.  (If that turns out to be 1, then we'll output a
701      decimal point without any digits following; that's what the
702      # flag does in the call to sprintf, below.) */
703   fraction_width = MIN (MIN (format->d + 1, format->w - width), 16);
704   if (format->type != FMT_E && fraction_width == 1)
705     fraction_width = 0;
706   width += fraction_width;
707
708   /* Format (except suffix). */
709   p = buf;
710   if (width < format->w)
711     p = mempset (p, ' ', format->w - width);
712   if (number < 0)
713     p = stpcpy (p, style->neg_prefix.s);
714   if (add_affixes)
715     p = stpcpy (p, style->prefix.s);
716   if (fraction_width > 0)
717     sprintf (p, "%#.*E", fraction_width - 1, fabs (number));
718   else
719     sprintf (p, "%.0E", fabs (number));
720
721   /* The C locale always uses a period `.' as a decimal point.
722      Translate to comma if necessary. */
723   if (style->decimal != '.')
724     {
725       char *cp = strchr (p, '.');
726       if (cp != NULL)
727         *cp = style->decimal;
728     }
729
730   /* Make exponent have exactly three digits, plus sign. */
731   {
732     char *cp = strchr (p, 'E') + 1;
733     long int exponent = strtol (cp, NULL, 10);
734     if (abs (exponent) > 999)
735       return false;
736     sprintf (cp, "%+04ld", exponent);
737   }
738
739   /* Add suffixes. */
740   p = strchr (p, '\0');
741   if (add_affixes)
742     p = stpcpy (p, style->suffix.s);
743   if (number < 0)
744     p = stpcpy (p, style->neg_suffix.s);
745   else
746     p = mempset (p, ' ', style->neg_suffix.width);
747
748   assert (p >= output + format->w);
749   assert (p <= output + format->w + style->extra_bytes);
750   *p = '\0';
751
752   return true;
753 }
754 \f
755 /* Returns true if the magnitude represented by R should be
756    rounded up when chopped off at DECIMALS decimal places, false
757    if it should be rounded down. */
758 static bool
759 should_round_up (const struct rounder *r, int decimals)
760 {
761   int digit = r->string[r->integer_digits + decimals + 1];
762   assert (digit >= '0' && digit <= '9');
763   return digit >= '5';
764 }
765
766 /* Initializes R for formatting the magnitude of NUMBER to no
767    more than MAX_DECIMAL decimal places. */
768 static void
769 rounder_init (struct rounder *r, double number, int max_decimals)
770 {
771   assert (fabs (number) < 1e41);
772   assert (max_decimals >= 0 && max_decimals <= 16);
773   if (max_decimals == 0)
774     {
775       /* Fast path.  No rounding needed.
776
777          We append ".00" to the integer representation because
778          round_up assumes that fractional digits are present.  */
779       sprintf (r->string, "%.0f.00", fabs (round (number)));
780     }
781   else
782     {
783       /* Slow path.
784
785          This is more difficult than it really should be because
786          we have to make sure that numbers that are exactly
787          halfway between two representations are always rounded
788          away from zero.  This is not what sprintf normally does
789          (usually it rounds to even), so we have to fake it as
790          best we can, by formatting with extra precision and then
791          doing the rounding ourselves.
792
793          We take up to two rounds to format numbers.  In the
794          first round, we obtain 2 digits of precision beyond
795          those requested by the user.  If those digits are
796          exactly "50", then in a second round we format with as
797          many digits as are significant in a "double".
798
799          It might be better to directly implement our own
800          floating-point formatting routine instead of relying on
801          the system's sprintf implementation.  But the classic
802          Steele and White paper on printing floating-point
803          numbers does not hint how to do what we want, and it's
804          not obvious how to change their algorithms to do so.  It
805          would also be a lot of work. */
806       sprintf (r->string, "%.*f", max_decimals + 2, fabs (number));
807       if (!strcmp (r->string + strlen (r->string) - 2, "50"))
808         {
809           int binary_exponent, decimal_exponent, format_decimals;
810           frexp (number, &binary_exponent);
811           decimal_exponent = binary_exponent * 3 / 10;
812           format_decimals = (DBL_DIG + 1) - decimal_exponent;
813           if (format_decimals > max_decimals + 2)
814             sprintf (r->string, "%.*f", format_decimals, fabs (number));
815         }
816     }
817
818   if (r->string[0] == '0')
819     memmove (r->string, &r->string[1], strlen (r->string));
820
821   r->leading_zeros = strspn (r->string, "0.");
822   r->leading_nines = strspn (r->string, "9.");
823   r->integer_digits = strchr (r->string, '.') - r->string;
824   r->negative = number < 0;
825 }
826
827 /* Returns the number of characters required to format the
828    magnitude represented by R to DECIMALS decimal places.
829    The return value includes integer digits and a decimal point
830    and fractional digits, if any, but it does not include any
831    negative prefix or suffix or other affixes.
832
833    *INTEGER_DIGITS is set to the number of digits before the
834    decimal point in the output, between 0 and 40.
835
836    If R represents a negative number and its rounded
837    representation would include at least one nonzero digit,
838    *NEGATIVE is set to true; otherwise, it is set to false. */
839 static int
840 rounder_width (const struct rounder *r, int decimals,
841                int *integer_digits, bool *negative)
842 {
843   /* Calculate base measures. */
844   int width = r->integer_digits;
845   if (decimals > 0)
846     width += decimals + 1;
847   *integer_digits = r->integer_digits;
848   *negative = r->negative;
849
850   /* Rounding can cause adjustments. */
851   if (should_round_up (r, decimals))
852     {
853       /* Rounding up leading 9s adds a new digit (a 1). */
854       if (r->leading_nines >= width)
855         {
856           width++;
857           ++*integer_digits;
858         }
859     }
860   else
861     {
862       /* Rounding down. */
863       if (r->leading_zeros >= width)
864         {
865           /* All digits that remain after rounding are zeros.
866              Therefore we drop the negative sign. */
867           *negative = false;
868           if (r->integer_digits == 0 && decimals == 0)
869             {
870               /* No digits at all are left.  We need to display
871                  at least a single digit (a zero). */
872               assert (width == 0);
873               width++;
874               *integer_digits = 1;
875             }
876         }
877     }
878   return width;
879 }
880
881 /* Formats the magnitude represented by R into OUTPUT, rounding
882    to DECIMALS decimal places.  Exactly as many characters as
883    indicated by rounder_width are written.  No terminating null
884    is appended. */
885 static void
886 rounder_format (const struct rounder *r, int decimals, char *output)
887 {
888   int base_width = r->integer_digits + (decimals > 0 ? decimals + 1 : 0);
889   if (should_round_up (r, decimals))
890     {
891       if (r->leading_nines < base_width)
892         {
893           /* Rounding up.  This is the common case where rounding
894              up doesn't add an extra digit. */
895           char *p;
896           memcpy (output, r->string, base_width);
897           for (p = output + base_width - 1; ; p--)
898             {
899               assert (p >= output);
900               if (*p == '9')
901                 *p = '0';
902               else if (*p >= '0' && *p <= '8')
903                 {
904                   (*p)++;
905                   break;
906                 }
907               else
908                 assert (*p == '.');
909             }
910         }
911       else
912         {
913           /* Rounding up leading 9s causes the result to be a 1
914              followed by a number of 0s, plus a decimal point. */
915           char *p = output;
916           *p++ = '1';
917           p = mempset (p, '0', r->integer_digits);
918           if (decimals > 0)
919             {
920               *p++ = '.';
921               p = mempset (p, '0', decimals);
922             }
923           assert (p == output + base_width + 1);
924         }
925     }
926   else
927     {
928       /* Rounding down. */
929       if (r->integer_digits != 0 || decimals != 0)
930         {
931           /* Common case: just copy the digits. */
932           memcpy (output, r->string, base_width);
933         }
934       else
935         {
936           /* No digits remain.  The output is just a zero. */
937           output[0] = '0';
938         }
939     }
940 }
941 \f
942 /* Helper functions. */
943
944 /* Returns 10**X. */
945 static double PURE_FUNCTION
946 power10 (int x)
947 {
948   static const double p[] =
949     {
950       1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
951       1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
952       1e20, 1e21, 1e22, 1e23, 1e24, 1e25, 1e26, 1e27, 1e28, 1e29,
953       1e30, 1e31, 1e32, 1e33, 1e34, 1e35, 1e36, 1e37, 1e38, 1e39,
954       1e40,
955     };
956   return x >= 0 && x < sizeof p / sizeof *p ? p[x] : pow (10.0, x);
957 }
958
959 /* Returns 256**X. */
960 static double PURE_FUNCTION
961 power256 (int x)
962 {
963   static const double p[] =
964     {
965       1.0,
966       256.0,
967       65536.0,
968       16777216.0,
969       4294967296.0,
970       1099511627776.0,
971       281474976710656.0,
972       72057594037927936.0,
973       18446744073709551616.0
974     };
975   return x >= 0 && x < sizeof p / sizeof *p ? p[x] : pow (256.0, x);
976 }
977
978 /* Formats non-finite NUMBER into OUTPUT according to the width
979    given in FORMAT. */
980 static void
981 output_infinite (double number, const struct fmt_spec *format, char *output)
982 {
983   assert (!isfinite (number));
984
985   if (format->w >= 3)
986     {
987       const char *s;
988
989       if (isnan (number))
990         s = "NaN";
991       else if (isinf (number))
992         s = number > 0 ? "+Infinity" : "-Infinity";
993       else
994         s = "Unknown";
995
996       buf_copy_str_lpad (output, format->w, s, ' ');
997     }
998   else
999     output_overflow (format, output);
1000
1001   output[format->w] = '\0';
1002 }
1003
1004 /* Formats OUTPUT as a missing value for the given FORMAT. */
1005 static void
1006 output_missing (const struct fmt_spec *format, char *output)
1007 {
1008   memset (output, ' ', format->w);
1009
1010   if (format->type != FMT_N)
1011     {
1012       int dot_ofs = (format->type == FMT_PCT ? 2
1013                      : format->type == FMT_E ? 5
1014                      : 1);
1015       output[MAX (0, format->w - format->d - dot_ofs)] = '.';
1016     }
1017   else
1018     output[format->w - 1] = '.';
1019
1020   output[format->w] = '\0';
1021 }
1022
1023 /* Formats OUTPUT for overflow given FORMAT. */
1024 static void
1025 output_overflow (const struct fmt_spec *format, char *output)
1026 {
1027   memset (output, '*', format->w);
1028   output[format->w] = '\0';
1029 }
1030
1031 /* Converts the integer part of NUMBER to a packed BCD number
1032    with the given number of DIGITS in OUTPUT.  If DIGITS is odd,
1033    the least significant nibble of the final byte in OUTPUT is
1034    set to 0.  Returns true if successful, false if NUMBER is not
1035    representable.  On failure, OUTPUT is cleared to all zero
1036    bytes. */
1037 static bool
1038 output_bcd_integer (double number, int digits, char *output)
1039 {
1040   char decimal[64];
1041
1042   assert (digits < sizeof decimal);
1043
1044   output[DIV_RND_UP (digits, 2)] = '\0';
1045   if (number != SYSMIS
1046       && number >= 0.
1047       && number < power10 (digits)
1048       && sprintf (decimal, "%0*.0f", digits, round (number)) == digits)
1049     {
1050       const char *src = decimal;
1051       int i;
1052
1053       for (i = 0; i < digits / 2; i++)
1054         {
1055           int d0 = *src++ - '0';
1056           int d1 = *src++ - '0';
1057           *output++ = (d0 << 4) + d1;
1058         }
1059       if (digits % 2)
1060         *output = (*src - '0') << 4;
1061
1062       return true;
1063     }
1064   else
1065     {
1066       memset (output, 0, DIV_RND_UP (digits, 2));
1067       return false;
1068     }
1069 }
1070
1071 /* Writes VALUE to OUTPUT as a BYTES-byte binary integer of the
1072    given INTEGER_FORMAT. */
1073 static void
1074 output_binary_integer (uint64_t value, int bytes,
1075                        enum integer_format integer_format, char *output)
1076 {
1077   integer_put (value, integer_format, output, bytes);
1078 }
1079
1080 /* Converts the BYTES bytes in DATA to twice as many hexadecimal
1081    digits in OUTPUT. */
1082 static void
1083 output_hex (const void *data_, size_t bytes, char *output)
1084 {
1085   const uint8_t *data = data_;
1086   size_t i;
1087
1088   for (i = 0; i < bytes; i++)
1089     {
1090       static const char hex_digits[] = "0123456789ABCDEF";
1091       *output++ = hex_digits[data[i] >> 4];
1092       *output++ = hex_digits[data[i] & 15];
1093     }
1094   *output = '\0';
1095 }