text-item: Merge "title" and "subtitle" items into a new "page title".
[pspp] / src / output / driver.c
index 653aa8d9c30031e86d271ce6d7734738aad9024e..b07330633386aff1d5854a318a602d14839bfa7e 100644 (file)
@@ -38,6 +38,7 @@
 #include "output/output-item.h"
 #include "output/text-item.h"
 
+#include "gl/error.h"
 #include "gl/xalloc.h"
 #include "gl/xmemdup0.h"
 
@@ -49,6 +50,7 @@ struct output_engine
     struct llx_list drivers;       /* Contains "struct output_driver"s. */
     struct string deferred_syntax; /* TEXT_ITEM_SYNTAX being accumulated. */
     char *command_name;            /* Name of command being processed. */
+    char *title, *subtitle;        /* Components of page title. */
   };
 
 static const struct output_driver_factory *factories[];
@@ -77,6 +79,8 @@ output_engine_push (void)
   llx_init (&e->drivers);
   ds_init_empty (&e->deferred_syntax);
   e->command_name = NULL;
+  e->title = NULL;
+  e->subtitle = NULL;
 }
 
 void
@@ -93,6 +97,8 @@ output_engine_pop (void)
     }
   ds_destroy (&e->deferred_syntax);
   free (e->command_name);
+  free (e->title);
+  free (e->subtitle);
 }
 
 void
@@ -224,6 +230,37 @@ output_flush (void)
         d->class->flush (d);
     }
 }
+
+static void
+output_set_title__ (struct output_engine *e, char **dst, const char *src)
+{
+  free (*dst);
+  *dst = src ? xstrdup (src) : NULL;
+
+  char *page_title
+    = (e->title && e->subtitle ? xasprintf ("%s\n%s", e->title, e->subtitle)
+       : e->title ? xstrdup (e->title)
+       : e->subtitle ? xstrdup (e->subtitle)
+       : xzalloc (1));
+  text_item_submit (text_item_create_nocopy (TEXT_ITEM_PAGE_TITLE,
+                                             page_title));
+}
+
+void
+output_set_title (const char *title)
+{
+  struct output_engine *e = engine_stack_top ();
+
+  output_set_title__ (e, &e->title, title);
+}
+
+void
+output_set_subtitle (const char *subtitle)
+{
+  struct output_engine *e = engine_stack_top ();
+
+  output_set_title__ (e, &e->subtitle, subtitle);
+}
 \f
 void
 output_driver_init (struct output_driver *driver,
@@ -403,3 +440,25 @@ output_driver_create (struct string_map *options)
 
   return driver;
 }
+
+void
+output_driver_parse_option (const char *option, struct string_map *options)
+{
+  const char *equals = strchr (option, '=');
+  if (equals == NULL)
+    {
+      error (0, 0, _("%s: output option missing `='"), option);
+      return;
+    }
+
+  char *key = xmemdup0 (option, equals - option);
+  if (string_map_contains (options, key))
+    {
+      error (0, 0, _("%s: output option specified more than once"), key);
+      free (key);
+      return;
+    }
+
+  char *value = xmemdup0 (equals + 1, strlen (equals + 1));
+  string_map_insert_nocopy (options, key, value);
+}