driver: New function output_driver_parse_option().
[pspp] / src / output / driver.c
index afb5084fa1d2abc0d76c59d77179fe5a86902b97..f2d581cfe4558d5e1171b937f7f7ad72eb12bbeb 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"
 
@@ -48,6 +49,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. */
   };
 
 static const struct output_driver_factory *factories[];
@@ -75,6 +77,7 @@ output_engine_push (void)
   e = &engine_stack[n_stack++];
   llx_init (&e->drivers);
   ds_init_empty (&e->deferred_syntax);
+  e->command_name = NULL;
 }
 
 void
@@ -90,6 +93,7 @@ output_engine_pop (void)
       output_driver_destroy (d);
     }
   ds_destroy (&e->deferred_syntax);
+  free (e->command_name);
 }
 
 void
@@ -139,9 +143,13 @@ flush_deferred_syntax (struct output_engine *e)
 {
   if (!ds_is_empty (&e->deferred_syntax))
     {
-      char *syntax = ds_steal_cstr (&e->deferred_syntax);
-      output_submit__ (e, text_item_super (
-                         text_item_create_nocopy (TEXT_ITEM_SYNTAX, syntax)));
+      ds_trim (&e->deferred_syntax, ss_cstr ("\n"));
+      if (!ds_is_empty (&e->deferred_syntax))
+        {
+          char *syntax = ds_steal_cstr (&e->deferred_syntax);
+          output_submit__ (e, text_item_super (text_item_create_nocopy (
+                                                 TEXT_ITEM_SYNTAX, syntax)));
+        }
     }
 }
 
@@ -170,6 +178,33 @@ output_submit (struct output_item *item)
     }
 
   flush_deferred_syntax (e);
+
+  if (is_text_item (item))
+    {
+      const struct text_item *text_item = to_text_item (item);
+      const char *text = text_item_get_text (text_item);
+      enum text_item_type type = text_item_get_type (text_item);
+
+      if (type == TEXT_ITEM_COMMAND_OPEN)
+        {
+          free (e->command_name);
+          e->command_name = xstrdup (text);
+        }
+      else if (type == TEXT_ITEM_COMMAND_CLOSE)
+        {
+          free (e->command_name);
+          e->command_name = NULL;
+        }
+    }
+  else if (is_message_item (item))
+    {
+      struct message_item *message_item = to_message_item (item);
+      free (message_item->command_name);
+      message_item->command_name = (e->command_name
+                                    ? xstrdup (e->command_name)
+                                    : NULL);
+    }
+
   output_submit__ (e, item);
 }
 
@@ -258,38 +293,11 @@ output_driver_is_registered (const struct output_driver *driver)
   return output_driver_get_engine (driver) != NULL;
 }
 \f
-/* Useful functions for output driver implementation. */
-
-void
-output_driver_track_current_command (const struct output_item *output_item,
-                                     char **command_namep)
-{
-  if (is_text_item (output_item))
-    {
-      const struct text_item *item = to_text_item (output_item);
-      const char *text = text_item_get_text (item);
-      enum text_item_type type = text_item_get_type (item);
-
-      if (type == TEXT_ITEM_COMMAND_OPEN)
-        {
-          free (*command_namep);
-          *command_namep = xstrdup (text);
-        }
-      else if (type == TEXT_ITEM_COMMAND_CLOSE)
-        {
-          free (*command_namep);
-          *command_namep = NULL;
-        }
-    }
-}
-\f
 extern const struct output_driver_factory txt_driver_factory;
 extern const struct output_driver_factory list_driver_factory;
 extern const struct output_driver_factory html_driver_factory;
 extern const struct output_driver_factory csv_driver_factory;
-#ifdef ODF_WRITE_SUPPORT
 extern const struct output_driver_factory odt_driver_factory;
-#endif
 #ifdef HAVE_CAIRO
 extern const struct output_driver_factory pdf_driver_factory;
 extern const struct output_driver_factory ps_driver_factory;
@@ -302,9 +310,7 @@ static const struct output_driver_factory *factories[] =
     &list_driver_factory,
     &html_driver_factory,
     &csv_driver_factory,
-#ifdef ODF_WRITE_SUPPORT
     &odt_driver_factory,
-#endif
 #ifdef HAVE_CAIRO
     &pdf_driver_factory,
     &ps_driver_factory,
@@ -398,3 +404,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);
+}