b3cb17692895b01c3132e9880eb079a4ba34d861
[pspp-builds.git] / src / language / data-io / print-space.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2006 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    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, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include <limits.h>
22 #include <stdlib.h>
23
24 #include <data/procedure.h>
25 #include <data/value.h>
26 #include <language/command.h>
27 #include <language/data-io/data-writer.h>
28 #include <language/data-io/file-handle.h>
29 #include <language/expressions/public.h>
30 #include <language/lexer/lexer.h>
31 #include <output/manager.h>
32 #include <libpspp/message.h>
33
34 #include "xalloc.h"
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38
39 /* PRINT SPACE transformation. */
40 struct print_space_trns
41   {
42     struct dfm_writer *writer;  /* Output data file. */
43     struct expression *expr;    /* Number of lines; NULL means 1. */
44   };
45
46 static trns_proc_func print_space_trns_proc;
47 static trns_free_func print_space_trns_free;
48
49 int
50 cmd_print_space (struct lexer *lexer, struct dataset *ds)
51 {
52   struct print_space_trns *trns;
53   struct file_handle *handle;
54   struct expression *expr;
55   struct dfm_writer *writer;
56
57   if (lex_match_id (lexer, "OUTFILE"))
58     {
59       lex_match (lexer, '=');
60
61       handle = fh_parse (lexer, FH_REF_FILE);
62       if (handle == NULL)
63         return CMD_FAILURE;
64       lex_get (lexer);
65     }
66   else
67     handle = NULL;
68
69   if (lex_token (lexer) != '.')
70     {
71       expr = expr_parse (lexer, ds, EXPR_NUMBER);
72       if (lex_token (lexer) != '.')
73         {
74           expr_free (expr);
75           lex_error (lexer, _("expecting end of command"));
76           return CMD_FAILURE;
77         }
78     }
79   else
80     expr = NULL;
81
82   if (handle != NULL)
83     {
84       writer = dfm_open_writer (handle);
85       if (writer == NULL)
86         {
87           expr_free (expr);
88           return CMD_FAILURE;
89         }
90     }
91   else
92     writer = NULL;
93
94   trns = xmalloc (sizeof *trns);
95   trns->writer = writer;
96   trns->expr = expr;
97
98   add_transformation (ds,
99                       print_space_trns_proc, print_space_trns_free, trns);
100   return CMD_SUCCESS;
101 }
102
103 /* Executes a PRINT SPACE transformation. */
104 static int
105 print_space_trns_proc (void *t_, struct ccase *c,
106                        casenumber case_num UNUSED)
107 {
108   struct print_space_trns *trns = t_;
109   int n;
110
111   n = 1;
112   if (trns->expr)
113     {
114       double f = expr_evaluate_num (trns->expr, c, case_num);
115       if (f == SYSMIS)
116         msg (SW, _("The expression on PRINT SPACE evaluated to the "
117                    "system-missing value."));
118       else if (f < 0 || f > INT_MAX)
119         msg (SW, _("The expression on PRINT SPACE evaluated to %g."), f);
120       else
121         n = f;
122     }
123
124   while (n--)
125     if (trns->writer == NULL)
126       som_blank_line ();
127     else
128       dfm_put_record (trns->writer, " ", 1);
129
130   if (trns->writer != NULL && dfm_write_error (trns->writer))
131     return TRNS_ERROR;
132   return TRNS_CONTINUE;
133 }
134
135 /* Frees a PRINT SPACE transformation.
136    Returns true if successful, false if an I/O error occurred. */
137 static bool
138 print_space_trns_free (void *trns_)
139 {
140   struct print_space_trns *trns = trns_;
141   bool ok = dfm_close_writer (trns->writer);
142   expr_free (trns->expr);
143   free (trns);
144   return ok;
145 }