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