Changed a lot of ints to bools.
[pspp-builds.git] / src / language / data-io / data-writer.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-2004 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 #include <language/data-io/data-writer.h>
22 #include <assert.h>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <libpspp/alloc.h>
26 #include <libpspp/message.h>
27 #include <language/data-io/file-handle.h>
28 #include <data/file-name.h>
29 #include <libpspp/str.h>
30
31 #include "gettext.h"
32 #define _(msgid) gettext (msgid)
33
34 /* Data file writer. */
35 struct dfm_writer
36   {
37     struct file_handle *fh;     /* File handle. */
38     FILE *file;                 /* Associated file. */
39     char *bounce;               /* Bounce buffer for fixed-size fields. */
40   };
41
42 /* Opens a file handle for writing as a data file. */
43 struct dfm_writer *
44 dfm_open_writer (struct file_handle *fh)
45 {
46   struct dfm_writer *w;
47   void **aux;
48   
49   aux = fh_open (fh, FH_REF_FILE, "data file", "ws");
50   if (aux == NULL)
51     return NULL;
52   if (*aux != NULL)
53     return *aux;
54
55   w = *aux = xmalloc (sizeof *w);
56   w->fh = fh;
57   w->file = fn_open (fh_get_file_name (w->fh), "wb");
58   w->bounce = NULL;
59
60   if (w->file == NULL)
61     {
62       msg (ME, _("An error occurred while opening \"%s\" for writing "
63                  "as a data file: %s."),
64            fh_get_file_name (w->fh), strerror (errno));
65       goto error;
66     }
67
68   return w;
69
70  error:
71   dfm_close_writer (w);
72   return NULL;
73 }
74
75 /* Returns false if an I/O error occurred on WRITER, true otherwise. */
76 bool
77 dfm_write_error (const struct dfm_writer *writer) 
78 {
79   return ferror (writer->file);
80 }
81
82 /* Writes record REC having length LEN to the file corresponding to
83    HANDLE.  REC is not null-terminated.  Returns true on success,
84    false on failure. */
85 bool
86 dfm_put_record (struct dfm_writer *w, const char *rec, size_t len)
87 {
88   assert (w != NULL);
89
90   if (dfm_write_error (w))
91     return false;
92   
93   if (fh_get_mode (w->fh) == FH_MODE_BINARY
94       && len < fh_get_record_width (w->fh))
95     {
96       size_t rec_width = fh_get_record_width (w->fh);
97       if (w->bounce == NULL)
98         w->bounce = xmalloc (rec_width);
99       memcpy (w->bounce, rec, len);
100       memset (&w->bounce[len], 0, rec_width - len);
101       rec = w->bounce;
102       len = rec_width;
103     }
104
105   fwrite (rec, len, 1, w->file);
106   return !dfm_write_error (w);
107 }
108
109 /* Closes data file writer W. */
110 bool
111 dfm_close_writer (struct dfm_writer *w)
112 {
113   char *file_name;
114   bool ok;
115
116   if (w == NULL)
117     return true;
118   file_name = xstrdup (fh_get_name (w->fh));
119   if (fh_close (w->fh, "data file", "ws"))
120     {
121       free (file_name);
122       return true; 
123     }
124
125   ok = true;
126   if (w->file != NULL)
127     {
128       ok = !dfm_write_error (w) && !fn_close (file_name, w->file);
129
130       if (!ok)
131         msg (ME, _("I/O error occurred writing data file \"%s\"."), file_name);
132     }
133   free (w->bounce);
134   free (w);
135   free (file_name);
136
137   return ok;
138 }