Change some aspects of the PRINT, PRINT EJECT, and WRITE
[pspp-builds.git] / src / language / data-io / data-writer.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-2004, 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 <language/data-io/data-writer.h>
23
24 #include <assert.h>
25 #include <errno.h>
26 #include <stdlib.h>
27
28 #include <data/file-name.h>
29 #include <language/data-io/file-handle.h>
30 #include <libpspp/alloc.h>
31 #include <libpspp/assertion.h>
32 #include <libpspp/message.h>
33 #include <libpspp/str.h>
34
35 #include "minmax.h"
36
37 #include "gettext.h"
38 #define _(msgid) gettext (msgid)
39
40 /* Data file writer. */
41 struct dfm_writer
42   {
43     struct file_handle *fh;     /* File handle. */
44     FILE *file;                 /* Associated file. */
45   };
46
47 /* Opens a file handle for writing as a data file. */
48 struct dfm_writer *
49 dfm_open_writer (struct file_handle *fh)
50 {
51   struct dfm_writer *w;
52   void **aux;
53   
54   aux = fh_open (fh, FH_REF_FILE, "data file", "ws");
55   if (aux == NULL)
56     return NULL;
57   if (*aux != NULL)
58     return *aux;
59
60   w = *aux = xmalloc (sizeof *w);
61   w->fh = fh;
62   w->file = fn_open (fh_get_file_name (w->fh), "wb");
63
64   if (w->file == NULL)
65     {
66       msg (ME, _("An error occurred while opening \"%s\" for writing "
67                  "as a data file: %s."),
68            fh_get_file_name (w->fh), strerror (errno));
69       goto error;
70     }
71
72   return w;
73
74  error:
75   dfm_close_writer (w);
76   return NULL;
77 }
78
79 /* Returns false if an I/O error occurred on WRITER, true otherwise. */
80 bool
81 dfm_write_error (const struct dfm_writer *writer) 
82 {
83   return ferror (writer->file);
84 }
85
86 /* Writes record REC (which need not be null-terminated) having
87    length LEN to the file corresponding to HANDLE.  Adds any
88    needed formatting, such as a trailing new-line.  Returns true
89    on success, false on failure. */
90 bool
91 dfm_put_record (struct dfm_writer *w, const char *rec, size_t len)
92 {
93   assert (w != NULL);
94
95   if (dfm_write_error (w))
96     return false;
97
98   switch (fh_get_mode (w->fh)) 
99     {
100     case FH_MODE_TEXT:
101       fwrite (rec, len, 1, w->file);
102       putc ('\n', w->file);
103       break;
104
105     case FH_MODE_BINARY:
106       {
107         size_t record_width = fh_get_record_width (w->fh);
108         size_t write_bytes = MIN (len, record_width);
109         size_t pad_bytes = record_width - write_bytes;
110         fwrite (rec, write_bytes, 1, w->file);
111         while (pad_bytes > 0) 
112           {
113             static const char spaces[32] = "                                ";
114             size_t chunk = MIN (pad_bytes, sizeof spaces);
115             fwrite (spaces, chunk, 1, w->file);
116             pad_bytes -= chunk;
117           }
118       }
119       break;
120
121     default:
122       NOT_REACHED ();
123     }
124
125   return !dfm_write_error (w);
126 }
127
128 /* Closes data file writer W. */
129 bool
130 dfm_close_writer (struct dfm_writer *w)
131 {
132   char *file_name;
133   bool ok;
134
135   if (w == NULL)
136     return true;
137   file_name = xstrdup (fh_get_name (w->fh));
138   if (fh_close (w->fh, "data file", "ws"))
139     {
140       free (file_name);
141       return true; 
142     }
143
144   ok = true;
145   if (w->file != NULL)
146     {
147       ok = !dfm_write_error (w) && !fn_close (file_name, w->file);
148
149       if (!ok)
150         msg (ME, _("I/O error occurred writing data file \"%s\"."), file_name);
151     }
152   free (w);
153   free (file_name);
154
155   return ok;
156 }