1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2005, 2006, 2009, 2010, 2011 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include "value-parser.h"
23 #include "data/data-in.h"
24 #include "data/format.h"
25 #include "data/value.h"
26 #include "language/lexer/lexer.h"
27 #include "libpspp/cast.h"
28 #include "libpspp/i18n.h"
29 #include "libpspp/message.h"
30 #include "libpspp/str.h"
33 #define _(msgid) gettext (msgid)
34 #define N_(msgid) msgid
36 static bool parse_number (struct lexer *, double *, const enum fmt_type *);
38 /* Parses and stores a numeric value, or a range of the form "x
39 THRU y". Open-ended ranges may be specified as "LO(WEST) THRU
40 y" or "x THRU HI(GHEST)". Sets *X and *Y to the range or the
41 value and returns success.
43 Numeric values are always accepted. If FORMAT is nonnull,
44 then string values are also accepted, and converted to numeric
45 values using *FORMAT. */
47 parse_num_range (struct lexer *lexer,
48 double *x, double *y, const enum fmt_type *format)
50 if (lex_match_id (lexer, "LO") || lex_match_id (lexer, "LOWEST"))
52 else if (!parse_number (lexer, x, format))
55 if (lex_match_id (lexer, "THRU"))
57 if (lex_match_id (lexer, "HI") || lex_match_id (lexer, "HIGHEST"))
59 else if (!parse_number (lexer, y, format))
65 msg (SW, _("Low end of range (%g) is below high end (%g). "
66 "The range will be treated as reversed."),
73 msg (SW, _("Ends of range are equal (%g)."), *x);
81 msg (SE, _("LO or LOWEST must be part of a range."));
90 /* Parses a number and stores it in *X. Returns success.
92 Numeric values are always accepted. If FORMAT is nonnull,
93 then string values are also accepted, and converted to numeric
94 values using *FORMAT. */
96 parse_number (struct lexer *lexer, double *x, const enum fmt_type *format)
98 if (lex_is_number (lexer))
100 *x = lex_number (lexer);
104 else if (lex_is_string (lexer) && format != NULL)
108 assert (fmt_get_category (*format) != FMT_CAT_STRING);
110 if (!data_in_msg (lex_tokss (lexer), "UTF-8", *format, &v, 0, NULL))
117 msg (SE, _("System-missing value is not valid here."));
125 lex_error (lexer, _("expecting number or data string"));
127 lex_force_num (lexer);
132 /* Parses the current token from LEXER into value V, which must already have
133 been initialized with the specified VAR's WIDTH. Returns true if
134 successful, false otherwise. */
136 parse_value (struct lexer *lexer, union value *v, const struct variable *var)
138 int width = var_get_width (var);
140 return parse_number (lexer, &v->f, &var_get_print_format (var)->type);
141 else if (lex_force_string (lexer))
143 const char *s = lex_tokcstr (lexer);
144 value_copy_str_rpad (v, width, CHAR_CAST_BUG (const uint8_t *, s), ' ');