51484a3b1f1b260c1d02088888c2c56cf884a640
[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 <data/variable.h>
28 #include <libpspp/str.h>
29
30 /* Format type categories. */
31 enum fmt_category 
32   {
33     /* Numeric formats. */
34     FMT_CAT_BASIC          = 0x001,     /* Basic numeric formats. */
35     FMT_CAT_CUSTOM         = 0x002,     /* Custom currency formats. */
36     FMT_CAT_LEGACY         = 0x004,     /* Legacy numeric formats. */
37     FMT_CAT_BINARY         = 0x008,     /* Binary formats. */
38     FMT_CAT_HEXADECIMAL    = 0x010,     /* Hexadecimal formats. */
39     FMT_CAT_DATE           = 0x020,     /* Date formats. */
40     FMT_CAT_TIME           = 0x040,     /* Time formats. */
41     FMT_CAT_DATE_COMPONENT = 0x080,     /* Date component formats. */
42
43     /* String formats. */
44     FMT_CAT_STRING         = 0x100      /* String formats. */
45   };
46
47 enum fmt_type
48   {
49 #define FMT(NAME, METHOD, IMIN, OMIN, IO, CATEGORY) FMT_##NAME,
50 #include "format.def"
51     FMT_NUMBER_OF_FORMATS,
52   };
53
54 /* Length of longest format specifier name,
55    not including terminating null. */
56 #define FMT_TYPE_LEN_MAX 8
57
58 /* Length of longest string representation of fmt_spec,
59    not including terminating null. */
60 #define FMT_STRING_LEN_MAX 32
61
62 /* Display format. */
63 struct fmt_spec
64   {
65     enum fmt_type type;         /* One of FMT_*. */
66     int w;                      /* Width. */
67     int d;                      /* Number of implied decimal places. */
68   };
69
70 union value;
71
72 /* Initialization. */
73 void fmt_init (void);
74 void fmt_done (void);
75
76 /* Constructing formats. */
77 struct fmt_spec fmt_for_input (enum fmt_type, int w, int d) PURE_FUNCTION;
78 struct fmt_spec fmt_for_output (enum fmt_type, int w, int d) PURE_FUNCTION;
79 struct fmt_spec fmt_for_output_from_input (const struct fmt_spec *);
80 struct fmt_spec fmt_default_for_width (int var_width);
81
82 /* Verifying formats. */
83 bool fmt_check (const struct fmt_spec *, bool for_input);
84 bool fmt_check_input (const struct fmt_spec *);
85 bool fmt_check_output (const struct fmt_spec *);
86 bool fmt_check_type_compat (const struct fmt_spec *, enum var_type);
87 bool fmt_check_width_compat (const struct fmt_spec *, int var_width);
88
89 /* Working with formats. */
90 int fmt_var_width (const struct fmt_spec *);
91 char *fmt_to_string (const struct fmt_spec *, char s[FMT_STRING_LEN_MAX + 1]);
92 bool fmt_equal (const struct fmt_spec *, const struct fmt_spec *);
93
94 /* Format types. */
95 const char *fmt_name (enum fmt_type) PURE_FUNCTION;
96 bool fmt_from_name (const char *name, enum fmt_type *);
97
98 bool fmt_takes_decimals (enum fmt_type) PURE_FUNCTION;
99
100 int fmt_min_input_width (enum fmt_type) PURE_FUNCTION;
101 int fmt_max_input_width (enum fmt_type) PURE_FUNCTION;
102 int fmt_max_input_decimals (enum fmt_type, int width) PURE_FUNCTION;
103 int fmt_min_output_width (enum fmt_type) PURE_FUNCTION;
104 int fmt_max_output_width (enum fmt_type) PURE_FUNCTION;
105 int fmt_max_output_decimals (enum fmt_type, int width) PURE_FUNCTION;
106 int fmt_step_width (enum fmt_type) PURE_FUNCTION;
107
108 bool fmt_is_string (enum fmt_type) PURE_FUNCTION;
109 bool fmt_is_numeric (enum fmt_type) PURE_FUNCTION;
110 enum fmt_category fmt_get_category (enum fmt_type) PURE_FUNCTION;
111
112 enum fmt_type fmt_input_to_output (enum fmt_type) PURE_FUNCTION;
113
114 int fmt_to_io (enum fmt_type) PURE_FUNCTION;
115 bool fmt_from_io (int io, enum fmt_type *);
116
117 bool fmt_usable_for_input (enum fmt_type) PURE_FUNCTION;
118 const char *fmt_date_template (enum fmt_type) PURE_FUNCTION;
119 char *fmt_dollar_template (const struct fmt_spec *);
120 \f
121 /* Maximum length of prefix or suffix string in
122    struct fmt_number_style. */
123 #define FMT_STYLE_AFFIX_MAX 16
124
125 /* A numeric output style. */
126 struct fmt_number_style
127   {
128     struct substring neg_prefix;      /* Negative prefix. */
129     struct substring prefix;          /* Prefix. */
130     struct substring suffix;          /* Suffix. */
131     struct substring neg_suffix;      /* Negative suffix. */
132     char decimal;                     /* Decimal point: '.' or ','. */
133     char grouping;                    /* Grouping character: ',', '.', or 0. */
134   };
135
136 struct fmt_number_style *fmt_number_style_create (void);
137 void fmt_number_style_destroy (struct fmt_number_style *);
138
139 const struct fmt_number_style *fmt_get_style (enum fmt_type);
140 void fmt_set_style (enum fmt_type, struct fmt_number_style *);
141
142 int fmt_affix_width (const struct fmt_number_style *);
143 int fmt_neg_affix_width (const struct fmt_number_style *);
144
145 int fmt_decimal_char (enum fmt_type);
146 int fmt_grouping_char (enum fmt_type);
147
148 void fmt_set_decimal (char);
149
150 #endif /* format.h */