1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2010, 2011, 2012 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. */
24 #include "data/val-type.h"
25 #include "libpspp/str.h"
27 /* How a format is going to be used. */
30 FMT_FOR_INPUT, /* For parsing data input, e.g. data_in(). */
31 FMT_FOR_OUTPUT /* For formatting data output, e.g. data_out(). */
34 /* Format type categories.
36 Each format is in exactly one category. We give categories
37 bitwise disjoint values only to enable bitwise comparisons
38 against a mask of FMT_CAT_* values, not to allow multiple
39 categories per format. */
42 /* Numeric formats. */
43 FMT_CAT_BASIC = 0x001, /* Basic numeric formats. */
44 FMT_CAT_CUSTOM = 0x002, /* Custom currency formats. */
45 FMT_CAT_LEGACY = 0x004, /* Legacy numeric formats. */
46 FMT_CAT_BINARY = 0x008, /* Binary formats. */
47 FMT_CAT_HEXADECIMAL = 0x010, /* Hexadecimal formats. */
48 FMT_CAT_DATE = 0x020, /* Date formats. */
49 FMT_CAT_TIME = 0x040, /* Time formats. */
50 FMT_CAT_DATE_COMPONENT = 0x080, /* Date component formats. */
53 FMT_CAT_STRING = 0x100 /* String formats. */
59 #define FMT(NAME, METHOD, IMIN, OMIN, IO, CATEGORY) FMT_##NAME,
61 FMT_NUMBER_OF_FORMATS,
64 /* Length of longest format specifier name,
65 not including terminating null. */
66 #define FMT_TYPE_LEN_MAX 8
68 /* Length of longest string representation of fmt_spec,
69 not including terminating null. */
70 #define FMT_STRING_LEN_MAX 32
75 enum fmt_type type; /* One of FMT_*. */
77 int d; /* Number of decimal places. */
80 /* Maximum width of any numeric format. */
81 #define FMT_MAX_NUMERIC_WIDTH 40
83 /* Constructing formats. */
84 struct fmt_spec fmt_for_input (enum fmt_type, int w, int d) PURE_FUNCTION;
85 struct fmt_spec fmt_for_output (enum fmt_type, int w, int d) PURE_FUNCTION;
86 struct fmt_spec fmt_for_output_from_input (const struct fmt_spec *);
87 struct fmt_spec fmt_default_for_width (int width);
89 /* Verifying formats. */
90 bool fmt_check (const struct fmt_spec *, enum fmt_use);
91 bool fmt_check_input (const struct fmt_spec *);
92 bool fmt_check_output (const struct fmt_spec *);
93 bool fmt_check_type_compat (const struct fmt_spec *, enum val_type);
94 bool fmt_check_width_compat (const struct fmt_spec *, int var_width);
96 /* Working with formats. */
97 int fmt_var_width (const struct fmt_spec *);
98 char *fmt_to_string (const struct fmt_spec *, char s[FMT_STRING_LEN_MAX + 1]);
99 bool fmt_equal (const struct fmt_spec *, const struct fmt_spec *);
100 bool fmt_resize (struct fmt_spec *, int new_width);
102 void fmt_fix (struct fmt_spec *, enum fmt_use);
103 void fmt_fix_input (struct fmt_spec *);
104 void fmt_fix_output (struct fmt_spec *);
106 void fmt_change_width (struct fmt_spec *, int width, enum fmt_use);
107 void fmt_change_decimals (struct fmt_spec *, int decimals, enum fmt_use);
110 bool is_fmt_type (enum fmt_type);
112 const char *fmt_name (enum fmt_type) PURE_FUNCTION;
113 bool fmt_from_name (const char *name, enum fmt_type *);
115 bool fmt_takes_decimals (enum fmt_type) PURE_FUNCTION;
117 int fmt_min_width (enum fmt_type, enum fmt_use) PURE_FUNCTION;
118 int fmt_max_width (enum fmt_type, enum fmt_use) PURE_FUNCTION;
119 int fmt_max_decimals (enum fmt_type, int width, enum fmt_use) PURE_FUNCTION;
120 int fmt_min_input_width (enum fmt_type) PURE_FUNCTION;
121 int fmt_max_input_width (enum fmt_type) PURE_FUNCTION;
122 int fmt_max_input_decimals (enum fmt_type, int width) PURE_FUNCTION;
123 int fmt_min_output_width (enum fmt_type) PURE_FUNCTION;
124 int fmt_max_output_width (enum fmt_type) PURE_FUNCTION;
125 int fmt_max_output_decimals (enum fmt_type, int width) PURE_FUNCTION;
126 int fmt_step_width (enum fmt_type) PURE_FUNCTION;
128 bool fmt_is_string (enum fmt_type) PURE_FUNCTION;
129 bool fmt_is_numeric (enum fmt_type) PURE_FUNCTION;
130 enum fmt_category fmt_get_category (enum fmt_type) PURE_FUNCTION;
132 enum fmt_type fmt_input_to_output (enum fmt_type) PURE_FUNCTION;
133 bool fmt_usable_for_input (enum fmt_type) PURE_FUNCTION;
135 int fmt_to_io (enum fmt_type) PURE_FUNCTION;
136 bool fmt_from_io (int io, enum fmt_type *);
137 bool fmt_from_u32 (uint32_t, int var_width, bool loose, struct fmt_spec *);
139 const char *fmt_date_template (enum fmt_type, int width) PURE_FUNCTION;
140 const char *fmt_gui_name (enum fmt_type);
142 /* A prefix or suffix for a numeric output format. */
145 char *s; /* String contents of affix, in UTF-8. */
146 int width; /* Display width in columns (see wcwidth()). */
149 /* A numeric output style. This can express the basic numeric formats (in the
150 FMT_CAT_BASIC category) and custom currency formats (FMT_CCx). */
151 struct fmt_number_style
153 struct fmt_affix neg_prefix; /* Negative prefix. */
154 struct fmt_affix prefix; /* Prefix. */
155 struct fmt_affix suffix; /* Suffix. */
156 struct fmt_affix neg_suffix; /* Negative suffix. */
157 char decimal; /* Decimal point: '.' or ','. */
158 char grouping; /* Grouping character: ',', '.', or 0. */
160 /* A fmt_affix may require more bytes than its display width; for example,
161 U+00A5 (¥) is 2 bytes in UTF-8 but occupies only one display column.
162 This member is the sum of the number of bytes required by all of the
163 fmt_affix members in this struct, minus their display widths. Thus, it
164 can be used to size memory allocations: for example, the formatted
165 result of CCA20.5 requires no more than (20 + extra_bytes) bytes in
170 struct fmt_number_style *fmt_number_style_from_string (const char *);
171 struct fmt_number_style *fmt_number_style_clone (
172 const struct fmt_number_style *);
173 void fmt_number_style_destroy (struct fmt_number_style *);
175 int fmt_affix_width (const struct fmt_number_style *);
176 int fmt_neg_affix_width (const struct fmt_number_style *);
178 /* Number of custom currency styles. */
179 #define FMT_N_CCS 5 /* FMT_CCA through FMT_CCE. */
183 char decimal; /* '.' or ','. */
184 struct fmt_number_style *ccs[FMT_N_CCS]; /* CCA through CCE. */
186 #define FMT_SETTINGS_INIT { .decimal = '.' }
188 void fmt_settings_init (struct fmt_settings *);
189 void fmt_settings_uninit (struct fmt_settings *);
190 void fmt_settings_copy (struct fmt_settings *, const struct fmt_settings *);
192 const struct fmt_number_style *fmt_settings_get_style (
193 const struct fmt_settings *, enum fmt_type);
195 void fmt_settings_set_cc (struct fmt_settings *, enum fmt_type,
196 struct fmt_number_style *);
198 extern const struct fmt_spec F_8_0 ;
199 extern const struct fmt_spec F_8_2 ;
200 extern const struct fmt_spec F_4_3 ;
201 extern const struct fmt_spec F_5_1 ;
203 #endif /* data/format.h */