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