Implemented support for very long strings a la spss v13/v14
[pspp-builds.git] / src / language / lexer / format-parser.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include <data/format.h>
22 #include <ctype.h>
23 #include <libpspp/message.h>
24 #include <stdlib.h>
25 #include <libpspp/message.h>
26 #include "lexer.h"
27 #include <libpspp/misc.h>
28 #include <libpspp/str.h>
29 #include <data/variable.h>
30
31 #include "gettext.h"
32 #define _(msgid) gettext (msgid)
33
34
35 /* Parses the alphabetic prefix of the current token as a format
36    specifier name.  Returns the corresponding format specifier
37    type if successful, or -1 on failure.  If ALLOW_XT is zero,
38    then X and T format specifiers are not allowed.  If CP is
39    nonzero, then *CP is set to the first non-alphabetic character
40    in the current token on success or to a null pointer on
41    failure. */
42 int
43 parse_format_specifier_name (const char **cp, enum fmt_parse_flags flags)
44 {
45   char *sp, *ep;
46   int idx;
47
48   sp = ep = ds_c_str (&tokstr);
49   while (isalpha ((unsigned char) *ep))
50     ep++;
51
52   if (sp != ep) 
53     {
54       /* Find format. */
55       for (idx = 0; idx < FMT_NUMBER_OF_FORMATS; idx++)
56         if (strlen (formats[idx].name) == ep - sp
57             && !buf_compare_case (formats[idx].name, sp, ep - sp))
58           break;
59
60       /* Check format. */
61       if (idx < FMT_NUMBER_OF_FORMATS)
62         {
63           if (!(flags & FMTP_ALLOW_XT) && (idx == FMT_T || idx == FMT_X)) 
64             {
65               if (!(flags & FMTP_SUPPRESS_ERRORS))
66                 msg (SE, _("X and T format specifiers not allowed here."));
67               idx = -1; 
68             }
69         }
70       else 
71         {
72           /* No match. */
73           if (!(flags & FMTP_SUPPRESS_ERRORS))
74             msg (SE, _("%.*s is not a valid data format."),
75                  (int) (ep - sp), ds_c_str (&tokstr));
76           idx = -1; 
77         }
78     }
79   else 
80     {
81       lex_error ("expecting data format");
82       idx = -1;
83     }
84       
85   if (cp != NULL) 
86     {
87       if (idx != -1)
88         *cp = ep;
89       else
90         *cp = NULL;
91     }
92
93   return idx;
94 }
95
96
97 /* Parses a format specifier from the token stream and returns
98    nonzero only if successful.  Emits an error message on
99    failure.  Allows X and T format specifiers only if ALLOW_XT is
100    nonzero.  The caller should call check_input_specifier() or
101    check_output_specifier() on the parsed format as
102    necessary.  */
103 int
104 parse_format_specifier (struct fmt_spec *input, enum fmt_parse_flags flags)
105 {
106   struct fmt_spec spec;
107   struct fmt_desc *f;
108   const char *cp;
109   char *cp2;
110   int type, w, d;
111
112   if (token != T_ID)
113     {
114       if (!(flags & FMTP_SUPPRESS_ERRORS))
115         msg (SE, _("Format specifier expected."));
116       return 0;
117     }
118   type = parse_format_specifier_name (&cp, flags);
119   if (type == -1)
120     return 0;
121   f = &formats[type];
122
123   w = strtol (cp, &cp2, 10);
124   if (cp2 == cp && type != FMT_X)
125     {
126       if (!(flags & FMTP_SUPPRESS_ERRORS))
127         msg (SE, _("Data format %s does not specify a width."),
128              ds_c_str (&tokstr));
129       return 0;
130     }
131   if ( w > MAX_STRING )
132     {
133       msg (SE, _("String variable width may not exceed %d"), MAX_STRING);
134       return 0;
135     }
136
137   cp = cp2;
138   if (f->n_args > 1 && *cp == '.')
139     {
140       cp++;
141       d = strtol (cp, &cp2, 10);
142       cp = cp2;
143     }
144   else
145     d = 0;
146
147   if (*cp)
148     {
149       if (!(flags & FMTP_SUPPRESS_ERRORS))
150         msg (SE, _("Data format %s is not valid."), ds_c_str (&tokstr));
151       return 0;
152     }
153   lex_get ();
154
155   spec.type = type;
156   spec.w = w;
157   spec.d = d;
158   *input = spec;
159
160   return 1;
161 }
162