323bdf3fa51e79d35c375e8fc6a4dde22431a979
[pspp] / src / language / utilities / title.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2007, 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 <ctype.h>
20 #include <stdlib.h>
21
22 #include "data/dataset.h"
23 #include "data/dictionary.h"
24 #include "data/variable.h"
25 #include "language/command.h"
26 #include "language/lexer/lexer.h"
27 #include "language/lexer/token.h"
28 #include "libpspp/message.h"
29 #include "libpspp/start-date.h"
30 #include "libpspp/version.h"
31 #include "output/driver.h"
32
33 #include "gl/c-ctype.h"
34 #include "gl/xalloc.h"
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38
39 static int
40 parse_title (struct lexer *lexer, void (*set_title) (const char *))
41 {
42   if (lex_token (lexer) == T_STRING)
43     {
44       set_title (lex_tokcstr (lexer));
45       lex_get (lexer);
46     }
47   else if (lex_token (lexer) == T_ENDCMD)
48     {
49       /* This would be a bad special case below because n-1 would be
50          SIZE_MAX. */
51       set_title ("");
52     }
53   else
54     {
55       /* Count the tokens in the title. */
56       size_t n = 0;
57       while (lex_next (lexer, n)->type != T_ENDCMD)
58         n++;
59
60       /* Get the raw representation of all the tokens, including any space
61          between them, and use it as the title. */
62       char *title = lex_next_representation (lexer, 0, n - 1);
63       set_title (title);
64       free (title);
65
66       /* Skip past the tokens. */
67       for (size_t i = 0; i < n; i++)
68         lex_get (lexer);
69     }
70   return CMD_SUCCESS;
71 }
72
73 int
74 cmd_title (struct lexer *lexer, struct dataset *ds UNUSED)
75 {
76   return parse_title (lexer, output_set_title);
77 }
78
79 int
80 cmd_subtitle (struct lexer *lexer, struct dataset *ds UNUSED)
81 {
82   return parse_title (lexer, output_set_subtitle);
83 }
84
85 /* Performs the FILE LABEL command. */
86 int
87 cmd_file_label (struct lexer *lexer, struct dataset *ds)
88 {
89   if (!lex_force_string (lexer))
90     return CMD_FAILURE;
91
92   dict_set_label (dataset_dict (ds), lex_tokcstr (lexer));
93   lex_get (lexer);
94
95   return CMD_SUCCESS;
96 }
97
98 /* Performs the DOCUMENT command. */
99 int
100 cmd_document (struct lexer *lexer, struct dataset *ds)
101 {
102   struct dictionary *dict = dataset_dict (ds);
103   char *trailer;
104
105   if (!lex_force_string (lexer))
106     return CMD_FAILURE;
107
108   while (lex_is_string (lexer))
109     {
110       dict_add_document_line (dict, lex_tokcstr (lexer), true);
111       lex_get (lexer);
112     }
113
114   trailer = xasprintf (_("   (Entered %s)"), get_start_date ());
115   dict_add_document_line (dict, trailer, true);
116   free (trailer);
117
118   return CMD_SUCCESS;
119 }
120
121 /* Performs the ADD DOCUMENTS command. */
122 int
123 cmd_add_documents (struct lexer *lexer, struct dataset *ds)
124 {
125   return cmd_document (lexer, ds);
126 }
127
128 /* Performs the DROP DOCUMENTS command. */
129 int
130 cmd_drop_documents (struct lexer *lexer UNUSED, struct dataset *ds)
131 {
132   dict_clear_documents (dataset_dict (ds));
133   return CMD_SUCCESS;
134 }