Continue reforming procedure execution. In this phase, get rid of
[pspp-builds.git] / src / language / dictionary / formats.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
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #include <procedure.h>
27 #include <data/variable.h>
28 #include <language/command.h>
29 #include <language/lexer/lexer.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 (int);
44
45 int
46 cmd_print_formats (void)
47 {
48   return internal_cmd_formats (FORMATS_PRINT);
49 }
50
51 int
52 cmd_write_formats (void)
53 {
54   return internal_cmd_formats (FORMATS_WRITE);
55 }
56
57 int
58 cmd_formats (void)
59 {
60   return internal_cmd_formats (FORMATS_PRINT | FORMATS_WRITE);
61 }
62
63 int
64 internal_cmd_formats (int which)
65 {
66   /* Variables. */
67   struct variable **v;
68   size_t cv;
69
70   /* Format to set the variables to. */
71   struct fmt_spec f;
72
73   /* Numeric or string. */
74   int type;
75
76   /* Counter. */
77   size_t i;
78
79   for (;;)
80     {
81       if (token == '.')
82         break;
83
84       if (!parse_variables (default_dict, &v, &cv, PV_NUMERIC))
85         return CMD_FAILURE;
86       type = v[0]->type;
87
88       if (!lex_match ('('))
89         {
90           msg (SE, _("`(' expected after variable list"));
91           goto fail;
92         }
93       if (!parse_format_specifier (&f, 0)
94           || !check_output_specifier (&f, true)
95           || !check_specifier_type (&f, NUMERIC, true))
96         goto fail;
97
98       if (!lex_match (')'))
99         {
100           msg (SE, _("`)' expected after output format."));
101           goto fail;
102         }
103
104       for (i = 0; i < cv; i++)
105         {
106           if (which & FORMATS_PRINT)
107             v[i]->print = f;
108           if (which & FORMATS_WRITE)
109             v[i]->write = f;
110         }
111       free (v);
112       v = NULL;
113     }
114   return CMD_SUCCESS;
115
116 fail:
117   free (v);
118   return CMD_FAILURE;
119 }