1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2006, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
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.
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.
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/>. */
22 #include "data/dataset.h"
23 #include "data/value.h"
24 #include "language/command.h"
25 #include "language/commands/data-writer.h"
26 #include "language/commands/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"
32 #include "gl/xalloc.h"
35 #define _(msgid) gettext (msgid)
37 /* PRINT SPACE transformation. */
38 struct print_space_trns
40 struct dfm_writer *writer; /* Output data file. */
41 struct expression *expr; /* Number of lines; NULL means 1. */
42 struct msg_location *expr_location;
45 static const struct trns_class print_space_class;
48 cmd_print_space (struct lexer *lexer, struct dataset *ds)
50 struct file_handle *handle = NULL;
51 struct expression *expr = NULL;
52 struct msg_location *expr_location = NULL;
53 char *encoding = NULL;
55 if (lex_match_id (lexer, "OUTFILE"))
57 lex_match (lexer, T_EQUALS);
59 handle = fh_parse (lexer, FH_REF_FILE, NULL);
63 if (lex_match_id (lexer, "ENCODING"))
65 lex_match (lexer, T_EQUALS);
66 if (!lex_force_string (lexer))
69 encoding = ss_xstrdup (lex_tokss (lexer));
77 if (lex_token (lexer) != T_ENDCMD)
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);
86 if (lex_token (lexer) != T_ENDCMD)
88 lex_error (lexer, _("Syntax error expecting end of command."));
95 struct dfm_writer *writer = NULL;
98 writer = dfm_open_writer (handle, encoding);
103 struct print_space_trns *trns = xmalloc (sizeof *trns);
104 *trns = (struct print_space_trns) {
107 .expr_location = expr_location,
110 add_transformation (ds, &print_space_class, trns);
116 msg_location_destroy (expr_location);
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)
128 struct print_space_trns *trns = t_;
134 double f = expr_evaluate_num (trns->expr, *c, case_num);
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);
147 if (trns->writer == NULL)
148 output_log ("%s", "");
150 dfm_put_record (trns->writer, " ", 1); /* XXX */
152 if (trns->writer != NULL && dfm_write_error (trns->writer))
154 return TRNS_CONTINUE;
157 /* Frees a PRINT SPACE transformation.
158 Returns true if successful, false if an I/O error occurred. */
160 print_space_trns_free (void *trns_)
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);
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,