/* PSPP - a program for statistical analysis.
- Copyright (C) 2009 Free Software Foundation, Inc.
+ Copyright (C) 2009, 2010, 2011, 2012 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
/* A driver for creating OpenDocument Format text files from PSPP's output */
-#include <libpspp/assertion.h>
-#include <libpspp/cast.h>
-#include <libpspp/str.h>
-#include <libpspp/version.h>
-#include <output/driver-provider.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 <errno.h>
#include <libgen.h>
#include <libxml/xmlwriter.h>
+#ifdef HAVE_PWD_H
#include <pwd.h>
+#endif
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
-#include "xalloc.h"
-#include "error.h"
+#include "libpspp/assertion.h"
+#include "libpspp/cast.h"
+#include "libpspp/str.h"
+#include "libpspp/temp-file.h"
+#include "libpspp/version.h"
+#include "libpspp/zip-writer.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)
-#define _xml(X) (const xmlChar *)(X)
+#define _xml(X) (CHAR_CAST (const xmlChar *, X))
struct odt_driver
{
struct output_driver driver;
+ struct zip_writer *zip; /* ZIP file writer. */
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;
+
+ /* 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
-create_mimetype (const char *dirname)
+static bool
+create_mimetype (struct zip_writer *zip)
{
FILE *fp;
- struct string filename;
- ds_init_cstr (&filename, dirname);
- ds_put_cstr (&filename, "/mimetype");
- fp = fopen (ds_cstr (&filename), "w");
- ds_destroy (&filename);
- assert (fp);
+ fp = create_temp_file ();
+ if (fp == NULL)
+ {
+ error (0, errno, _("error creating temporary file"));
+ return false;
+ }
+
fprintf (fp, "application/vnd.oasis.opendocument.text");
- fclose (fp);
+ zip_writer_add (zip, fp, "mimetype");
+ close_temp_file (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)
+/* 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)
{
- 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);
+ /* XXX this can fail */
+ *file = create_temp_file ();
+ *w = xmlNewTextWriter (xmlOutputBufferCreateFile (*file, NULL));
- return w;
+ xmlTextWriterStartDocument (*w, NULL, "UTF-8", NULL);
}
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"));
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"));
{
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 (const char *file_name, enum settings_output_devices device_type,
+ struct string_map *o UNUSED)
{
struct output_driver *d;
struct odt_driver *odt;
+ struct zip_writer *zip;
+
+ 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);
+ output_driver_init (d, &odt_driver_class, file_name, device_type);
- odt->file_name = parse_string (opt (d, o, "output-file", "pspp.odt"));
- odt->debug = parse_boolean (opt (d, o, "debug", "false"));
+ odt->zip = zip;
+ odt->file_name = xstrdup (file_name);
- odt->dirname = xstrdup ("odt-XXXXXX");
- mkdtemp (odt->dirname);
-
- create_mimetype (odt->dirname);
+ if (!create_mimetype (zip))
+ {
+ output_driver_destroy (d);
+ return NULL;
+ }
/* 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"),
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");
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;
}
{
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)
+ {
+ 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);
- if ( !odt->debug )
- {
- /* Remove the temp dir */
- ds_init_empty (&rm_cmd);
- ds_put_format (&rm_cmd, "rm -r %s", odt->dirname);
- system (ds_cstr (&rm_cmd));
- ds_destroy (&rm_cmd);
+ zip_writer_close (odt->zip);
}
- else
- fprintf (stderr, "Not removing directory %s\n", odt->dirname);
-
- free (odt->dirname);
+
+ free (odt->file_name);
+ free (odt->command_name);
free (odt);
}
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);
+ struct text_item *text_item = to_text_item (output_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);
+ 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))
+ {
+ 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", "pspp.odf", odt_create };
+
+static const struct output_driver_class odt_driver_class =
{
"odf",
- odt_create,
odt_destroy,
odt_submit,
NULL,