work toward getting rid of struct table in table_item
[pspp] / src / output / pivot-table.c
index 3d76ea2afa93ce79f88667380ce789641280757b..96dd9d2413abaac75fd58a251a2aa41ef46c8a96 100644 (file)
@@ -148,7 +148,10 @@ default_look (const struct pivot_table_look *new)
     {
       char *error = pivot_table_look_read ("default.stt", &look);
       if (error)
-        look = pivot_table_look_ref (pivot_table_look_builtin_default ());
+        {
+          free (error);
+          look = pivot_table_look_ref (pivot_table_look_builtin_default ());
+        }
     }
   return look;
 }
@@ -175,10 +178,11 @@ pivot_table_look_read (const char *name, struct pivot_table_look **lookp)
   size_t n = 0;
   path[n++] = ".";
   const char *home = getenv ("HOME");
+  char *allocated = NULL;
   if (home != NULL)
-    path[n++] = xasprintf ("%s/.pspp/looks", home);
-  char *allocated;
-  path[n++] = relocate2 (PKGDATADIR "/looks", &allocated);
+    path[n++] = allocated = xasprintf ("%s/.pspp/looks", home);
+  char *allocated2;
+  path[n++] = relocate2 (PKGDATADIR "/looks", &allocated2);
   path[n++] = NULL;
 
   /* Search path. */
@@ -190,6 +194,7 @@ pivot_table_look_read (const char *name, struct pivot_table_look **lookp)
       free (name2);
     }
   free (allocated);
+  free (allocated2);
   if (!file)
     return xasprintf ("%s: not found", name);
 
@@ -365,7 +370,8 @@ pivot_axis_iterator_next (size_t *indexes, const struct pivot_axis *axis)
           if (axis->dimensions[i]->n_leaves == 0)
             return NULL;
 
-      return xcalloc (axis->n_dimensions, sizeof *indexes);
+      size_t size = axis->n_dimensions * sizeof *indexes;
+      return xzalloc (MAX (size, 1));
     }
 
   for (size_t i = 0; i < axis->n_dimensions; i++)
@@ -812,11 +818,11 @@ pivot_table_create (const char *title)
   return pivot_table_create__ (pivot_value_new_text (title), title);
 }
 
-/* Creates and returns a new pivot table with the given TITLE, and takes
-   ownership of TITLE.  The new pivot table's subtype is SUBTYPE, which
-   should be an untranslated English string that describes the contents of
-   the table at a high level without being specific about the variables or
-   other context involved.
+/* Creates and returns a new pivot table with the given TITLE (which may be
+   null), and takes ownership of TITLE.  The new pivot table's subtype is
+   SUBTYPE, which should be an untranslated English string that describes the
+   contents of the table at a high level without being specific about the
+   variables or other context involved.
 
    Operations commonly performed on the new pivot_table:
 
@@ -833,6 +839,7 @@ pivot_table_create__ (struct pivot_value *title, const char *subtype)
 {
   struct pivot_table *table = xzalloc (sizeof *table);
   table->ref_cnt = 1;
+  table->show_title = true;
   table->show_caption = true;
   table->weight_format = (struct fmt_spec) { FMT_F, 40, 0 };
   table->title = title;
@@ -987,6 +994,25 @@ pivot_table_is_empty (const struct pivot_table *table)
   return hmap_is_empty (&table->cells);
 }
 
+size_t *
+pivot_table_next_display_layer (const struct pivot_table *pt, size_t *indexes,
+                                bool print)
+{
+  const struct pivot_axis *layer_axis = &pt->axes[PIVOT_AXIS_LAYER];
+  if (print && pt->look->print_all_layers)
+    return pivot_axis_iterator_next (indexes, layer_axis);
+  else if (!indexes)
+    {
+      size_t size = layer_axis->n_dimensions * sizeof *pt->current_layer;
+      return xmemdup (pt->current_layer, MAX (size, 1));
+    }
+  else
+    {
+      free (indexes);
+      return NULL;
+    }
+}
+
 static unsigned int
 pivot_cell_hash_indexes (const size_t *indexes, size_t n_idx)
 {
@@ -1594,6 +1620,7 @@ pivot_table_dump (const struct pivot_table *table, int indentation)
     settings_set_decimal_char (table->decimal);
 
   pivot_table_dump_value (table->title, "title", indentation);
+  pivot_table_dump_value (table->subtype, "subtype", indentation);
   pivot_table_dump_string (table->command_c, "command", indentation);
   pivot_table_dump_string (table->dataset, "dataset", indentation);
   pivot_table_dump_string (table->datafile, "datafile", indentation);
@@ -1672,8 +1699,7 @@ pivot_table_dump (const struct pivot_table *table, int indentation)
           pivot_value_dump (d->root->name);
           fputs (" =", stdout);
 
-          struct pivot_value **names = xnmalloc (layer_axis->label_depth,
-                                                 sizeof *names);
+          struct pivot_value **names = xnmalloc (d->n_leaves, sizeof *names);
           size_t n_names = 0;
           for (const struct pivot_category *c
                  = d->presentation_leaves[layer_indexes[i]];
@@ -1912,7 +1938,10 @@ interpret_show (enum settings_value_show global_show,
 /* Appends a text representation of the body of VALUE to OUT.  SHOW_VALUES and
    SHOW_VARIABLES control whether variable and value labels are included.
 
-   The "body" omits subscripts and superscripts and footnotes. */
+   The "body" omits subscripts and superscripts and footnotes.
+
+   Returns true if OUT is a number (or a number plus a value label), false
+   otherwise.  */
 bool
 pivot_value_format_body (const struct pivot_value *value,
                          enum settings_value_show show_values,
@@ -2001,7 +2030,7 @@ pivot_value_format_body (const struct pivot_value *value,
 /* Appends a text representation of VALUE to OUT.  SHOW_VALUES and
    SHOW_VARIABLES control whether variable and value labels are included.
 
-   Subscripts and superscripts and footnotes are included. */
+   Subscripts and footnotes are included. */
 void
 pivot_value_format (const struct pivot_value *value,
                     enum settings_value_show show_values,
@@ -2016,9 +2045,6 @@ pivot_value_format (const struct pivot_value *value,
         ds_put_format (out, "%c%s", i ? ',' : '_', value->subscripts[i]);
     }
 
-  if (value->superscript)
-    ds_put_format (out, "^%s", value->superscript);
-
   for (size_t i = 0; i < value->n_footnotes; i++)
     {
       ds_put_byte (out, '^');
@@ -2056,8 +2082,6 @@ pivot_value_destroy (struct pivot_value *value)
         free (value->subscripts[i]);
       free (value->subscripts);
 
-      free (value->superscript);
-
       switch (value->type)
         {
         case PIVOT_VALUE_NUMERIC:
@@ -2118,16 +2142,29 @@ pivot_value_get_style (struct pivot_value *value,
 void
 pivot_value_set_style (struct pivot_value *value,
                        const struct table_area_style *area)
+{
+  pivot_value_set_font_style (value, &area->font_style);
+  pivot_value_set_cell_style (value, &area->cell_style);
+}
+
+void
+pivot_value_set_font_style (struct pivot_value *value,
+                            const struct font_style *font_style)
 {
   if (value->font_style)
     font_style_uninit (value->font_style);
   else
     value->font_style = xmalloc (sizeof *value->font_style);
-  font_style_copy (NULL, value->font_style, &area->font_style);
+  font_style_copy (NULL, value->font_style, font_style);
+}
 
+void
+pivot_value_set_cell_style (struct pivot_value *value,
+                            const struct cell_style *cell_style)
+{
   if (!value->cell_style)
     value->cell_style = xmalloc (sizeof *value->cell_style);
-  *value->cell_style = area->cell_style;
+  *value->cell_style = *cell_style;
 }
 
 /* Frees the data owned by ARG (but not ARG itself). */