f077e9c39573e30760303704d0c356e95895bbf7
[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   };
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 = NULL;
52   struct expression *expr = NULL;
53   struct dfm_writer *writer;
54   char *encoding = NULL;
55
56   if (lex_match_id (lexer, "OUTFILE"))
57     {
58       lex_match (lexer, T_EQUALS);
59
60       handle = fh_parse (lexer, FH_REF_FILE, NULL);
61       if (handle == NULL)
62         return CMD_FAILURE;
63
64       if (lex_match_id (lexer, "ENCODING"))
65         {
66           lex_match (lexer, T_EQUALS);
67           if (!lex_force_string (lexer))
68             goto error;
69
70           encoding = ss_xstrdup (lex_tokss (lexer));
71
72           lex_get (lexer);
73         }
74     }
75   else
76     handle = NULL;
77
78   if (lex_token (lexer) != T_ENDCMD)
79     {
80       expr = expr_parse (lexer, NULL, ds, VAL_NUMERIC);
81       if (lex_token (lexer) != T_ENDCMD)
82         {
83           lex_error (lexer, _("expecting end of command"));
84           goto error;
85         }
86     }
87   else
88     expr = NULL;
89
90   if (handle != NULL)
91     {
92       writer = dfm_open_writer (handle, encoding);
93       if (writer == NULL)
94         goto error;
95     }
96   else
97     writer = NULL;
98
99   trns = xmalloc (sizeof *trns);
100   trns->writer = writer;
101   trns->expr = expr;
102
103   add_transformation (ds,
104                       print_space_trns_proc, print_space_trns_free, trns);
105   fh_unref (handle);
106   return CMD_SUCCESS;
107
108 error:
109   fh_unref (handle);
110   expr_free (expr);
111   return CMD_FAILURE;
112 }
113
114 /* Executes a PRINT SPACE transformation. */
115 static int
116 print_space_trns_proc (void *t_, struct ccase **c,
117                        casenumber case_num UNUSED)
118 {
119   struct print_space_trns *trns = t_;
120   int n;
121
122   n = 1;
123   if (trns->expr)
124     {
125       double f = expr_evaluate_num (trns->expr, *c, case_num);
126       if (f == SYSMIS)
127         msg (SW, _("The expression on %s evaluated to the "
128                    "system-missing value."), "PRINT SPACE");
129       else if (f < 0 || f > INT_MAX)
130         msg (SW, _("The expression on %s evaluated to %g."), "PRINT SPACE", f);
131       else
132         n = f;
133     }
134
135   while (n--)
136     if (trns->writer == NULL)
137       output_log ("%s", "");
138     else
139       dfm_put_record (trns->writer, " ", 1); /* XXX */
140
141   if (trns->writer != NULL && dfm_write_error (trns->writer))
142     return TRNS_ERROR;
143   return TRNS_CONTINUE;
144 }
145
146 /* Frees a PRINT SPACE transformation.
147    Returns true if successful, false if an I/O error occurred. */
148 static bool
149 print_space_trns_free (void *trns_)
150 {
151   struct print_space_trns *trns = trns_;
152   bool ok = dfm_close_writer (trns->writer);
153   expr_free (trns->expr);
154   free (trns);
155   return ok;
156 }