1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2010, 2011 Free Software Foundation, Inc.
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.
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.
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/>. */
25 #include "data/identifier.h"
26 #include "data/settings.h"
27 #include "data/value.h"
28 #include "data/variable.h"
29 #include "libpspp/assertion.h"
30 #include "libpspp/cast.h"
31 #include "libpspp/compiler.h"
32 #include "libpspp/message.h"
33 #include "libpspp/misc.h"
34 #include "libpspp/str.h"
36 #include "gl/minmax.h"
37 #include "gl/xalloc.h"
40 #define _(msgid) gettext (msgid)
44 struct fmt_number_style styles[FMT_NUMBER_OF_FORMATS];
47 bool is_fmt_type (enum fmt_type);
49 static bool valid_width (enum fmt_type, int width, bool for_input);
51 static int max_digits_for_bytes (int bytes);
52 static void fmt_clamp_width (struct fmt_spec *, bool for_input);
53 static void fmt_clamp_decimals (struct fmt_spec *, bool for_input);
55 static void fmt_affix_set (struct fmt_affix *, const char *);
56 static void fmt_affix_free (struct fmt_affix *);
58 static void fmt_number_style_init (struct fmt_number_style *);
59 static void fmt_number_style_clone (struct fmt_number_style *,
60 const struct fmt_number_style *);
61 static void fmt_number_style_destroy (struct fmt_number_style *);
63 /* Creates and returns a new struct fmt_settings with default format styles. */
65 fmt_settings_create (void)
67 struct fmt_settings *settings;
70 settings = xzalloc (sizeof *settings);
71 for (t = 0 ; t < FMT_NUMBER_OF_FORMATS ; ++t )
72 fmt_number_style_init (&settings->styles[t]);
73 fmt_settings_set_decimal (settings, '.');
78 /* Destroys SETTINGS. */
80 fmt_settings_destroy (struct fmt_settings *settings)
86 for (t = 0 ; t < FMT_NUMBER_OF_FORMATS ; ++t )
87 fmt_number_style_destroy (&settings->styles[t]);
89 free (settings->styles);
93 /* Returns a copy of SETTINGS. */
95 fmt_settings_clone (const struct fmt_settings *old)
97 struct fmt_settings *new;
100 new = xmalloc (sizeof *new);
101 for (t = 0 ; t < FMT_NUMBER_OF_FORMATS ; ++t )
102 fmt_number_style_clone (&new->styles[t], &old->styles[t]);
107 /* Returns the number formatting style associated with the given
109 const struct fmt_number_style *
110 fmt_settings_get_style (const struct fmt_settings *settings,
113 assert (is_fmt_type (type));
114 return &settings->styles[type];
117 /* Sets the number style for TYPE to have the given DECIMAL and GROUPING
118 characters, negative prefix NEG_PREFIX, prefix PREFIX, suffix SUFFIX, and
119 negative suffix NEG_SUFFIX. All of the strings are UTF-8 encoded. */
121 fmt_settings_set_style (struct fmt_settings *settings, enum fmt_type type,
122 char decimal, char grouping,
123 const char *neg_prefix, const char *prefix,
124 const char *suffix, const char *neg_suffix)
126 struct fmt_number_style *style = &settings->styles[type];
127 int total_bytes, total_width;
129 assert (grouping == '.' || grouping == ',' || grouping == 0);
130 assert (decimal == '.' || decimal == ',');
131 assert (decimal != grouping);
133 fmt_number_style_destroy (style);
135 fmt_affix_set (&style->neg_prefix, neg_prefix);
136 fmt_affix_set (&style->prefix, prefix);
137 fmt_affix_set (&style->suffix, suffix);
138 fmt_affix_set (&style->neg_suffix, neg_suffix);
139 style->decimal = decimal;
140 style->grouping = grouping;
142 total_bytes = (strlen (neg_prefix) + strlen (prefix)
143 + strlen (suffix) + strlen (neg_suffix));
144 total_width = (style->neg_prefix.width + style->prefix.width
145 + style->suffix.width + style->neg_suffix.width);
146 style->extra_bytes = MAX (0, total_bytes - total_width);
149 /* Sets the decimal point character for the settings in S to DECIMAL.
151 This has no effect on custom currency formats. */
153 fmt_settings_set_decimal (struct fmt_settings *s, char decimal)
155 int grouping = decimal == '.' ? ',' : '.';
156 assert (decimal == '.' || decimal == ',');
158 fmt_settings_set_style (s, FMT_F, decimal, 0, "-", "", "", "");
159 fmt_settings_set_style (s, FMT_E, decimal, 0, "-", "", "", "");
160 fmt_settings_set_style (s, FMT_COMMA, decimal, grouping, "-", "", "", "");
161 fmt_settings_set_style (s, FMT_DOT, grouping, decimal, "-", "", "", "");
162 fmt_settings_set_style (s, FMT_DOLLAR, decimal, grouping, "-", "$", "", "");
163 fmt_settings_set_style (s, FMT_PCT, decimal, 0, "-", "", "%", "");
166 /* Returns an input format specification with type TYPE, width W,
169 fmt_for_input (enum fmt_type type, int w, int d)
175 assert (fmt_check_input (&f));
179 /* Returns an output format specification with type TYPE, width
180 W, and D decimals. */
182 fmt_for_output (enum fmt_type type, int w, int d)
188 assert (fmt_check_output (&f));
192 /* Returns the output format specifier corresponding to input
193 format specifier INPUT. */
195 fmt_for_output_from_input (const struct fmt_spec *input)
197 struct fmt_spec output;
199 assert (fmt_check_input (input));
201 output.type = fmt_input_to_output (input->type);
203 if (output.w > fmt_max_output_width (output.type))
204 output.w = fmt_max_output_width (output.type);
205 else if (output.w < fmt_min_output_width (output.type))
206 output.w = fmt_min_output_width (output.type);
223 const struct fmt_number_style *style =
224 settings_get_style (input->type);
226 output.w += fmt_affix_width (style);
227 if (style->grouping != 0 && input->w - input->d >= 3)
228 output.w += (input->w - input->d - 1) / 3;
240 output.d = MAX (input->d, 3);
241 output.w = MAX (input->w, output.d + 7);
245 output.w = max_digits_for_bytes (input->w / 2) + 1;
256 output.w = 2 * input->w + (input->d > 0);
261 output.w = max_digits_for_bytes (input->w) + 1;
277 output.w = input->w / 2;
299 if (output.w > fmt_max_output_width (output.type))
300 output.w = fmt_max_output_width (output.type);
302 assert (fmt_check_output (&output));
306 /* Returns the default format for the given WIDTH: F8.2 format
307 for a numeric value, A format for a string value. */
309 fmt_default_for_width (int width)
312 ? fmt_for_output (FMT_F, 8, 2)
313 : fmt_for_output (FMT_A, width, 0));
316 /* Checks whether SPEC is valid as an input format (if FOR_INPUT)
317 or an output format (otherwise) and returns nonzero if so.
318 Otherwise, emits an error message and returns zero. */
320 fmt_check (const struct fmt_spec *spec, bool for_input)
322 const char *io_fmt = for_input ? _("Input format") : _("Output format");
323 char str[FMT_STRING_LEN_MAX + 1];
324 int min_w, max_w, max_d;
326 assert (is_fmt_type (spec->type));
327 fmt_to_string (spec, str);
329 if (for_input && !fmt_usable_for_input (spec->type))
331 msg (SE, _("Format %s may not be used for input."), str);
335 if (spec->w % fmt_step_width (spec->type))
337 assert (fmt_step_width (spec->type) == 2);
338 msg (SE, _("%s specifies width %d, but %s requires an even width."),
339 str, spec->w, fmt_name (spec->type));
343 min_w = fmt_min_width (spec->type, for_input);
344 max_w = fmt_max_width (spec->type, for_input);
345 if (spec->w < min_w || spec->w > max_w)
347 msg (SE, _("%s %s specifies width %d, but "
348 "%s requires a width between %d and %d."),
349 io_fmt, str, spec->w, fmt_name (spec->type), min_w, max_w);
353 max_d = fmt_max_decimals (spec->type, spec->w, for_input);
354 if (!fmt_takes_decimals (spec->type) && spec->d != 0)
356 msg (SE, ngettext ("%s %s specifies %d decimal place, but "
357 "%s does not allow any decimals.",
358 "%s %s specifies %d decimal places, but "
359 "%s does not allow any decimals.",
361 io_fmt, str, spec->d, fmt_name (spec->type));
364 else if (spec->d > max_d)
367 msg (SE, ngettext ("%s %s specifies %d decimal place, but "
368 "the given width allows at most %d decimals.",
369 "%s %s specifies %d decimal places, but "
370 "the given width allows at most %d decimals.",
372 io_fmt, str, spec->d, max_d);
374 msg (SE, ngettext ("%s %s specifies %d decimal place, but "
375 "the given width does not allow for any decimals.",
376 "%s %s specifies %d decimal places, but "
377 "the given width does not allow for any decimals.",
379 io_fmt, str, spec->d);
386 /* Checks whether SPEC is valid as an input format and returns
387 nonzero if so. Otherwise, emits an error message and returns
390 fmt_check_input (const struct fmt_spec *spec)
392 return fmt_check (spec, true);
395 /* Checks whether SPEC is valid as an output format and returns
396 true if so. Otherwise, emits an error message and returns false. */
398 fmt_check_output (const struct fmt_spec *spec)
400 return fmt_check (spec, false);
403 /* Checks that FORMAT is appropriate for a variable of the given
404 VAR_TYPE and returns true if so. Otherwise returns false and
405 emits an error message. */
407 fmt_check_type_compat (const struct fmt_spec *format, enum val_type var_type)
409 assert (val_type_is_valid (var_type));
410 if ((var_type == VAL_STRING) != (fmt_is_string (format->type) != 0))
412 char str[FMT_STRING_LEN_MAX + 1];
413 msg (SE, _("%s variables are not compatible with %s format %s."),
414 var_type == VAL_STRING ? _("String") : _("Numeric"),
415 var_type == VAL_STRING ? _("numeric") : _("string"),
416 fmt_to_string (format, str));
422 /* Checks that FORMAT is appropriate for a variable of the given
423 WIDTH and returns true if so. Otherwise returns false and
424 emits an error message. */
426 fmt_check_width_compat (const struct fmt_spec *format, int width)
428 if (!fmt_check_type_compat (format, val_type_from_width (width)))
430 if (fmt_var_width (format) != width)
432 char str[FMT_STRING_LEN_MAX + 1];
433 msg (SE, _("String variable with width %d is not compatible with "
435 width, fmt_to_string (format, str));
441 /* Returns the width corresponding to FORMAT. The return value
442 is the width of the `union value's required by FORMAT. */
444 fmt_var_width (const struct fmt_spec *format)
446 return (format->type == FMT_AHEX ? format->w / 2
447 : format->type == FMT_A ? format->w
451 /* Converts F to its string representation (for instance, "F8.2")
452 in BUFFER. Returns BUFFER.
454 If F has decimals, they are included in the output string,
455 even if F's format type does not allow decimals, to allow
456 accurately presenting incorrect formats to the user. */
458 fmt_to_string (const struct fmt_spec *f, char buffer[FMT_STRING_LEN_MAX + 1])
460 if (fmt_takes_decimals (f->type) || f->d > 0)
461 snprintf (buffer, FMT_STRING_LEN_MAX + 1,
462 "%s%d.%d", fmt_name (f->type), f->w, f->d);
464 snprintf (buffer, FMT_STRING_LEN_MAX + 1,
465 "%s%d", fmt_name (f->type), f->w);
469 /* Returns true if A and B are identical formats,
472 fmt_equal (const struct fmt_spec *a, const struct fmt_spec *b)
474 return a->type == b->type && a->w == b->w && a->d == b->d;
477 /* Adjusts FMT to be valid for a value of the given WIDTH. */
479 fmt_resize (struct fmt_spec *fmt, int width)
481 if ((width > 0) != fmt_is_string (fmt->type))
483 /* Changed from numeric to string or vice versa. Set to
484 default format for new width. */
485 *fmt = fmt_default_for_width (width);
489 /* Changed width of string. Preserve format type, adjust
491 fmt->w = fmt->type == FMT_AHEX ? width * 2 : width;
499 /* Adjusts FMT's width and decimal places to be valid for an
500 input format (if FOR_INPUT) or an output format (if
503 fmt_fix (struct fmt_spec *fmt, bool for_input)
505 /* Clamp width to those allowed by format. */
506 fmt_clamp_width (fmt, for_input);
508 /* If FMT has more decimal places than allowed, attempt to increase FMT's
509 width until that number of decimal places can be achieved. */
510 if (fmt->d > fmt_max_decimals (fmt->type, fmt->w, for_input)
511 && fmt_takes_decimals (fmt->type))
513 int max_w = fmt_max_width (fmt->type, for_input);
514 for (; fmt->w < max_w; fmt->w++)
515 if (fmt->d <= fmt_max_decimals (fmt->type, fmt->w, for_input))
519 /* Clamp decimals to those allowed by format and width. */
520 fmt_clamp_decimals (fmt, for_input);
523 /* Adjusts FMT's width and decimal places to be valid for an
526 fmt_fix_input (struct fmt_spec *fmt)
531 /* Adjusts FMT's width and decimal places to be valid for an
534 fmt_fix_output (struct fmt_spec *fmt)
536 fmt_fix (fmt, false);
539 /* Sets FMT's width to WIDTH (or the nearest width allowed by FMT's type) and
540 reduces its decimal places as necessary (if necessary) for that width. */
542 fmt_change_width (struct fmt_spec *fmt, int width, bool for_input)
545 fmt_clamp_width (fmt, for_input);
546 fmt_clamp_decimals (fmt, for_input);
549 /* Sets FMT's decimal places to DECIMALS (or the nearest number of decimal
550 places allowed by FMT's type) and increases its width as necessary (if
551 necessary) for that number of decimal places. */
553 fmt_change_decimals (struct fmt_spec *fmt, int decimals, bool for_input)
556 fmt_fix (fmt, for_input);
559 /* Describes a display format. */
563 int min_input_width, min_output_width;
565 enum fmt_category category;
568 static const struct fmt_desc *get_fmt_desc (enum fmt_type type);
570 /* Returns the name of the given format TYPE. */
572 fmt_name (enum fmt_type type)
574 return get_fmt_desc (type)->name;
577 /* Tries to parse NAME as a format type.
578 If successful, stores the type in *TYPE and returns true.
579 On failure, returns false. */
581 fmt_from_name (const char *name, enum fmt_type *type)
585 for (i = 0; i < FMT_NUMBER_OF_FORMATS; i++)
586 if (!strcasecmp (name, get_fmt_desc (i)->name))
594 /* Returns true if TYPE accepts decimal places,
597 fmt_takes_decimals (enum fmt_type type)
599 return fmt_max_output_decimals (type, fmt_max_output_width (type)) > 0;
602 /* Returns the minimum width of the given format TYPE,
603 for input if FOR_INPUT is true,
604 for output otherwise. */
606 fmt_min_width (enum fmt_type type, bool for_input)
608 return for_input ? fmt_min_input_width (type) : fmt_min_output_width (type);
611 /* Returns the maximum width of the given format TYPE,
612 for input if FOR_INPUT is true,
613 for output otherwise. */
615 fmt_max_width (enum fmt_type type, bool for_input UNUSED)
617 /* Maximum width is actually invariant of whether the format is
618 for input or output, so FOR_INPUT is unused. */
619 assert (is_fmt_type (type));
637 return 2 * MAX_STRING;
644 /* Returns the maximum number of decimal places allowed for the
645 given format TYPE with a width of WIDTH places,
646 for input if FOR_INPUT is true,
647 for output otherwise. */
649 fmt_max_decimals (enum fmt_type type, int width, bool for_input)
658 max_d = for_input ? width : width - 1;
663 max_d = for_input ? width : width - 2;
667 max_d = for_input ? width : width - 7;
685 max_d = width * 2 - 1;
694 max_d = max_digits_for_bytes (width);
747 /* Returns the minimum acceptable width for an input field
748 formatted with the given TYPE. */
750 fmt_min_input_width (enum fmt_type type)
752 return get_fmt_desc (type)->min_input_width;
755 /* Returns the maximum acceptable width for an input field
756 formatted with the given TYPE. */
758 fmt_max_input_width (enum fmt_type type)
760 return fmt_max_width (type, true);
763 /* Returns the maximum number of decimal places allowed in an
764 input field of the given TYPE and WIDTH. */
766 fmt_max_input_decimals (enum fmt_type type, int width)
768 assert (valid_width (type, width, true));
769 return fmt_max_decimals (type, width, true);
772 /* Returns the minimum acceptable width for an output field
773 formatted with the given TYPE. */
775 fmt_min_output_width (enum fmt_type type)
777 return get_fmt_desc (type)->min_output_width;
780 /* Returns the maximum acceptable width for an output field
781 formatted with the given TYPE. */
783 fmt_max_output_width (enum fmt_type type)
785 return fmt_max_width (type, false);
788 /* Returns the maximum number of decimal places allowed in an
789 output field of the given TYPE and WIDTH. */
791 fmt_max_output_decimals (enum fmt_type type, int width)
793 assert (valid_width (type, width, false));
794 return fmt_max_decimals (type, width, false);
797 /* Returns the width step for a field formatted with the given
798 TYPE. Field width must be a multiple of the width step. */
800 fmt_step_width (enum fmt_type type)
802 return (fmt_get_category (type) == FMT_CAT_HEXADECIMAL || type == FMT_AHEX
806 /* Returns true if TYPE is used for string fields,
807 false if it is used for numeric fields. */
809 fmt_is_string (enum fmt_type type)
811 return fmt_get_category (type) == FMT_CAT_STRING;
814 /* Returns true if TYPE is used for numeric fields,
815 false if it is used for string fields. */
817 fmt_is_numeric (enum fmt_type type)
819 return !fmt_is_string (type);
822 /* Returns the format TYPE's category.
823 Each format type is in exactly one category,
824 and each category's value is bitwise disjoint from every other
825 category. Thus, the return value may be tested for equality
826 or compared bitwise against a mask of FMT_CAT_* values. */
828 fmt_get_category (enum fmt_type type)
830 return get_fmt_desc (type)->category;
833 /* Returns the output format selected by default when TYPE is
834 used as an input format. */
836 fmt_input_to_output (enum fmt_type type)
838 switch (fmt_get_category (type))
845 case FMT_CAT_HEXADECIMAL:
853 /* Returns the SPSS format type corresponding to the given PSPP
856 fmt_to_io (enum fmt_type type)
858 return get_fmt_desc (type)->io;
861 /* Determines the PSPP format corresponding to the given SPSS
862 format type. If successful, sets *FMT_TYPE to the PSPP format
863 and returns true. On failure, return false. */
865 fmt_from_io (int io, enum fmt_type *fmt_type)
869 #define FMT(NAME, METHOD, IMIN, OMIN, IO, CATEGORY) \
871 *fmt_type = FMT_##NAME; \
873 #include "format.def"
879 /* Returns true if TYPE may be used as an input format,
882 fmt_usable_for_input (enum fmt_type type)
884 assert (is_fmt_type (type));
885 return fmt_get_category (type) != FMT_CAT_CUSTOM;
888 /* For time and date formats, returns a template used for input
891 fmt_date_template (enum fmt_type type)
912 return "dd-mmm-yyyy HH:MM";
922 /* Returns a string representing the format TYPE for use in a GUI dialog. */
924 fmt_gui_name (enum fmt_type type)
938 return _("Scientific");
969 return fmt_name (type);
973 /* Returns true if TYPE is a valid format type,
976 is_fmt_type (enum fmt_type type)
978 return type < FMT_NUMBER_OF_FORMATS;
981 /* Returns true if WIDTH is a valid width for the given format
983 for input if FOR_INPUT is true,
984 for output otherwise. */
986 valid_width (enum fmt_type type, int width, bool for_input)
988 return (width >= fmt_min_width (type, for_input)
989 && width <= fmt_max_width (type, for_input));
992 /* Returns the maximum number of decimal digits in an unsigned
993 binary number that is BYTES bytes long. */
995 max_digits_for_bytes (int bytes)
997 int map[8] = {3, 5, 8, 10, 13, 15, 17, 20};
998 assert (bytes > 0 && bytes <= sizeof map / sizeof *map);
999 return map[bytes - 1];
1002 /* Clamp FMT's width to the range and values allowed by FMT's type. */
1004 fmt_clamp_width (struct fmt_spec *fmt, bool for_input)
1009 min_w = fmt_min_width (fmt->type, for_input);
1010 max_w = fmt_max_width (fmt->type, for_input);
1013 else if (fmt->w > max_w)
1016 /* Round width to step. */
1017 step = fmt_step_width (fmt->type);
1018 fmt->w = ROUND_DOWN (fmt->w, step);
1021 /* Clamp FMT's decimal places to the range allowed by FMT's type and width. */
1023 fmt_clamp_decimals (struct fmt_spec *fmt, bool for_input)
1027 max_d = fmt_max_decimals (fmt->type, fmt->w, for_input);
1030 else if (fmt->d > max_d)
1034 /* Sets AFFIX's string value to S, a UTF-8 encoded string. */
1036 fmt_affix_set (struct fmt_affix *affix, const char *s)
1038 affix->s = s[0] == '\0' ? CONST_CAST (char *, "") : xstrdup (s);
1039 affix->width = u8_strwidth (CHAR_CAST (const uint8_t *, s), "UTF-8");
1042 /* Frees data in AFFIX. */
1044 fmt_affix_free (struct fmt_affix *affix)
1051 fmt_number_style_init (struct fmt_number_style *style)
1053 fmt_affix_set (&style->neg_prefix, "");
1054 fmt_affix_set (&style->prefix, "");
1055 fmt_affix_set (&style->suffix, "");
1056 fmt_affix_set (&style->neg_suffix, "");
1057 style->decimal = '.';
1058 style->grouping = 0;
1062 fmt_number_style_clone (struct fmt_number_style *new,
1063 const struct fmt_number_style *old)
1065 fmt_affix_set (&new->neg_prefix, old->neg_prefix.s);
1066 fmt_affix_set (&new->prefix, old->prefix.s);
1067 fmt_affix_set (&new->suffix, old->suffix.s);
1068 fmt_affix_set (&new->neg_suffix, old->neg_suffix.s);
1069 new->decimal = old->decimal;
1070 new->grouping = old->grouping;
1071 new->extra_bytes = old->extra_bytes;
1074 /* Destroys a struct fmt_number_style. */
1076 fmt_number_style_destroy (struct fmt_number_style *style)
1080 fmt_affix_free (&style->neg_prefix);
1081 fmt_affix_free (&style->prefix);
1082 fmt_affix_free (&style->suffix);
1083 fmt_affix_free (&style->neg_suffix);
1087 /* Returns the total width of the standard prefix and suffix for STYLE, in
1088 display columns (e.g. as returned by u8_strwidth()). */
1090 fmt_affix_width (const struct fmt_number_style *style)
1092 return style->prefix.width + style->suffix.width;
1095 /* Returns the total width of the negative prefix and suffix for STYLE, in
1096 display columns (e.g. as returned by u8_strwidth()). */
1098 fmt_neg_affix_width (const struct fmt_number_style *style)
1100 return style->neg_prefix.width + style->neg_suffix.width;
1103 /* Returns the struct fmt_desc for the given format TYPE. */
1104 static const struct fmt_desc *
1105 get_fmt_desc (enum fmt_type type)
1107 static const struct fmt_desc formats[FMT_NUMBER_OF_FORMATS] =
1109 #define FMT(NAME, METHOD, IMIN, OMIN, IO, CATEGORY) \
1110 {#NAME, IMIN, OMIN, IO, CATEGORY},
1111 #include "format.def"
1114 assert (is_fmt_type (type));
1115 return &formats[type];
1118 const struct fmt_spec F_8_0 = {FMT_F, 8, 0};