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)
38 #define N_(msgid) (msgid)
40 /* Data file writer. */
43 struct file_handle *fh; /* File handle. */
44 struct fh_lock *lock; /* Exclusive access to file. */
45 FILE *file; /* Associated file. */
46 struct replace_file *rf; /* Atomic file replacement support. */
49 /* Opens a file handle for writing as a data file. */
51 dfm_open_writer (struct file_handle *fh)
56 lock = fh_lock (fh, FH_REF_FILE, N_("data file"), FH_ACC_WRITE, false);
60 w = fh_lock_get_aux (lock);
64 w = xmalloc (sizeof *w);
67 w->rf = replace_file_start (fh_get_file_name (w->fh), "wb",
68 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
69 | S_IROTH | S_IWOTH), &w->file, NULL);
72 msg (ME, _("An error occurred while opening \"%s\" for writing "
73 "as a data file: %s."),
74 fh_get_file_name (w->fh), strerror (errno));
78 fh_lock_set_aux (lock, w);
83 /* Returns false if an I/O error occurred on WRITER, true otherwise. */
85 dfm_write_error (const struct dfm_writer *writer)
87 return ferror (writer->file);
90 /* Writes record REC (which need not be null-terminated) having
91 length LEN to the file corresponding to HANDLE. Adds any
92 needed formatting, such as a trailing new-line. Returns true
93 on success, false on failure. */
95 dfm_put_record (struct dfm_writer *w, const char *rec, size_t len)
99 if (dfm_write_error (w))
102 switch (fh_get_mode (w->fh))
105 fwrite (rec, len, 1, w->file);
106 putc ('\n', w->file);
111 size_t record_width = fh_get_record_width (w->fh);
112 size_t write_bytes = MIN (len, record_width);
113 size_t pad_bytes = record_width - write_bytes;
114 fwrite (rec, write_bytes, 1, w->file);
115 while (pad_bytes > 0)
117 static const char spaces[32] = " ";
118 size_t chunk = MIN (pad_bytes, sizeof spaces);
119 fwrite (spaces, chunk, 1, w->file);
129 return !dfm_write_error (w);
132 /* Closes data file writer W. */
134 dfm_close_writer (struct dfm_writer *w)
140 if (fh_unlock (w->lock))
146 const char *file_name = fh_get_file_name (w->fh);
147 ok = !dfm_write_error (w) && !fn_close (file_name, w->file);
150 msg (ME, _("I/O error occurred writing data file \"%s\"."), file_name);
152 if (ok ? !replace_file_commit (w->rf) : !replace_file_abort (w->rf))