7b01b2b889b37385b203e5777ffe43aaa8f3be85
[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 <data/procedure.h>
27 #include <data/variable.h>
28 #include <language/command.h>
29 #include <language/lexer/lexer.h>
30 #include <language/lexer/variable-parser.h>
31 #include <libpspp/message.h>
32 #include <libpspp/misc.h>
33 #include <libpspp/str.h>
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37
38 enum
39   {
40     FORMATS_PRINT = 001,
41     FORMATS_WRITE = 002
42   };
43
44 static int internal_cmd_formats (int);
45
46 int
47 cmd_print_formats (void)
48 {
49   return internal_cmd_formats (FORMATS_PRINT);
50 }
51
52 int
53 cmd_write_formats (void)
54 {
55   return internal_cmd_formats (FORMATS_WRITE);
56 }
57
58 int
59 cmd_formats (void)
60 {
61   return internal_cmd_formats (FORMATS_PRINT | FORMATS_WRITE);
62 }
63
64 int
65 internal_cmd_formats (int which)
66 {
67   /* Variables. */
68   struct variable **v;
69   size_t cv;
70
71   /* Format to set the variables to. */
72   struct fmt_spec f;
73
74   /* Numeric or string. */
75   int type;
76
77   /* Counter. */
78   size_t i;
79
80   for (;;)
81     {
82       if (token == '.')
83         break;
84
85       if (!parse_variables (default_dict, &v, &cv, PV_NUMERIC))
86         return CMD_FAILURE;
87       type = v[0]->type;
88
89       if (!lex_match ('('))
90         {
91           msg (SE, _("`(' expected after variable list"));
92           goto fail;
93         }
94       if (!parse_format_specifier (&f, 0)
95           || !check_output_specifier (&f, true)
96           || !check_specifier_type (&f, NUMERIC, true))
97         goto fail;
98
99       if (!lex_match (')'))
100         {
101           msg (SE, _("`)' expected after output format."));
102           goto fail;
103         }
104
105       for (i = 0; i < cv; i++)
106         {
107           if (which & FORMATS_PRINT)
108             v[i]->print = f;
109           if (which & FORMATS_WRITE)
110             v[i]->write = f;
111         }
112       free (v);
113       v = NULL;
114     }
115   return CMD_SUCCESS;
116
117 fail:
118   free (v);
119   return CMD_FAILURE;
120 }