output-item: Collapse the inheritance hierarchy into a single struct.
[pspp] / src / output / page-setup.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.h"
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "gl/xalloc.h"
25
26 bool
27 page_paragraph_equals (const struct page_paragraph *a,
28                        const struct page_paragraph *b)
29 {
30   return (!a || !b ? a == b
31           : !a->markup || !b->markup ? a->markup == b->markup
32           : !strcmp (a->markup, b->markup) && a->halign == b->halign);
33 }
34
35 void
36 page_heading_copy (struct page_heading *dst, const struct page_heading *src)
37 {
38   dst->n = src->n;
39   dst->paragraphs = xmalloc (dst->n * sizeof *dst->paragraphs);
40   for (size_t i = 0; i < dst->n; i++)
41     {
42       dst->paragraphs[i].markup = xstrdup (src->paragraphs[i].markup);
43       dst->paragraphs[i].halign = src->paragraphs[i].halign;
44     }
45 }
46
47 void
48 page_heading_uninit (struct page_heading *ph)
49 {
50   if (!ph)
51     return;
52
53   for (size_t i = 0; i < ph->n; i++)
54     free (ph->paragraphs[i].markup);
55   free (ph->paragraphs);
56 }
57
58 bool
59 page_heading_equals (const struct page_heading *a,
60                      const struct page_heading *b)
61 {
62   if (!a || !b)
63     return a == b;
64
65   if (a->n != b->n)
66     return false;
67
68   for (size_t i = 0; i < a->n; i++)
69     if (!page_paragraph_equals (&a->paragraphs[i], &b->paragraphs[i]))
70       return false;
71
72   return true;
73 }
74
75 struct page_setup *
76 page_setup_clone (const struct page_setup *old)
77 {
78   struct page_setup *new = xmalloc (sizeof *new);
79   *new = *old;
80   for (int i = 0; i < 2; i++)
81     page_heading_copy (&new->headings[i], &old->headings[i]);
82   if (new->file_name)
83     new->file_name = xstrdup (new->file_name);
84   return new;
85 }
86
87 void
88 page_setup_destroy (struct page_setup *ps)
89 {
90   if (ps)
91     {
92       for (int i = 0; i < 2; i++)
93         page_heading_uninit (&ps->headings[i]);
94       free (ps->file_name);
95       free (ps);
96     }
97 }