X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Fsys-file-writer.c;h=af0ead79b33af64037b97b736920f16a005cccff;hb=refs%2Fheads%2Ffbuf;hp=df5108e2a062758ae79d561f5d1ee51b29a1c152;hpb=3db83b515247dca69abbf7ad05d3dbdfec4b524c;p=pspp diff --git a/src/data/sys-file-writer.c b/src/data/sys-file-writer.c index df5108e2a0..af0ead79b3 100644 --- a/src/data/sys-file-writer.c +++ b/src/data/sys-file-writer.c @@ -41,6 +41,7 @@ #include "data/short-names.h" #include "data/value-labels.h" #include "data/variable.h" +#include "libpspp/fbuf.h" #include "libpspp/float-format.h" #include "libpspp/i18n.h" #include "libpspp/integer-format.h" @@ -69,7 +70,7 @@ struct sfm_writer { struct file_handle *fh; /* File handle. */ struct fh_lock *lock; /* Mutual exclusion for file. */ - FILE *file; /* File stream. */ + struct fbuf *fbuf; /* File stream. */ struct replace_file *rf; /* Ticket for replacing output file. */ enum any_compression compression; @@ -216,7 +217,7 @@ sfm_open_writer (struct file_handle *fh, struct dictionary *d, w = xzalloc (sizeof *w); w->fh = fh_ref (fh); w->lock = NULL; - w->file = NULL; + w->fbuf = NULL; w->rf = NULL; /* Use the requested compression, except that no EBCDIC-based ZLIB compressed @@ -250,13 +251,16 @@ sfm_open_writer (struct file_handle *fh, struct dictionary *d, mode = 0444; if (opts.create_writeable) mode |= 0222; - w->rf = replace_file_start (fh, "wb", mode, &w->file); + + int fd; + w->rf = replace_file_start_fd (fh, true, mode, &fd); if (w->rf == NULL) { msg (ME, _("Error opening `%s' for writing as a system file: %s."), fh_get_file_name (fh), strerror (errno)); goto error; } + w->fbuf = fbuf_open_fd (fd); get_encoding_info (&encoding_info, dict_get_encoding (d)); w->space = encoding_info.space[0]; @@ -309,7 +313,7 @@ sfm_open_writer (struct file_handle *fh, struct dictionary *d, w->zstream.zalloc = Z_NULL; w->zstream.zfree = Z_NULL; w->zstream.opaque = Z_NULL; - w->zstart = ftello (w->file); + w->zstart = fbuf_tell (w->fbuf); write_int64 (w, w->zstart); write_int64 (w, 0); @@ -985,7 +989,7 @@ write_long_string_value_labels (struct sfm_writer *w, write_int (w, 1); /* Data item (byte) size. */ write_int (w, size); /* Number of data items. */ - start = ftello (w->file); + start = fbuf_tell (w->fbuf); for (i = 0; i < n_vars; i++) { struct variable *var = dict_get_var (dict, i); @@ -1022,7 +1026,7 @@ write_long_string_value_labels (struct sfm_writer *w, free (label); } } - assert (ftello (w->file) == start + size); + assert (fbuf_tell (w->fbuf) == start + size); } static void @@ -1058,7 +1062,7 @@ write_long_string_missing_values (struct sfm_writer *w, write_int (w, 1); /* Data item (byte) size. */ write_int (w, size); /* Number of data items. */ - start = ftello (w->file); + start = fbuf_tell (w->fbuf); for (i = 0; i < n_vars; i++) { struct variable *var = dict_get_var (dict, i); @@ -1087,7 +1091,7 @@ write_long_string_missing_values (struct sfm_writer *w, write_bytes (w, value_str (value, width), 8); } } - assert (ftello (w->file) == start + size); + assert (fbuf_tell (w->fbuf) == start + size); } static void @@ -1205,7 +1209,7 @@ sys_file_casewriter_write (struct casewriter *writer, void *w_, { struct sfm_writer *w = w_; - if (ferror (w->file)) + if (fbuf_get_status (w->fbuf) > 0) { casewriter_force_error (writer); case_unref (c); @@ -1235,7 +1239,7 @@ sys_file_casewriter_destroy (struct casewriter *writer, void *w_) static bool write_error (const struct sfm_writer *writer) { - return ferror (writer->file); + return fbuf_get_status (writer->fbuf) > 0; } /* Closes a system file after we're done with it. @@ -1249,7 +1253,7 @@ close_writer (struct sfm_writer *w) return true; ok = true; - if (w->file != NULL) + if (w->fbuf != NULL) { /* Flush buffer. */ flush_compressed (w); @@ -1258,20 +1262,20 @@ close_writer (struct sfm_writer *w) finish_zstream (w); write_ztrailer (w); } - fflush (w->file); + fbuf_flush (w->fbuf); ok = !write_error (w); /* Seek back to the beginning and update the number of cases. This is just a courtesy to later readers, so there's no need to check return values or report errors. */ - if (ok && w->case_cnt <= INT32_MAX && !fseeko (w->file, 80, SEEK_SET)) + if (ok && w->case_cnt <= INT32_MAX && !fbuf_seek (w->fbuf, 80)) { write_int (w, w->case_cnt); - clearerr (w->file); + fbuf_clear_status (w->fbuf); } - if (fclose (w->file) == EOF) + if (fbuf_close (w->fbuf) != 0) ok = false; if (!ok) @@ -1489,7 +1493,7 @@ write_ztrailer (struct sfm_writer *w) compressed_ofs += block->compressed_size; } - if (!fseeko (w->file, w->zstart + 8, SEEK_SET)) + if (!fbuf_seek (w->fbuf, w->zstart + 8)) { write_int64 (w, compressed_ofs); write_int64 (w, 24 + (w->n_blocks * 24)); @@ -1613,7 +1617,7 @@ write_string (struct sfm_writer *w, const char *string, size_t width) size_t pad_bytes = width - data_bytes; write_bytes (w, string, data_bytes); while (pad_bytes-- > 0) - putc (w->space, w->file); + fbuf_putc (w->fbuf, w->space); } /* Recodes null-terminated UTF-8 encoded STRING into ENCODING, and writes the @@ -1658,7 +1662,7 @@ write_string_record (struct sfm_writer *w, static void write_bytes (struct sfm_writer *w, const void *data, size_t size) { - fwrite (data, 1, size, w->file); + fbuf_write (w->fbuf, data, size); } /* Writes N zeros to W's output file. */ @@ -1666,7 +1670,7 @@ static void write_zeros (struct sfm_writer *w, size_t n) { while (n-- > 0) - putc (0, w->file); + fbuf_putc (w->fbuf, 0); } /* Writes N spaces to W's output file. */ @@ -1674,5 +1678,5 @@ static void write_spaces (struct sfm_writer *w, size_t n) { while (n-- > 0) - putc (w->space, w->file); + fbuf_putc (w->fbuf, w->space); }