Change license from GPLv2+ to GPLv3+.
[pspp-builds.git] / src / language / data-io / print-space.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2006 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/procedure.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 <output/manager.h>
30 #include <libpspp/message.h>
31
32 #include "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 trns_proc_func print_space_trns_proc;
45 static trns_free_func print_space_trns_free;
46
47 int
48 cmd_print_space (struct lexer *lexer, struct dataset *ds)
49 {
50   struct print_space_trns *trns;
51   struct file_handle *handle;
52   struct expression *expr;
53   struct dfm_writer *writer;
54
55   if (lex_match_id (lexer, "OUTFILE"))
56     {
57       lex_match (lexer, '=');
58
59       handle = fh_parse (lexer, FH_REF_FILE);
60       if (handle == NULL)
61         return CMD_FAILURE;
62       lex_get (lexer);
63     }
64   else
65     handle = NULL;
66
67   if (lex_token (lexer) != '.')
68     {
69       expr = expr_parse (lexer, ds, EXPR_NUMBER);
70       if (lex_token (lexer) != '.')
71         {
72           expr_free (expr);
73           lex_error (lexer, _("expecting end of command"));
74           return CMD_FAILURE;
75         }
76     }
77   else
78     expr = NULL;
79
80   if (handle != NULL)
81     {
82       writer = dfm_open_writer (handle);
83       if (writer == NULL)
84         {
85           expr_free (expr);
86           return CMD_FAILURE;
87         }
88     }
89   else
90     writer = NULL;
91
92   trns = xmalloc (sizeof *trns);
93   trns->writer = writer;
94   trns->expr = expr;
95
96   add_transformation (ds,
97                       print_space_trns_proc, print_space_trns_free, trns);
98   return CMD_SUCCESS;
99 }
100
101 /* Executes a PRINT SPACE transformation. */
102 static int
103 print_space_trns_proc (void *t_, struct ccase *c,
104                        casenumber case_num UNUSED)
105 {
106   struct print_space_trns *trns = t_;
107   int n;
108
109   n = 1;
110   if (trns->expr)
111     {
112       double f = expr_evaluate_num (trns->expr, c, case_num);
113       if (f == SYSMIS)
114         msg (SW, _("The expression on PRINT SPACE evaluated to the "
115                    "system-missing value."));
116       else if (f < 0 || f > INT_MAX)
117         msg (SW, _("The expression on PRINT SPACE evaluated to %g."), f);
118       else
119         n = f;
120     }
121
122   while (n--)
123     if (trns->writer == NULL)
124       som_blank_line ();
125     else
126       dfm_put_record (trns->writer, " ", 1);
127
128   if (trns->writer != NULL && dfm_write_error (trns->writer))
129     return TRNS_ERROR;
130   return TRNS_CONTINUE;
131 }
132
133 /* Frees a PRINT SPACE transformation.
134    Returns true if successful, false if an I/O error occurred. */
135 static bool
136 print_space_trns_free (void *trns_)
137 {
138   struct print_space_trns *trns = trns_;
139   bool ok = dfm_close_writer (trns->writer);
140   expr_free (trns->expr);
141   free (trns);
142   return ok;
143 }