ddfcc3f42ee33d26099d4121f8fd5c6fe856f756
[pspp] / src / output / message-item.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2010 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/message-item.h"
20
21 #include <stdlib.h>
22
23 #include "libpspp/message.h"
24 #include "output/driver.h"
25 #include "output/output-item-provider.h"
26 #include "output/text-item.h"
27
28 #include "gl/xalloc.h"
29
30 struct message_item *
31 message_item_create (const struct msg *msg)
32 {
33   struct message_item *item;
34
35   item = xmalloc (sizeof *msg);
36   output_item_init (&item->output_item, &message_item_class);
37   item->msg = msg_dup (msg);
38
39   return item;
40 }
41
42 const struct msg *
43 message_item_get_msg (const struct message_item *item)
44 {
45   return item->msg;
46 }
47
48 struct text_item *
49 message_item_to_text_item (struct message_item *message_item)
50 {
51   struct text_item *text_item = text_item_create_nocopy (
52     TEXT_ITEM_LOG, msg_to_string (message_item_get_msg (message_item)));
53   message_item_unref (message_item);
54   return text_item;
55 }
56
57 static void
58 message_item_destroy (struct output_item *output_item)
59 {
60   struct message_item *item = to_message_item (output_item);
61   msg_destroy (item->msg);
62   free (item);
63 }
64
65 /* Submits ITEM to the configured output drivers, and transfers ownership to
66    the output subsystem. */
67 void
68 message_item_submit (struct message_item *item)
69 {
70   output_submit (&item->output_item);
71 }
72
73 const struct output_item_class message_item_class =
74   {
75     "message",
76     message_item_destroy,
77   };