output-item: Make command name part of every output_item.
[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 bool
29 page_paragraph_equals (const struct page_paragraph *a,
30                        const struct page_paragraph *b)
31 {
32   return (!a || !b ? a == b
33           : !a->markup || !b->markup ? a->markup == b->markup
34           : !strcmp (a->markup, b->markup) && a->halign == b->halign);
35 }
36
37 void
38 page_heading_copy (struct page_heading *dst, const struct page_heading *src)
39 {
40   dst->n = src->n;
41   dst->paragraphs = xmalloc (dst->n * sizeof *dst->paragraphs);
42   for (size_t i = 0; i < dst->n; i++)
43     {
44       dst->paragraphs[i].markup = xstrdup (src->paragraphs[i].markup);
45       dst->paragraphs[i].halign = src->paragraphs[i].halign;
46     }
47 }
48
49 void
50 page_heading_uninit (struct page_heading *ph)
51 {
52   if (!ph)
53     return;
54
55   for (size_t i = 0; i < ph->n; i++)
56     free (ph->paragraphs[i].markup);
57   free (ph->paragraphs);
58 }
59
60 bool
61 page_heading_equals (const struct page_heading *a,
62                      const struct page_heading *b)
63 {
64   if (!a || !b)
65     return a == b;
66
67   if (a->n != b->n)
68     return false;
69
70   for (size_t i = 0; i < a->n; i++)
71     if (!page_paragraph_equals (&a->paragraphs[i], &b->paragraphs[i]))
72       return false;
73
74   return true;
75 }
76
77 struct page_setup *
78 page_setup_clone (const struct page_setup *old)
79 {
80   struct page_setup *new = xmalloc (sizeof *new);
81   *new = *old;
82   for (int i = 0; i < 2; i++)
83     page_heading_copy (&new->headings[i], &old->headings[i]);
84   if (new->file_name)
85     new->file_name = xstrdup (new->file_name);
86   return new;
87 }
88
89 void
90 page_setup_destroy (struct page_setup *ps)
91 {
92   if (ps)
93     {
94       for (int i = 0; i < 2; i++)
95         page_heading_uninit (&ps->headings[i]);
96       free (ps->file_name);
97       free (ps);
98     }
99 }
100 \f
101 struct page_setup_item *
102 page_setup_item_create (const struct page_setup *ps)
103 {
104   struct page_setup_item *item = xmalloc (sizeof *item);
105   *item = (struct page_setup_item) {
106     .output_item = OUTPUT_ITEM_INITIALIZER (&page_setup_item_class),
107     .page_setup = page_setup_clone (ps),
108   };
109   return item;
110 }
111
112 /* Submits ITEM to the configured output drivers, and transfers ownership to
113    the output subsystem. */
114 void
115 page_setup_item_submit (struct page_setup_item *item)
116 {
117   output_submit (&item->output_item);
118 }
119
120 static const char *
121 page_setup_item_get_label (const struct output_item *output_item UNUSED)
122 {
123   /* Not marked for translation: user should never see it. */
124   return "Page Setup";
125 }
126
127 static void
128 page_setup_item_destroy (struct output_item *output_item)
129 {
130   struct page_setup_item *item = to_page_setup_item (output_item);
131   page_setup_destroy (item->page_setup);
132   free (item);
133 }
134
135 const struct output_item_class page_setup_item_class =
136   {
137     page_setup_item_get_label,
138     page_setup_item_destroy,
139   };