-/* Closes WRITER.
- Returns true if successful, false if an I/O error occurred on WRITER. */
-bool
-scratch_writer_close (struct scratch_writer *writer)
-{
- struct casefile *cf = writer->handle->casefile;
- bool ok = casefile_error (cf);
- fh_close (writer->fh, "scratch file", "we");
+ struct scratch_writer *writer = writer_;
+ struct casereader *reader = casewriter_make_reader (writer->subwriter);
+ if (!casereader_error (reader))
+ {
+ /* Destroy previous contents of handle. */
+ struct scratch_handle *sh = fh_get_scratch_handle (writer->fh);
+ if (sh != NULL)
+ scratch_handle_destroy (sh);
+
+ /* Create new contents. */
+ sh = xmalloc (sizeof *sh);
+ sh->unique_id = ++next_unique_id;
+ sh->dictionary = writer->dict;
+ sh->casereader = reader;
+ fh_set_scratch_handle (writer->fh, sh);
+ }
+ else
+ {
+ casereader_destroy (reader);
+ dict_destroy (writer->dict);
+ }
+
+ fh_unlock (writer->lock);
+ fh_unref (writer->fh);