ba3979fb5f1cee1fcf77d793b9fbc30d51184f9a
[pspp] / src / data / format.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2010 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 <libpspp/str.h>
24 #include <data/val-type.h>
25
26 /* Format type categories.
27
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. */
32 enum fmt_category
33   {
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. */
43
44     /* String formats. */
45     FMT_CAT_STRING         = 0x100      /* String formats. */
46   };
47
48 /* Format type. */
49 enum fmt_type
50   {
51 #define FMT(NAME, METHOD, IMIN, OMIN, IO, CATEGORY) FMT_##NAME,
52 #include "format.def"
53     FMT_NUMBER_OF_FORMATS,
54   };
55
56 /* Length of longest format specifier name,
57    not including terminating null. */
58 #define FMT_TYPE_LEN_MAX 8
59
60 /* Length of longest string representation of fmt_spec,
61    not including terminating null. */
62 #define FMT_STRING_LEN_MAX 32
63
64 /* Display format. */
65 struct fmt_spec
66   {
67     enum fmt_type type;         /* One of FMT_*. */
68     int w;                      /* Width. */
69     int d;                      /* Number of implied decimal places. */
70   };
71
72 /* Maximum width of any numeric format. */
73 #define FMT_MAX_NUMERIC_WIDTH 40
74
75 /* A numeric output style. */
76 struct fmt_number_style
77   {
78     struct substring neg_prefix;      /* Negative prefix. */
79     struct substring prefix;          /* Prefix. */
80     struct substring suffix;          /* Suffix. */
81     struct substring neg_suffix;      /* Negative suffix. */
82     char decimal;                     /* Decimal point: '.' or ','. */
83     char grouping;                    /* Grouping character: ',', '.', or 0. */
84   };
85
86
87 /* Initialization. */
88 struct fmt_number_style * fmt_create (void);
89 void fmt_done (struct fmt_number_style *);
90
91 /* Constructing formats. */
92 struct fmt_spec fmt_for_input (enum fmt_type, int w, int d) PURE_FUNCTION;
93 struct fmt_spec fmt_for_output (enum fmt_type, int w, int d) PURE_FUNCTION;
94 struct fmt_spec fmt_for_output_from_input (const struct fmt_spec *);
95 struct fmt_spec fmt_default_for_width (int width);
96
97 /* Verifying formats. */
98 bool fmt_check (const struct fmt_spec *, bool for_input);
99 bool fmt_check_input (const struct fmt_spec *);
100 bool fmt_check_output (const struct fmt_spec *);
101 bool fmt_check_type_compat (const struct fmt_spec *, enum val_type);
102 bool fmt_check_width_compat (const struct fmt_spec *, int var_width);
103
104 /* Working with formats. */
105 int fmt_var_width (const struct fmt_spec *);
106 char *fmt_to_string (const struct fmt_spec *, char s[FMT_STRING_LEN_MAX + 1]);
107 bool fmt_equal (const struct fmt_spec *, const struct fmt_spec *);
108 void fmt_resize (struct fmt_spec *, int new_width);
109
110 void fmt_fix (struct fmt_spec *, bool for_input);
111 void fmt_fix_input (struct fmt_spec *);
112 void fmt_fix_output (struct fmt_spec *);
113
114 /* Format types. */
115 const char *fmt_name (enum fmt_type) PURE_FUNCTION;
116 bool fmt_from_name (const char *name, enum fmt_type *);
117
118 bool fmt_takes_decimals (enum fmt_type) PURE_FUNCTION;
119
120 int fmt_min_width (enum fmt_type, bool for_input) PURE_FUNCTION;
121 int fmt_max_width (enum fmt_type, bool for_input) PURE_FUNCTION;
122 int fmt_max_decimals (enum fmt_type, int width, bool for_input) PURE_FUNCTION;
123 int fmt_min_input_width (enum fmt_type) PURE_FUNCTION;
124 int fmt_max_input_width (enum fmt_type) PURE_FUNCTION;
125 int fmt_max_input_decimals (enum fmt_type, int width) PURE_FUNCTION;
126 int fmt_min_output_width (enum fmt_type) PURE_FUNCTION;
127 int fmt_max_output_width (enum fmt_type) PURE_FUNCTION;
128 int fmt_max_output_decimals (enum fmt_type, int width) PURE_FUNCTION;
129 int fmt_step_width (enum fmt_type) PURE_FUNCTION;
130
131 bool fmt_is_string (enum fmt_type) PURE_FUNCTION;
132 bool fmt_is_numeric (enum fmt_type) PURE_FUNCTION;
133 enum fmt_category fmt_get_category (enum fmt_type) PURE_FUNCTION;
134
135 enum fmt_type fmt_input_to_output (enum fmt_type) PURE_FUNCTION;
136 bool fmt_usable_for_input (enum fmt_type) PURE_FUNCTION;
137
138 int fmt_to_io (enum fmt_type) PURE_FUNCTION;
139 bool fmt_from_io (int io, enum fmt_type *);
140
141 const char *fmt_date_template (enum fmt_type) PURE_FUNCTION;
142 \f
143 /* Maximum length of prefix or suffix string in
144    struct fmt_number_style. */
145 #define FMT_STYLE_AFFIX_MAX 16
146
147
148 struct fmt_number_style *fmt_number_style_create (void);
149 void fmt_number_style_destroy (struct fmt_number_style *);
150
151 int fmt_affix_width (const struct fmt_number_style *);
152 int fmt_neg_affix_width (const struct fmt_number_style *);
153
154 bool is_fmt_type (enum fmt_type);
155
156 const struct fmt_number_style *fmt_get_style (const struct fmt_number_style *,      enum fmt_type);
157
158 void fmt_check_style (const struct fmt_number_style *style);
159
160 int fmt_grouping_char (const struct fmt_number_style *, enum fmt_type);
161
162 void fmt_set_decimal (struct fmt_number_style *, char);
163
164 extern const struct fmt_spec F_8_0 ;
165
166 #endif /* data/format.h */