Remove unneeded #includes.
[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     struct msg_location *expr_location;
43   };
44
45 static const struct trns_class print_space_class;
46
47 int
48 cmd_print_space (struct lexer *lexer, struct dataset *ds)
49 {
50   struct file_handle *handle = NULL;
51   struct expression *expr = NULL;
52   struct msg_location *expr_location = NULL;
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       int start_ofs = lex_ofs (lexer);
80       expr = expr_parse (lexer, ds, VAL_NUMERIC);
81       int end_ofs = lex_ofs (lexer) - 1;
82       expr_location = lex_ofs_location (lexer, start_ofs, end_ofs);
83       if (!expr)
84         goto error;
85
86       if (lex_token (lexer) != T_ENDCMD)
87         {
88           lex_error (lexer, _("Syntax error expecting end of command."));
89           goto error;
90         }
91     }
92   else
93     expr = NULL;
94
95   struct dfm_writer *writer = NULL;
96   if (handle != NULL)
97     {
98       writer = dfm_open_writer (handle, encoding);
99       if (writer == NULL)
100         goto error;
101     }
102
103   struct print_space_trns *trns = xmalloc (sizeof *trns);
104   *trns = (struct print_space_trns) {
105     .writer = writer,
106     .expr = expr,
107     .expr_location = expr_location,
108   };
109
110   add_transformation (ds, &print_space_class, trns);
111   fh_unref (handle);
112   free (encoding);
113   return CMD_SUCCESS;
114
115 error:
116   msg_location_destroy (expr_location);
117   fh_unref (handle);
118   expr_free (expr);
119   free (encoding);
120   return CMD_FAILURE;
121 }
122
123 /* Executes a PRINT SPACE transformation. */
124 static enum trns_result
125 print_space_trns_proc (void *t_, struct ccase **c,
126                        casenumber case_num UNUSED)
127 {
128   struct print_space_trns *trns = t_;
129   int n;
130
131   n = 1;
132   if (trns->expr)
133     {
134       double f = expr_evaluate_num (trns->expr, *c, case_num);
135       if (f == SYSMIS)
136         msg_at (SW, trns->expr_location,
137                 _("The expression on %s evaluated to the "
138                   "system-missing value."), "PRINT SPACE");
139       else if (f < 0 || f > INT_MAX)
140         msg_at (SW, trns->expr_location,
141                 _("The expression on %s evaluated to %g."), "PRINT SPACE", f);
142       else
143         n = f;
144     }
145
146   while (n--)
147     if (trns->writer == NULL)
148       output_log ("%s", "");
149     else
150       dfm_put_record (trns->writer, " ", 1); /* XXX */
151
152   if (trns->writer != NULL && dfm_write_error (trns->writer))
153     return TRNS_ERROR;
154   return TRNS_CONTINUE;
155 }
156
157 /* Frees a PRINT SPACE transformation.
158    Returns true if successful, false if an I/O error occurred. */
159 static bool
160 print_space_trns_free (void *trns_)
161 {
162   struct print_space_trns *trns = trns_;
163   bool ok = dfm_close_writer (trns->writer);
164   expr_free (trns->expr);
165   msg_location_destroy (trns->expr_location);
166   free (trns);
167   return ok;
168 }
169
170 static const struct trns_class print_space_class = {
171   .name = "PRINT SPACE",
172   .execute = print_space_trns_proc,
173   .destroy = print_space_trns_free,
174 };