1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2005, 2006, 2009, 2010 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/message.h"
29 #include "libpspp/str.h"
32 #define _(msgid) gettext (msgid)
33 #define N_(msgid) msgid
35 static bool parse_number (struct lexer *, double *, const enum fmt_type *);
37 /* Parses and stores a numeric value, or a range of the form "x
38 THRU y". Open-ended ranges may be specified as "LO(WEST) THRU
39 y" or "x THRU HI(GHEST)". Sets *X and *Y to the range or the
40 value and returns success.
42 Numeric values are always accepted. If FORMAT is nonnull,
43 then string values are also accepted, and converted to numeric
44 values using *FORMAT. */
46 parse_num_range (struct lexer *lexer,
47 double *x, double *y, const enum fmt_type *format)
49 if (lex_match_id (lexer, "LO") || lex_match_id (lexer, "LOWEST"))
51 else if (!parse_number (lexer, x, format))
54 if (lex_match_id (lexer, "THRU"))
56 if (lex_match_id (lexer, "HI") || lex_match_id (lexer, "HIGHEST"))
58 else if (!parse_number (lexer, y, format))
64 msg (SW, _("Low end of range (%g) is below high end (%g). "
65 "The range will be treated as reversed."),
72 msg (SW, _("Ends of range are equal (%g)."), *x);
80 msg (SE, _("LO or LOWEST must be part of a range."));
89 /* Parses a number and stores it in *X. Returns success.
91 Numeric values are always accepted. If FORMAT is nonnull,
92 then string values are also accepted, and converted to numeric
93 values using *FORMAT. */
95 parse_number (struct lexer *lexer, double *x, const enum fmt_type *format)
97 if (lex_is_number (lexer))
99 *x = lex_number (lexer);
103 else if (lex_is_string (lexer) && format != NULL)
107 assert (fmt_get_category (*format) != FMT_CAT_STRING);
109 if (!data_in_msg (ds_ss (lex_tokstr (lexer)), LEGACY_NATIVE,
110 *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
133 already have been initialized with the specified WIDTH.
134 Returns true if successful, false otherwise. */
136 parse_value (struct lexer *lexer, union value *v, int width)
140 if (!lex_force_num (lexer))
142 v->f = lex_tokval (lexer);
144 else if (lex_force_string (lexer))
146 const char *s = ds_cstr (lex_tokstr (lexer));
147 value_copy_str_rpad (v, width, CHAR_CAST_BUG (const uint8_t *, s), ' ');