398288b812c9d966c6e6de13a76edeffbb784f06
[pspp-builds.git] / 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/dictionary.h"
23 #include "data/procedure.h"
24 #include "data/variable.h"
25 #include "language/command.h"
26 #include "language/lexer/lexer.h"
27 #include "libpspp/message.h"
28 #include "libpspp/start-date.h"
29 #include "libpspp/version.h"
30 #include "output/text-item.h"
31
32 #include "gl/xalloc.h"
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37 static int parse_title (struct lexer *, enum text_item_type);
38 static void set_title (const char *title, enum text_item_type);
39
40 int
41 cmd_title (struct lexer *lexer, struct dataset *ds UNUSED)
42 {
43   return parse_title (lexer, TEXT_ITEM_TITLE);
44 }
45
46 int
47 cmd_subtitle (struct lexer *lexer, struct dataset *ds UNUSED)
48 {
49   return parse_title (lexer, TEXT_ITEM_SUBTITLE);
50 }
51
52 static int
53 parse_title (struct lexer *lexer, enum text_item_type type)
54 {
55   if (!lex_force_string (lexer))
56     return CMD_FAILURE;
57   set_title (lex_tokcstr (lexer), type);
58   lex_get (lexer);
59   return CMD_SUCCESS;
60 }
61
62 static void
63 set_title (const char *title, enum text_item_type type)
64 {
65   text_item_submit (text_item_create (type, title));
66 }
67
68 /* Performs the FILE LABEL command. */
69 int
70 cmd_file_label (struct lexer *lexer, struct dataset *ds)
71 {
72   if (!lex_force_string (lexer))
73     return CMD_FAILURE;
74
75   dict_set_label (dataset_dict (ds), lex_tokcstr (lexer));
76   lex_get (lexer);
77
78   return CMD_SUCCESS;
79 }
80
81 /* Performs the DOCUMENT command. */
82 int
83 cmd_document (struct lexer *lexer, struct dataset *ds)
84 {
85   struct dictionary *dict = dataset_dict (ds);
86   char *trailer;
87
88   if (!lex_force_string (lexer))
89     return CMD_FAILURE;
90
91   while (lex_is_string (lexer))
92     {
93       dict_add_document_line (dict, lex_tokcstr (lexer), true);
94       lex_get (lexer);
95     }
96
97   trailer = xasprintf (_("   (Entered %s)"), get_start_date ());
98   dict_add_document_line (dict, trailer, true);
99   free (trailer);
100
101   return CMD_SUCCESS;
102 }
103
104 /* Performs the ADD DOCUMENTS command. */
105 int
106 cmd_add_documents (struct lexer *lexer, struct dataset *ds)
107 {
108   return cmd_document (lexer, ds);
109 }
110
111 /* Performs the DROP DOCUMENTS command. */
112 int
113 cmd_drop_documents (struct lexer *lexer UNUSED, struct dataset *ds)
114 {
115   dict_clear_documents (dataset_dict (ds));
116   return CMD_SUCCESS;
117 }