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