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