Added a basic output viewer.
[pspp-builds.git] / src / output / ascii.c
index cc173cc414b4cebec10c63f49f5a61bb6c690795..2ac0401d5504f84ea5bb085496a4801f7df92837 100644 (file)
@@ -1,26 +1,25 @@
-/* PSPP - computes sample statistics.
-   Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
+/* PSPP - a program for statistical analysis.
+   Copyright (C) 1997-9, 2000, 2007 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 the Free Software Foundation; either version 2 of the
-   License, or (at your option) any later version.
+   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
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
 
-   This program is distributed in the hope that it will be useful, but
-   WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   General Public License for more details.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-   02110-1301, USA. */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
 
 #include <ctype.h>
 #include <errno.h>
 #include <limits.h>
+#include <stdint.h>
 #include <stdlib.h>
 
 #include <data/file-name.h>
 /* ASCII driver options: (defaults listed first)
 
    output-file="pspp.list"
+   append=no|yes                If output-file exists, append to it?
+   chart-files="pspp-#.png"     Name used for charts.
+   chart-type=png               Format of charts (use "none" to disable).
+
    paginate=on|off              Formfeeds are desired?
    tab-width=8                  Width of a tab; 0 to not use tabs.
 
@@ -55,6 +58,7 @@
    bottom-margin=2
 
    box[x]="strng"               Sets box character X (X in base 4: 0-3333).
+   init="string"                Set initialization string.
  */
 
 /* Disable messages by failed range checks. */
@@ -97,17 +101,21 @@ struct ascii_driver_ext
     struct pool *pool;
 
     /* User parameters. */
+    bool append;                /* Append if output-file already exists? */
     bool headers;              /* Print headers at top of page? */
     bool paginate;             /* Insert formfeeds? */
     bool squeeze_blank_lines;   /* Squeeze multiple blank lines into one? */
     enum emphasis_style emphasis; /* How to emphasize text. */
     int tab_width;             /* Width of a tab; 0 not to use tabs. */
+    const char *chart_type;     /* Type of charts to output; NULL for none. */
+    const char *chart_file_name; /* Name of files used for charts. */
 
     int page_length;           /* Page length before subtracting margins. */
     int top_margin;            /* Top margin in lines. */
     int bottom_margin;         /* Bottom margin in lines. */
 
     char *box[LNS_COUNT];       /* Line & box drawing characters. */
+    char *init;                 /* Device initialization string. */
 
     /* Internal state. */
     char *file_name;            /* Output file name. */
@@ -115,8 +123,10 @@ struct ascii_driver_ext
     int page_number;           /* Current page number. */
     struct line *lines;         /* Page content. */
     int line_cap;               /* Number of lines allocated. */
+    int chart_cnt;              /* Number of charts so far. */
   };
 
+static void ascii_flush (struct outp_driver *);
 static int get_default_box_char (size_t idx);
 static bool handle_option (struct outp_driver *this, const char *key,
                            const struct string *val);
@@ -135,32 +145,30 @@ ascii_open_driver (struct outp_driver *this, struct substring options)
     this->horiz_line_width[i] = this->vert_line_width[i] = i != OUTP_L_NONE;
 
   this->ext = x = pool_create_container (struct ascii_driver_ext, pool);
+  x->append = false;
   x->headers = true;
   x->paginate = true;
   x->squeeze_blank_lines = false;
   x->emphasis = EMPH_BOLD;
   x->tab_width = 8;
+  x->chart_file_name = pool_strdup (x->pool, "pspp-#.png");
+  x->chart_type = pool_strdup (x->pool, "png");
   x->page_length = 66;
   x->top_margin = 2;
   x->bottom_margin = 2;
   for (i = 0; i < LNS_COUNT; i++)
     x->box[i] = NULL;
+  x->init = NULL;
   x->file_name = pool_strdup (x->pool, "pspp.list");
   x->file = NULL;
   x->page_number = 0;
   x->lines = NULL;
   x->line_cap = 0;
+  x->chart_cnt = 0;
 
   if (!outp_parse_options (options, handle_option, this))
     goto error;
 
-  x->file = pool_fopen (x->pool, x->file_name, "w");
-  if (x->file == NULL)
-    {
-      error (0, errno, _("ascii: opening output file \"%s\""), x->file_name);
-      goto error;
-    }
-
   this->length = x->page_length - x->top_margin - x->bottom_margin - 1;
   if (x->headers)
     this->length -= 3;
@@ -228,8 +236,7 @@ ascii_close_driver (struct outp_driver *this)
 {
   struct ascii_driver_ext *x = this->ext;
 
-  if (fn_close (x->file_name, x->file) != 0)
-    error (0, errno, _("ascii: closing output file \"%s\""), x->file_name);
+  ascii_flush (this);
   pool_detach_file (x->pool, x->file);
   pool_destroy (x->pool);
 
@@ -240,10 +247,10 @@ ascii_close_driver (struct outp_driver *this)
 enum
   {
     boolean_arg,
-    string_arg,
+    emphasis_arg,
     nonneg_int_arg,
     pos_int_arg,
-    output_file_arg
+    string_arg
   };
 
 static const struct outp_option option_tab[] =
@@ -251,10 +258,9 @@ static const struct outp_option option_tab[] =
     {"headers", boolean_arg, 0},
     {"paginate", boolean_arg, 1},
     {"squeeze", boolean_arg, 2},
+    {"append", boolean_arg, 3},
 
-    {"emphasis", string_arg, 3},
-
-    {"output-file", output_file_arg, 0},
+    {"emphasis", emphasis_arg, 0},
 
     {"length", pos_int_arg, 0},
     {"width", pos_int_arg, 1},
@@ -263,6 +269,11 @@ static const struct outp_option option_tab[] =
     {"bottom-margin", nonneg_int_arg, 1},
     {"tab-width", nonneg_int_arg, 2},
 
+    {"output-file", string_arg, 0},
+    {"chart-files", string_arg, 1},
+    {"chart-type", string_arg, 2},
+    {"init", string_arg, 3},
+
     {NULL, 0, 0},
   };
 
@@ -298,9 +309,6 @@ handle_option (struct outp_driver *this, const char *key,
     case -1:
       error (0, 0, _("ascii: unknown parameter `%s'"), key);
       break;
-    case output_file_arg:
-      x->file_name = pool_strdup (x->pool, value);
-      break;
     case pos_int_arg:
       {
        char *tail;
@@ -327,7 +335,7 @@ handle_option (struct outp_driver *this, const char *key,
          }
       }
       break;
-    case string_arg:
+    case emphasis_arg:
       if (!strcmp (value, "bold"))
         x->emphasis = EMPH_BOLD;
       else if (!strcmp (value, "underline"))
@@ -394,11 +402,37 @@ handle_option (struct outp_driver *this, const char *key,
           case 2:
             x->squeeze_blank_lines = setting;
             break;
+          case 3:
+            x->append = setting;
+            break;
          default:
            NOT_REACHED ();
          }
       }
       break;
+    case string_arg:
+      switch (subcat)
+        {
+        case 0:
+          x->file_name = pool_strdup (x->pool, value);
+          break;
+        case 1:
+          if (ds_find_char (val, '#') != SIZE_MAX)
+            x->chart_file_name = pool_strdup (x->pool, value);
+          else
+            error (0, 0, _("`chart-files' value must contain `#'"));
+          break;
+        case 2:
+          if (value[0] != '\0')
+            x->chart_type = pool_strdup (x->pool, value);
+          else
+            x->chart_type = NULL;
+          break;
+        case 3:
+          x->init = pool_strdup (x->pool, value);
+          break;
+        }
+      break;
     default:
       NOT_REACHED ();
     }
@@ -412,6 +446,21 @@ ascii_open_page (struct outp_driver *this)
   struct ascii_driver_ext *x = this->ext;
   int i;
 
+  if (x->file == NULL)
+    {
+      x->file = fn_open (x->file_name, x->append ? "a" : "w");
+      if (x->file == NULL)
+        {
+          error (0, errno, _("ascii: opening output file \"%s\""),
+                 x->file_name);
+          return;
+        }
+      pool_attach_file (x->pool, x->file);
+
+      if (x->init != NULL)
+        fputs (x->init, x->file);
+    }
+
   x->page_number++;
 
   if (this->length > x->line_cap)
@@ -488,6 +537,15 @@ ascii_line (struct outp_driver *this,
     }
 }
 
+static void
+ascii_submit (struct outp_driver *this UNUSED, struct som_entity *s)
+{
+  extern struct som_table_class tab_table_class;
+
+  assert (s->class == &tab_table_class);
+  assert (s->type == SOM_CHART);
+}
+
 static void
 text_draw (struct outp_driver *this,
            enum outp_font font,
@@ -527,9 +585,10 @@ text_draw (struct outp_driver *this,
     ext->lines[y].chars[x++] = *string++ | attr;
 }
 
-/* Divides the text T->S into lines of width T->H.  Sets T->V to the
-   number of lines necessary.  Actually draws the text if DRAW is
-   true. */
+/* Divides the text T->S into lines of width T->H.  Sets *WIDTH
+   to the maximum width of a line and *HEIGHT to the number of
+   lines, if those arguments are non-null.  Actually draws the
+   text if DRAW is true. */
 static void
 delineate (struct outp_driver *this, const struct outp_text *text, bool draw,
            int *width, int *height)
@@ -607,7 +666,6 @@ ascii_text_draw (struct outp_driver *this, const struct outp_text *t)
   assert (this->page_open);
   delineate (this, t, true, NULL, NULL);
 }
-
 \f
 /* ascii_close_page () and support routines. */
 
@@ -672,6 +730,9 @@ ascii_close_page (struct outp_driver *this)
   struct string out;
   int line_num;
 
+  if (x->file == NULL)
+    return;
+
   ds_init_empty (&out);
 
   ds_put_char_multiple (&out, '\n', x->top_margin);
@@ -720,17 +781,74 @@ ascii_close_page (struct outp_driver *this)
   ds_destroy (&out);
 }
 
+/* Flushes all output to the user and lets the user deal with it.
+   This is applied only to output drivers that are designated as
+   "screen" drivers that the user is interacting with in real
+   time. */
 static void
-ascii_chart_initialise (struct outp_driver *d UNUSED, struct chart *ch)
+ascii_flush (struct outp_driver *this)
 {
-  error (0, 0, _("ascii: charts are unsupported by this driver"));
-  ch->lp = 0;
+  struct ascii_driver_ext *x = this->ext;
+  if (x->file != NULL)
+    {
+      if (fn_close (x->file_name, x->file) != 0)
+        error (0, errno, _("ascii: closing output file \"%s\""),
+               x->file_name);
+      pool_detach_file (x->pool, x->file);
+      x->file = NULL;
+    }
 }
 
 static void
-ascii_chart_finalise (struct outp_driver *d UNUSED, struct chart *ch UNUSED)
+ascii_chart_initialise (struct outp_driver *this, struct chart *ch)
 {
+  struct ascii_driver_ext *x = this->ext;
+  struct outp_text t;
+  char *text;
+
+  if (x->chart_type == NULL)
+    return;
 
+  /* Initialize chart. */
+  chart_init_separate (ch, x->chart_type, x->chart_file_name, ++x->chart_cnt);
+  if (ch->file_name == NULL)
+    return;
+
+  /* Mention chart in output.
+     First advance current position. */
+  if (!this->page_open)
+    outp_open_page (this);
+  else
+    {
+      this->cp_y++;
+      if (this->cp_y >= this->length)
+        {
+          outp_close_page (this);
+          outp_open_page (this);
+        }
+    }
+
+  /* Then write the text. */
+  text = xasprintf ("See %s for a chart.", ch->file_name);
+  t.font = OUTP_FIXED;
+  t.justification = OUTP_LEFT;
+  t.string = ss_cstr (text);
+  t.h = this->width;
+  t.v = 1;
+  t.x = 0;
+  t.y = this->cp_y;
+  ascii_text_draw (this, &t);
+  this->cp_y++;
+
+  free (text);
+}
+
+static void
+ascii_chart_finalise (struct outp_driver *this, struct chart *ch)
+{
+  struct ascii_driver_ext *x = this->ext;
+  if (x->chart_type != NULL)
+    chart_finalise_separate (ch);
 }
 
 const struct outp_class ascii_class =
@@ -743,8 +861,9 @@ const struct outp_class ascii_class =
 
   ascii_open_page,
   ascii_close_page,
+  ascii_flush,
 
-  NULL,
+  ascii_submit,
 
   ascii_line,
   ascii_text_metrics,