Fix up potential overflows in size calculations by replacing instances
[pspp] / src / plot-chart.c
index ddbb36085f69817e8e22b510260872a44f622c7e..bfe38c8192f105da9453c6dc7c7b5f79296568f5 100644 (file)
@@ -14,8 +14,8 @@
 
    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., 59 Temple Place - Suite 330, Boston, MA
-   02111-1307, USA. */
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA. */
 
 #include <config.h>
 #include <stdio.h>
@@ -29,6 +29,9 @@
 
 #include "chart.h"
 #include "str.h"
+#include "alloc.h"
+#include "som.h"
+#include "output.h"
 
 
 const char *data_colour[] = {
@@ -45,17 +48,27 @@ const char *data_colour[] = {
 
 
 
-int
-chart_initialise(struct chart *chart)
+struct chart *
+chart_create(void)
 {
-
-  chart->pl_params = pl_newplparams();
-
-  chart->lp = pl_newpl_r ("X",0,stdout,stderr,chart->pl_params);
+  struct chart *chart;
+  struct outp_driver *d;
+
+  d = outp_drivers (NULL);
+  if (d == NULL)
+    return NULL;
+  
+  chart = xmalloc (sizeof *chart);
+  d->class->initialise_chart(d, chart);
+  if (!chart->lp) 
+    {
+      free (chart);
+      return NULL; 
+    }
 
   if (pl_openpl_r (chart->lp) < 0)      /* open Plotter */
-      return 1;
-
+    return NULL;
+  
   pl_fspace_r (chart->lp, 0.0, 0.0, 1000.0, 1000.0); /* set coordinate system */
   pl_flinewidth_r (chart->lp, 0.25);    /* set line thickness */
   pl_pencolorname_r (chart->lp, "black"); 
@@ -63,8 +76,6 @@ chart_initialise(struct chart *chart)
   pl_erase_r (chart->lp);               /* erase graphics display */
   pl_filltype_r(chart->lp,0);
 
-
-
   pl_savestate_r(chart->lp);
 
   /* Set default chartetry */
@@ -80,7 +91,6 @@ chart_initialise(struct chart *chart)
   chart->font_size = 0;
   strcpy(chart->fill_colour,"red");
 
-
   /* Get default font size */
   if ( !chart->font_size) 
     chart->font_size = pl_fontsize_r(chart->lp, -1);
@@ -90,12 +100,9 @@ chart_initialise(struct chart *chart)
           chart->data_left, chart->data_bottom, 
           chart->data_right, chart->data_top);
 
-  return 0;
-
+  return chart;
 }
 
-
-
 /* Draw a tick mark at position
    If label is non zero, then print it at the tick mark
 */
@@ -107,6 +114,8 @@ draw_tick(struct chart *chart,
 {
   const int tickSize = 10;
 
+  assert(chart);
+
   pl_savestate_r(chart->lp);
 
   pl_move_r(chart->lp, chart->data_left, chart->data_bottom);
@@ -150,6 +159,9 @@ chart_write_title(struct chart *chart, const char *title, ...)
   va_list ap;
   char buf[100];
 
+  if ( ! chart ) 
+         return ;
+
   pl_savestate_r(chart->lp);
   pl_ffontsize_r(chart->lp,chart->font_size * 1.5);
   pl_move_r(chart->lp,chart->data_left, chart->title_bottom);
@@ -163,22 +175,36 @@ chart_write_title(struct chart *chart, const char *title, ...)
 }
 
 
+extern struct som_table_class tab_table_class;
 
 void
-chart_finalise(struct chart *chart)
+chart_submit(struct chart *chart)
 {
+  struct som_entity s;
+  struct outp_driver *d;
+
+  if ( ! chart ) 
+     return ;
+
   pl_restorestate_r(chart->lp);
 
+  s.class = &tab_table_class;
+  s.ext = chart;
+  s.type = SOM_CHART;
+  som_submit (&s);
+  
   if (pl_closepl_r (chart->lp) < 0)     /* close Plotter */
     {
       fprintf (stderr, "Couldn't close Plotter\n");
     }
 
-
   pl_deletepl_r(chart->lp);
 
   pl_deleteplparams(chart->pl_params);
 
+  d = outp_drivers (NULL);
+  d->class->finalise_chart(d, chart);
+  free(chart);
 }
 
 
@@ -191,9 +217,13 @@ chart_write_xscale(struct chart *ch, double min, double max, int ticks)
   const double tick_interval = 
     chart_rounded_tick( (max - min) / (double) ticks);
 
+  assert ( ch );
+
+
   ch->x_max = ceil( max / tick_interval ) * tick_interval ; 
   ch->x_min = floor ( min / tick_interval ) * tick_interval ;
 
+
   ch->abscissa_scale = fabs(ch->data_right - ch->data_left) / 
     fabs(ch->x_max - ch->x_min);
 
@@ -215,6 +245,10 @@ chart_write_yscale(struct chart *ch, double smin, double smax, int ticks)
   const double tick_interval = 
     chart_rounded_tick( (smax - smin) / (double) ticks);
 
+
+  if ( !ch ) 
+         return;
+
   ch->y_max = ceil  ( smax / tick_interval ) * tick_interval ; 
   ch->y_min = floor ( smin / tick_interval ) * tick_interval ;