spv-legacy-decoder: Always give the pivot table a title.
[pspp] / src / output / spv / spv-legacy-decoder.c
index b90d6b0ab39492a9fffd7e67d80088edfbfe0694..e103dd842e8247379cbfc670803c24bc9f3a8f83 100644 (file)
@@ -203,7 +203,9 @@ spv_map_insert (struct hmap *map, double from, const char *to,
       else
         {
           union value v = { .f = mapping->to.d };
-          mapping->to.s = data_out_stretchy (&v, NULL, format, NULL);
+          mapping->to.s = data_out_stretchy (&v, NULL, format,
+                                             settings_get_fmt_settings (),
+                                             NULL);
           mapping->to.width = strlen (mapping->to.s);
         }
     }
@@ -275,7 +277,7 @@ spv_series_parse_value_map_entry (struct hmap *map,
                           vme->from);
 
       char *error = spv_map_insert (map, from, vme->to, true,
-                                    &(struct fmt_spec) { FMT_A, 40, 0 });
+                                    &(struct fmt_spec) { .type = FMT_A, .w = 40 });
       if (error)
         return error;
 
@@ -365,7 +367,7 @@ decode_number_format (const struct spvdx_number_format *nf)
   if (d < 0 || d > 15)
     d = 2;
 
-  struct fmt_spec f = (struct fmt_spec) { type, 40, d };
+  struct fmt_spec f = (struct fmt_spec) { .type = type, .w = 40, .d = d };
   fmt_fix_output (&f);
   return f;
 }
@@ -702,7 +704,7 @@ decode_label_frame (struct pivot_table *table,
 
   if (target)
     {
-      struct pivot_value *value = xzalloc (sizeof *value);
+      struct pivot_value *value = XZALLOC (struct pivot_value);
       value->type = PIVOT_VALUE_TEXT;
       for (size_t i = 0; i < lf->label->n_text; i++)
         {
@@ -718,6 +720,9 @@ decode_label_frame (struct pivot_table *table,
               value->text.local = new;
             }
         }
+      if (!value->text.local)
+        value->text.local = xstrdup ("");
+      value->text.c = value->text.id = value->text.local;
       pivot_value_destroy (*target);
       *target = value;
     }
@@ -756,7 +761,7 @@ static char BAD_REFERENCE;
 
 static char * WARN_UNUSED_RESULT
 decode_spvdx_source_variable (const struct spvxml_node *node,
-                              struct spv_data *data,
+                              const struct spv_data *data,
                               struct hmap *series_map)
 {
   const struct spvdx_source_variable *sv = spvdx_cast_source_variable (node);
@@ -779,10 +784,10 @@ decode_spvdx_source_variable (const struct spvxml_node *node,
                       "source %s variable %s.",
                       sv->node_.id, sv->source, sv->source_name);
 
-  struct spv_series *s = xzalloc (sizeof *s);
+  struct spv_series *s = XZALLOC (struct spv_series);
   s->name = xstrdup (node->id);
   s->xml = node;
-  s->label = sv->label ? xstrdup (sv->label) : NULL;
+  s->label = xstrdup_if_nonnull (sv->label);
   s->label_series = label_series;
   s->values = spv_data_values_clone (var->values, var->n_values);
   s->n_values = var->n_values;
@@ -803,7 +808,8 @@ decode_spvdx_source_variable (const struct spvxml_node *node,
             if (label_series->values[i].width < 0)
               {
                 union value v = { .f = label_series->values[i].d };
-                dest = data_out_stretchy (&v, "UTF-8", &s->format, NULL);
+                dest = data_out_stretchy (&v, "UTF-8", &s->format,
+                                          settings_get_fmt_settings (), NULL);
               }
             else
               dest = label_series->values[i].s;
@@ -863,7 +869,7 @@ decode_spvdx_derived_variable (const struct spvxml_node *node,
     return xasprintf ("Derived variable %s has unknown value \"%s\"",
                       node->id, dv->value);
 
-  struct spv_series *s = xzalloc (sizeof *s);
+  struct spv_series *s = XZALLOC (struct spv_series);
   s->format = F_8_0;
   s->name = xstrdup (node->id);
   s->values = values;
@@ -953,7 +959,7 @@ pivot_value_from_data_value (const struct spv_data_value *data,
   if (error)
     return error;
 
-  struct pivot_value *v = xzalloc (sizeof *v);
+  struct pivot_value *v = XZALLOC (struct pivot_value);
   if (data->width >= 0)
     {
       if (format && fmt_get_category (f.type) == FMT_CAT_DATE)
@@ -965,8 +971,8 @@ pivot_value_from_data_value (const struct spv_data_value *data,
               && len == 23
               && data->s[len] == '\0')
             {
-              double date = calendar_gregorian_to_offset (year, month, day,
-                                                          NULL);
+              double date = calendar_gregorian_to_offset (
+                year, month, day, settings_get_fmt_settings (), NULL);
               if (date != SYSMIS)
                 {
                   v->type = PIVOT_VALUE_NUMERIC;
@@ -1138,7 +1144,7 @@ add_dimension (struct spv_series **series, size_t n,
   assert (n_cats > 0);
 
   /* Make the categories. */
-  struct pivot_dimension *d = xzalloc (sizeof *d);
+  struct pivot_dimension *d = XZALLOC (struct pivot_dimension);
   table->dimensions[table->n_dimensions++] = d;
 
   series[0]->n_index = max_cat + 1;
@@ -1149,7 +1155,7 @@ add_dimension (struct spv_series **series, size_t n,
     {
       struct spv_data_value *dv = &series[0]->values[cat_rows[k]];
       int dv_num = dv ? dv->d : dv->index;
-      struct pivot_category *cat = xzalloc (sizeof *cat);
+      struct pivot_category *cat = XZALLOC (struct pivot_category);
       char *retval = pivot_value_from_data_value (
         spv_map_lookup (&series[0]->map, dv), NULL, NULL, &cat->name);
       if (retval)
@@ -1269,6 +1275,11 @@ add_dimension (struct spv_series **series, size_t n,
   /* Now drop unnamed 1-category groups and add parent pointers. */
   for (size_t j = 0; j < n_cats; j++)
     add_parents (cats[j], d->root, j);
+  for (size_t j = 0; j < d->n_leaves; j++)
+    {
+      d->data_leaves[j]->data_index = j;
+      d->presentation_leaves[j]->presentation_index = j;
+    }
 
   d->root->subs = cats;
   d->root->n_subs = n_cats;
@@ -1416,11 +1427,11 @@ apply_styles_to_value (struct pivot_table *table,
 {
   if (sf)
     {
-      if (sf->reset > 0)
+      if (sf->reset > 0 && value->ex)
         {
-          free (value->footnote_indexes);
-          value->footnote_indexes = NULL;
-          value->n_footnotes = 0;
+          free (value->ex->footnote_indexes);
+          value->ex->footnote_indexes = NULL;
+          value->ex->n_footnotes = 0;
         }
 
       struct fmt_spec format = { .w = 0 };
@@ -1465,11 +1476,12 @@ apply_styles_to_value (struct pivot_table *table,
     }
   if (fg || bg)
     {
+      const struct pivot_value_ex *ex = pivot_value_ex (value);
       struct table_area_style area;
       pivot_value_get_style (
         value,
-        value->font_style ? value->font_style : &base_area_style->font_style,
-        value->cell_style ? value->cell_style : &base_area_style->cell_style,
+        ex->font_style ? ex->font_style : &base_area_style->font_style,
+        ex->cell_style ? ex->cell_style : &base_area_style->cell_style,
         &area);
       decode_spvdx_style_incremental (fg, bg, &area);
       pivot_value_set_style (value, &area);
@@ -1757,9 +1769,10 @@ format_map_destroy (struct hmap *format_map)
 char * WARN_UNUSED_RESULT
 decode_spvdx_table (const struct spvdx_visualization *v, const char *subtype,
                     const struct pivot_table_look *look,
-                    struct spv_data *data, struct pivot_table **outp)
+                    const struct spv_data *data, struct pivot_table **outp)
 {
-  struct pivot_table *table = pivot_table_create__ (NULL, subtype);
+  struct pivot_table *table = pivot_table_create__ (
+    pivot_value_new_user_text (v->name, SIZE_MAX), subtype);
 
   pivot_table_set_look (table, look);
   table->look = pivot_table_look_unshare (table->look);
@@ -1985,7 +1998,7 @@ decode_spvdx_table (const struct spvdx_visualization *v, const char *subtype,
 
       if (value->type == PIVOT_VALUE_NUMERIC
           && value->numeric.x == SYSMIS
-          && !value->n_footnotes)
+          && !pivot_value_ex (value)->n_footnotes)
         {
           /* Apparently, system-missing values are just empty cells? */
           pivot_value_destroy (value);