0c55ce68e0eaaa96cdb6052c53ec29fdc2a0f5d8
[pspp-builds.git] / src / language / utilities / title.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000, 2007 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    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, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include <ctype.h>
22 #include <stdlib.h>
23
24 #include <data/dictionary.h>
25 #include <data/procedure.h>
26 #include <data/variable.h>
27 #include <language/command.h>
28 #include <language/lexer/lexer.h>
29 #include <libpspp/alloc.h>
30 #include <libpspp/message.h>
31 #include <libpspp/start-date.h>
32 #include <libpspp/version.h>
33 #include <output/output.h>
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37
38 static int get_title (struct lexer *, const char *cmd, char **title);
39
40 int
41 cmd_title (struct lexer *lexer, struct dataset *ds UNUSED)
42 {
43   return get_title (lexer, "TITLE", &outp_title);
44 }
45
46 int
47 cmd_subtitle (struct lexer *lexer, struct dataset *ds UNUSED)
48 {
49   return get_title (lexer, "SUBTITLE", &outp_subtitle);
50 }
51
52 static int
53 get_title (struct lexer *lexer, const char *cmd, char **title)
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       if (*title)
64         free (*title);
65       *title = ds_xstrdup (lex_tokstr (lexer));
66       lex_get (lexer);
67       if (lex_token (lexer) != '.')
68         {
69           msg (SE, _("%s: `.' expected after string."), cmd);
70           return CMD_FAILURE;
71         }
72     }
73   else
74     {
75       char *cp;
76
77       if (*title)
78         free (*title);
79       *title = xstrdup (lex_rest_of_line (lexer));
80       lex_discard_line (lexer);
81       for (cp = *title; *cp; cp++)
82         *cp = toupper ((unsigned char) (*cp));
83     }
84   return CMD_SUCCESS;
85 }
86
87 /* Performs the FILE LABEL command. */
88 int
89 cmd_file_label (struct lexer *lexer, struct dataset *ds)
90 {
91   const char *label;
92
93   label = lex_rest_of_line (lexer);
94   lex_discard_line (lexer);
95   while (isspace ((unsigned char) *label))
96     label++;
97
98   dict_set_label (dataset_dict (ds), label);
99
100   return CMD_SUCCESS;
101 }
102
103 /* Add entry date line to DICT's documents. */
104 static void
105 add_document_trailer (struct dictionary *dict)
106 {
107   char buf[64];
108
109   sprintf (buf, _("   (Entered %s)"), get_start_date ());
110   dict_add_document_line (dict, buf);
111 }
112
113 /* Performs the DOCUMENT command. */
114 int
115 cmd_document (struct lexer *lexer, struct dataset *ds)
116 {
117   struct dictionary *dict = dataset_dict (ds);
118   struct string line = DS_EMPTY_INITIALIZER;
119   bool end_dot;
120
121   do
122     {
123       end_dot = lex_end_dot (lexer);
124       ds_assign_string (&line, lex_entire_line_ds (lexer));
125       if (end_dot)
126         ds_put_char (&line, '.');
127       dict_add_document_line (dict, ds_cstr (&line));
128
129       lex_discard_line (lexer);
130       lex_get_line (lexer);
131     }
132   while (!end_dot);
133
134   add_document_trailer (dict);
135
136   return CMD_SUCCESS;
137 }
138
139 /* Performs the DROP DOCUMENTS command. */
140 int
141 cmd_drop_documents (struct lexer *lexer, struct dataset *ds)
142 {
143   dict_clear_documents (dataset_dict (ds));
144
145   return lex_end_of_command (lexer);
146 }
147
148
149 /* Performs the ADD DOCUMENTS command. */
150 int
151 cmd_add_documents (struct lexer *lexer, struct dataset *ds)
152 {
153   struct dictionary *dict = dataset_dict (ds);
154
155   if ( ! lex_force_string (lexer) )
156     return CMD_FAILURE;
157
158   while ( lex_is_string (lexer))
159     {
160       dict_add_document_line (dict, ds_cstr (lex_tokstr (lexer)));
161       lex_get (lexer);
162     }
163
164   add_document_trailer (dict);
165
166   return lex_end_of_command (lexer) ;
167 }