output: Implement nested tables.
[pspp] / src / output / odt.c
index fdaa6f902de642efbd7a438a745e67feb21e56f2..24da4522c0799b15d38ed90ca003ca538227994d 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2009, 2010, 2011 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
@@ -31,6 +31,7 @@
 
 #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"
@@ -44,7 +45,6 @@
 #include "output/text-item.h"
 
 #include "gl/xalloc.h"
-#include "gl/error.h"
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
@@ -75,6 +75,8 @@ struct odt_driver
 
 static const struct output_driver_class odt_driver_class;
 
+static void write_table (struct odt_driver *, const struct table *);
+
 static struct odt_driver *
 odt_driver_cast (struct output_driver *driver)
 {
@@ -91,7 +93,7 @@ create_mimetype (struct zip_writer *zip)
   fp = create_temp_file ();
   if (fp == NULL)
     {
-      error (0, errno, _("error creating temporary file"));
+      msg_error (errno, _("error creating temporary file"));
       return false;
     }
 
@@ -380,16 +382,40 @@ odt_destroy (struct output_driver *driver)
       zip_writer_close (odt->zip);
     }
   
+  free (odt->file_name);
   free (odt->command_name);
   free (odt);
 }
 
+static void
+write_xml_with_line_breaks (xmlTextWriterPtr writer, char *line)
+{
+  char *newline;
+  char *p;
+
+  for (p = line; *p; p = newline + 1)
+    {
+      newline = strchr (p, '\n');
+
+      if (!newline)
+        {
+          xmlTextWriterWriteString (writer, _xml(p));
+          return;
+        }
+
+      if (newline > p && newline[-1] == '\r')
+        newline[-1] = '\0';
+      else
+        *newline = '\0';
+      xmlTextWriterWriteString (writer, _xml(p));
+      xmlTextWriterWriteElement (writer, _xml("text:line-break"), _xml(""));
+    }
+}
+
 static void
 odt_submit_table (struct odt_driver *odt, struct table_item *item)
 {
-  const struct table *tab = table_item_get_table (item);
   const char *caption = table_item_get_caption (item);
-  int r, c;
 
   /* Write a heading for the table */
   if (caption != NULL)
@@ -402,6 +428,14 @@ odt_submit_table (struct odt_driver *odt, struct table_item *item)
       xmlTextWriterEndElement (odt->content_wtr);
     }
 
+  write_table (odt, table_item_get_table (item));
+}
+
+static void
+write_table (struct odt_driver *odt, const struct table *tab)
+{
+  int r, c;
+
   /* Start table */
   xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
   xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"), 
@@ -429,6 +463,7 @@ odt_submit_table (struct odt_driver *odt, struct table_item *item)
       for (c = 0 ; c < table_nc (tab) ; ++c)
        {
           struct table_cell cell;
+          size_t i;
 
           table_get_cell (tab, c, r, &cell);
 
@@ -450,17 +485,37 @@ 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"));
-
-             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"));
-
-             xmlTextWriterWriteString (odt->content_wtr, _xml(cell.contents));
-
-             xmlTextWriterEndElement (odt->content_wtr); /* text:p */
-             xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
+              for (i = 0; i < cell.n_contents; i++)
+                {
+                  const struct cell_contents *contents = &cell.contents[i];
+
+                  if (contents->text)
+                    {
+                      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 (strchr (contents->text, '\n'))
+                        {
+                          char *line = xstrdup (contents->text);
+                          write_xml_with_line_breaks (odt->content_wtr, line);
+                          free (line);
+                        }
+                      else
+                        xmlTextWriterWriteString (odt->content_wtr, _xml(contents->text));
+
+                      xmlTextWriterEndElement (odt->content_wtr); /* text:p */
+                    }
+                  else if (contents->table)
+                    {
+                      write_table (odt, contents->table);
+                      continue;
+                    }
+                }
+              xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
            }
          else
            {
@@ -516,7 +571,8 @@ odt_submit (struct output_driver *driver,
     }
 }
 
-struct output_driver_factory odt_driver_factory = { "odt", odt_create };
+struct output_driver_factory odt_driver_factory =
+  { "odt", "pspp.odf", odt_create };
 
 static const struct output_driver_class odt_driver_class =
 {