csv: Change footnote format.
[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 void
37 table_item_layer_copy (struct table_item_layer *dst,
38                        const struct table_item_layer *src)
39 {
40   dst->content = xstrdup (src->content);
41   dst->footnotes = xmemdup (src->footnotes,
42                             src->n_footnotes * sizeof *src->footnotes);
43   dst->n_footnotes = src->n_footnotes;
44 }
45  
46 void
47 table_item_layer_uninit (struct table_item_layer *layer)
48 {
49   if (layer)
50     {
51       free (layer->content);
52       free (layer->footnotes);
53     }
54 }
55
56 struct table_item_layers *
57 table_item_layers_clone (const struct table_item_layers *old)
58 {
59   if (!old)
60     return NULL;
61
62   struct table_item_layers *new = xmalloc (sizeof *new);
63   *new = (struct table_item_layers) {
64     .layers = xnmalloc (old->n_layers, sizeof *new->layers),
65     .n_layers = old->n_layers,
66     .style = table_area_style_clone (NULL, old->style),
67   };
68   for (size_t i = 0; i < new->n_layers; i++)
69     table_item_layer_copy (&new->layers[i], &old->layers[i]);
70   return new;
71 }
72
73 void
74 table_item_layers_destroy (struct table_item_layers *layers)
75 {
76   if (layers)
77     {
78       for (size_t i = 0; i < layers->n_layers; i++)
79         table_item_layer_uninit (&layers->layers[i]);
80       free (layers->layers);
81       table_area_style_free (layers->style);
82       free (layers);
83     }
84 }
85
86 /* Initializes ITEM as a table item for rendering TABLE.  Takes ownership of
87    TABLE. */
88 struct table_item *
89 table_item_create (struct table *table)
90 {
91   struct table_item *item = xmalloc (sizeof *item);
92   *item = (struct table_item) {
93     .output_item = OUTPUT_ITEM_INITIALIZER (&table_item_class),
94     .table = table,
95   };
96   return item;
97 }
98
99 /* Returns the table contained by TABLE_ITEM.  The caller must not modify or
100    unref the returned table. */
101 const struct table *
102 table_item_get_table (const struct table_item *table_item)
103 {
104   return table_item->table;
105 }
106
107 /* Returns ITEM's title, which is a null pointer if no title has been
108    set. */
109 const struct table_cell *
110 table_item_get_title (const struct table_item *item)
111 {
112   return item->title;
113 }
114
115 /* Sets ITEM's title to TITLE, replacing any previous title.  Specify NULL for
116    TITLE to clear any title from ITEM.  The caller retains ownership of TITLE.
117
118    This function may only be used on a table_item that is unshared. */
119 void
120 table_item_set_title (struct table_item *item, const struct table_cell *title)
121 {
122   assert (!table_item_is_shared (item));
123   table_cell_destroy (item->title);
124   item->title = table_cell_clone (title);
125 }
126
127 /* Returns ITEM's layers, which will be a null pointer if no layers have been
128    set. */
129 const struct table_item_layers *
130 table_item_get_layers (const struct table_item *item)
131 {
132   return item->layers;
133 }
134
135 /* Sets ITEM's layers to LAYERS, replacing any previous layers.  Specify NULL
136    for LAYERS to clear any layers from ITEM.  The caller retains ownership of
137    LAYERS.
138
139    This function may only be used on a table_item that is unshared. */
140 void
141 table_item_set_layers (struct table_item *item,
142                        const struct table_item_layers *layers)
143 {
144   assert (!table_item_is_shared (item));
145   table_item_layers_destroy (item->layers);
146   item->layers = table_item_layers_clone (layers);
147 }
148
149 /* Returns ITEM's caption, which is a null pointer if no caption has been
150    set. */
151 const struct table_cell *
152 table_item_get_caption (const struct table_item *item)
153 {
154   return item->caption;
155 }
156
157 /* Sets ITEM's caption to CAPTION, replacing any previous caption.  Specify
158    NULL for CAPTION to clear any caption from ITEM.  The caller retains
159    ownership of CAPTION.
160
161    This function may only be used on a table_item that is unshared. */
162 void
163 table_item_set_caption (struct table_item *item,
164                         const struct table_cell *caption)
165 {
166   assert (!table_item_is_shared (item));
167   table_cell_destroy (item->caption);
168   item->caption = table_cell_clone (caption);
169 }
170
171 /* Returns ITEM's notes, which is a null pointer if ITEM has no notes. */
172 const char *
173 table_item_get_notes (const struct table_item *item)
174 {
175   return item->notes;
176 }
177
178 /* Sets ITEM's notes to NOTES, replacing any previous notes.  Specify NULL for
179    NOTES to clear any notes from ITEM.  The caller retains ownership of
180    NOTES.
181
182    This function may only be used on a table_item that is unshared.*/
183 void
184 table_item_set_notes (struct table_item *item, const char *notes)
185 {
186   assert (!table_item_is_shared (item));
187   free (item->notes);
188   item->notes = notes ? xstrdup (notes) : NULL;
189 }
190
191 /* Submits TABLE_ITEM to the configured output drivers, and transfers ownership
192    to the output subsystem. */
193 void
194 table_item_submit (struct table_item *table_item)
195 {
196   output_submit (&table_item->output_item);
197 }
198 \f
199 static const char *
200 table_item_get_label (const struct output_item *output_item)
201 {
202   const struct table_item *item = to_table_item (output_item);
203   return (item->title && item->title->text
204           ? item->title->text
205           : _("Table"));
206 }
207
208 static void
209 table_item_destroy (struct output_item *output_item)
210 {
211   struct table_item *item = to_table_item (output_item);
212   table_cell_destroy (item->title);
213   table_cell_destroy (item->caption);
214   table_item_layers_destroy (item->layers);
215   free (item->notes);
216   pivot_table_unref (item->pt);
217   table_unref (item->table);
218   free (item);
219 }
220
221 const struct output_item_class table_item_class =
222   {
223     table_item_get_label,
224     table_item_destroy,
225   };