Replace numerous instances of xzalloc with XZALLOC
[pspp] / src / data / sys-file-writer.c
index 8cfd577f1a6c656ea0ae70d5a87dcfdd0ecf9a38..54282a95f313831dde81df976d8eae2df7ad19e4 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 1997-2000, 2006-2013 Free Software Foundation, Inc.
+   Copyright (C) 1997-2000, 2006-2014 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -33,7 +33,6 @@
 #include "data/casewriter.h"
 #include "data/dictionary.h"
 #include "data/file-handle-def.h"
-#include "data/file-name.h"
 #include "data/format.h"
 #include "data/make-file.h"
 #include "data/missing-values.h"
@@ -73,7 +72,7 @@ struct sfm_writer
     FILE *file;                        /* File stream. */
     struct replace_file *rf;    /* Ticket for replacing output file. */
 
-    enum sfm_compression compression;
+    enum any_compression compression;
     casenumber case_cnt;       /* Number of cases written so far. */
     uint8_t space;              /* ' ' in the file's character encoding. */
 
@@ -183,8 +182,8 @@ sfm_writer_default_options (void)
 {
   struct sfm_write_options opts;
   opts.compression = (settings_get_scompression ()
-                      ? SFM_COMP_SIMPLE
-                      : SFM_COMP_NONE);
+                      ? ANY_COMP_SIMPLE
+                      : ANY_COMP_NONE);
   opts.create_writeable = true;
   opts.version = 3;
   return opts;
@@ -201,7 +200,6 @@ sfm_open_writer (struct file_handle *fh, struct dictionary *d,
                  struct sfm_write_options opts)
 {
   struct encoding_info encoding_info;
-  struct sfm_writer *w;
   mode_t mode;
   int i;
 
@@ -214,7 +212,7 @@ sfm_open_writer (struct file_handle *fh, struct dictionary *d,
     }
 
   /* Create and initialize writer. */
-  w = xzalloc (sizeof *w);
+  struct sfm_writer *w = XZALLOC (struct sfm_writer);
   w->fh = fh_ref (fh);
   w->lock = NULL;
   w->file = NULL;
@@ -224,9 +222,9 @@ sfm_open_writer (struct file_handle *fh, struct dictionary *d,
      files have been observed, so drop back to simple compression for those
      files. */
   w->compression = opts.compression;
-  if (w->compression == SFM_COMP_ZLIB
+  if (w->compression == ANY_COMP_ZLIB
       && is_encoding_ebcdic_compatible (dict_get_encoding (d)))
-    w->compression = SFM_COMP_SIMPLE;
+    w->compression = ANY_COMP_SIMPLE;
 
   w->case_cnt = 0;
 
@@ -251,8 +249,7 @@ sfm_open_writer (struct file_handle *fh, struct dictionary *d,
   mode = 0444;
   if (opts.create_writeable)
     mode |= 0222;
-  w->rf = replace_file_start (fh_get_file_name (fh), "wb", mode,
-                              &w->file, NULL);
+  w->rf = replace_file_start (fh, "wb", mode, &w->file);
   if (w->rf == NULL)
     {
       msg (ME, _("Error opening `%s' for writing as a system file: %s."),
@@ -306,7 +303,7 @@ sfm_open_writer (struct file_handle *fh, struct dictionary *d,
   write_int (w, 999);
   write_int (w, 0);
 
-  if (w->compression == SFM_COMP_ZLIB)
+  if (w->compression == ANY_COMP_ZLIB)
     {
       w->zstream.zalloc = Z_NULL;
       w->zstream.zfree = Z_NULL;
@@ -367,8 +364,6 @@ write_header (struct sfm_writer *w, const struct dictionary *d)
 {
   const char *dict_encoding = dict_get_encoding (d);
   char prod_name[61];
-  char creation_date[10];
-  char creation_time[9];
   const char *file_label;
   struct variable *weight;
 
@@ -377,7 +372,7 @@ write_header (struct sfm_writer *w, const struct dictionary *d)
   /* Record-type code. */
   if (is_encoding_ebcdic_compatible (dict_encoding))
     write_string (w, EBCDIC_MAGIC, 4);
-  else if (w->compression == SFM_COMP_ZLIB)
+  else if (w->compression == ANY_COMP_ZLIB)
     write_string (w, ASCII_ZMAGIC, 4);
   else
     write_string (w, ASCII_MAGIC, 4);
@@ -394,8 +389,8 @@ write_header (struct sfm_writer *w, const struct dictionary *d)
   write_int (w, calc_oct_idx (d, NULL));
 
   /* Compressed? */
-  write_int (w, (w->compression == SFM_COMP_NONE ? 0
-                 : w->compression == SFM_COMP_SIMPLE ? 1
+  write_int (w, (w->compression == ANY_COMP_NONE ? 0
+                 : w->compression == ANY_COMP_SIMPLE ? 1
                  : 2));
 
   /* Weight variable. */
@@ -411,10 +406,11 @@ write_header (struct sfm_writer *w, const struct dictionary *d)
   write_float (w, COMPRESSION_BIAS);
 
   /* Creation date and time. */
+  char *creation_date, *creation_time;
   if (time (&t) == (time_t) -1)
     {
-      strcpy (creation_date, "01 Jan 70");
-      strcpy (creation_time, "00:00:00");
+      creation_date = xstrdup ("01 Jan 70");
+      creation_time = xstrdup ("00:00:00");
     }
   else
     {
@@ -431,13 +427,14 @@ write_header (struct sfm_writer *w, const struct dictionary *d)
       int min = rerange (tmp->tm_min + 1);
       int sec = rerange (tmp->tm_sec + 1);
 
-      snprintf (creation_date, sizeof creation_date,
-                "%02d %s %02d", day, month_name[mon - 1], year);
-      snprintf (creation_time, sizeof creation_time,
-                "%02d:%02d:%02d", hour - 1, min - 1, sec - 1);
+      creation_date = xasprintf ("%02d %s %02d",
+                                 day, month_name[mon - 1], year);
+      creation_time = xasprintf ("%02d:%02d:%02d", hour - 1, min - 1, sec - 1);
     }
   write_utf8_string (w, dict_encoding, creation_date, 9);
   write_utf8_string (w, dict_encoding, creation_time, 8);
+  free (creation_time);
+  free (creation_date);
 
   /* File label. */
   file_label = dict_get_label (d);
@@ -509,6 +506,7 @@ write_variable (struct sfm_writer *w, const struct variable *v)
 
      Missing values for long string variables are written in a separate
      record. */
+  enum { MAX_SHORT_STRING = 8 };
   if (width <= MAX_SHORT_STRING)
     {
       const struct missing_values *mv = var_get_missing_values (v);
@@ -717,14 +715,14 @@ put_attrset (struct string *string, const struct attrset *attrs)
   struct attrset_iterator i;
 
   for (attr = attrset_first (attrs, &i); attr != NULL;
-       attr = attrset_next (attrs, &i)) 
+       attr = attrset_next (attrs, &i))
     {
       size_t n_values = attribute_get_n_values (attr);
       size_t j;
 
       ds_put_cstr (string, attribute_get_name (attr));
       ds_put_byte (string, '(');
-      for (j = 0; j < n_values; j++) 
+      for (j = 0; j < n_values; j++)
         ds_put_format (string, "'%s'\n", attribute_get_value (attr, j));
       ds_put_byte (string, ')');
     }
@@ -789,7 +787,7 @@ write_variable_attributes (struct sfm_writer *w, const struct dictionary *d)
   size_t i;
 
   for (i = 0; i < n_vars; i++)
-    { 
+    {
       struct variable *v = dict_get_var (d, i);
       struct attrset attrs;
 
@@ -855,8 +853,7 @@ write_mrsets (struct sfm_writer *w, const struct dictionary *dict,
           if (mrset->width == 0)
             counted = xasprintf ("%.0f", mrset->counted.f);
           else
-            counted = xmemdup0 (value_str (&mrset->counted, mrset->width),
-                                mrset->width);
+            counted = xmemdup0 (mrset->counted.s, mrset->width);
           ds_put_format (&s, "%zu %s", strlen (counted), counted);
           free (counted);
         }
@@ -955,7 +952,6 @@ write_long_string_value_labels (struct sfm_writer *w,
   const char *encoding = dict_get_encoding (dict);
   size_t n_vars = dict_get_var_cnt (dict);
   size_t size, i;
-  off_t start UNUSED;
 
   /* Figure out the size in advance. */
   size = 0;
@@ -987,7 +983,6 @@ 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);
   for (i = 0; i < n_vars; i++)
     {
       struct variable *var = dict_get_var (dict, i);
@@ -1013,8 +1008,7 @@ write_long_string_value_labels (struct sfm_writer *w,
           size_t len;
 
           write_int (w, width);
-          write_bytes (w, value_str (val_lab_get_value (val_lab), width),
-                       width);
+          write_bytes (w, val_lab_get_value (val_lab)->s, width);
 
           label = recode_string (var_get_encoding (var), "UTF-8",
                                  val_lab_get_escaped_label (val_lab), -1);
@@ -1024,7 +1018,6 @@ write_long_string_value_labels (struct sfm_writer *w,
           free (label);
         }
     }
-  assert (ftello (w->file) == start + size);
 }
 
 static void
@@ -1034,7 +1027,6 @@ write_long_string_missing_values (struct sfm_writer *w,
   const char *encoding = dict_get_encoding (dict);
   size_t n_vars = dict_get_var_cnt (dict);
   size_t size, i;
-  off_t start UNUSED;
 
   /* Figure out the size in advance. */
   size = 0;
@@ -1060,7 +1052,6 @@ 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);
   for (i = 0; i < n_vars; i++)
     {
       struct variable *var = dict_get_var (dict, i);
@@ -1086,10 +1077,9 @@ write_long_string_missing_values (struct sfm_writer *w,
           const union value *value = mv_get_value (mv, j);
 
           write_int (w, 8);
-          write_bytes (w, value_str (value, width), 8);
+          write_bytes (w, value->s, 8);
         }
     }
-  assert (ftello (w->file) == start + size);
 }
 
 static void
@@ -1216,7 +1206,7 @@ sys_file_casewriter_write (struct casewriter *writer, void *w_,
 
   w->case_cnt++;
 
-  if (w->compression == SFM_COMP_NONE)
+  if (w->compression == ANY_COMP_NONE)
     write_case_uncompressed (w, c);
   else
     write_case_compressed (w, c);
@@ -1255,7 +1245,7 @@ close_writer (struct sfm_writer *w)
     {
       /* Flush buffer. */
       flush_compressed (w);
-      if (w->compression == SFM_COMP_ZLIB)
+      if (w->compression == ANY_COMP_ZLIB)
         {
           finish_zstream (w);
           write_ztrailer (w);
@@ -1422,6 +1412,7 @@ finish_zstream (struct sfm_writer *w)
   block = &w->blocks[w->n_blocks++];
   block->uncompressed_size = w->zstream.total_in;
   block->compressed_size = w->zstream.total_out;
+  deflateEnd (&w->zstream);
 }
 
 static void
@@ -1507,7 +1498,7 @@ flush_compressed (struct sfm_writer *w)
   if (w->n_opcodes)
     {
       unsigned int n = 8 * (1 + w->n_elements);
-      if (w->compression == SFM_COMP_SIMPLE)
+      if (w->compression == ANY_COMP_SIMPLE)
         write_bytes (w, w->cbuf, n);
       else
         write_zlib (w, w->cbuf, n);
@@ -1599,7 +1590,7 @@ write_value (struct sfm_writer *w, const union value *value, int width)
     write_float (w, value->f);
   else
     {
-      write_bytes (w, value_str (value, width), width);
+      write_bytes (w, value->s, width);
       write_zeros (w, 8 - width);
     }
 }