Replace numerous instances of xzalloc with XZALLOC
[pspp] / src / data / sys-file-writer.c
index 05d42b080ea9152ae280f6b2ccc327f87a551608..54282a95f313831dde81df976d8eae2df7ad19e4 100644 (file)
@@ -200,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;
 
@@ -213,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;
@@ -365,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;
 
@@ -409,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
     {
@@ -429,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);
@@ -507,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);
@@ -715,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, ')');
     }
@@ -787,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;
 
@@ -853,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);
         }
@@ -953,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;
@@ -985,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);
@@ -1011,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);
@@ -1022,7 +1018,6 @@ write_long_string_value_labels (struct sfm_writer *w,
           free (label);
         }
     }
-  assert (ftello (w->file) == start + size);
 }
 
 static void
@@ -1032,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;
@@ -1058,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);
@@ -1084,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
@@ -1420,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
@@ -1597,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);
     }
 }