cb21caa2e7c0e5addd823055fca48c1dc4b472e6
[pspp-builds.git] / src / output / text-item.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2011 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/text-item.h"
20
21 #include <assert.h>
22 #include <stdarg.h>
23 #include <stdlib.h>
24
25 #include "libpspp/cast.h"
26 #include "output/driver.h"
27 #include "output/output-item-provider.h"
28
29 #include "gl/xalloc.h"
30 #include "gl/xvasprintf.h"
31
32 #include "gettext.h"
33 #define _(msgid) gettext (msgid)
34
35 static struct text_item *
36 allocate_text_item (enum text_item_type type, char *text)
37 {
38   struct text_item *item = xmalloc (sizeof *item);
39   output_item_init (&item->output_item, &text_item_class);
40   item->text = text;
41   item->type = type;
42   return item;
43 }
44
45 /* Creates and returns a new text item containing a copy of TEXT and the
46    specified TYPE.  The caller retains ownership of TEXT. */
47 struct text_item *
48 text_item_create (enum text_item_type type, const char *text)
49 {
50   return allocate_text_item (type, xstrdup (text));
51 }
52
53 /* Creates and returns a new text item containing a copy of FORMAT, which is
54    formatted as if by printf(), and the specified TYPE.  The caller retains
55    ownership of FORMAT. */
56 struct text_item *
57 text_item_create_format (enum text_item_type type, const char *format, ...)
58 {
59   struct text_item *item;
60   va_list args;
61
62   va_start (args, format);
63   item = allocate_text_item (type, xvasprintf (format, args));
64   va_end (args);
65
66   return item;
67 }
68
69 /* Returns ITEM's type. */
70 enum text_item_type
71 text_item_get_type (const struct text_item *item)
72 {
73   return item->type;
74 }
75
76 /* Returns ITEM's text, which the caller may not modify or free. */
77 const char *
78 text_item_get_text (const struct text_item *item)
79 {
80   return item->text;
81 }
82
83 /* Submits ITEM to the configured output drivers, and transfers ownership to
84    the output subsystem. */
85 void
86 text_item_submit (struct text_item *item)
87 {
88   output_submit (&item->output_item);
89 }
90 \f
91 static void
92 text_item_destroy (struct output_item *output_item)
93 {
94   struct text_item *item = to_text_item (output_item);
95   free (item->text);
96   free (item);
97 }
98
99 const struct output_item_class text_item_class =
100   {
101     text_item_destroy,
102   };