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