output: Expand footnote support.
[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/table-item.h"
28
29 #include "gl/xalloc.h"
30
31 struct table_item_text *
32 table_item_text_create (const char *content)
33 {
34   if (!content)
35     return NULL;
36
37   struct table_item_text *text = xmalloc (sizeof *text);
38   *text = (struct table_item_text) { .content = xstrdup (content) };
39   return text;
40 }
41
42 struct table_item_text *
43 table_item_text_clone (const struct table_item_text *old)
44 {
45   if (!old)
46     return NULL;
47
48   struct table_item_text *new = xmalloc (sizeof *new);
49   *new = (struct table_item_text) {
50     .content = xstrdup (old->content),
51     .footnotes = xmemdup (old->footnotes,
52                           old->n_footnotes * sizeof *old->footnotes),
53     .n_footnotes = old->n_footnotes,
54   };
55   return new;
56 }
57
58 void
59 table_item_text_destroy (struct table_item_text *text)
60 {
61   if (text)
62     {
63       free (text->content);
64       free (text->footnotes);
65       free (text);
66     }
67 }
68
69 /* Initializes ITEM as a table item for rendering TABLE.  The new table item
70    initially has the specified TITLE and CAPTION, which may each be NULL.  The
71    caller retains ownership of TITLE and CAPTION. */
72 struct table_item *
73 table_item_create (struct table *table, const char *title, const char *caption)
74 {
75   struct table_item *item = xmalloc (sizeof *item);
76   output_item_init (&item->output_item, &table_item_class);
77   item->table = table;
78   item->title = table_item_text_create (title);
79   item->caption = table_item_text_create (caption);
80   return item;
81 }
82
83 /* Returns the table contained by TABLE_ITEM.  The caller must not modify or
84    unref the returned table. */
85 const struct table *
86 table_item_get_table (const struct table_item *table_item)
87 {
88   return table_item->table;
89 }
90
91 /* Returns ITEM's title, which is a null pointer if no title has been
92    set. */
93 const struct table_item_text *
94 table_item_get_title (const struct table_item *item)
95 {
96   return item->title;
97 }
98
99 /* Sets ITEM's title to TITLE, replacing any previous title.  Specify NULL for
100    TITLE to clear any title from ITEM.  The caller retains ownership of TITLE.
101
102    This function may only be used on a table_item that is unshared. */
103 void
104 table_item_set_title (struct table_item *item,
105                       const struct table_item_text *title)
106 {
107   assert (!table_item_is_shared (item));
108   table_item_text_destroy (item->title);
109   item->title = table_item_text_clone (title);
110 }
111
112 /* Returns ITEM's caption, which is a null pointer if no caption has been
113    set. */
114 const struct table_item_text *
115 table_item_get_caption (const struct table_item *item)
116 {
117   return item->caption;
118 }
119
120 /* Sets ITEM's caption to CAPTION, replacing any previous caption.  Specify
121    NULL for CAPTION to clear any caption from ITEM.  The caller retains
122    ownership of CAPTION.
123
124    This function may only be used on a table_item that is unshared. */
125 void
126 table_item_set_caption (struct table_item *item,
127                         const struct table_item_text *caption)
128 {
129   assert (!table_item_is_shared (item));
130   table_item_text_destroy (item->caption);
131   item->caption = table_item_text_clone (caption);
132 }
133
134 /* Submits TABLE_ITEM to the configured output drivers, and transfers ownership
135    to the output subsystem. */
136 void
137 table_item_submit (struct table_item *table_item)
138 {
139   output_submit (&table_item->output_item);
140 }
141 \f
142 static void
143 table_item_destroy (struct output_item *output_item)
144 {
145   struct table_item *item = to_table_item (output_item);
146   free (item->title);
147   free (item->caption);
148   table_unref (item->table);
149   free (item);
150 }
151
152 const struct output_item_class table_item_class =
153   {
154     table_item_destroy,
155   };