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