X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Foutput%2Fodt.c;h=4a87815548d43b2473e58f977e233870470e2663;hb=d55fbbb4fda554d02c93dd46c6fa66128060e6f0;hp=f02ae3defd559fbf54fb3504b78490d07b9182aa;hpb=4f5789a000760e2c6f9779b53b695d3e22e8f3e4;p=pspp diff --git a/src/output/odt.c b/src/output/odt.c index f02ae3defd..4a87815548 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 @@ -21,7 +21,9 @@ #include #include #include +#ifdef HAVE_PWD_H #include +#endif #include #include #include @@ -29,98 +31,71 @@ #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" -#include "output/tab.h" +#include "output/pivot-table.h" +#include "output/pivot-output.h" #include "output/table-item.h" #include "output/table-provider.h" #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)) + +/* This file uses TABLE_HORZ and TABLE_VERT enough to warrant abbreviating. */ +#define H TABLE_HORZ +#define V TABLE_VERT struct odt_driver { struct output_driver driver; + struct zip_writer *zip; /* ZIP file writer. */ + struct file_handle *handle; /* Handle for 'file_name'. */ char *file_name; /* Output file name. */ - bool debug; - - /* The name of the temporary directory used to construct the ODF */ - char *dirname; - /* Writer for the content.xml file */ - xmlTextWriterPtr content_wtr; + /* content.xml */ + xmlTextWriterPtr content_wtr; /* XML writer. */ + FILE *content_file; /* Temporary file. */ - /* Writer fot the manifest.xml file */ - xmlTextWriterPtr manifest_wtr; + /* manifest.xml */ + xmlTextWriterPtr manifest_wtr; /* XML writer. */ + FILE *manifest_file; /* Temporary file. */ /* Number of tables so far. */ int table_num; }; +static const struct output_driver_class odt_driver_class; + static struct odt_driver * odt_driver_cast (struct output_driver *driver) { - assert (driver->class == &odt_class); + assert (driver->class == &odt_driver_class); return UP_CAST (driver, struct odt_driver, driver); } -/* Create the "mimetype" file needed by ODF */ -static bool -create_mimetype (const char *dirname) +/* Creates a new temporary file and stores it in *FILE, then creates an XML + writer for it and stores it in *W. */ +static void +create_writer (FILE **file, xmlTextWriterPtr *w) { - FILE *fp; - struct string filename; - ds_init_cstr (&filename, dirname); - ds_put_cstr (&filename, "/mimetype"); - fp = fopen (ds_cstr (&filename), "w"); - - if (fp == NULL) - { - error (0, errno, _("failed to create output file %s"), - ds_cstr (&filename)); - ds_destroy (&filename); - return false; - } - ds_destroy (&filename); + /* XXX this can fail */ + *file = create_temp_file (); + *w = xmlNewTextWriter (xmlOutputBufferCreateFile (*file, NULL)); - fprintf (fp, "application/vnd.oasis.opendocument.text"); - fclose (fp); - - return true; -} - -/* Create a new XML file called FILENAME in the temp directory, and return a writer for it */ -static xmlTextWriterPtr -create_writer (const struct odt_driver *driver, const char *filename) -{ - char *copy = NULL; - xmlTextWriterPtr w; - struct string str; - ds_init_cstr (&str, driver->dirname); - ds_put_cstr (&str, "/"); - ds_put_cstr (&str, filename); - - /* dirname modifies its argument, so we must copy it */ - copy = xstrdup (ds_cstr (&str)); - mkdir (dirname (copy), 0700); - free (copy); - - w = xmlNewTextWriterFilename (ds_cstr (&str), 0); - - ds_destroy (&str); - - xmlTextWriterStartDocument (w, NULL, "UTF-8", NULL); - - return w; + xmlTextWriterStartDocument (*w, NULL, "UTF-8", NULL); } @@ -137,7 +112,10 @@ register_file (struct odt_driver *odt, const char *filename) static void write_style_data (struct odt_driver *odt) { - xmlTextWriterPtr w = create_writer (odt, "styles.xml"); + xmlTextWriterPtr w; + FILE *file; + + create_writer (&file, &w); register_file (odt, "styles.xml"); xmlTextWriterStartElement (w, _xml ("office:document-styles")); @@ -148,10 +126,10 @@ write_style_data (struct odt_driver *odt) _xml ("urn:oasis:names:tc:opendocument:xmlns:style:1.0")); xmlTextWriterWriteAttribute (w, _xml ("xmlns:fo"), - _xml ("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0") ); + _xml ("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0")); xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml ("1.1")); - + xmlTextWriterStartElement (w, _xml ("office:styles")); @@ -218,18 +196,35 @@ write_style_data (struct odt_driver *odt) xmlTextWriterEndElement (w); /* style:style */ } + { + xmlTextWriterStartElement (w, _xml ("style:style")); + xmlTextWriterWriteAttribute (w, _xml ("style:name"), _xml ("superscript")); + xmlTextWriterWriteAttribute (w, _xml ("style:family"), _xml ("text")); + + xmlTextWriterStartElement (w, _xml ("style:text-properties")); + xmlTextWriterWriteAttribute (w, _xml ("style:text-position"), + _xml ("super 58%")); + xmlTextWriterEndElement (w); /* style:text-properties */ + + xmlTextWriterEndElement (w); /* style:style */ + } xmlTextWriterEndElement (w); /* office:styles */ xmlTextWriterEndElement (w); /* office:document-styles */ xmlTextWriterEndDocument (w); xmlFreeTextWriter (w); + zip_writer_add (odt->zip, file, "styles.xml"); + close_temp_file (file); } static void write_meta_data (struct odt_driver *odt) { - xmlTextWriterPtr w = create_writer (odt, "meta.xml"); + xmlTextWriterPtr w; + FILE *file; + + create_writer (&file, &w); register_file (odt, "meta.xml"); xmlTextWriterStartElement (w, _xml ("office:document-meta")); @@ -242,81 +237,78 @@ 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); } { char buf[30]; - struct passwd *pw = getpwuid (getuid ()); time_t t = time (NULL); struct tm *tm = localtime (&t); strftime (buf, 30, "%Y-%m-%dT%H:%M:%S", tm); - xmlTextWriterStartElement (w, _xml ("meta:initial-creator")); - xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ","))); - xmlTextWriterEndElement (w); - xmlTextWriterStartElement (w, _xml ("meta:creation-date")); xmlTextWriterWriteString (w, _xml (buf)); xmlTextWriterEndElement (w); - xmlTextWriterStartElement (w, _xml ("dc:creator")); - xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ","))); - - xmlTextWriterEndElement (w); - xmlTextWriterStartElement (w, _xml ("dc:date")); xmlTextWriterWriteString (w, _xml (buf)); xmlTextWriterEndElement (w); } +#ifdef HAVE_PWD_H + { + struct passwd *pw = getpwuid (getuid ()); + if (pw != NULL) + { + xmlTextWriterStartElement (w, _xml ("meta:initial-creator")); + xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ","))); + xmlTextWriterEndElement (w); + + xmlTextWriterStartElement (w, _xml ("dc:creator")); + xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ","))); + xmlTextWriterEndElement (w); + } + } +#endif + xmlTextWriterEndElement (w); xmlTextWriterEndElement (w); xmlTextWriterEndDocument (w); xmlFreeTextWriter (w); -} - -enum -{ - output_file_arg, - boolean_arg, -}; - -static struct driver_option * -opt (struct output_driver *d, struct string_map *options, const char *key, - const char *default_value) -{ - return driver_option_get (d, options, key, default_value); + zip_writer_add (odt->zip, file, "meta.xml"); + close_temp_file (file); } static struct output_driver * -odt_create (const char *name, enum output_device_type device_type, - struct string_map *o) +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) + return NULL; odt = xzalloc (sizeof *odt); d = &odt->driver; - output_driver_init (d, &odt_class, name, device_type); - odt->file_name = parse_string (opt (d, o, "output-file", "pspp.odt")); - odt->debug = parse_boolean (opt (d, o, "debug", "false")); + output_driver_init (d, &odt_driver_class, file_name, device_type); - odt->dirname = xstrdup ("odt-XXXXXX"); - mkdtemp (odt->dirname); + odt->zip = zip; + odt->handle = fh; + odt->file_name = xstrdup (file_name); - if (!create_mimetype (odt->dirname)) - { - output_driver_destroy (d); - return NULL; - } + zip_writer_add_string (zip, "mimetype", + "application/vnd.oasis.opendocument.text"); /* Create the manifest */ - odt->manifest_wtr = create_writer (odt, "META-INF/manifest.xml"); + create_writer (&odt->manifest_file, &odt->manifest_wtr); xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:manifest")); xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("xmlns:manifest"), @@ -333,7 +325,7 @@ odt_create (const char *name, enum output_device_type device_type, write_meta_data (odt); write_style_data (odt); - odt->content_wtr = create_writer (odt, "content.xml"); + create_writer (&odt->content_file, &odt->content_wtr); register_file (odt, "content.xml"); @@ -359,6 +351,8 @@ odt_create (const char *name, enum output_device_type device_type, xmlTextWriterEndElement (odt->manifest_wtr); xmlTextWriterEndDocument (odt->manifest_wtr); xmlFreeTextWriter (odt->manifest_wtr); + zip_writer_add (odt->zip, odt->manifest_file, "META-INF/manifest.xml"); + close_temp_file (odt->manifest_file); return d; } @@ -370,90 +364,158 @@ odt_destroy (struct output_driver *driver) if (odt->content_wtr != NULL) { - struct string zip_cmd; - xmlTextWriterEndElement (odt->content_wtr); /* office:text */ xmlTextWriterEndElement (odt->content_wtr); /* office:body */ xmlTextWriterEndElement (odt->content_wtr); /* office:document-content */ xmlTextWriterEndDocument (odt->content_wtr); xmlFreeTextWriter (odt->content_wtr); + zip_writer_add (odt->zip, odt->content_file, "content.xml"); + close_temp_file (odt->content_file); - /* Zip up the directory */ - ds_init_empty (&zip_cmd); - ds_put_format (&zip_cmd, - "cd %s ; rm -f ../%s; zip -q -X ../%s mimetype; zip -q -X -u -r ../%s .", - odt->dirname, odt->file_name, odt->file_name, odt->file_name); - system (ds_cstr (&zip_cmd)); - ds_destroy (&zip_cmd); + zip_writer_close (odt->zip); } - if ( !odt->debug ) + fh_unref (odt->handle); + free (odt->file_name); + free (odt); +} + +static void +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 { - /* Remove the temp dir */ - struct string rm_cmd; + char *line = xstrdup (line_); + char *newline; + char *p; + + for (p = line; *p; p = newline + 1) + { + newline = strchr (p, '\n'); - ds_init_empty (&rm_cmd); - ds_put_format (&rm_cmd, "rm -r %s", odt->dirname); - system (ds_cstr (&rm_cmd)); - ds_destroy (&rm_cmd); + 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("")); + } } - else - fprintf (stderr, "Not removing directory %s\n", odt->dirname); +} - free (odt->dirname); - free (odt); +static void +write_footnotes (struct odt_driver *odt, + const struct pivot_table *pt, + const size_t *footnote_indexes, + size_t n_footnotes) +{ + for (size_t i = 0; i < n_footnotes; i++) + { + const struct pivot_footnote *f = pt->footnotes[footnote_indexes[i]]; + if (f->show) + { + xmlTextWriterStartElement (odt->content_wtr, _xml("text:span")); + xmlTextWriterWriteAttribute (odt->content_wtr, + _xml("text:style-name"), + _xml("superscript")); + char *s = pivot_footnote_marker_string (f, pt); + write_xml_with_line_breaks (odt, s); + free (s); + xmlTextWriterEndElement (odt->content_wtr); + } + } } static void -odt_submit_table (struct odt_driver *odt, struct table_item *item) +write_table_item_cell (struct odt_driver *odt, + const struct pivot_table *pt, + const struct table_cell *cell) { - const struct table *tab = table_item_get_table (item); - const char *caption = table_item_get_caption (item); - int r, c; + struct string body = DS_EMPTY_INITIALIZER; + pivot_value_format_body (cell->value, pt, &body); + xmlTextWriterWriteString (odt->content_wtr, _xml (ds_cstr (&body))); + ds_destroy (&body); - /* Write a heading for the table */ - if (caption != NULL) + write_footnotes (odt, pt, cell->value->footnote_indexes, + cell->value->n_footnotes); +} + +static void +write_table__ (struct odt_driver *odt, const struct pivot_table *pt, + const struct table *t) +{ + if (t) { - xmlTextWriterStartElement (odt->content_wtr, _xml("text:h")); - xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("text:level"), - "%d", 2); - xmlTextWriterWriteString (odt->content_wtr, - _xml (table_item_get_caption (item)) ); - xmlTextWriterEndElement (odt->content_wtr); + for (size_t y = 0; y < t->n[V]; y++) + { + xmlTextWriterStartElement (odt->content_wtr, _xml("text:h")); + xmlTextWriterWriteFormatAttribute (odt->content_wtr, + _xml("text:outline-level"), "%d", 2); + + struct table_cell cell; + table_get_cell (t, 0, y, &cell); + write_table_item_cell (odt, pt, &cell); + + xmlTextWriterEndElement (odt->content_wtr); + } } +} + +static void +write_table_layer (struct odt_driver *odt, const struct pivot_table *pt, + const size_t *layer_indexes) +{ + struct table *title, *layers, *body, *caption, *footnotes; + pivot_output (pt, layer_indexes, true, &title, &layers, &body, + &caption, &footnotes, NULL, NULL); + + /* Write a heading for the table */ + write_table__ (odt, pt, title); + write_table__ (odt, pt, layers); /* Start table */ xmlTextWriterStartElement (odt->content_wtr, _xml("table:table")); - xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"), + xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"), "TABLE-%d", odt->table_num++); /* Start column definitions */ xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-column")); - xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:number-columns-repeated"), "%d", table_nc (tab)); + xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:number-columns-repeated"), "%d", body->n[H]); xmlTextWriterEndElement (odt->content_wtr); /* Deal with row headers */ - if ( table_ht (tab) > 0) + if (body->h[V][0] > 0) xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-header-rows")); - + /* Write all the rows */ - for (r = 0 ; r < table_nr (tab); ++r) + for (int r = 0 ; r < body->n[V]; ++r) { /* Start row definition */ xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-row")); /* Write all the columns */ - for (c = 0 ; c < table_nc (tab) ; ++c) + for (int c = 0 ; c < body->n[H] ; ++c) { struct table_cell cell; - table_get_cell (tab, c, r, &cell); + table_get_cell (body, c, r, &cell); - if (c == cell.d[TABLE_HORZ][0] && r == cell.d[TABLE_VERT][0]) + if (c == cell.d[H][0] && r == cell.d[V][0]) { int colspan = table_cell_colspan (&cell); int rowspan = table_cell_rowspan (&cell); @@ -471,34 +533,59 @@ 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")); + 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 < body->h[V][0] || c < body->h[H][0]) + 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")); - xmlTextWriterWriteString (odt->content_wtr, _xml(cell.contents)); + write_table_item_cell (odt, pt, &cell); - xmlTextWriterEndElement (odt->content_wtr); /* text:p */ - xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */ + xmlTextWriterEndElement (odt->content_wtr); /* text:p */ + xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */ } else { xmlTextWriterStartElement (odt->content_wtr, _xml("table:covered-table-cell")); xmlTextWriterEndElement (odt->content_wtr); } - - table_cell_free (&cell); } - + xmlTextWriterEndElement (odt->content_wtr); /* row */ - if ( table_ht (tab) > 0 && r == table_ht (tab) - 1) + int ht = body->h[V][0]; + if (ht > 0 && r == ht - 1) xmlTextWriterEndElement (odt->content_wtr); /* table-header-rows */ } xmlTextWriterEndElement (odt->content_wtr); /* table */ + + /* Write a caption for the table */ + write_table__ (odt, pt, caption); + write_table__ (odt, pt, footnotes); + + table_unref (title); + table_unref (layers); + table_unref (body); + table_unref (caption); + table_unref (footnotes); +} + +static void +write_table (struct odt_driver *odt, const struct table_item *item) +{ + size_t *layer_indexes; + PIVOT_OUTPUT_FOR_EACH_LAYER (layer_indexes, item->pt, true) + write_table_layer (odt, item->pt, layer_indexes); +} + +static void +odt_output_text (struct odt_driver *odt, const char *text) +{ + xmlTextWriterStartElement (odt->content_wtr, _xml("text:p")); + xmlTextWriterWriteString (odt->content_wtr, _xml(text)); + xmlTextWriterEndElement (odt->content_wtr); } /* Submit a table to the ODT driver */ @@ -507,25 +594,26 @@ odt_submit (struct output_driver *driver, const struct output_item *output_item) { struct odt_driver *odt = odt_driver_cast (driver); + 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)) + odt_output_text (odt, text_item_get_text (to_text_item (output_item))); + else if (is_message_item (output_item)) { - const struct text_item *text_item = to_text_item (output_item); - const char *text = text_item_get_text (text_item); - - /* XXX apply different styles based on text_item's type. */ - xmlTextWriterStartElement (odt->content_wtr, _xml("text:p")); - xmlTextWriterWriteString (odt->content_wtr, _xml(text)); - xmlTextWriterEndElement (odt->content_wtr); + const struct message_item *message_item = to_message_item (output_item); + char *s = msg_to_string (message_item_get_msg (message_item)); + odt_output_text (odt, s); + free (s); } } -/* ODT driver class. */ -const struct output_driver_class odt_class = +struct output_driver_factory odt_driver_factory = + { "odt", "pspp.odf", odt_create }; + +static const struct output_driver_class odt_driver_class = { "odf", - odt_create, odt_destroy, odt_submit, NULL,