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