pivot-table: Allow all pivot_value formatting functions to use defaults.
[pspp] / src / output / pivot-table.c
index 6d1efb882b4748b3a009d6d3e0e64c05b0b1e624..ec652e50de0b532e1230a93941369a99e3cdb7c1 100644 (file)
@@ -2374,8 +2374,15 @@ get_text_from_markup (const char *markup, struct string *out)
   xmlFreeParserCtxt (parser);
 }
 
-/* Appends a text representation of the body of VALUE to OUT.  Settings on
-   PT control whether variable and value labels are included.
+static const struct pivot_table pivot_value_format_defaults = {
+  .show_values = SETTINGS_VALUE_SHOW_DEFAULT,
+  .show_variables = SETTINGS_VALUE_SHOW_DEFAULT,
+  .settings = FMT_SETTINGS_INIT,
+};
+
+/* Appends a text representation of the body of VALUE to OUT.  Settings on PT
+   control whether variable and value labels are included (pass NULL for PT to
+   get default formatting in the absence of a pivot table).
 
    The "body" omits subscripts and superscripts and footnotes.
 
@@ -2383,9 +2390,10 @@ get_text_from_markup (const char *markup, struct string *out)
    otherwise.  */
 bool
 pivot_value_format_body (const struct pivot_value *value,
-                         const struct pivot_table *pt,
+                         const struct pivot_table *pt_,
                          struct string *out)
 {
+  const struct pivot_table *pt = pt_ ? pt_ : &pivot_value_format_defaults;
   enum settings_value_show show;
   bool numeric = false;
 
@@ -2476,8 +2484,9 @@ pivot_value_format_body (const struct pivot_value *value,
   return numeric;
 }
 
-/* Appends a text representation of VALUE to OUT.  Settings on
-   PT control whether variable and value labels are included.
+/* Appends a text representation of VALUE to OUT.  Settings on PT control
+   whether variable and value labels are included (pass NULL for PT to get
+   default formatting in the absence of a pivot table).
 
    Subscripts and footnotes are included.
 
@@ -2485,9 +2494,10 @@ pivot_value_format_body (const struct pivot_value *value,
    otherwise.  */
 bool
 pivot_value_format (const struct pivot_value *value,
-                    const struct pivot_table *pt,
+                    const struct pivot_table *pt_,
                     struct string *out)
 {
+  const struct pivot_table *pt = pt_ ? pt_ : &pivot_value_format_defaults;
   bool numeric = pivot_value_format_body (value, pt, out);
 
   const struct pivot_value_ex *ex = value->ex;
@@ -2515,7 +2525,9 @@ pivot_value_format (const struct pivot_value *value,
 }
 
 /* Returns a text representation of VALUE.  The caller must free the string,
-   with free(). */
+   with free().  Settings on PT control whether variable and value labels are
+   included (pass NULL for PT to get default formatting in the absence of a
+   pivot table). */
 char *
 pivot_value_to_string (const struct pivot_value *value,
                        const struct pivot_table *pt)
@@ -2525,17 +2537,6 @@ pivot_value_to_string (const struct pivot_value *value,
   return ds_steal_cstr (&s);
 }
 
-char *
-pivot_value_to_string_defaults (const struct pivot_value *value)
-{
-  static const struct pivot_table pt = {
-    .show_values = SETTINGS_VALUE_SHOW_DEFAULT,
-    .show_variables = SETTINGS_VALUE_SHOW_DEFAULT,
-    .settings = FMT_SETTINGS_INIT,
-  };
-  return pivot_value_to_string (value, &pt);
-}
-
 struct pivot_value *
 pivot_value_clone (const struct pivot_value *old)
 {
@@ -3043,10 +3044,8 @@ struct pivot_splits_value
 struct pivot_splits_var
   {
     struct pivot_dimension *dimension;
-    char *name;
+    const struct variable *var;
     int width;
-    size_t idx;
-    struct fmt_spec format;
     struct hmap values;
   };
 
@@ -3092,11 +3091,9 @@ pivot_splits_create (struct pivot_table *pt,
 
       *psvar = (struct pivot_splits_var) {
         .width = var_get_width (var),
-        .name = xstrdup (var_get_name (var)),
-        .idx = var_get_case_index (var),
-        .format = *var_get_print_format (var),
         .values = HMAP_INITIALIZER (psvar->values),
         .dimension = d,
+        .var = var,
       };
     }
 
@@ -3136,7 +3133,6 @@ pivot_splits_destroy (struct pivot_splits *ps)
           hmap_delete (&psvar->values, &psval->hmap_node);
           free (psval);
         }
-      free (psvar->name);
       hmap_destroy (&psvar->values);
     }
   free (ps->vars);
@@ -3173,7 +3169,7 @@ pivot_splits_new_split (struct pivot_splits *ps, const struct ccase *example)
   for (size_t i = 0; i < ps->n; i++)
     {
       struct pivot_splits_var *psvar = &ps->vars[i];
-      const union value *value = case_data_idx (example, psvar->idx);
+      const union value *value = case_data (example, psvar->var);
       struct pivot_splits_value *psval = pivot_splits_value_find (psvar, value);
       if (!psval)
         {
@@ -3183,8 +3179,7 @@ pivot_splits_new_split (struct pivot_splits *ps, const struct ccase *example)
           value_clone (&psval->value, value, psvar->width);
           psval->leaf = pivot_category_create_leaf (
             psvar->dimension->root,
-            pivot_value_new_value (value, psvar->width, &psvar->format,
-                                   ps->encoding));
+            pivot_value_new_var_value (psvar->var, value));
           n_new++;
         }
 
@@ -3202,10 +3197,11 @@ pivot_splits_new_split (struct pivot_splits *ps, const struct ccase *example)
                 ds_put_cstr (&s, ", ");
 
               struct pivot_splits_var *psvar = &ps->vars[i];
-              const union value *value = case_data_idx (example, psvar->idx);
-              ds_put_format (&s, "%s = ", psvar->name);
+              const union value *value = case_data (example, psvar->var);
+              ds_put_format (&s, "%s = ", var_get_name (psvar->var));
 
-              char *s2 = data_out (value, ps->encoding, &psvar->format,
+              char *s2 = data_out (value, ps->encoding,
+                                   var_get_print_format (psvar->var),
                                    settings_get_fmt_settings ());
               ds_put_cstr (&s, s2 + strspn (s2, " "));
               free (s2);
@@ -3220,11 +3216,10 @@ pivot_splits_new_split (struct pivot_splits *ps, const struct ccase *example)
         }
 
       struct pivot_splits_var *psvar = &ps->vars[0];
-      const union value *value = case_data_idx (example, psvar->idx);
+      const union value *value = case_data (example, psvar->var);
       ps->dindexes[0] = pivot_category_create_leaf (
         psvar->dimension->root,
-        pivot_value_new_value (value, psvar->width, &psvar->format,
-                               ps->encoding));
+        pivot_value_new_var_value (psvar->var, value));
     }
 }