c7c0f91c9cea8ca20911661fa0ee7698f72d95bf
[pspp-builds.git] / src / output / table-item.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009 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/table-item.h>
28
29 #include "gl/xalloc.h"
30
31 /* Initializes ITEM as a table item for rendering TABLE.  The new table item
32    initially has the specified CAPTION, which may be NULL if no caption is yet
33    available.  The caller retains ownership of CAPTION. */
34 struct table_item *
35 table_item_create (struct table *table, const char *caption)
36 {
37   struct table_item *item = xmalloc (sizeof *item);
38   output_item_init (&item->output_item, &table_item_class);
39   item->table = table;
40   item->caption = caption != NULL ? xstrdup (caption) : NULL;
41   return item;
42 }
43
44 /* Returns the table contained by TABLE_ITEM.  The caller must not modify or
45    unref the returned table. */
46 const struct table *
47 table_item_get_table (const struct table_item *table_item)
48 {
49   return table_item->table;
50 }
51
52 /* Returns ITEM's caption, which is a null pointer if no caption has been
53    set. */
54 const char *
55 table_item_get_caption (const struct table_item *item)
56 {
57   return item->caption;
58 }
59
60 /* Sets ITEM's caption to CAPTION, replacing any previous caption.  Specify
61    NULL for CAPTION to clear any caption from ITEM.  The caller retains
62    ownership of CAPTION.
63
64    This function may only be used on a table_item that is unshared. */
65 void
66 table_item_set_caption (struct table_item *item, const char *caption)
67 {
68   assert (!table_item_is_shared (item));
69   free (item->caption);
70   item->caption = caption != NULL ? xstrdup (caption) : NULL;
71 }
72
73 /* Submits TABLE_ITEM to the configured output drivers, and transfers ownership
74    to the output subsystem. */
75 void
76 table_item_submit (struct table_item *table_item)
77 {
78   output_submit (&table_item->output_item);
79 }
80 \f
81 static void
82 table_item_destroy (struct output_item *output_item)
83 {
84   struct table_item *item = to_table_item (output_item);
85   free (item->caption);
86   table_unref (item->table);
87   free (item);
88 }
89
90 const struct output_item_class table_item_class =
91   {
92     table_item_destroy,
93   };