X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Foutput%2Fodt.c;h=521e5b877fd1f905681ce9f3c67cbf384f1acdaa;hb=03de35c0fe7c134458408dcb32c23bba8b1bb078;hp=49a55b4ccef3cbf9d3827607f673397d2f3cb670;hpb=f550aee00a62fe1d8baf62d83cd7efef6cc2ee92;p=pspp diff --git a/src/output/odt.c b/src/output/odt.c index 49a55b4cce..521e5b877f 100644 --- a/src/output/odt.c +++ b/src/output/odt.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2009, 2010 Free Software Foundation, Inc. + Copyright (C) 2009-2014 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 @@ -31,9 +31,12 @@ #include "libpspp/assertion.h" #include "libpspp/cast.h" +#include "libpspp/message.h" #include "libpspp/str.h" +#include "libpspp/temp-file.h" #include "libpspp/version.h" #include "libpspp/zip-writer.h" +#include "data/file-handle-def.h" #include "output/driver-provider.h" #include "output/message-item.h" #include "output/options.h" @@ -43,12 +46,11 @@ #include "output/text-item.h" #include "gl/xalloc.h" -#include "gl/error.h" #include "gettext.h" #define _(msgid) gettext (msgid) -#define _xml(X) (const xmlChar *)(X) +#define _xml(X) (CHAR_CAST (const xmlChar *, X)) struct odt_driver { @@ -70,6 +72,9 @@ struct odt_driver /* Name of current command. */ char *command_name; + + /* Number of footnotes so far. */ + int n_footnotes; }; static const struct output_driver_class odt_driver_class; @@ -87,16 +92,16 @@ create_mimetype (struct zip_writer *zip) { FILE *fp; - fp = tmpfile (); + fp = create_temp_file (); if (fp == NULL) { - error (0, errno, _("error creating temporary file")); + msg_error (errno, _("error creating temporary file")); return false; } fprintf (fp, "application/vnd.oasis.opendocument.text"); zip_writer_add (zip, fp, "mimetype"); - fclose (fp); + close_temp_file (fp); return true; } @@ -107,7 +112,7 @@ static void create_writer (FILE **file, xmlTextWriterPtr *w) { /* XXX this can fail */ - *file = tmpfile (); + *file = create_temp_file (); *w = xmlNewTextWriter (xmlOutputBufferCreateFile (*file, NULL)); xmlTextWriterStartDocument (*w, NULL, "UTF-8", NULL); @@ -218,7 +223,7 @@ write_style_data (struct odt_driver *odt) xmlTextWriterEndDocument (w); xmlFreeTextWriter (w); zip_writer_add (odt->zip, file, "styles.xml"); - fclose (file); + close_temp_file (file); } static void @@ -240,7 +245,7 @@ write_meta_data (struct odt_driver *odt) xmlTextWriterStartElement (w, _xml ("office:meta")); { xmlTextWriterStartElement (w, _xml ("meta:generator")); - xmlTextWriterWriteString (w, _xml (stat_version)); + xmlTextWriterWriteString (w, _xml (version)); xmlTextWriterEndElement (w); } @@ -282,16 +287,17 @@ write_meta_data (struct odt_driver *odt) xmlTextWriterEndDocument (w); xmlFreeTextWriter (w); zip_writer_add (odt->zip, file, "meta.xml"); - fclose (file); + close_temp_file (file); } static struct output_driver * -odt_create (const char *file_name, enum settings_output_devices device_type, +odt_create (struct file_handle *fh, enum settings_output_devices device_type, struct string_map *o UNUSED) { struct output_driver *d; struct odt_driver *odt; struct zip_writer *zip; + const char *file_name = fh_get_file_name (fh); zip = zip_writer_create (file_name); if (zip == NULL) @@ -299,6 +305,7 @@ odt_create (const char *file_name, enum settings_output_devices device_type, odt = xzalloc (sizeof *odt); d = &odt->driver; + output_driver_init (d, &odt_driver_class, file_name, device_type); odt->zip = zip; @@ -355,7 +362,7 @@ odt_create (const char *file_name, enum settings_output_devices device_type, xmlTextWriterEndDocument (odt->manifest_wtr); xmlFreeTextWriter (odt->manifest_wtr); zip_writer_add (odt->zip, odt->manifest_file, "META-INF/manifest.xml"); - fclose (odt->manifest_file); + close_temp_file (odt->manifest_file); return d; } @@ -374,30 +381,94 @@ odt_destroy (struct output_driver *driver) xmlTextWriterEndDocument (odt->content_wtr); xmlFreeTextWriter (odt->content_wtr); zip_writer_add (odt->zip, odt->content_file, "content.xml"); - fclose (odt->content_file); + close_temp_file (odt->content_file); zip_writer_close (odt->zip); } + free (odt->file_name); free (odt->command_name); free (odt); } static void -odt_submit_table (struct odt_driver *odt, struct table_item *item) +write_xml_with_line_breaks (struct odt_driver *odt, const char *line_) +{ + xmlTextWriterPtr writer = odt->content_wtr; + + if (!strchr (line_, '\n')) + xmlTextWriterWriteString (writer, _xml(line_)); + else + { + char *line = xstrdup (line_); + char *newline; + char *p; + + for (p = line; *p; p = newline + 1) + { + newline = strchr (p, '\n'); + + if (!newline) + { + xmlTextWriterWriteString (writer, _xml(p)); + free (line); + return; + } + + if (newline > p && newline[-1] == '\r') + newline[-1] = '\0'; + else + *newline = '\0'; + xmlTextWriterWriteString (writer, _xml(p)); + xmlTextWriterWriteElement (writer, _xml("text:line-break"), _xml("")); + } + } +} + +static void +write_footnote (struct odt_driver *odt, const char *footnote) +{ + char marker[16]; + + xmlTextWriterStartElement (odt->content_wtr, _xml("text:note")); + xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:note-class"), + _xml("footnote")); + + xmlTextWriterStartElement (odt->content_wtr, _xml("text:note-citation")); + str_format_26adic (++odt->n_footnotes, false, marker, sizeof marker); + if (strlen (marker) > 1) + xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("text:label"), + "(%s)", marker); + else + xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:label"), + _xml(marker)); + xmlTextWriterEndElement (odt->content_wtr); + + xmlTextWriterStartElement (odt->content_wtr, _xml("text:note-body")); + xmlTextWriterStartElement (odt->content_wtr, _xml("text:p")); + write_xml_with_line_breaks (odt, footnote); + xmlTextWriterEndElement (odt->content_wtr); + xmlTextWriterEndElement (odt->content_wtr); + + xmlTextWriterEndElement (odt->content_wtr); +} + +static void +write_table (struct odt_driver *odt, const struct table_item *item) { const struct table *tab = table_item_get_table (item); const char *caption = table_item_get_caption (item); + const char *title = table_item_get_title (item); int r, c; /* Write a heading for the table */ - if (caption != NULL) + if (title != NULL) { xmlTextWriterStartElement (odt->content_wtr, _xml("text:h")); - xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("text:level"), - "%d", 2); + xmlTextWriterWriteFormatAttribute (odt->content_wtr, + _xml("text:outline-level"), "%d", 2); xmlTextWriterWriteString (odt->content_wtr, - _xml (table_item_get_caption (item)) ); + _xml (table_item_get_title (item)) ); xmlTextWriterEndElement (odt->content_wtr); } @@ -428,6 +499,7 @@ odt_submit_table (struct odt_driver *odt, struct table_item *item) for (c = 0 ; c < table_nc (tab) ; ++c) { struct table_cell cell; + size_t i; table_get_cell (tab, c, r, &cell); @@ -449,17 +521,32 @@ odt_submit_table (struct odt_driver *odt, struct table_item *item) odt->content_wtr, _xml("table:number-rows-spanned"), "%d", rowspan); - xmlTextWriterStartElement (odt->content_wtr, _xml("text:p")); + for (i = 0; i < cell.n_contents; i++) + { + const struct cell_contents *contents = &cell.contents[i]; + int j; + + if (contents->text) + { + xmlTextWriterStartElement (odt->content_wtr, _xml("text:p")); + + if ( r < table_ht (tab) || c < table_hl (tab) ) + xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading")); + else + xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents")); - if ( r < table_ht (tab) || c < table_hl (tab) ) - xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading")); - else - xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents")); + write_xml_with_line_breaks (odt, contents->text); - xmlTextWriterWriteString (odt->content_wtr, _xml(cell.contents)); + for (j = 0; j < contents->n_footnotes; j++) + write_footnote (odt, contents->footnotes[j]); - xmlTextWriterEndElement (odt->content_wtr); /* text:p */ - xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */ + xmlTextWriterEndElement (odt->content_wtr); /* text:p */ + } + else if (contents->table) + write_table (odt, contents->table); + + } + xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */ } else { @@ -477,6 +564,18 @@ odt_submit_table (struct odt_driver *odt, struct table_item *item) } xmlTextWriterEndElement (odt->content_wtr); /* table */ + + /* Write a caption for the table */ + if (caption != NULL) + { + xmlTextWriterStartElement (odt->content_wtr, _xml("text:h")); + xmlTextWriterWriteFormatAttribute (odt->content_wtr, + _xml("text:outline-level"), "%d", 2); + xmlTextWriterWriteString (odt->content_wtr, + _xml (table_item_get_caption (item)) ); + xmlTextWriterEndElement (odt->content_wtr); + } + } static void @@ -497,11 +596,13 @@ odt_submit (struct output_driver *driver, output_driver_track_current_command (output_item, &odt->command_name); if (is_table_item (output_item)) - odt_submit_table (odt, to_table_item (output_item)); + write_table (odt, to_table_item (output_item)); else if (is_text_item (output_item)) { - /* XXX apply different styles based on text_item's type. */ - odt_output_text (odt, text_item_get_text (to_text_item (output_item))); + struct text_item *text_item = to_text_item (output_item); + + if (text_item_get_type (text_item) != TEXT_ITEM_COMMAND_CLOSE) + odt_output_text (odt, text_item_get_text (text_item)); } else if (is_message_item (output_item)) { @@ -513,7 +614,8 @@ odt_submit (struct output_driver *driver, } } -struct output_driver_factory odt_driver_factory = { "odt", odt_create }; +struct output_driver_factory odt_driver_factory = + { "odt", "pspp.odf", odt_create }; static const struct output_driver_class odt_driver_class = {