a90f46e8b8efe6f4e4ce9f7efa9ca2b27572f450
[pspp-builds.git] / src / language / dictionary / formats.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    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, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include <data/procedure.h>
26 #include <data/variable.h>
27 #include <language/command.h>
28 #include <language/lexer/format-parser.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 (struct lexer *, struct dataset *ds, int);
45
46 int
47 cmd_print_formats (struct lexer *lexer, struct dataset *ds)
48 {
49   return internal_cmd_formats (lexer, ds, FORMATS_PRINT);
50 }
51
52 int
53 cmd_write_formats (struct lexer *lexer, struct dataset *ds)
54 {
55   return internal_cmd_formats (lexer, ds, FORMATS_WRITE);
56 }
57
58 int
59 cmd_formats (struct lexer *lexer, struct dataset *ds)
60 {
61   return internal_cmd_formats (lexer, ds, FORMATS_PRINT | FORMATS_WRITE);
62 }
63
64 static int
65 internal_cmd_formats (struct lexer *lexer, struct dataset *ds, 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 (lex_token (lexer) == '.')
83         break;
84
85       if (!parse_variables (lexer, dataset_dict (ds), &v, &cv, PV_NUMERIC))
86         return CMD_FAILURE;
87       type = var_get_type (v[0]);
88
89       if (!lex_match (lexer, '('))
90         {
91           msg (SE, _("`(' expected after variable list."));
92           goto fail;
93         }
94       if (!parse_format_specifier (lexer, &f)
95           || !fmt_check_output (&f)
96           || !fmt_check_type_compat (&f, VAR_NUMERIC))
97         goto fail;
98
99       if (!lex_match (lexer, ')'))
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             var_set_print_format (v[i], &f);
109           if (which & FORMATS_WRITE)
110             var_set_write_format (v[i], &f);
111         }
112       free (v);
113       v = NULL;
114     }
115   return CMD_SUCCESS;
116
117 fail:
118   free (v);
119   return CMD_FAILURE;
120 }