/* 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
/* 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>
#include <pwd.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/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 "gl/xalloc.h"
+#include "gl/error.h"
#include "gettext.h"
#define _(msgid) gettext (msgid)
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 void
+static bool
create_mimetype (const char *dirname)
{
FILE *fp;
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);
- 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 */
}
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;
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");
{
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));
}
}
-/* 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,