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