Update all #include directives to the currently preferred style.
[pspp-builds.git] / src / language / lexer / format-parser.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2010, 2011 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 #include <config.h>
18
19 #include "language/lexer/format-parser.h"
20
21 #include <ctype.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24
25 #include "data/format.h"
26 #include "data/variable.h"
27 #include "language/lexer/format-parser.h"
28 #include "language/lexer/lexer.h"
29 #include "libpspp/message.h"
30 #include "libpspp/misc.h"
31 #include "libpspp/str.h"
32
33 #include "gettext.h"
34 #define _(msgid) gettext (msgid)
35
36 static bool
37 parse_abstract_format_specifier__ (struct lexer *lexer,
38                                    char type[FMT_TYPE_LEN_MAX + 1],
39                                    int *width, int *decimals)
40 {
41   struct substring s;
42   struct substring type_ss, width_ss, decimals_ss;
43   bool has_decimals;
44
45   if (lex_token (lexer) != T_ID)
46     goto error;
47
48   /* Extract pieces. */
49   s = ss_cstr (lex_tokcstr (lexer));
50   ss_get_bytes (&s, ss_span (s, ss_cstr (CC_LETTERS)), &type_ss);
51   ss_get_bytes (&s, ss_span (s, ss_cstr (CC_DIGITS)), &width_ss);
52   if (ss_match_byte (&s, '.'))
53     {
54       has_decimals = true;
55       ss_get_bytes (&s, ss_span (s, ss_cstr (CC_DIGITS)), &decimals_ss);
56     }
57   else
58     has_decimals = false;
59
60   /* Check pieces. */
61   if (ss_is_empty (type_ss) || ss_length (type_ss) > FMT_TYPE_LEN_MAX)
62     goto error;
63   if (has_decimals && ss_is_empty (decimals_ss))
64     goto error;
65   if (!ss_is_empty (s))
66     goto error;
67
68   /* Return pieces.
69      These uses of strtol are valid only because we know that
70      their substrings are followed by non-digit characters. */
71   str_copy_buf_trunc (type, FMT_TYPE_LEN_MAX + 1,
72                       ss_data (type_ss), ss_length (type_ss));
73   *width = strtol (ss_data (width_ss), NULL, 10);
74   *decimals = has_decimals ? strtol (ss_data (decimals_ss), NULL, 10) : 0;
75
76   return true;
77
78 error:
79   lex_error (lexer, _("expecting valid format specifier"));
80   return false;
81 }
82
83 /* Parses a token taking the form of a format specifier and
84    returns true only if successful.  Emits an error message on
85    failure.  Stores a null-terminated string representing the
86    format type in TYPE, and the width and number of decimal
87    places in *WIDTH and *DECIMALS.
88
89    TYPE is not checked as to whether it is really the name of a
90    format.  Both width and decimals are considered optional.  If
91    missing, *WIDTH or *DECIMALS or both will be set to 0. */
92 bool
93 parse_abstract_format_specifier (struct lexer *lexer,
94                                  char type[FMT_TYPE_LEN_MAX + 1],
95                                  int *width, int *decimals)
96 {
97   bool ok = parse_abstract_format_specifier__ (lexer, type, width, decimals);
98   if (ok)
99     lex_get (lexer);
100   return ok;
101 }
102
103 /* Parses a format specifier from the token stream and returns
104    true only if successful.  Emits an error message on
105    failure.  The caller should call check_input_specifier() or
106    check_output_specifier() on the parsed format as
107    necessary.  */
108 bool
109 parse_format_specifier (struct lexer *lexer, struct fmt_spec *format)
110 {
111   char type[FMT_TYPE_LEN_MAX + 1];
112
113   if (!parse_abstract_format_specifier__ (lexer, type, &format->w, &format->d))
114     return false;
115
116   if (!fmt_from_name (type, &format->type))
117     {
118       msg (SE, _("Unknown format type `%s'."), type);
119       return false;
120     }
121
122   lex_get (lexer);
123   return true;
124 }
125
126 /* Parses a token containing just the name of a format type and
127    returns true if successful. */
128 bool
129 parse_format_specifier_name (struct lexer *lexer, enum fmt_type *type)
130 {
131   if (lex_token (lexer) != T_ID)
132     {
133       lex_error (lexer, _("expecting format type"));
134       return false;
135     }
136   if (!fmt_from_name (lex_tokcstr (lexer), type))
137     {
138       msg (SE, _("Unknown format type `%s'."), lex_tokcstr (lexer));
139       return false;
140     }
141   lex_get (lexer);
142   return true;
143 }