output-item: Fix memory leak in output_item_dump().
[pspp] / src / output / output-item.c
index 5bfd1254134629be3d92bc437cbcffa6623f77c8..02aca446363f7d88395dfafa1aa161fad9606cbe 100644 (file)
@@ -28,7 +28,6 @@
 #include "libpspp/zip-reader.h"
 #include "output/chart.h"
 #include "output/driver.h"
-#include "output/page-setup.h"
 #include "output/pivot-table.h"
 
 #include "gl/xalloc.h"
@@ -47,7 +46,6 @@ output_item_type_to_string (enum output_item_type type)
     case OUTPUT_ITEM_IMAGE: return "image";
     case OUTPUT_ITEM_MESSAGE: return "message";
     case OUTPUT_ITEM_PAGE_BREAK: return "page break";
-    case OUTPUT_ITEM_PAGE_SETUP: return "page setup";
     case OUTPUT_ITEM_TABLE: return "table";
     case OUTPUT_ITEM_TEXT: return "text";
     }
@@ -88,6 +86,7 @@ output_item_unref (struct output_item *item)
             case OUTPUT_ITEM_GROUP:
               for (size_t i = 0; i < item->group.n_children; i++)
                 output_item_unref (item->group.children[i]);
+              free (item->group.children);
               break;
 
             case OUTPUT_ITEM_IMAGE:
@@ -101,10 +100,6 @@ output_item_unref (struct output_item *item)
             case OUTPUT_ITEM_PAGE_BREAK:
               break;
 
-            case OUTPUT_ITEM_PAGE_SETUP:
-              page_setup_destroy (item->page_setup);
-              break;
-
             case OUTPUT_ITEM_TABLE:
               pivot_table_unref (item->table);
               break;
@@ -170,7 +165,7 @@ output_item_unshare (struct output_item *old)
         = old->group.n_children;
 
       for (size_t i = 0; i < new->group.n_children; i++)
-        output_item_ref (new->group.children[i]);
+        new->group.children[i] = output_item_ref (new->group.children[i]);
       break;
 
     case OUTPUT_ITEM_IMAGE:
@@ -184,10 +179,6 @@ output_item_unshare (struct output_item *old)
     case OUTPUT_ITEM_PAGE_BREAK:
       break;
 
-    case OUTPUT_ITEM_PAGE_SETUP:
-      new->page_setup = page_setup_clone (old->page_setup);
-      break;
-
     case OUTPUT_ITEM_TABLE:
       new->table = pivot_table_ref (old->table);
       break;
@@ -254,10 +245,6 @@ output_item_get_label (const struct output_item *item)
     case OUTPUT_ITEM_PAGE_BREAK:
       return _("Page Break");
 
-    case OUTPUT_ITEM_PAGE_SETUP:
-      /* Not marked for translation: user should never see it. */
-      return "Page Setup";
-
     case OUTPUT_ITEM_TABLE:
       if (!item->cached_label)
         {
@@ -372,18 +359,17 @@ output_item_dump (const struct output_item *item, int indentation)
       printf ("page break\n");
       break;
 
-    case OUTPUT_ITEM_PAGE_SETUP:
-      printf ("page setup\n");
-      break;
-
     case OUTPUT_ITEM_TABLE:
       pivot_table_dump (item->table, indentation + 1);
       break;
 
     case OUTPUT_ITEM_TEXT:
-      printf ("text %s \"%s\"\n",
-              text_item_subtype_to_string (item->text.subtype),
-              pivot_value_to_string_defaults (item->text.content));
+      {
+        char *s = pivot_value_to_string_defaults (item->text.content);
+        printf ("text %s \"%s\"\n",
+                text_item_subtype_to_string (item->text.subtype), s);
+        free (s);
+      }
       break;
     }
 }
@@ -556,17 +542,6 @@ page_break_item_create (void)
   return item;
 }
 \f
-struct output_item *
-page_setup_item_create (const struct page_setup *ps)
-{
-  struct output_item *item = xmalloc (sizeof *item);
-  *item = (struct output_item) {
-    OUTPUT_ITEM_INITIALIZER (OUTPUT_ITEM_PAGE_SETUP),
-    .page_setup = page_setup_clone (ps),
-  };
-  return item;
-}
-\f
 /* Returns a new output_item for rendering TABLE.  Takes ownership of
    TABLE. */
 struct output_item *
@@ -615,17 +590,18 @@ text_item_create_value (enum text_item_subtype subtype,
 {
   if (subtype == TEXT_ITEM_SYNTAX || subtype == TEXT_ITEM_LOG)
     {
-      if (!value->font_style)
+      struct pivot_value_ex *ex = pivot_value_ex_rw (value);
+      if (!ex->font_style)
         {
-          value->font_style = xmalloc (sizeof *value->font_style);
-          *value->font_style = (struct font_style) FONT_STYLE_INITIALIZER;
+          ex->font_style = xmalloc (sizeof *value->ex->font_style);
+          *ex->font_style = (struct font_style) FONT_STYLE_INITIALIZER;
         }
 
-      free (value->font_style->typeface);
-      value->font_style->typeface = xstrdup ("Monospaced");
+      free (ex->font_style->typeface);
+      ex->font_style->typeface = xstrdup ("Monospaced");
     }
 
-  struct output_item *item = xzalloc (sizeof *item);
+  struct output_item *item = XZALLOC (struct output_item);
   *item = (struct output_item) {
     OUTPUT_ITEM_INITIALIZER (OUTPUT_ITEM_TEXT),
     .command_name = xstrdup_if_nonnull (output_get_command_name ()),
@@ -681,8 +657,9 @@ text_item_append (struct output_item *dst, const struct output_item *src)
   if (ds != ss
       || (ds != TEXT_ITEM_SYNTAX && ds != TEXT_ITEM_LOG)
       || strcmp (output_item_get_label (dst), output_item_get_label (src))
-      || !nullable_font_style_equal (dc->font_style, sc->font_style)
-      || (dc->font_style && dc->font_style->markup)
+      || !nullable_font_style_equal (dc->ex ? dc->ex->font_style : NULL,
+                                     sc->ex ? sc->ex->font_style : NULL)
+      || (dc->ex && dc->ex->font_style && dc->ex->font_style->markup)
       || sc->type != PIVOT_VALUE_TEXT
       || dc->type != PIVOT_VALUE_TEXT)
     return false;