e971ea1110734c98554d8144830bc562601edc0e
[pspp-builds.git] / src / dfm-write.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 "dfm-write.h"
22 #include <assert.h>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include "alloc.h"
26 #include "error.h"
27 #include "file-handle.h"
28 #include "filename.h"
29 #include "str.h"
30
31 /* Data file writer. */
32 struct dfm_writer
33   {
34     struct file_handle *fh;     /* File handle. */
35     struct file_ext file;       /* Associated file. */
36     char *bounce;               /* Bounce buffer for fixed-size fields. */
37   };
38
39 /* Opens a file handle for writing as a data file. */
40 struct dfm_writer *
41 dfm_open_writer (struct file_handle *fh)
42 {
43   struct dfm_writer *w;
44   void **aux;
45   
46   aux = fh_open (fh, "data file", "ws");
47   if (aux == NULL)
48     return NULL;
49   if (*aux != NULL)
50     return *aux;
51
52   w = *aux = xmalloc (sizeof *w);
53   w->fh = fh;
54   w->file.file = NULL;
55   w->bounce = NULL;
56
57   w->file.filename = xstrdup (handle_get_filename (w->fh));
58   w->file.mode = "wb";
59   w->file.file = NULL;
60   w->file.sequence_no = NULL;
61   w->file.param = NULL;
62   w->file.postopen = NULL;
63   w->file.preclose = NULL;
64       
65   if (!fn_open_ext (&w->file))
66     {
67       msg (ME, _("An error occurred while opening \"%s\" for writing "
68                  "as a data file: %s."),
69            handle_get_filename (w->fh), strerror (errno));
70       goto error;
71     }
72
73   return w;
74
75  error:
76   err_cond_fail ();
77   dfm_close_writer (w);
78   return NULL;
79 }
80
81 /* Writes record REC having length LEN to the file corresponding to
82    HANDLE.  REC is not null-terminated.  Returns nonzero on success,
83    zero on failure. */
84 int
85 dfm_put_record (struct dfm_writer *w, const char *rec, size_t len)
86 {
87   assert (w != NULL);
88
89   if (handle_get_mode (w->fh) == MODE_BINARY
90       && len < handle_get_record_width (w->fh))
91     {
92       size_t rec_width = handle_get_record_width (w->fh);
93       if (w->bounce == NULL)
94         w->bounce = xmalloc (rec_width);
95       memcpy (w->bounce, rec, len);
96       memset (&w->bounce[len], 0, rec_width - len);
97       rec = w->bounce;
98       len = rec_width;
99     }
100
101   if (fwrite (rec, len, 1, w->file.file) != 1)
102     {
103       msg (ME, _("Error writing file %s: %s."),
104            handle_get_name (w->fh), strerror (errno));
105       err_cond_fail ();
106       return 0;
107     }
108
109   return 1;
110 }
111
112 /* Closes data file writer W. */
113 void
114 dfm_close_writer (struct dfm_writer *w)
115 {
116   if (fh_close (w->fh, "data file", "ws"))
117     return;
118   
119   if (w->file.file)
120     {
121       fn_close_ext (&w->file);
122       free (w->file.filename);
123       w->file.filename = NULL;
124     }
125   free (w->bounce);
126   free (w);
127 }