output-item: Make command name part of every output_item.
[pspp] / src / output / table-item.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2011, 2014 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "output/table-provider.h"
20
21 #include <assert.h>
22
23 #include "libpspp/assertion.h"
24 #include "libpspp/cast.h"
25 #include "output/driver.h"
26 #include "output/output-item-provider.h"
27 #include "output/pivot-table.h"
28 #include "output/table-item.h"
29 #include "output/table-provider.h"
30
31 #include "gl/xalloc.h"
32
33 #include "gettext.h"
34 #define _(msgid) gettext (msgid)
35
36 /* Initializes ITEM as a table item for rendering PT.  Takes ownership of
37    PT. */
38 struct table_item *
39 table_item_create (struct pivot_table *pt)
40 {
41   pivot_table_assign_label_depth (pt);
42
43   struct table_item *item = xmalloc (sizeof *item);
44   *item = (struct table_item) {
45     .output_item = OUTPUT_ITEM_INITIALIZER (&table_item_class),
46     .output_item.command_name = xstrdup_if_nonnull (pt->command_c),
47     .pt = pt,
48   };
49   return item;
50 }
51
52 /* Submits TABLE_ITEM to the configured output drivers, and transfers ownership
53    to the output subsystem. */
54 void
55 table_item_submit (struct table_item *table_item)
56 {
57   output_submit (&table_item->output_item);
58 }
59 \f
60 static const char *
61 table_item_get_label (const struct output_item *output_item)
62 {
63   struct table_item *item = to_table_item (output_item);
64
65   if (!item->cached_label)
66     {
67       if (!item->pt->title)
68         return _("Table");
69
70       item->cached_label = pivot_value_to_string (item->pt->title, item->pt);
71     }
72   return item->cached_label;
73 }
74
75 static void
76 table_item_destroy (struct output_item *output_item)
77 {
78   struct table_item *item = to_table_item (output_item);
79   pivot_table_unref (item->pt);
80   free (item->cached_label);
81   free (item);
82 }
83
84 const struct output_item_class table_item_class =
85   {
86     table_item_get_label,
87     table_item_destroy,
88   };