Added new files resulting from directory restructuring.
[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 "data-writer.h"
22 #include <assert.h>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include "alloc.h"
26 #include "message.h"
27 #include "file-handle.h"
28 #include "filename.h"
29 #include "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     struct file_ext 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.file = NULL;
58   w->bounce = NULL;
59
60   w->file.filename = xstrdup (fh_get_filename (w->fh));
61   w->file.mode = "wb";
62   w->file.file = NULL;
63   w->file.sequence_no = NULL;
64   w->file.param = NULL;
65   w->file.postopen = NULL;
66   w->file.preclose = NULL;
67       
68   if (!fn_open_ext (&w->file))
69     {
70       msg (ME, _("An error occurred while opening \"%s\" for writing "
71                  "as a data file: %s."),
72            fh_get_filename (w->fh), strerror (errno));
73       goto error;
74     }
75
76   return w;
77
78  error:
79   dfm_close_writer (w);
80   return NULL;
81 }
82
83 /* Returns false if an I/O error occurred on WRITER, true otherwise. */
84 bool
85 dfm_write_error (const struct dfm_writer *writer) 
86 {
87   return ferror (writer->file.file);
88 }
89
90 /* Writes record REC having length LEN to the file corresponding to
91    HANDLE.  REC is not null-terminated.  Returns nonzero on success,
92    zero on failure. */
93 int
94 dfm_put_record (struct dfm_writer *w, const char *rec, size_t len)
95 {
96   assert (w != NULL);
97
98   if (dfm_write_error (w))
99     return 0;
100   
101   if (fh_get_mode (w->fh) == FH_MODE_BINARY
102       && len < fh_get_record_width (w->fh))
103     {
104       size_t rec_width = fh_get_record_width (w->fh);
105       if (w->bounce == NULL)
106         w->bounce = xmalloc (rec_width);
107       memcpy (w->bounce, rec, len);
108       memset (&w->bounce[len], 0, rec_width - len);
109       rec = w->bounce;
110       len = rec_width;
111     }
112
113   fwrite (rec, len, 1, w->file.file);
114   return !dfm_write_error (w);
115 }
116
117 /* Closes data file writer W. */
118 bool
119 dfm_close_writer (struct dfm_writer *w)
120 {
121   bool ok;
122
123   if (w == NULL)
124     return true;
125   if (fh_close (w->fh, "data file", "ws"))
126     return true;
127
128   ok = true;
129   if (w->file.file != NULL)
130     {
131       ok = !dfm_write_error (w);
132       if (!fn_close_ext (&w->file))
133         ok = false;
134
135       if (!ok)
136         msg (ME, _("I/O error occurred writing data file \"%s\"."),
137              fh_get_filename (w->fh));
138
139       free (w->file.filename);
140       w->file.filename = NULL;
141     }
142   free (w->bounce);
143   free (w);
144
145   return ok;
146 }