X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Futilities%2Ftitle.c;h=323bdf3fa51e79d35c375e8fc6a4dde22431a979;hb=e868585d6cc2e5a86b83ea3efade350b23e30188;hp=8b8ac77c2ea0af0f0cdd5f13c5e9dcbfe4a2fe7b;hpb=d8493b3b0617cc447446a70b031a69079bc19002;p=pspp diff --git a/src/language/utilities/title.c b/src/language/utilities/title.c index 8b8ac77c2e..323bdf3fa5 100644 --- a/src/language/utilities/title.c +++ b/src/language/utilities/title.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-9, 2000, 2007, 2010 Free Software Foundation, Inc. + Copyright (C) 1997-9, 2000, 2007, 2010, 2011 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -19,144 +19,116 @@ #include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "data/dataset.h" +#include "data/dictionary.h" +#include "data/variable.h" +#include "language/command.h" +#include "language/lexer/lexer.h" +#include "language/lexer/token.h" +#include "libpspp/message.h" +#include "libpspp/start-date.h" +#include "libpspp/version.h" +#include "output/driver.h" -#include "xalloc.h" +#include "gl/c-ctype.h" +#include "gl/xalloc.h" #include "gettext.h" #define _(msgid) gettext (msgid) -static int parse_title (struct lexer *, enum text_item_type); -static void set_title (const char *title, enum text_item_type); - -int -cmd_title (struct lexer *lexer, struct dataset *ds UNUSED) -{ - return parse_title (lexer, TEXT_ITEM_TITLE); -} - -int -cmd_subtitle (struct lexer *lexer, struct dataset *ds UNUSED) -{ - return parse_title (lexer, TEXT_ITEM_SUBTITLE); -} - static int -parse_title (struct lexer *lexer, enum text_item_type type) +parse_title (struct lexer *lexer, void (*set_title) (const char *)) { - int c; - - c = lex_look_ahead (lexer); - if (c == '"' || c == '\'') + if (lex_token (lexer) == T_STRING) { + set_title (lex_tokcstr (lexer)); lex_get (lexer); - if (!lex_force_string (lexer)) - return CMD_FAILURE; - set_title (ds_cstr (lex_tokstr (lexer)), type); - lex_get (lexer); - return lex_end_of_command (lexer); + } + else if (lex_token (lexer) == T_ENDCMD) + { + /* This would be a bad special case below because n-1 would be + SIZE_MAX. */ + set_title (""); } else { - set_title (lex_rest_of_line (lexer), type); - lex_discard_line (lexer); + /* Count the tokens in the title. */ + size_t n = 0; + while (lex_next (lexer, n)->type != T_ENDCMD) + n++; + + /* Get the raw representation of all the tokens, including any space + between them, and use it as the title. */ + char *title = lex_next_representation (lexer, 0, n - 1); + set_title (title); + free (title); + + /* Skip past the tokens. */ + for (size_t i = 0; i < n; i++) + lex_get (lexer); } return CMD_SUCCESS; } -static void -set_title (const char *title, enum text_item_type type) +int +cmd_title (struct lexer *lexer, struct dataset *ds UNUSED) { - text_item_submit (text_item_create (type, title)); + return parse_title (lexer, output_set_title); +} + +int +cmd_subtitle (struct lexer *lexer, struct dataset *ds UNUSED) +{ + return parse_title (lexer, output_set_subtitle); } /* Performs the FILE LABEL command. */ int cmd_file_label (struct lexer *lexer, struct dataset *ds) { - const char *label; - - label = lex_rest_of_line (lexer); - lex_discard_line (lexer); - while (isspace ((unsigned char) *label)) - label++; + if (!lex_force_string (lexer)) + return CMD_FAILURE; - dict_set_label (dataset_dict (ds), label); + dict_set_label (dataset_dict (ds), lex_tokcstr (lexer)); + lex_get (lexer); return CMD_SUCCESS; } -/* Add entry date line to DICT's documents. */ -static void -add_document_trailer (struct dictionary *dict) -{ - char buf[64]; - - sprintf (buf, _(" (Entered %s)"), get_start_date ()); - dict_add_document_line (dict, buf); -} - /* Performs the DOCUMENT command. */ int cmd_document (struct lexer *lexer, struct dataset *ds) { struct dictionary *dict = dataset_dict (ds); - struct string line = DS_EMPTY_INITIALIZER; - bool end_dot; + char *trailer; + + if (!lex_force_string (lexer)) + return CMD_FAILURE; - do + while (lex_is_string (lexer)) { - end_dot = lex_end_dot (lexer); - ds_assign_string (&line, lex_entire_line_ds (lexer)); - if (end_dot) - ds_put_byte (&line, '.'); - dict_add_document_line (dict, ds_cstr (&line)); - - lex_discard_line (lexer); - lex_get_line (lexer); + dict_add_document_line (dict, lex_tokcstr (lexer), true); + lex_get (lexer); } - while (!end_dot); - add_document_trailer (dict); - ds_destroy (&line); + trailer = xasprintf (_(" (Entered %s)"), get_start_date ()); + dict_add_document_line (dict, trailer, true); + free (trailer); return CMD_SUCCESS; } -/* Performs the DROP DOCUMENTS command. */ +/* Performs the ADD DOCUMENTS command. */ int -cmd_drop_documents (struct lexer *lexer, struct dataset *ds) +cmd_add_documents (struct lexer *lexer, struct dataset *ds) { - dict_clear_documents (dataset_dict (ds)); - - return lex_end_of_command (lexer); + return cmd_document (lexer, ds); } - -/* Performs the ADD DOCUMENTS command. */ +/* Performs the DROP DOCUMENTS command. */ int -cmd_add_documents (struct lexer *lexer, struct dataset *ds) +cmd_drop_documents (struct lexer *lexer UNUSED, struct dataset *ds) { - struct dictionary *dict = dataset_dict (ds); - - if ( ! lex_force_string (lexer) ) - return CMD_FAILURE; - - while ( lex_is_string (lexer)) - { - dict_add_document_line (dict, ds_cstr (lex_tokstr (lexer))); - lex_get (lexer); - } - - add_document_trailer (dict); - - return lex_end_of_command (lexer) ; + dict_clear_documents (dataset_dict (ds)); + return CMD_SUCCESS; }