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