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/>. */
18 #define DATA_FORMAT_H 1
20 /* Display format types. */
23 #include "data/val-type.h"
24 #include "libpspp/str.h"
26 /* Format type categories.
28 Each format is in exactly one category. We give categories
29 bitwise disjoint values only to enable bitwise comparisons
30 against a mask of FMT_CAT_* values, not to allow multiple
31 categories per format. */
34 /* Numeric formats. */
35 FMT_CAT_BASIC = 0x001, /* Basic numeric formats. */
36 FMT_CAT_CUSTOM = 0x002, /* Custom currency formats. */
37 FMT_CAT_LEGACY = 0x004, /* Legacy numeric formats. */
38 FMT_CAT_BINARY = 0x008, /* Binary formats. */
39 FMT_CAT_HEXADECIMAL = 0x010, /* Hexadecimal formats. */
40 FMT_CAT_DATE = 0x020, /* Date formats. */
41 FMT_CAT_TIME = 0x040, /* Time formats. */
42 FMT_CAT_DATE_COMPONENT = 0x080, /* Date component formats. */
45 FMT_CAT_STRING = 0x100 /* String formats. */
51 #define FMT(NAME, METHOD, IMIN, OMIN, IO, CATEGORY) FMT_##NAME,
53 FMT_NUMBER_OF_FORMATS,
56 /* Length of longest format specifier name,
57 not including terminating null. */
58 #define FMT_TYPE_LEN_MAX 8
60 /* Length of longest string representation of fmt_spec,
61 not including terminating null. */
62 #define FMT_STRING_LEN_MAX 32
67 enum fmt_type type; /* One of FMT_*. */
69 int d; /* Number of implied decimal places. */
72 /* Maximum width of any numeric format. */
73 #define FMT_MAX_NUMERIC_WIDTH 40
75 /* Constructing formats. */
76 struct fmt_spec fmt_for_input (enum fmt_type, int w, int d) PURE_FUNCTION;
77 struct fmt_spec fmt_for_output (enum fmt_type, int w, int d) PURE_FUNCTION;
78 struct fmt_spec fmt_for_output_from_input (const struct fmt_spec *);
79 struct fmt_spec fmt_default_for_width (int width);
81 /* Verifying formats. */
82 bool fmt_check (const struct fmt_spec *, bool for_input);
83 bool fmt_check_input (const struct fmt_spec *);
84 bool fmt_check_output (const struct fmt_spec *);
85 bool fmt_check_type_compat (const struct fmt_spec *, enum val_type);
86 bool fmt_check_width_compat (const struct fmt_spec *, int var_width);
88 /* Working with formats. */
89 int fmt_var_width (const struct fmt_spec *);
90 char *fmt_to_string (const struct fmt_spec *, char s[FMT_STRING_LEN_MAX + 1]);
91 bool fmt_equal (const struct fmt_spec *, const struct fmt_spec *);
92 void fmt_resize (struct fmt_spec *, int new_width);
94 void fmt_fix (struct fmt_spec *, bool for_input);
95 void fmt_fix_input (struct fmt_spec *);
96 void fmt_fix_output (struct fmt_spec *);
99 bool is_fmt_type (enum fmt_type);
101 const char *fmt_name (enum fmt_type) PURE_FUNCTION;
102 bool fmt_from_name (const char *name, enum fmt_type *);
104 bool fmt_takes_decimals (enum fmt_type) PURE_FUNCTION;
106 int fmt_min_width (enum fmt_type, bool for_input) PURE_FUNCTION;
107 int fmt_max_width (enum fmt_type, bool for_input) PURE_FUNCTION;
108 int fmt_max_decimals (enum fmt_type, int width, bool for_input) PURE_FUNCTION;
109 int fmt_min_input_width (enum fmt_type) PURE_FUNCTION;
110 int fmt_max_input_width (enum fmt_type) PURE_FUNCTION;
111 int fmt_max_input_decimals (enum fmt_type, int width) PURE_FUNCTION;
112 int fmt_min_output_width (enum fmt_type) PURE_FUNCTION;
113 int fmt_max_output_width (enum fmt_type) PURE_FUNCTION;
114 int fmt_max_output_decimals (enum fmt_type, int width) PURE_FUNCTION;
115 int fmt_step_width (enum fmt_type) PURE_FUNCTION;
117 bool fmt_is_string (enum fmt_type) PURE_FUNCTION;
118 bool fmt_is_numeric (enum fmt_type) PURE_FUNCTION;
119 enum fmt_category fmt_get_category (enum fmt_type) PURE_FUNCTION;
121 enum fmt_type fmt_input_to_output (enum fmt_type) PURE_FUNCTION;
122 bool fmt_usable_for_input (enum fmt_type) PURE_FUNCTION;
124 int fmt_to_io (enum fmt_type) PURE_FUNCTION;
125 bool fmt_from_io (int io, enum fmt_type *);
127 const char *fmt_date_template (enum fmt_type) PURE_FUNCTION;
128 const char *fmt_gui_name (enum fmt_type);
132 A fmt_settings is really just a collection of one "struct fmt_number_style"
133 for each format type. */
134 struct fmt_settings *fmt_settings_create (void);
135 void fmt_settings_destroy (struct fmt_settings *);
136 struct fmt_settings *fmt_settings_clone (const struct fmt_settings *);
138 void fmt_settings_set_decimal (struct fmt_settings *, char);
140 const struct fmt_number_style *fmt_settings_get_style (
141 const struct fmt_settings *, enum fmt_type);
142 void fmt_settings_set_style (struct fmt_settings *, enum fmt_type,
143 char decimal, char grouping,
144 const char *neg_prefix, const char *prefix,
145 const char *suffix, const char *neg_suffix);
147 /* A prefix or suffix for a numeric output format. */
150 char *s; /* String contents of affix, in UTF-8. */
151 int width; /* Display width in columns (see wcwidth()). */
154 /* A numeric output style. */
155 struct fmt_number_style
157 struct fmt_affix neg_prefix; /* Negative prefix. */
158 struct fmt_affix prefix; /* Prefix. */
159 struct fmt_affix suffix; /* Suffix. */
160 struct fmt_affix neg_suffix; /* Negative suffix. */
161 char decimal; /* Decimal point: '.' or ','. */
162 char grouping; /* Grouping character: ',', '.', or 0. */
164 /* A fmt_affix may require more bytes than its display width; for example,
165 U+00A5 (¥) is 3 bytes in UTF-8 but occupies only one display column.
166 This member is the sum of the number of bytes required by all of the
167 fmt_affix members in this struct, minus their display widths. Thus, it
168 can be used to size memory allocations: for example, the formatted
169 result of CCA20.5 requires no more than (20 + extra_bytes) bytes in
174 int fmt_affix_width (const struct fmt_number_style *);
175 int fmt_neg_affix_width (const struct fmt_number_style *);
177 extern const struct fmt_spec F_8_0 ;
179 #endif /* data/format.h */