Added result_class parameter to tab_double and updated all callers. Removed tab_fixed
[pspp] / src / data / format.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2010, 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 #ifndef DATA_FORMAT_H
18 #define DATA_FORMAT_H 1
19
20 /* Display format types. */
21
22 #include <stdbool.h>
23 #include "data/val-type.h"
24 #include "libpspp/str.h"
25
26 /* How a format is going to be used. */
27 enum fmt_use
28   {
29     FMT_FOR_INPUT,           /* For parsing data input, e.g. data_in(). */
30     FMT_FOR_OUTPUT           /* For formatting data output, e.g. data_out(). */
31   };
32
33 /* Format type categories.
34
35    Each format is in exactly one category.  We give categories
36    bitwise disjoint values only to enable bitwise comparisons
37    against a mask of FMT_CAT_* values, not to allow multiple
38    categories per format. */
39 enum fmt_category
40   {
41     /* Numeric formats. */
42     FMT_CAT_BASIC          = 0x001,     /* Basic numeric formats. */
43     FMT_CAT_CUSTOM         = 0x002,     /* Custom currency formats. */
44     FMT_CAT_LEGACY         = 0x004,     /* Legacy numeric formats. */
45     FMT_CAT_BINARY         = 0x008,     /* Binary formats. */
46     FMT_CAT_HEXADECIMAL    = 0x010,     /* Hexadecimal formats. */
47     FMT_CAT_DATE           = 0x020,     /* Date formats. */
48     FMT_CAT_TIME           = 0x040,     /* Time formats. */
49     FMT_CAT_DATE_COMPONENT = 0x080,     /* Date component formats. */
50
51     /* String formats. */
52     FMT_CAT_STRING         = 0x100      /* String formats. */
53   };
54
55 /* Format type. */
56 enum fmt_type
57   {
58 #define FMT(NAME, METHOD, IMIN, OMIN, IO, CATEGORY) FMT_##NAME,
59 #include "format.def"
60     FMT_NUMBER_OF_FORMATS,
61   };
62
63 /* Length of longest format specifier name,
64    not including terminating null. */
65 #define FMT_TYPE_LEN_MAX 8
66
67 /* Length of longest string representation of fmt_spec,
68    not including terminating null. */
69 #define FMT_STRING_LEN_MAX 32
70
71 /* Display format. */
72 struct fmt_spec
73   {
74     enum fmt_type type;         /* One of FMT_*. */
75     int w;                      /* Width. */
76     int d;                      /* Number of decimal places. */
77   };
78
79 /* Maximum width of any numeric format. */
80 #define FMT_MAX_NUMERIC_WIDTH 40
81
82 /* Constructing formats. */
83 struct fmt_spec fmt_for_input (enum fmt_type, int w, int d) PURE_FUNCTION;
84 struct fmt_spec fmt_for_output (enum fmt_type, int w, int d) PURE_FUNCTION;
85 struct fmt_spec fmt_for_output_from_input (const struct fmt_spec *);
86 struct fmt_spec fmt_default_for_width (int width);
87
88 /* Verifying formats. */
89 bool fmt_check (const struct fmt_spec *, enum fmt_use);
90 bool fmt_check_input (const struct fmt_spec *);
91 bool fmt_check_output (const struct fmt_spec *);
92 bool fmt_check_type_compat (const struct fmt_spec *, enum val_type);
93 bool fmt_check_width_compat (const struct fmt_spec *, int var_width);
94
95 /* Working with formats. */
96 int fmt_var_width (const struct fmt_spec *);
97 char *fmt_to_string (const struct fmt_spec *, char s[FMT_STRING_LEN_MAX + 1]);
98 bool fmt_equal (const struct fmt_spec *, const struct fmt_spec *);
99 bool fmt_resize (struct fmt_spec *, int new_width);
100
101 void fmt_fix (struct fmt_spec *, enum fmt_use);
102 void fmt_fix_input (struct fmt_spec *);
103 void fmt_fix_output (struct fmt_spec *);
104
105 void fmt_change_width (struct fmt_spec *, int width, enum fmt_use);
106 void fmt_change_decimals (struct fmt_spec *, int decimals, enum fmt_use);
107
108 /* Format types. */
109 bool is_fmt_type (enum fmt_type);
110
111 const char *fmt_name (enum fmt_type) PURE_FUNCTION;
112 bool fmt_from_name (const char *name, enum fmt_type *);
113
114 bool fmt_takes_decimals (enum fmt_type) PURE_FUNCTION;
115
116 int fmt_min_width (enum fmt_type, enum fmt_use) PURE_FUNCTION;
117 int fmt_max_width (enum fmt_type, enum fmt_use) PURE_FUNCTION;
118 int fmt_max_decimals (enum fmt_type, int width, enum fmt_use) PURE_FUNCTION;
119 int fmt_min_input_width (enum fmt_type) PURE_FUNCTION;
120 int fmt_max_input_width (enum fmt_type) PURE_FUNCTION;
121 int fmt_max_input_decimals (enum fmt_type, int width) PURE_FUNCTION;
122 int fmt_min_output_width (enum fmt_type) PURE_FUNCTION;
123 int fmt_max_output_width (enum fmt_type) PURE_FUNCTION;
124 int fmt_max_output_decimals (enum fmt_type, int width) PURE_FUNCTION;
125 int fmt_step_width (enum fmt_type) PURE_FUNCTION;
126
127 bool fmt_is_string (enum fmt_type) PURE_FUNCTION;
128 bool fmt_is_numeric (enum fmt_type) PURE_FUNCTION;
129 enum fmt_category fmt_get_category (enum fmt_type) PURE_FUNCTION;
130
131 enum fmt_type fmt_input_to_output (enum fmt_type) PURE_FUNCTION;
132 bool fmt_usable_for_input (enum fmt_type) PURE_FUNCTION;
133
134 int fmt_to_io (enum fmt_type) PURE_FUNCTION;
135 bool fmt_from_io (int io, enum fmt_type *);
136
137 const char *fmt_date_template (enum fmt_type, int width) PURE_FUNCTION;
138 const char *fmt_gui_name (enum fmt_type);
139 \f
140 /* Format settings.
141
142    A fmt_settings is really just a collection of one "struct fmt_number_style"
143    for each format type. */
144 struct fmt_settings *fmt_settings_create (void);
145 void fmt_settings_destroy (struct fmt_settings *);
146 struct fmt_settings *fmt_settings_clone (const struct fmt_settings *);
147
148 void fmt_settings_set_decimal (struct fmt_settings *, char);
149
150 const struct fmt_number_style *fmt_settings_get_style (
151   const struct fmt_settings *, enum fmt_type);
152 void fmt_settings_set_style (struct fmt_settings *, enum fmt_type,
153                              char decimal, char grouping,
154                              const char *neg_prefix, const char *prefix,
155                              const char *suffix, const char *neg_suffix);
156 \f
157 /* A prefix or suffix for a numeric output format. */
158 struct fmt_affix
159   {
160     char *s;                    /* String contents of affix, in UTF-8. */
161     int width;                  /* Display width in columns (see wcwidth()). */
162   };
163
164 /* A numeric output style. */
165 struct fmt_number_style
166   {
167     struct fmt_affix neg_prefix; /* Negative prefix. */
168     struct fmt_affix prefix;     /* Prefix. */
169     struct fmt_affix suffix;     /* Suffix. */
170     struct fmt_affix neg_suffix; /* Negative suffix. */
171     char decimal;                /* Decimal point: '.' or ','. */
172     char grouping;               /* Grouping character: ',', '.', or 0. */
173
174     /* A fmt_affix may require more bytes than its display width; for example,
175        U+00A5 (¥) is 3 bytes in UTF-8 but occupies only one display column.
176        This member is the sum of the number of bytes required by all of the
177        fmt_affix members in this struct, minus their display widths.  Thus, it
178        can be used to size memory allocations: for example, the formatted
179        result of CCA20.5 requires no more than (20 + extra_bytes) bytes in
180        UTF-8. */
181     int extra_bytes;
182   };
183
184 int fmt_affix_width (const struct fmt_number_style *);
185 int fmt_neg_affix_width (const struct fmt_number_style *);
186
187 extern const struct fmt_spec F_8_0 ;
188 extern const struct fmt_spec F_8_2 ;
189 extern const struct fmt_spec F_4_3 ;
190 extern const struct fmt_spec F_5_1 ;
191
192 #endif /* data/format.h */