Clean up how transformations work.
[pspp] / src / language / data-io / print-space.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2006, 2009, 2010, 2011, 2012 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 <stdlib.h>
21
22 #include "data/dataset.h"
23 #include "data/value.h"
24 #include "language/command.h"
25 #include "language/data-io/data-writer.h"
26 #include "language/data-io/file-handle.h"
27 #include "language/expressions/public.h"
28 #include "language/lexer/lexer.h"
29 #include "libpspp/message.h"
30 #include "output/driver.h"
31
32 #include "gl/xalloc.h"
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37 /* PRINT SPACE transformation. */
38 struct print_space_trns
39   {
40     struct dfm_writer *writer;  /* Output data file. */
41     struct expression *expr;    /* Number of lines; NULL means 1. */
42   };
43
44 static const struct trns_class print_space_class;
45
46 int
47 cmd_print_space (struct lexer *lexer, struct dataset *ds)
48 {
49   struct print_space_trns *trns;
50   struct file_handle *handle = NULL;
51   struct expression *expr = NULL;
52   struct dfm_writer *writer;
53   char *encoding = NULL;
54
55   if (lex_match_id (lexer, "OUTFILE"))
56     {
57       lex_match (lexer, T_EQUALS);
58
59       handle = fh_parse (lexer, FH_REF_FILE, NULL);
60       if (handle == NULL)
61         return CMD_FAILURE;
62
63       if (lex_match_id (lexer, "ENCODING"))
64         {
65           lex_match (lexer, T_EQUALS);
66           if (!lex_force_string (lexer))
67             goto error;
68
69           encoding = ss_xstrdup (lex_tokss (lexer));
70
71           lex_get (lexer);
72         }
73     }
74   else
75     handle = NULL;
76
77   if (lex_token (lexer) != T_ENDCMD)
78     {
79       expr = expr_parse (lexer, ds, VAL_NUMERIC);
80       if (lex_token (lexer) != T_ENDCMD)
81         {
82           lex_error (lexer, _("expecting end of command"));
83           goto error;
84         }
85     }
86   else
87     expr = NULL;
88
89   if (handle != NULL)
90     {
91       writer = dfm_open_writer (handle, encoding);
92       if (writer == NULL)
93         goto error;
94     }
95   else
96     writer = NULL;
97
98   trns = xmalloc (sizeof *trns);
99   trns->writer = writer;
100   trns->expr = expr;
101
102   add_transformation (ds, &print_space_class, trns);
103   fh_unref (handle);
104   return CMD_SUCCESS;
105
106 error:
107   fh_unref (handle);
108   expr_free (expr);
109   return CMD_FAILURE;
110 }
111
112 /* Executes a PRINT SPACE transformation. */
113 static enum trns_result
114 print_space_trns_proc (void *t_, struct ccase **c,
115                        casenumber case_num UNUSED)
116 {
117   struct print_space_trns *trns = t_;
118   int n;
119
120   n = 1;
121   if (trns->expr)
122     {
123       double f = expr_evaluate_num (trns->expr, *c, case_num);
124       if (f == SYSMIS)
125         msg (SW, _("The expression on %s evaluated to the "
126                    "system-missing value."), "PRINT SPACE");
127       else if (f < 0 || f > INT_MAX)
128         msg (SW, _("The expression on %s evaluated to %g."), "PRINT SPACE", f);
129       else
130         n = f;
131     }
132
133   while (n--)
134     if (trns->writer == NULL)
135       output_log ("%s", "");
136     else
137       dfm_put_record (trns->writer, " ", 1); /* XXX */
138
139   if (trns->writer != NULL && dfm_write_error (trns->writer))
140     return TRNS_ERROR;
141   return TRNS_CONTINUE;
142 }
143
144 /* Frees a PRINT SPACE transformation.
145    Returns true if successful, false if an I/O error occurred. */
146 static bool
147 print_space_trns_free (void *trns_)
148 {
149   struct print_space_trns *trns = trns_;
150   bool ok = dfm_close_writer (trns->writer);
151   expr_free (trns->expr);
152   free (trns);
153   return ok;
154 }
155
156 static const struct trns_class print_space_class = {
157   .name = "PRINT SPACE",
158   .execute = print_space_trns_proc,
159   .destroy = print_space_trns_free,
160 };