Rewrite and improve formatted output routines.
[pspp-builds.git] / src / data / format.h
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #ifndef FORMAT_H
21 #define FORMAT_H 1
22
23 /* Display format types. */
24
25 #include <stdbool.h>
26 #include <stddef.h>
27 #include <libpspp/str.h>
28
29 /* Format type categories. */
30 enum fmt_category 
31   {
32     /* Numeric formats. */
33     FMT_CAT_BASIC          = 0x001,     /* Basic numeric formats. */
34     FMT_CAT_CUSTOM         = 0x002,     /* Custom currency formats. */
35     FMT_CAT_LEGACY         = 0x004,     /* Legacy numeric formats. */
36     FMT_CAT_BINARY         = 0x008,     /* Binary formats. */
37     FMT_CAT_HEXADECIMAL    = 0x010,     /* Hexadecimal formats. */
38     FMT_CAT_DATE           = 0x020,     /* Date formats. */
39     FMT_CAT_TIME           = 0x040,     /* Time formats. */
40     FMT_CAT_DATE_COMPONENT = 0x080,     /* Date component formats. */
41
42     /* String formats. */
43     FMT_CAT_STRING         = 0x100      /* String formats. */
44   };
45
46 enum fmt_type
47   {
48 #define FMT(NAME, METHOD, IMIN, OMIN, IO, CATEGORY) FMT_##NAME,
49 #include "format.def"
50     FMT_NUMBER_OF_FORMATS,
51   };
52
53 /* Length of longest format specifier name,
54    not including terminating null. */
55 #define FMT_TYPE_LEN_MAX 8
56
57 /* Length of longest string representation of fmt_spec,
58    not including terminating null. */
59 #define FMT_STRING_LEN_MAX 32
60
61 /* Display format. */
62 struct fmt_spec
63   {
64     enum fmt_type type;         /* One of FMT_*. */
65     int w;                      /* Width. */
66     int d;                      /* Number of implied decimal places. */
67   };
68
69 union value;
70
71 /* Initialization. */
72 void fmt_init (void);
73 void fmt_done (void);
74
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
80 /* Verifying formats. */
81 bool fmt_check (const struct fmt_spec *, bool for_input);
82 bool fmt_check_input (const struct fmt_spec *);
83 bool fmt_check_output (const struct fmt_spec *);
84 bool fmt_check_type_compat (const struct fmt_spec *, int var_type);
85 bool fmt_check_width_compat (const struct fmt_spec *, int width);
86
87 /* Working with formats. */
88 int fmt_var_width (const struct fmt_spec *);
89 char *fmt_to_string (const struct fmt_spec *, char s[FMT_STRING_LEN_MAX + 1]);
90
91 /* Format types. */
92 const char *fmt_name (enum fmt_type) PURE_FUNCTION;
93 bool fmt_from_name (const char *name, enum fmt_type *);
94
95 bool fmt_takes_decimals (enum fmt_type) PURE_FUNCTION;
96
97 int fmt_min_input_width (enum fmt_type) PURE_FUNCTION;
98 int fmt_max_input_width (enum fmt_type) PURE_FUNCTION;
99 int fmt_max_input_decimals (enum fmt_type, int width) PURE_FUNCTION;
100 int fmt_min_output_width (enum fmt_type) PURE_FUNCTION;
101 int fmt_max_output_width (enum fmt_type) PURE_FUNCTION;
102 int fmt_max_output_decimals (enum fmt_type, int width) PURE_FUNCTION;
103 int fmt_step_width (enum fmt_type) PURE_FUNCTION;
104
105 bool fmt_is_string (enum fmt_type) PURE_FUNCTION;
106 bool fmt_is_numeric (enum fmt_type) PURE_FUNCTION;
107 enum fmt_category fmt_get_category (enum fmt_type) PURE_FUNCTION;
108
109 enum fmt_type fmt_input_to_output (enum fmt_type) PURE_FUNCTION;
110
111 int fmt_to_io (enum fmt_type) PURE_FUNCTION;
112 bool fmt_from_io (int io, enum fmt_type *);
113
114 bool fmt_usable_for_input (enum fmt_type) PURE_FUNCTION;
115 const char *fmt_date_template (enum fmt_type) PURE_FUNCTION;
116 \f
117 /* Maximum length of prefix or suffix string in
118    struct fmt_number_style. */
119 #define FMT_STYLE_AFFIX_MAX 16
120
121 /* A numeric output style. */
122 struct fmt_number_style
123   {
124     struct substring neg_prefix;      /* Negative prefix. */
125     struct substring prefix;          /* Prefix. */
126     struct substring suffix;          /* Suffix. */
127     struct substring neg_suffix;      /* Negative suffix. */
128     char decimal;                     /* Decimal point: '.' or ','. */
129     char grouping;                    /* Grouping character: ',', '.', or 0. */
130   };
131
132 struct fmt_number_style *fmt_number_style_create (void);
133 void fmt_number_style_destroy (struct fmt_number_style *);
134
135 const struct fmt_number_style *fmt_get_style (enum fmt_type);
136 void fmt_set_style (enum fmt_type, struct fmt_number_style *);
137
138 int fmt_affix_width (const struct fmt_number_style *);
139 int fmt_neg_affix_width (const struct fmt_number_style *);
140
141 int fmt_decimal_char (enum fmt_type);
142 int fmt_grouping_char (enum fmt_type);
143
144 void fmt_set_decimal (char);
145 \f
146 /* Alignment of data for display. */
147 enum alignment 
148   {
149     ALIGN_LEFT = 0,
150     ALIGN_RIGHT = 1,
151     ALIGN_CENTRE = 2, 
152     n_ALIGN
153   };
154
155 /* How data is measured. */
156 enum measure
157   {
158     MEASURE_NOMINAL=1,
159     MEASURE_ORDINAL=2,
160     MEASURE_SCALE=3,
161     n_MEASURES
162   };
163
164 bool measure_is_valid(enum measure m);
165 bool alignment_is_valid(enum alignment a);
166
167 #endif /* format.h */