1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-2004 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA
21 #include "dfm-write.h"
27 #include "file-handle.h"
31 /* Data file writer. */
34 struct file_handle *fh; /* File handle. */
35 struct file_ext file; /* Associated file. */
36 char *bounce; /* Bounce buffer for fixed-size fields. */
39 /* Opens a file handle for writing as a data file. */
41 dfm_open_writer (struct file_handle *fh)
46 aux = fh_open (fh, "data file", "ws");
52 w = *aux = xmalloc (sizeof *w);
57 w->file.filename = xstrdup (handle_get_filename (w->fh));
60 w->file.sequence_no = NULL;
62 w->file.postopen = NULL;
63 w->file.preclose = NULL;
65 if (!fn_open_ext (&w->file))
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));
81 /* Writes record REC having length LEN to the file corresponding to
82 HANDLE. REC is not null-terminated. Returns nonzero on success,
85 dfm_put_record (struct dfm_writer *w, const char *rec, size_t len)
89 if (handle_get_mode (w->fh) == MODE_BINARY
90 && len < handle_get_record_width (w->fh))
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);
101 if (fwrite (rec, len, 1, w->file.file) != 1)
103 msg (ME, _("Error writing file %s: %s."),
104 handle_get_name (w->fh), strerror (errno));
112 /* Closes data file writer W. */
114 dfm_close_writer (struct dfm_writer *w)
116 if (fh_close (w->fh, "data file", "ws"))
121 fn_close_ext (&w->file);
122 free (w->file.filename);
123 w->file.filename = NULL;