FORMATS: Allow an optional slash before each set of variable names.
[pspp-builds.git] / src / language / dictionary / formats.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2010, 2011 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 <limits.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 #include "data/dataset.h"
24 #include "data/format.h"
25 #include "data/variable.h"
26 #include "language/command.h"
27 #include "language/lexer/format-parser.h"
28 #include "language/lexer/lexer.h"
29 #include "language/lexer/variable-parser.h"
30 #include "libpspp/message.h"
31 #include "libpspp/misc.h"
32 #include "libpspp/str.h"
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37 enum
38   {
39     FORMATS_PRINT = 001,
40     FORMATS_WRITE = 002
41   };
42
43 static int internal_cmd_formats (struct lexer *, struct dataset *ds, int);
44
45 int
46 cmd_print_formats (struct lexer *lexer, struct dataset *ds)
47 {
48   return internal_cmd_formats (lexer, ds, FORMATS_PRINT);
49 }
50
51 int
52 cmd_write_formats (struct lexer *lexer, struct dataset *ds)
53 {
54   return internal_cmd_formats (lexer, ds, FORMATS_WRITE);
55 }
56
57 int
58 cmd_formats (struct lexer *lexer, struct dataset *ds)
59 {
60   return internal_cmd_formats (lexer, ds, FORMATS_PRINT | FORMATS_WRITE);
61 }
62
63 static int
64 internal_cmd_formats (struct lexer *lexer, struct dataset *ds, int which)
65 {
66   /* Variables. */
67   struct variable **v;
68   size_t cv;
69
70   for (;;)
71     {
72       struct fmt_spec f;
73       int width;
74       size_t i;
75
76       lex_match (lexer, T_SLASH);
77
78       if (lex_token (lexer) == T_ENDCMD)
79         break;
80
81       if (!parse_variables (lexer, dataset_dict (ds), &v, &cv, PV_SAME_WIDTH))
82         return CMD_FAILURE;
83       width = var_get_width (v[0]);
84
85       if (!lex_match (lexer, T_LPAREN))
86         {
87           msg (SE, _("`(' expected after variable list."));
88           goto fail;
89         }
90       if (!parse_format_specifier (lexer, &f)
91           || !fmt_check_output (&f)
92           || !fmt_check_width_compat (&f, width))
93         goto fail;
94
95       if (!lex_match (lexer, T_RPAREN))
96         {
97           msg (SE, _("`)' expected after output format."));
98           goto fail;
99         }
100
101       for (i = 0; i < cv; i++)
102         {
103           if (which & FORMATS_PRINT)
104             var_set_print_format (v[i], &f);
105           if (which & FORMATS_WRITE)
106             var_set_write_format (v[i], &f);
107         }
108       free (v);
109       v = NULL;
110     }
111   return CMD_SUCCESS;
112
113 fail:
114   free (v);
115   return CMD_FAILURE;
116 }