c9cecb0daac315ee6d9948b294ff8b6a5501d25f
[pspp] / src / output / page-setup-item.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2018 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/page-setup-item.h"
20
21 #include <stdlib.h>
22
23 #include "output/driver-provider.h"
24 #include "output/output-item-provider.h"
25
26 #include "gl/xalloc.h"
27
28 void
29 page_heading_copy (struct page_heading *dst, const struct page_heading *src)
30 {
31   dst->n = src->n;
32   dst->paragraphs = xmalloc (dst->n * sizeof *dst->paragraphs);
33   for (size_t i = 0; i < dst->n; i++)
34     {
35       dst->paragraphs[i].markup = xstrdup (src->paragraphs[i].markup);
36       dst->paragraphs[i].halign = src->paragraphs[i].halign;
37     }
38 }
39
40 void
41 page_heading_uninit (struct page_heading *ph)
42 {
43   if (!ph)
44     return;
45
46   for (size_t i = 0; i < ph->n; i++)
47     free (ph->paragraphs[i].markup);
48   free (ph->paragraphs);
49 }
50
51 struct page_setup *
52 page_setup_clone (const struct page_setup *old)
53 {
54   struct page_setup *new = xmalloc (sizeof *new);
55   *new = *old;
56   for (int i = 0; i < 2; i++)
57     page_heading_copy (&new->headings[i], &old->headings[i]);
58   if (new->file_name)
59     new->file_name = xstrdup (new->file_name);
60   return new;
61 }
62
63 void
64 page_setup_destroy (struct page_setup *ps)
65 {
66   if (ps)
67     {
68       for (int i = 0; i < 2; i++)
69         page_heading_uninit (&ps->headings[i]);
70       free (ps->file_name);
71       free (ps);
72     }
73 }
74 \f
75 struct page_setup_item *
76 page_setup_item_create (const struct page_setup *ps)
77 {
78   struct page_setup_item *item = xmalloc (sizeof *item);
79   output_item_init (&item->output_item, &page_setup_item_class);
80   item->page_setup = page_setup_clone (ps);
81   return item;
82 }
83
84 /* Submits ITEM to the configured output drivers, and transfers ownership to
85    the output subsystem. */
86 void
87 page_setup_item_submit (struct page_setup_item *item)
88 {
89   output_submit (&item->output_item);
90 }
91
92 static void
93 page_setup_item_destroy (struct output_item *output_item)
94 {
95   struct page_setup_item *item = to_page_setup_item (output_item);
96   page_setup_destroy (item->page_setup);
97   free (item);
98 }
99
100 const struct output_item_class page_setup_item_class =
101   {
102     page_setup_item_destroy,
103   };