1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-2004, 2006 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include <language/data-io/data-writer.h>
26 #include <data/file-name.h>
27 #include <data/make-file.h>
28 #include <language/data-io/file-handle.h>
29 #include <libpspp/assertion.h>
30 #include <libpspp/message.h>
31 #include <libpspp/str.h>
37 #define _(msgid) gettext (msgid)
39 /* Data file writer. */
42 struct file_handle *fh; /* File handle. */
43 struct fh_lock *lock; /* Exclusive access to file. */
44 FILE *file; /* Associated file. */
45 struct replace_file *rf; /* Atomic file replacement support. */
48 /* Opens a file handle for writing as a data file. */
50 dfm_open_writer (struct file_handle *fh)
55 lock = fh_lock (fh, FH_REF_FILE, "data file", FH_ACC_WRITE, false);
59 w = fh_lock_get_aux (lock);
63 w = xmalloc (sizeof *w);
66 w->rf = replace_file_start (fh_get_file_name (w->fh), "wb",
67 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
68 | S_IROTH | S_IWOTH), &w->file, NULL);
71 msg (ME, _("An error occurred while opening \"%s\" for writing "
72 "as a data file: %s."),
73 fh_get_file_name (w->fh), strerror (errno));
77 fh_lock_set_aux (lock, w);
82 /* Returns false if an I/O error occurred on WRITER, true otherwise. */
84 dfm_write_error (const struct dfm_writer *writer)
86 return ferror (writer->file);
89 /* Writes record REC (which need not be null-terminated) having
90 length LEN to the file corresponding to HANDLE. Adds any
91 needed formatting, such as a trailing new-line. Returns true
92 on success, false on failure. */
94 dfm_put_record (struct dfm_writer *w, const char *rec, size_t len)
98 if (dfm_write_error (w))
101 switch (fh_get_mode (w->fh))
104 fwrite (rec, len, 1, w->file);
105 putc ('\n', w->file);
110 size_t record_width = fh_get_record_width (w->fh);
111 size_t write_bytes = MIN (len, record_width);
112 size_t pad_bytes = record_width - write_bytes;
113 fwrite (rec, write_bytes, 1, w->file);
114 while (pad_bytes > 0)
116 static const char spaces[32] = " ";
117 size_t chunk = MIN (pad_bytes, sizeof spaces);
118 fwrite (spaces, chunk, 1, w->file);
128 return !dfm_write_error (w);
131 /* Closes data file writer W. */
133 dfm_close_writer (struct dfm_writer *w)
139 if (fh_unlock (w->lock))
145 const char *file_name = fh_get_file_name (w->fh);
146 ok = !dfm_write_error (w) && !fn_close (file_name, w->file);
149 msg (ME, _("I/O error occurred writing data file \"%s\"."), file_name);
151 if (ok ? !replace_file_commit (w->rf) : !replace_file_abort (w->rf))