Rewrite PSPP output engine.
[pspp-builds.git] / src / language / utilities / title.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2007 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 "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   int c;
56
57   c = lex_look_ahead (lexer);
58   if (c == '"' || c == '\'')
59     {
60       lex_get (lexer);
61       if (!lex_force_string (lexer))
62         return CMD_FAILURE;
63       set_title (ds_cstr (lex_tokstr (lexer)), type);
64       lex_get (lexer);
65       return lex_end_of_command (lexer);
66     }
67   else
68     {
69       set_title (lex_rest_of_line (lexer), type);
70       lex_discard_line (lexer);
71     }
72   return CMD_SUCCESS;
73 }
74
75 static void
76 set_title (const char *title, enum text_item_type type)
77 {
78   text_item_submit (text_item_create (type, title));
79 }
80
81 /* Performs the FILE LABEL command. */
82 int
83 cmd_file_label (struct lexer *lexer, struct dataset *ds)
84 {
85   const char *label;
86
87   label = lex_rest_of_line (lexer);
88   lex_discard_line (lexer);
89   while (isspace ((unsigned char) *label))
90     label++;
91
92   dict_set_label (dataset_dict (ds), label);
93
94   return CMD_SUCCESS;
95 }
96
97 /* Add entry date line to DICT's documents. */
98 static void
99 add_document_trailer (struct dictionary *dict)
100 {
101   char buf[64];
102
103   sprintf (buf, _("   (Entered %s)"), get_start_date ());
104   dict_add_document_line (dict, buf);
105 }
106
107 /* Performs the DOCUMENT command. */
108 int
109 cmd_document (struct lexer *lexer, struct dataset *ds)
110 {
111   struct dictionary *dict = dataset_dict (ds);
112   struct string line = DS_EMPTY_INITIALIZER;
113   bool end_dot;
114
115   do
116     {
117       end_dot = lex_end_dot (lexer);
118       ds_assign_string (&line, lex_entire_line_ds (lexer));
119       if (end_dot)
120         ds_put_char (&line, '.');
121       dict_add_document_line (dict, ds_cstr (&line));
122
123       lex_discard_line (lexer);
124       lex_get_line (lexer);
125     }
126   while (!end_dot);
127
128   add_document_trailer (dict);
129   ds_destroy (&line);
130
131   return CMD_SUCCESS;
132 }
133
134 /* Performs the DROP DOCUMENTS command. */
135 int
136 cmd_drop_documents (struct lexer *lexer, struct dataset *ds)
137 {
138   dict_clear_documents (dataset_dict (ds));
139
140   return lex_end_of_command (lexer);
141 }
142
143
144 /* Performs the ADD DOCUMENTS command. */
145 int
146 cmd_add_documents (struct lexer *lexer, struct dataset *ds)
147 {
148   struct dictionary *dict = dataset_dict (ds);
149
150   if ( ! lex_force_string (lexer) )
151     return CMD_FAILURE;
152
153   while ( lex_is_string (lexer))
154     {
155       dict_add_document_line (dict, ds_cstr (lex_tokstr (lexer)));
156       lex_get (lexer);
157     }
158
159   add_document_trailer (dict);
160
161   return lex_end_of_command (lexer) ;
162 }