X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Foutput%2Fchart.c;h=6f1ce034a3a47875ccb51228513ce22de72e2199;hb=bcaaaebdde43e26a8d27c53590f34bf29eb56406;hp=b096a7d2195fc632c863a9e01a7319ea3dceeb80;hpb=a2a92dd38cf37e50ccfd56bedd4da21bc56dfe0f;p=pspp diff --git a/src/output/chart.c b/src/output/chart.c index b096a7d219..6f1ce034a3 100644 --- a/src/output/chart.c +++ b/src/output/chart.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2004, 2009 Free Software Foundation, Inc. + Copyright (C) 2004, 2009, 2011 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 @@ -16,165 +16,94 @@ #include -#include -#include - #include -#include -#include -#include -#include -#include -#include #include -#include -#include -#include -#include +#include "libpspp/cast.h" +#include "libpspp/compiler.h" +#include "libpspp/str.h" +#include "output/chart-provider.h" +#include "output/output-item.h" -#include "error.h" -#include "xalloc.h" +#include "gl/xalloc.h" #include "gettext.h" #define _(msgid) gettext (msgid) -extern struct som_table_class tab_table_class; - -void -chart_init (struct chart *chart, const struct chart_class *class) +struct chart * +chart_ref (const struct chart *chart_) { - chart->class = class; - chart->ref_cnt = 1; + struct chart *chart = CONST_CAST (struct chart *, chart_); + assert (chart->ref_cnt > 0); + chart->ref_cnt++; + return chart; } void -chart_geometry_init (plPlotter *lp, struct chart_geometry *geom) +chart_unref (struct chart *chart) { - /* Start output page. */ - pl_openpl_r (lp); - - /* Set coordinate system. */ - pl_fspace_r (lp, 0.0, 0.0, 1000.0, 1000.0); - - /* Set line thickness. */ - pl_flinewidth_r (lp, 0.25); - pl_pencolorname_r (lp, "black"); - - /* Erase graphics display. */ - pl_erase_r (lp); - - pl_filltype_r (lp, 0); - pl_savestate_r(lp); - - /* Set default chartetry. */ - geom->data_top = 900; - geom->data_right = 800; - geom->data_bottom = 120; - geom->data_left = 150; - geom->abscissa_top = 70; - geom->ordinate_right = 120; - geom->title_bottom = 920; - geom->legend_left = 810; - geom->legend_right = 1000; - geom->font_size = 0; - strcpy (geom->fill_colour, "red"); - - /* Get default font size */ - if (!geom->font_size) - geom->font_size = pl_fontsize_r (lp, -1); - - /* Draw the data area */ - pl_box_r (lp, - geom->data_left, geom->data_bottom, - geom->data_right, geom->data_top); + if (chart) + { + assert (chart->ref_cnt > 0); + if (!--chart->ref_cnt) + { + char *title = chart->title; + chart->class->destroy (chart); + free (title); + } + } } -void -chart_geometry_free (plPlotter *lp) +bool +chart_is_shared (const struct chart *chart) { - if (pl_closepl_r (lp) < 0) - fprintf (stderr, "Couldn't close Plotter\n"); + assert (chart->ref_cnt > 0); + return chart->ref_cnt > 1; } +/* Initializes CHART as a chart of the specified CLASS. The new chart + initially has the specified TITLE, which may be NULL if no title is yet + available. The caller retains ownership of TITLE. + + A chart is abstract, that is, a plain chart is not useful on its own. Thus, + this function is normally called from the initialization function of some + subclass of chart. */ void -chart_draw (const struct chart *chart, plPlotter *lp) +chart_init (struct chart *chart, const struct chart_class *class, + const char *title) { - chart->class->draw (chart, lp); + *chart = (struct chart) { + .ref_cnt = 1, + .class = class, + .title = xstrdup_if_nonnull (title), + }; } -struct chart * -chart_ref (const struct chart *chart_) +/* Returns CHART's title, which is a null pointer if no title has been set. */ +const char * +chart_get_title (const struct chart *chart) { - struct chart *chart = (struct chart *) chart_; - chart->ref_cnt++; - return chart; + return chart->title; } +/* Sets CHART's title to TITLE, replacing any previous title. Specify NULL for + TITLE to clear any title from CHART. The caller retains ownership of + TITLE. + + This function may only be used on a chart that is unshared. */ void -chart_unref (struct chart *chart) +chart_set_title (struct chart *chart, const char *title) { - assert (chart->ref_cnt > 0); - if (--chart->ref_cnt == 0) - chart->class->destroy (chart); + assert (!chart_is_shared (chart)); + free (chart->title); + chart->title = xstrdup_if_nonnull (title); } +/* Submits CHART to the configured output drivers, and transfers ownership to + the output subsystem. */ void chart_submit (struct chart *chart) { - struct outp_driver *d; - - for (d = outp_drivers (NULL); d; d = outp_drivers (d)) - if (d->class->output_chart != NULL) - d->class->output_chart (d, chart); - - chart_unref (chart); -} - -bool -chart_create_file (const char *type, const char *file_name_tmpl, int number, - plPlotterParams *params, char **file_namep, plPlotter **lpp) -{ - char *file_name = NULL; - FILE *fp = NULL; - int number_pos; - plPlotter *lp; - - number_pos = strchr (file_name_tmpl, '#') - file_name_tmpl; - file_name = xasprintf ("%.*s%d%s", number_pos, file_name_tmpl, - number, file_name_tmpl + number_pos + 1); - - fp = fopen (file_name, "wb"); - if (fp == NULL) - { - error (0, errno, _("creating \"%s\""), file_name); - goto error; - } - - if (params != NULL) - lp = pl_newpl_r (type, 0, fp, stderr, params); - else - { - params = pl_newplparams (); - lp = pl_newpl_r (type, 0, fp, stderr, params); - pl_deleteplparams (params); - } - if (lp == NULL) - goto error; - - *file_namep = file_name; - *lpp = lp; - return true; - -error: - if (fp != NULL) - { - fclose (fp); - if (file_name != NULL) - unlink (file_name); - } - free (file_name); - *file_namep = NULL; - *lpp = NULL; - return false; + if (chart) + output_item_submit (chart_item_create (chart)); }