3be32e76c62f8ed452c5630f29e582e07376816c
[pspp-builds.git] / src / language / dictionary / formats.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006 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/procedure.h>
24 #include <data/variable.h>
25 #include <language/command.h>
26 #include <language/lexer/format-parser.h>
27 #include <language/lexer/lexer.h>
28 #include <language/lexer/variable-parser.h>
29 #include <libpspp/message.h>
30 #include <libpspp/misc.h>
31 #include <libpspp/str.h>
32
33 #include "gettext.h"
34 #define _(msgid) gettext (msgid)
35
36 enum
37   {
38     FORMATS_PRINT = 001,
39     FORMATS_WRITE = 002
40   };
41
42 static int internal_cmd_formats (struct lexer *, struct dataset *ds, int);
43
44 int
45 cmd_print_formats (struct lexer *lexer, struct dataset *ds)
46 {
47   return internal_cmd_formats (lexer, ds, FORMATS_PRINT);
48 }
49
50 int
51 cmd_write_formats (struct lexer *lexer, struct dataset *ds)
52 {
53   return internal_cmd_formats (lexer, ds, FORMATS_WRITE);
54 }
55
56 int
57 cmd_formats (struct lexer *lexer, struct dataset *ds)
58 {
59   return internal_cmd_formats (lexer, ds, FORMATS_PRINT | FORMATS_WRITE);
60 }
61
62 static int
63 internal_cmd_formats (struct lexer *lexer, struct dataset *ds, int which)
64 {
65   /* Variables. */
66   struct variable **v;
67   size_t 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   size_t i;
77
78   for (;;)
79     {
80       if (lex_token (lexer) == '.')
81         break;
82
83       if (!parse_variables (lexer, dataset_dict (ds), &v, &cv, PV_NUMERIC))
84         return CMD_FAILURE;
85       type = var_get_type (v[0]);
86
87       if (!lex_match (lexer, '('))
88         {
89           msg (SE, _("`(' expected after variable list."));
90           goto fail;
91         }
92       if (!parse_format_specifier (lexer, &f)
93           || !fmt_check_output (&f)
94           || !fmt_check_type_compat (&f, VAR_NUMERIC))
95         goto fail;
96
97       if (!lex_match (lexer, ')'))
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             var_set_print_format (v[i], &f);
107           if (which & FORMATS_WRITE)
108             var_set_write_format (v[i], &f);
109         }
110       free (v);
111       v = NULL;
112     }
113   return CMD_SUCCESS;
114
115 fail:
116   free (v);
117   return CMD_FAILURE;
118 }