Consolidate quoting style in printed strings.
[pspp] / src / output / msglog.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/msglog.h"
20
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25
26 #include "data/file-name.h"
27 #include "data/settings.h"
28 #include "libpspp/cast.h"
29 #include "output/driver-provider.h"
30 #include "output/message-item.h"
31
32 #include "gl/error.h"
33 #include "gl/fwriteerror.h"
34 #include "gl/xalloc.h"
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38
39 struct msglog_driver
40   {
41     struct output_driver driver;
42     FILE *file;
43     char *file_name;
44     char *command_name;
45   };
46
47 static const struct output_driver_class msglog_class;
48
49 static struct msglog_driver *
50 msglog_driver_cast (struct output_driver *driver)
51 {
52   assert (driver->class == &msglog_class);
53   return UP_CAST (driver, struct msglog_driver, driver);
54 }
55
56 struct output_driver *
57 msglog_create (const char *file_name)
58 {
59   enum settings_output_devices type;
60   struct msglog_driver *ml;
61   FILE *file;
62
63   file = fn_open (file_name, "w");
64   if (file == NULL)
65     {
66       error (0, errno, _("error opening output file `%s'"), file_name);
67       return NULL;
68     }
69
70   type = (!strcmp (file_name, "-") || isatty (fileno (file))
71           ? SETTINGS_DEVICE_TERMINAL
72           : SETTINGS_DEVICE_UNFILTERED);
73
74   ml = xzalloc (sizeof *ml);
75   output_driver_init (&ml->driver, &msglog_class, file_name, type);
76   ml->file = file;
77   ml->file_name = xstrdup (file_name);
78   ml->command_name = NULL;
79
80   output_driver_register (&ml->driver);
81
82   return &ml->driver;
83 }
84
85 static void
86 msglog_destroy (struct output_driver *driver)
87 {
88   struct msglog_driver *ml = msglog_driver_cast (driver);
89
90   fn_close (ml->file_name, ml->file);
91   free (ml->file_name);
92   free (ml->command_name);
93   free (ml);
94 }
95
96 static void
97 msglog_submit (struct output_driver *driver, const struct output_item *item)
98 {
99   struct msglog_driver *ml = msglog_driver_cast (driver);
100
101   output_driver_track_current_command (item, &ml->command_name);
102
103   if (is_message_item (item))
104     {
105       const struct message_item *message_item = to_message_item (item);
106       const struct msg *msg = message_item_get_msg (message_item);
107       char *s = msg_to_string (msg, ml->command_name);
108       fprintf (ml->file, "%s\n", s);
109       free (s);
110     }
111 }
112
113 static const struct output_driver_class msglog_class =
114   {
115     "msglog",
116     msglog_destroy,
117     msglog_submit,
118     NULL
119   };