X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Foutput%2Fodt.c;h=99f5e60d6ec7cb8fdb45265f424b16a37f18d133;hb=16c70c9f2e041d4ff21f57c29be37e60be40c089;hp=41ef108b776062256a62beba949ea50b247112b1;hpb=943d4b7f3171402af7b7cd991d66b1dd6e625446;p=pspp-builds.git diff --git a/src/output/odt.c b/src/output/odt.c index 41ef108b..99f5e60d 100644 --- a/src/output/odt.c +++ b/src/output/odt.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2009 Free Software Foundation, Inc. + Copyright (C) 2009, 2010 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 @@ -18,27 +18,31 @@ /* A driver for creating OpenDocument Format text files from PSPP's output */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - +#include #include #include +#ifdef HAVE_PWD_H #include +#endif #include #include #include #include -#include "xalloc.h" -#include "error.h" +#include "libpspp/assertion.h" +#include "libpspp/cast.h" +#include "libpspp/str.h" +#include "libpspp/version.h" +#include "output/driver-provider.h" +#include "output/message-item.h" +#include "output/options.h" +#include "output/tab.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) @@ -63,17 +67,22 @@ struct odt_driver /* Number of tables so far. */ int table_num; + + /* Name of current command. */ + char *command_name; }; +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 void +static bool create_mimetype (const char *dirname) { FILE *fp; @@ -81,11 +90,20 @@ create_mimetype (const char *dirname) ds_init_cstr (&filename, dirname); ds_put_cstr (&filename, "/mimetype"); fp = fopen (ds_cstr (&filename), "w"); + + if (fp == NULL) + { + error (0, errno, _("error opening output file \"%s\""), + ds_cstr (&filename)); + ds_destroy (&filename); + return false; + } ds_destroy (&filename); - assert (fp); 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 */ @@ -239,30 +257,36 @@ write_meta_data (struct odt_driver *odt) { 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); @@ -283,7 +307,7 @@ opt (struct output_driver *d, struct string_map *options, const char *key, } static struct output_driver * -odt_create (const char *name, enum output_device_type device_type, +odt_create (const char *file_name, enum settings_output_devices device_type, struct string_map *o) { struct output_driver *d; @@ -291,15 +315,19 @@ odt_create (const char *name, enum output_device_type device_type, odt = xzalloc (sizeof *odt); d = &odt->driver; - output_driver_init (d, &odt_class, name, device_type); + output_driver_init (d, &odt_driver_class, file_name, device_type); - odt->file_name = parse_string (opt (d, o, "output-file", "pspp.odt")); + odt->file_name = xstrdup (file_name); odt->debug = parse_boolean (opt (d, o, "debug", "false")); odt->dirname = xstrdup ("odt-XXXXXX"); mkdtemp (odt->dirname); - create_mimetype (odt->dirname); + if (!create_mimetype (odt->dirname)) + { + output_driver_destroy (d); + return NULL; + } /* Create the manifest */ odt->manifest_wtr = create_writer (odt, "META-INF/manifest.xml"); @@ -354,28 +382,31 @@ odt_destroy (struct output_driver *driver) { struct odt_driver *odt = odt_driver_cast (driver); - struct string zip_cmd; - struct string rm_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 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 ../pspp.odt .", - odt->dirname, odt->file_name, odt->file_name); - system (ds_cstr (&zip_cmd)); - ds_destroy (&zip_cmd); - + 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 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); + } if ( !odt->debug ) { /* Remove the temp dir */ + struct string rm_cmd; + ds_init_empty (&rm_cmd); ds_put_format (&rm_cmd, "rm -r %s", odt->dirname); system (ds_cstr (&rm_cmd)); @@ -384,6 +415,7 @@ odt_destroy (struct output_driver *driver) else fprintf (stderr, "Not removing directory %s\n", odt->dirname); + free (odt->command_name); free (odt->dirname); free (odt); } @@ -484,31 +516,45 @@ odt_submit_table (struct odt_driver *odt, struct table_item *item) xmlTextWriterEndElement (odt->content_wtr); /* table */ } +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 */ static void odt_submit (struct output_driver *driver, const struct output_item *output_item) { struct odt_driver *odt = odt_driver_cast (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)); else if (is_text_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); + odt_output_text (odt, text_item_get_text (to_text_item (output_item))); + } + else if (is_message_item (output_item)) + { + const struct message_item *message_item = to_message_item (output_item); + const struct msg *msg = message_item_get_msg (message_item); + char *s = msg_to_string (msg, odt->command_name); + 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", odt_create }; + +static const struct output_driver_class odt_driver_class = { "odf", - odt_create, odt_destroy, odt_submit, NULL,