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