CTABLES
[pspp] / src / output / pivot-table.c
index cc541646c86985e58882f1e90d544924983d20f6..f8f917d9dd1d965b3d5b2830b725c7c12004eee1 100644 (file)
@@ -865,20 +865,20 @@ pivot_table_create (const char *title)
 struct pivot_table *
 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) { .type = FMT_F, .w = 40 };
-  table->title = title;
-  table->subtype = subtype ? pivot_value_new_text (subtype) : NULL;
-  table->command_c = xstrdup_if_nonempty (output_get_command_name ());
-  table->look = pivot_table_look_ref (pivot_table_look_get_default ());
-  table->settings = fmt_settings_copy (settings_get_fmt_settings ());
-  table->small = settings_get_small ();
-
-  hmap_init (&table->cells);
-
+  struct pivot_table *table = xmalloc (sizeof *table);
+  *table = (struct pivot_table) {
+    .ref_cnt = 1,
+    .show_title = true,
+    .show_caption = true,
+    .weight_format = (struct fmt_spec) { .type = FMT_F, .w = 40 },
+    .title = title,
+    .subtype = subtype ? pivot_value_new_text (subtype) : NULL,
+    .command_c = xstrdup_if_nonempty (output_get_command_name ()),
+    .look = pivot_table_look_ref (pivot_table_look_get_default ()),
+    .settings = fmt_settings_copy (settings_get_fmt_settings ()),
+    .small = settings_get_small (),
+    .cells = HMAP_INITIALIZER (table->cells),
+  };
   return table;
 }
 
@@ -968,7 +968,7 @@ clone_category (struct pivot_category *old,
     .extra_depth = old->extra_depth,
 
     .subs = (old->n_subs
-             ? xzalloc (old->n_subs * sizeof *new->subs)
+             ? xcalloc (old->n_subs, sizeof *new->subs)
              : NULL),
     .n_subs = old->n_subs,
     .allocated_subs = old->n_subs,
@@ -1006,9 +1006,9 @@ clone_dimension (struct pivot_dimension *old, struct pivot_table *new_pt)
     .axis_type = old->axis_type,
     .level = old->level,
     .top_index = old->top_index,
-    .data_leaves = xzalloc (old->n_leaves * sizeof *new->data_leaves),
-    .presentation_leaves = xzalloc (old->n_leaves
-                                    * sizeof *new->presentation_leaves),
+    .data_leaves = xcalloc (old->n_leaves , sizeof *new->data_leaves),
+    .presentation_leaves = xcalloc (old->n_leaves
+                                    , sizeof *new->presentation_leaves),
     .n_leaves = old->n_leaves,
     .allocated_leaves = old->n_leaves,
     .hide_all_labels = old->hide_all_labels,
@@ -1191,6 +1191,43 @@ pivot_table_is_shared (const struct pivot_table *table)
   return table->ref_cnt > 1;
 }
 
+static void
+pivot_table_set_value__ (struct pivot_value **dstp, struct pivot_value *src)
+{
+  pivot_value_destroy (*dstp);
+  *dstp = src;
+}
+
+/* Changes the title of TABLE to TITLE.  Takes ownership of TITLE. */
+void
+pivot_table_set_title (struct pivot_table *table, struct pivot_value *title)
+{
+  pivot_table_set_value__ (&table->title, title);
+}
+
+/* Changes the subtype of TABLE to SUBTYPE.  Takes ownership of SUBTYPE. */
+void
+pivot_table_set_subtype (struct pivot_table *table, struct pivot_value *subtype)
+{
+  pivot_table_set_value__ (&table->subtype, subtype);
+}
+
+/* Changes the corner text of TABLE to CORNER_TEXT.  Takes ownership of
+   CORNER_TEXT. */
+void
+pivot_table_set_corner_text (struct pivot_table *table,
+                             struct pivot_value *corner_text)
+{
+  pivot_table_set_value__ (&table->corner_text, corner_text);
+}
+
+/* Changes the caption of TABLE to CAPTION.  Takes ownership of CAPTION. */
+void
+pivot_table_set_caption (struct pivot_table *table, struct pivot_value *caption)
+{
+  pivot_table_set_value__ (&table->caption, caption);
+}
+
 /* Swaps axes A and B in TABLE. */
 void
 pivot_table_swap_axes (struct pivot_table *table,
@@ -1939,6 +1976,8 @@ compose_headings (const struct pivot_table *pt,
 static void
 free_headings (const struct pivot_axis *axis, char ***headings)
 {
+  if (!headings)
+    return;
   for (size_t i = 0; i < axis->label_depth; i++)
     {
       for (size_t j = 0; j < axis->extent; j++)
@@ -2094,21 +2133,30 @@ pivot_table_dump (const struct pivot_table *table, int indentation)
       size_t *row_enumeration = pivot_table_enumerate_axis (
         table, PIVOT_AXIS_ROW, layer_indexes, table->look->omit_empty, NULL);
 
+      /* Print column headings.
+
+         Ordinarily the test for nonnull 'column_headings' would be
+         unnecessary, because 'column_headings' is null only if the axis's
+         label_depth is 0, but there is a special case for the column axis only
+         in pivot_table_assign_label_depth(). */
       char ***column_headings = compose_headings (
         table, &table->axes[PIVOT_AXIS_COLUMN], column_enumeration);
-      for (size_t y = 0; y < table->axes[PIVOT_AXIS_COLUMN].label_depth; y++)
+      if (column_headings)
         {
-          indent (indentation + 1);
-          for (size_t x = 0; x < table->axes[PIVOT_AXIS_COLUMN].extent; x++)
+          for (size_t y = 0; y < table->axes[PIVOT_AXIS_COLUMN].label_depth; y++)
             {
-              if (x)
-                fputs ("; ", stdout);
-              if (column_headings[y][x])
-                fputs (column_headings[y][x], stdout);
+              indent (indentation + 1);
+              for (size_t x = 0; x < table->axes[PIVOT_AXIS_COLUMN].extent; x++)
+                {
+                  if (x)
+                    fputs ("; ", stdout);
+                  if (column_headings && column_headings[y] && column_headings[y][x])
+                    fputs (column_headings[y][x], stdout);
+                }
+              putchar ('\n');
             }
-          putchar ('\n');
+          free_headings (&table->axes[PIVOT_AXIS_COLUMN], column_headings);
         }
-      free_headings (&table->axes[PIVOT_AXIS_COLUMN], column_headings);
 
       indent (indentation + 1);
       printf ("-----------------------------------------------\n");
@@ -2693,9 +2741,9 @@ pivot_value_new_user_text_nocopy (char *text)
    that pivot_value_new_variable() should be used for variable names).  For
    strings that are part of the PSPP user interface, such as names of
    procedures, statistics, annotations, error messages, etc., use
-   pivot_value_new_text().j
+   pivot_value_new_text().
 
-   The caller retains ownership of TEXT.*/
+   The caller retains ownership of TEXT. */
 struct pivot_value *
 pivot_value_new_user_text (const char *text, size_t length)
 {
@@ -2729,7 +2777,7 @@ pivot_value_new_text (const char *text)
 }
 
 /* Same as pivot_value_new_text() but its argument is a printf()-like format
-   string. */
+   string.  The format string should generally be enclosed in N_(). */
 struct pivot_value * PRINTF_FORMAT (1, 2)
 pivot_value_new_text_format (const char *format, ...)
 {
@@ -2818,7 +2866,7 @@ struct pivot_value *
 pivot_value_new_value (const union value *value, int width,
                        const struct fmt_spec *format, const char *encoding)
 {
-  struct pivot_value *pv = xzalloc (sizeof *pv);
+  struct pivot_value *pv = XZALLOC (struct pivot_value);
   if (width > 0)
     {
       char *s = recode_string (UTF8, encoding, CHAR_CAST (char *, value->s),
@@ -2844,13 +2892,22 @@ pivot_value_new_value (const union value *value, int width,
 /* Returns a new pivot_value for VARIABLE. */
 struct pivot_value *
 pivot_value_new_variable (const struct variable *variable)
+{
+  return pivot_value_new_variable__ (var_get_name (variable),
+                                     var_get_label (variable));
+}
+
+/* Returns a new pivot_value for a variable with the given NAME and optional
+   LABEL. */
+struct pivot_value *
+pivot_value_new_variable__ (const char *name, const char *label)
 {
   struct pivot_value *value = xmalloc (sizeof *value);
   *value = (struct pivot_value) {
     .variable = {
       .type = PIVOT_VALUE_VARIABLE,
-      .var_name = xstrdup (var_get_name (variable)),
-      .var_label = xstrdup_if_nonempty (var_get_label (variable)),
+      .var_name = xstrdup (name),
+      .var_label = xstrdup_if_nonempty (label),
     },
   };
   return value;