1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2005, 2006, 2009 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/>. */
18 #include "value-parser.h"
20 #include <data/data-in.h>
21 #include <libpspp/message.h>
23 #include <libpspp/str.h>
24 #include <data/value.h>
25 #include <data/format.h>
28 #define _(msgid) gettext (msgid)
29 #define N_(msgid) msgid
31 static bool parse_number (struct lexer *, double *, const enum fmt_type *);
33 /* Parses and stores a numeric value, or a range of the form "x
34 THRU y". Open-ended ranges may be specified as "LO(WEST) THRU
35 y" or "x THRU HI(GHEST)". Sets *X and *Y to the range or the
36 value and returns success.
38 Numeric values are always accepted. If FORMAT is nonnull,
39 then string values are also accepted, and converted to numeric
40 values using *FORMAT. */
42 parse_num_range (struct lexer *lexer,
43 double *x, double *y, const enum fmt_type *format)
45 if (lex_match_id (lexer, "LO") || lex_match_id (lexer, "LOWEST"))
47 else if (!parse_number (lexer, x, format))
50 if (lex_match_id (lexer, "THRU"))
52 if (lex_match_id (lexer, "HI") || lex_match_id (lexer, "HIGHEST"))
54 else if (!parse_number (lexer, y, format))
60 msg (SW, _("Low end of range (%g) is below high end (%g). "
61 "The range will be treated as reversed."),
68 msg (SW, _("Ends of range are equal (%g)."), *x);
76 msg (SE, _("LO or LOWEST must be part of a range."));
85 /* Parses a number and stores it in *X. Returns success.
87 Numeric values are always accepted. If FORMAT is nonnull,
88 then string values are also accepted, and converted to numeric
89 values using *FORMAT. */
91 parse_number (struct lexer *lexer, double *x, const enum fmt_type *format)
93 if (lex_is_number (lexer))
95 *x = lex_number (lexer);
99 else if (lex_token (lexer) == T_STRING && format != NULL)
102 data_in (ds_ss (lex_tokstr (lexer)), LEGACY_NATIVE,
103 *format, 0, 0, 0, &v, 0);
108 msg (SE, _("System-missing value is not valid here."));
116 lex_error (lexer, _("expecting number or data string"));
118 lex_force_num (lexer);
123 /* Parses the current token from LEXER into value V, which must
124 already have been initialized with the specified WIDTH.
125 Returns true if successful, false otherwise. */
127 parse_value (struct lexer *lexer, union value *v, int width)
131 if (!lex_force_num (lexer))
133 v->f = lex_tokval (lexer);
137 if (!lex_force_string (lexer))
139 value_copy_str_rpad (v, width, ds_cstr (lex_tokstr (lexer)), ' ');