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