1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 2011, 2014 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include "output/table-provider.h"
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"
30 #include "gl/xalloc.h"
32 struct table_item_text *
33 table_item_text_create (const char *content)
38 struct table_item_text *text = xmalloc (sizeof *text);
39 *text = (struct table_item_text) { .content = xstrdup (content) };
43 struct table_item_text *
44 table_item_text_clone (const struct table_item_text *old)
49 struct table_item_text *new = xmalloc (sizeof *new);
50 *new = (struct table_item_text) {
51 .content = xstrdup (old->content),
52 .footnotes = xmemdup (old->footnotes,
53 old->n_footnotes * sizeof *old->footnotes),
54 .n_footnotes = old->n_footnotes,
55 .style = area_style_clone (NULL, old->style),
61 table_item_text_destroy (struct table_item_text *text)
66 free (text->footnotes);
67 area_style_free (text->style);
73 table_item_layer_copy (struct table_item_layer *dst,
74 const struct table_item_layer *src)
76 dst->content = xstrdup (src->content);
77 dst->footnotes = xmemdup (src->footnotes,
78 src->n_footnotes * sizeof *src->footnotes);
79 dst->n_footnotes = src->n_footnotes;
83 table_item_layer_uninit (struct table_item_layer *layer)
87 free (layer->content);
88 free (layer->footnotes);
92 struct table_item_layers *
93 table_item_layers_clone (const struct table_item_layers *old)
98 struct table_item_layers *new = xmalloc (sizeof *new);
99 *new = (struct table_item_layers) {
100 .layers = xnmalloc (old->n_layers, sizeof *new->layers),
101 .n_layers = old->n_layers,
102 .style = area_style_clone (NULL, old->style),
104 for (size_t i = 0; i < new->n_layers; i++)
105 table_item_layer_copy (&new->layers[i], &old->layers[i]);
110 table_item_layers_destroy (struct table_item_layers *layers)
114 for (size_t i = 0; i < layers->n_layers; i++)
115 table_item_layer_uninit (&layers->layers[i]);
116 free (layers->layers);
117 area_style_free (layers->style);
122 /* Initializes ITEM as a table item for rendering TABLE. The new table item
123 initially has the specified TITLE and CAPTION, which may each be NULL. The
124 caller retains ownership of TITLE and CAPTION. */
126 table_item_create (struct table *table, const char *title, const char *caption)
128 struct table_item *item = xmalloc (sizeof *item);
129 output_item_init (&item->output_item, &table_item_class);
131 item->title = table_item_text_create (title);
133 item->caption = table_item_text_create (caption);
137 /* Returns the table contained by TABLE_ITEM. The caller must not modify or
138 unref the returned table. */
140 table_item_get_table (const struct table_item *table_item)
142 return table_item->table;
145 /* Returns ITEM's title, which is a null pointer if no title has been
147 const struct table_item_text *
148 table_item_get_title (const struct table_item *item)
153 /* Sets ITEM's title to TITLE, replacing any previous title. Specify NULL for
154 TITLE to clear any title from ITEM. The caller retains ownership of TITLE.
156 This function may only be used on a table_item that is unshared. */
158 table_item_set_title (struct table_item *item,
159 const struct table_item_text *title)
161 assert (!table_item_is_shared (item));
162 table_item_text_destroy (item->title);
163 item->title = table_item_text_clone (title);
166 /* Returns ITEM's layers, which will be a null pointer if no layers have been
168 const struct table_item_layers *
169 table_item_get_layers (const struct table_item *item)
174 /* Sets ITEM's layers to LAYERS, replacing any previous layers. Specify NULL
175 for LAYERS to clear any layers from ITEM. The caller retains ownership of
178 This function may only be used on a table_item that is unshared. */
180 table_item_set_layers (struct table_item *item,
181 const struct table_item_layers *layers)
183 assert (!table_item_is_shared (item));
184 table_item_layers_destroy (item->layers);
185 item->layers = table_item_layers_clone (layers);
188 /* Returns ITEM's caption, which is a null pointer if no caption has been
190 const struct table_item_text *
191 table_item_get_caption (const struct table_item *item)
193 return item->caption;
196 /* Sets ITEM's caption to CAPTION, replacing any previous caption. Specify
197 NULL for CAPTION to clear any caption from ITEM. The caller retains
198 ownership of CAPTION.
200 This function may only be used on a table_item that is unshared. */
202 table_item_set_caption (struct table_item *item,
203 const struct table_item_text *caption)
205 assert (!table_item_is_shared (item));
206 table_item_text_destroy (item->caption);
207 item->caption = table_item_text_clone (caption);
210 /* Submits TABLE_ITEM to the configured output drivers, and transfers ownership
211 to the output subsystem. */
213 table_item_submit (struct table_item *table_item)
215 output_submit (&table_item->output_item);
219 table_item_destroy (struct output_item *output_item)
221 struct table_item *item = to_table_item (output_item);
222 table_item_text_destroy (item->title);
223 table_item_text_destroy (item->caption);
224 table_item_layers_destroy (item->layers);
225 table_unref (item->table);
229 const struct output_item_class table_item_class =