Replace numerous instances of xzalloc with XZALLOC
[pspp] / src / output / odt.c
index 065ef379a6d176455dde3d0a523b1c7ea28872b8..abb30cb530c7c1c85b3edd46afdd825fbe573fe6 100644 (file)
 #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/table-item.h"
+#include "output/output-item.h"
+#include "output/pivot-table.h"
+#include "output/pivot-output.h"
 #include "output/table-provider.h"
-#include "output/text-item.h"
 
 #include "gl/xalloc.h"
 
 
 #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. */
 
   /* content.xml */
@@ -69,12 +73,6 @@ struct odt_driver
 
   /* Number of tables so far. */
   int table_num;
-
-  /* Name of current command. */
-  char *command_name;
-
-  /* Number of footnotes so far. */
-  int n_footnotes;
 };
 
 static const struct output_driver_class odt_driver_class;
@@ -86,26 +84,6 @@ odt_driver_cast (struct output_driver *driver)
   return UP_CAST (driver, struct odt_driver, driver);
 }
 
-/* Create the "mimetype" file needed by ODF */
-static bool
-create_mimetype (struct zip_writer *zip)
-{
-  FILE *fp;
-
-  fp = create_temp_file ();
-  if (fp == NULL)
-    {
-      msg_error (errno, _("error creating temporary file"));
-      return false;
-    }
-
-  fprintf (fp, "application/vnd.oasis.opendocument.text");
-  zip_writer_add (zip, fp, "mimetype");
-  close_temp_file (fp);
-
-  return true;
-}
-
 /* Creates a new temporary file and stores it in *FILE, then creates an XML
    writer for it and stores it in *W. */
 static void
@@ -146,7 +124,7 @@ 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"));
 
@@ -216,6 +194,18 @@ 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 */
@@ -295,7 +285,6 @@ 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);
 
@@ -303,19 +292,17 @@ odt_create (struct file_handle *fh, enum settings_output_devices device_type,
   if (zip == NULL)
     return NULL;
 
-  odt = xzalloc (sizeof *odt);
+  struct odt_driver *odt = XZALLOC (struct odt_driver);
   d = &odt->driver;
 
   output_driver_init (d, &odt_driver_class, file_name, device_type);
 
   odt->zip = zip;
+  odt->handle = fh;
   odt->file_name = xstrdup (file_name);
 
-  if (!create_mimetype (zip))
-    {
-      output_driver_destroy (d);
-      return NULL;
-    }
+  zip_writer_add_string (zip, "mimetype",
+                         "application/vnd.oasis.opendocument.text");
 
   /* Create the manifest */
   create_writer (&odt->manifest_file, &odt->manifest_wtr);
@@ -386,8 +373,8 @@ odt_destroy (struct output_driver *driver)
       zip_writer_close (odt->zip);
     }
 
+  fh_unref (odt->handle);
   free (odt->file_name);
-  free (odt->command_name);
   free (odt);
 }
 
@@ -426,51 +413,74 @@ write_xml_with_line_breaks (struct odt_driver *odt, const char *line_)
 }
 
 static void
-write_footnote (struct odt_driver *odt, const char *footnote)
+write_footnotes (struct odt_driver *odt,
+                 const struct pivot_table *pt,
+                 const size_t *footnote_indexes,
+                 size_t n_footnotes)
 {
-  char marker[16];
+  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);
+        }
+    }
+}
 
-  xmlTextWriterStartElement (odt->content_wtr, _xml("text:note"));
-  xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:note-class"),
-                               _xml("footnote"));
+static void
+write_table_item_cell (struct odt_driver *odt,
+                       const struct pivot_table *pt,
+                       const struct table_cell *cell)
+{
+  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);
 
-  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);
+  const struct pivot_value_ex *ex = pivot_value_ex (cell->value);
+  write_footnotes (odt, pt, ex->footnote_indexes, ex->n_footnotes);
+}
 
-  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);
+static void
+write_table__ (struct odt_driver *odt, const struct pivot_table *pt,
+               const struct table *t)
+{
+  if (t)
+    {
+      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);
 
-  xmlTextWriterEndElement (odt->content_wtr);
+          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 (struct odt_driver *odt, const struct table_item *item)
+write_table_layer (struct odt_driver *odt, const struct pivot_table *pt,
+                   const size_t *layer_indexes)
 {
-  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;
+  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 */
-  if (title != 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_title (item)) );
-      xmlTextWriterEndElement (odt->content_wtr);
-    }
+  write_table__ (odt, pt, title);
+  write_table__ (odt, pt, layers);
 
   /* Start table */
   xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
@@ -480,30 +490,29 @@ write_table (struct odt_driver *odt, const struct table_item *item)
 
   /* 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;
-          size_t i;
 
-          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);
@@ -521,31 +530,16 @@ write_table (struct odt_driver *odt, const struct table_item *item)
                   odt->content_wtr, _xml("table:number-rows-spanned"),
                   "%d", rowspan);
 
-              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"));
+              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"));
 
-                      write_xml_with_line_breaks (odt, contents->text);
+              write_table_item_cell (odt, pt, &cell);
 
-                      for (j = 0; j < contents->n_footnotes; j++)
-                        write_footnote (odt, contents->footnotes[j]);
-
-                      xmlTextWriterEndElement (odt->content_wtr); /* text:p */
-                    }
-                  else if (contents->table)
-                    write_table (odt, contents->table);
-
-                }
+              xmlTextWriterEndElement (odt->content_wtr); /* text:p */
               xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
            }
          else
@@ -553,29 +547,34 @@ write_table (struct odt_driver *odt, const struct table_item *item)
              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 */
-  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);
-    }
+  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 pivot_table *pt)
+{
+  size_t *layer_indexes;
+  PIVOT_OUTPUT_FOR_EACH_LAYER (layer_indexes, pt, true)
+    write_table_layer (odt, pt, layer_indexes);
 }
 
 static void
@@ -588,29 +587,43 @@ odt_output_text (struct odt_driver *odt, const char *text)
 
 /* Submit a table to the ODT driver */
 static void
-odt_submit (struct output_driver *driver,
-            const struct output_item *output_item)
+odt_submit (struct output_driver *driver, const struct output_item *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))
-    write_table (odt, to_table_item (output_item));
-  else if (is_text_item (output_item))
+  switch (item->type)
     {
-      struct text_item *text_item = to_text_item (output_item);
+    case OUTPUT_ITEM_CHART:
+      break;
 
-      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);
+    case OUTPUT_ITEM_GROUP:
+      break;
+
+    case OUTPUT_ITEM_IMAGE:
+      break;
+
+    case OUTPUT_ITEM_MESSAGE:
+      {
+        char *s = msg_to_string (item->message);
+        odt_output_text (odt, s);
+        free (s);
+      }
+      break;
+
+    case OUTPUT_ITEM_PAGE_BREAK:
+      break;
+
+    case OUTPUT_ITEM_TABLE:
+      write_table (odt, item->table);
+      break;
+
+    case OUTPUT_ITEM_TEXT:
+      {
+        char *text = text_item_get_plain_text (item);
+        odt_output_text (odt, text);
+        free (text);
+      }
+      break;
     }
 }
 
@@ -619,8 +632,7 @@ struct output_driver_factory odt_driver_factory =
 
 static const struct output_driver_class odt_driver_class =
 {
-  "odf",
-  odt_destroy,
-  odt_submit,
-  NULL,
+  .name = "odf",
+  .destroy = odt_destroy,
+  .submit = odt_submit,
 };