/* PSPP - a program for statistical analysis.
- Copyright (C) 1997-9, 2000, 2007 Free Software Foundation, Inc.
+ Copyright (C) 1997-9, 2000, 2007, 2009 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
#include <language/command.h>
#include <language/lexer/lexer.h>
#include <language/lexer/variable-parser.h>
+#include <libpspp/cast.h>
#include <libpspp/ll.h>
#include <libpspp/message.h>
#include <libpspp/misc.h>
/* Makes appropriate DO REPEAT macro substitutions within the
repeated lines. */
static void
-do_repeat_filter (struct getl_interface *block_,
- struct string *line)
+do_repeat_filter (struct getl_interface *interface, struct string *line)
{
- struct repeat_block *block = (struct repeat_block *) block_;
+ struct repeat_block *block
+ = UP_CAST (interface, struct repeat_block, parent);
bool in_apos, in_quote, dot;
struct substring input;
struct string output;
static struct repeat_line *
current_line (const struct getl_interface *interface)
{
- struct repeat_block *block = (struct repeat_block *) interface;
+ struct repeat_block *block
+ = UP_CAST (interface, struct repeat_block, parent);
return (block->cur_line != ll_null (&block->lines)
? ll_data (block->cur_line, struct repeat_line, ll)
: NULL);
do_repeat_read (struct getl_interface *interface,
struct string *output)
{
- struct repeat_block *block = (struct repeat_block *) interface;
+ struct repeat_block *block
+ = UP_CAST (interface, struct repeat_block, parent);
struct repeat_line *line;
block->cur_line = ll_next (block->cur_line);
/* Frees a DO REPEAT block.
Called by getl to close out the DO REPEAT block. */
static void
-do_repeat_close (struct getl_interface *block_)
+do_repeat_close (struct getl_interface *interface)
{
- struct repeat_block *block = (struct repeat_block *) block_;
+ struct repeat_block *block
+ = UP_CAST (interface, struct repeat_block, parent);
pool_destroy (block->pool);
}
#include <language/stats/freq.h>
#include <language/stats/npar.h>
#include <libpspp/assertion.h>
+#include <libpspp/cast.h>
#include <libpspp/compiler.h>
#include <libpspp/hash.h>
#include <libpspp/message.h>
{
const struct dictionary *dict = dataset_dict (ds);
int v, i;
- struct chisquare_test *cst = (struct chisquare_test *) test;
+ struct chisquare_test *cst = UP_CAST (test, struct chisquare_test,
+ parent.parent);
struct one_sample_test *ost = &cst->parent;
int n_cells = 0;
double total_expected = 0.0;
#include <language/stats/chisquare.h>
#include <language/stats/wilcoxon.h>
#include <language/stats/sign.h>
+#include <libpspp/cast.h>
#include <libpspp/hash.h>
#include <libpspp/pool.h>
#include <libpspp/taint.h>
struct const_hsh_table *var_hash)
{
int i;
- struct one_sample_test *ost = (struct one_sample_test *) test;
+ struct one_sample_test *ost = UP_CAST (test, struct one_sample_test, parent);
for ( i = 0 ; i < ost->n_vars ; ++i )
const_hsh_insert (var_hash, ost->vars[i]);
/* PSPP - a program for statistical analysis.
- Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
+ Copyright (C) 1997-9, 2000, 2009 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
#include <language/command.h>
#include <language/lexer/lexer.h>
#include <libpspp/assertion.h>
+#include <libpspp/cast.h>
#include <libpspp/message.h>
#include <libpspp/message.h>
#include <libpspp/str.h>
static const char *
name (const struct getl_interface *s)
{
- const struct syntax_file_source *sfs = (const struct syntax_file_source *) s;
+ const struct syntax_file_source *sfs = UP_CAST (s, struct syntax_file_source,
+ parent);
return sfs->fn;
}
static int
line_number (const struct getl_interface *s)
{
- const struct syntax_file_source *sfs = (const struct syntax_file_source *) s;
+ const struct syntax_file_source *sfs = UP_CAST (s, struct syntax_file_source,
+ parent);
return sfs->ln;
}
read_syntax_file (struct getl_interface *s,
struct string *line)
{
- struct syntax_file_source *sfs = (struct syntax_file_source *) s;
+ struct syntax_file_source *sfs = UP_CAST (s, struct syntax_file_source,
+ parent);
/* Open file, if not yet opened. */
if (sfs->syntax_file == NULL)
static void
syntax_close (struct getl_interface *s)
{
- struct syntax_file_source *sfs = (struct syntax_file_source *) s;
+ struct syntax_file_source *sfs = UP_CAST (s, struct syntax_file_source,
+ parent);
if (sfs->syntax_file && EOF == fn_close (sfs->fn, sfs->syntax_file))
msg (MW, _("Closing `%s': %s."), sfs->fn, strerror (errno));
ss->parent.name = name ;
ss->parent.location = line_number;
- return (struct getl_interface *) ss;
+ return &ss->parent;
}
/* PSPPIRE - a graphical interface for PSPP.
- Copyright (C) 2007 Free Software Foundation, Inc.
+ Copyright (C) 2007, 2009 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
#include <config.h>
+#include <libpspp/cast.h>
#include <libpspp/getl.h>
#include <libpspp/compiler.h>
#include <libpspp/str.h>
static void
do_close (struct getl_interface *i )
{
- struct syntax_string_source *sss = (struct syntax_string_source *) i;
+ struct syntax_string_source *sss = UP_CAST (i, struct syntax_string_source,
+ parent);
ds_destroy (&sss->buffer);
read_single_line (struct getl_interface *i,
struct string *line)
{
- struct syntax_string_source *sss = (struct syntax_string_source *) i;
+ struct syntax_string_source *sss = UP_CAST (i, struct syntax_string_source,
+ parent);
size_t next;
sss->parent.location = location;
- return (struct getl_interface *) sss;
+ return &sss->parent;
}
/* Return the syntax currently contained in S.
--- /dev/null
+/* PSPP - a program for statistical analysis.
+ Copyright (C) 2009 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 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.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef LIBPSPP_CAST_H
+#define LIBPSPP_CAST_H 1
+
+#include <stddef.h>
+
+/* Given expressions A and B, both of which have pointer type,
+ expands to a void expression that causes a compiler warning if
+ A and B are not pointers to qualified or unqualified versions
+ of compatible types. */
+#define CHECK_POINTER_COMPATIBILITY(A, B) ((void) sizeof ((A) == (B)))
+
+/* Given POINTER, a pointer to the given MEMBER within structure
+ STRUCT, returns the address of the STRUCT. */
+#define UP_CAST(POINTER, STRUCT, MEMBER) \
+ (CHECK_POINTER_COMPATIBILITY (&((STRUCT *) 0)->MEMBER, POINTER), \
+ (STRUCT *) ((char *) (POINTER) - offsetof (STRUCT, MEMBER)))
+
+#endif /* libpspp/cast.h */
#include "tukey-hinges.h"
#include <gl/xalloc.h>
#include <libpspp/assertion.h>
+#include <libpspp/cast.h>
#include <math.h>
#include <float.h>
#include <data/val-type.h>
static void
destroy (struct statistic *s)
{
- struct order_stats *os = (struct order_stats *) s;
- struct box_whisker *bw = (struct box_whisker *) s;
+ struct box_whisker *bw = UP_CAST (s, struct box_whisker, parent.parent);
+ struct order_stats *os = &bw->parent;
struct ll *ll;
for (ll = ll_head (&bw->outliers); ll != ll_null (&bw->outliers); )
acc (struct statistic *s, const struct ccase *cx,
double c UNUSED, double cc UNUSED, double y)
{
- struct box_whisker *bw = (struct box_whisker *) s;
+ struct box_whisker *bw = UP_CAST (s, struct box_whisker, parent.parent);
bool extreme;
struct outlier *o;
const struct variable *id_var, size_t casenumber_idx)
{
struct box_whisker *w = xzalloc (sizeof (*w));
- struct order_stats *os = (struct order_stats *) w;
- struct statistic *stat = (struct statistic *) w;
+ struct order_stats *os = &w->parent;
+ struct statistic *stat = &os->parent;
os->n_k = 0;
#include <gl/xalloc.h>
#include <libpspp/assertion.h>
+#include <libpspp/cast.h>
#include <gsl/gsl_histogram.h>
#include "chart-geometry.h"
static void
acc (struct statistic *s, const struct ccase *cx UNUSED, double c, double cc UNUSED, double y)
{
- struct histogram *hist = (struct histogram *) s;
+ struct histogram *hist = UP_CAST (s, struct histogram, parent);
gsl_histogram_accumulate (hist->gsl_hist, y, c);
}
static void
destroy (struct statistic *s)
{
- struct histogram *h = (struct histogram *) s;
+ struct histogram *h = UP_CAST (s, struct histogram, parent);
gsl_histogram_free (h->gsl_hist);
free (s);
}
#include <data/case.h>
#include <data/casewriter.h>
#include <libpspp/compiler.h>
+#include <libpspp/cast.h>
#include <libpspp/misc.h>
#include <math/moments.h>
static void
destroy (struct statistic *stat)
{
- struct order_stats *os = (struct order_stats *) stat;
- free (os);
+ struct np *np = UP_CAST (stat, struct np, parent.parent);
+ free (np);
}
double c, double cc, double y)
{
struct ccase *cp;
- struct np *np = (struct np *) s;
+ struct np *np = UP_CAST (s, struct np, parent.parent);
double rank = np->prev_cc + (c + 1) / 2.0;
double ns = gsl_cdf_ugaussian_Pinv (rank / ( np->n + 1 ));
#define N_(msgid) msgid
#include <libpspp/assertion.h>
+#include <libpspp/cast.h>
#include <data/val-type.h>
#include <gl/xalloc.h>
#include <data/variable.h>
static void
destroy (struct statistic *stat)
{
- struct order_stats *os = (struct order_stats *) stat;
+ struct percentile *ptl = UP_CAST (stat, struct percentile, parent.parent);
+ struct order_stats *os = &ptl->parent;
free (os->k);
- free (os);
+ free (ptl);
}
#include <gl/xalloc.h>
#include <libpspp/assertion.h>
+#include <libpspp/cast.h>
#include <math.h>
#include <data/val-type.h>
static void
acc (struct statistic *s, const struct ccase *cx UNUSED, double c, double cc, double y)
{
- struct trimmed_mean *tm = (struct trimmed_mean *) s;
+ struct trimmed_mean *tm = UP_CAST (s, struct trimmed_mean, parent.parent);
struct order_stats *os = &tm->parent;
if ( cc > os->k[0].tc && cc < os->k[1].tc)
static void
destroy (struct statistic *s)
{
- struct order_stats *os = (struct order_stats *) s;
+ struct trimmed_mean *tm = UP_CAST (s, struct trimmed_mean, parent.parent);
+ struct order_stats *os = &tm->parent;
free (os->k);
- free (s);
+ free (tm);
}
struct trimmed_mean *
#include <gl/xalloc.h>
#include <libpspp/assertion.h>
+#include <libpspp/cast.h>
#include <math.h>
void
static void
destroy (struct statistic *s)
{
- struct order_stats *os = (struct order_stats *) s;
+ struct tukey_hinges *th = UP_CAST (s, struct tukey_hinges, parent.parent);
+ struct order_stats *os = &th->parent;
free (os->k);
free (s);
#include <assert.h>
#include <cairo/cairo.h>
+#include <libpspp/cast.h>
#include <libpspp/misc.h>
#include <math/chart-geometry.h>
#include <math/box-whisker.h>
boxplot_chart_draw (const struct chart *chart, cairo_t *cr,
struct chart_geometry *geom)
{
- const struct boxplot *boxplot = (struct boxplot *) chart;
+ const struct boxplot *boxplot = UP_CAST (chart, struct boxplot, chart);
double box_width;
size_t i;
static void
boxplot_chart_destroy (struct chart *chart)
{
- struct boxplot *boxplot = (struct boxplot *) chart;
+ struct boxplot *boxplot = UP_CAST (chart, struct boxplot, chart);
size_t i;
free (boxplot->title);
#include <data/casereader.h>
#include <data/casewriter.h>
+#include <libpspp/cast.h>
#include <libpspp/message.h>
#include <math/np.h>
#include <output/chart-provider.h>
np_plot_chart_draw (const struct chart *chart, cairo_t *cr,
struct chart_geometry *geom)
{
- const struct np_plot_chart *npp = (struct np_plot_chart *) chart;
+ const struct np_plot_chart *npp = UP_CAST (chart, struct np_plot_chart,
+ chart);
struct casereader *data;
struct ccase *c;
dnp_plot_chart_draw (const struct chart *chart, cairo_t *cr,
struct chart_geometry *geom)
{
- const struct np_plot_chart *dnpp = (struct np_plot_chart *) chart;
+ const struct np_plot_chart *dnpp = UP_CAST (chart, struct np_plot_chart,
+ chart);
struct casereader *data;
struct ccase *c;
static void
np_plot_chart_destroy (struct chart *chart)
{
- struct np_plot_chart *npp = (struct np_plot_chart *) chart;
-
+ struct np_plot_chart *npp = UP_CAST (chart, struct np_plot_chart, chart);
casereader_destroy (npp->data);
free (npp->label);
free (npp);
#include <stdio.h>
#include <data/value-labels.h>
+#include <libpspp/cast.h>
#include <libpspp/str.h>
#include <output/charts/plot-chart.h>
#include <output/chart-provider.h>
piechart_draw (const struct chart *chart, cairo_t *cr,
struct chart_geometry *geom)
{
- struct piechart *pie = (struct piechart *) chart;
+ const struct piechart *pie = UP_CAST (chart, struct piechart, chart);
double total_magnitude;
double left_label, right_label;
double centre_x, centre_y;
static void
piechart_destroy (struct chart *chart)
{
- struct piechart *pie = (struct piechart *) chart;
+ struct piechart *pie = UP_CAST (chart, struct piechart, chart);
int i;
free (pie->title);
#include <output/chart-provider.h>
#include <data/variable.h>
+#include <libpspp/cast.h>
#include <libpspp/hash.h>
#include <output/chart.h>
#include <math/histogram.h>
histogram_chart_draw (const struct chart *chart, cairo_t *cr,
struct chart_geometry *geom)
{
- struct histogram_chart *h = (struct histogram_chart *) chart;
+ struct histogram_chart *h = UP_CAST (chart, struct histogram_chart, chart);
int i;
int bins;
static void
histogram_chart_destroy (struct chart *chart)
{
- struct histogram_chart *h = (struct histogram_chart *) chart;
+ struct histogram_chart *h = UP_CAST (chart, struct histogram_chart, chart);
if (h->gsl_hist != NULL)
gsl_histogram_free (h->gsl_hist);
free (h->label);
#include <ctype.h>
#include <sys/types.h>
#include <regex.h>
+#include <libpspp/cast.h>
#include <libpspp/message.h>
#include <gtk/gtk.h>
static void
regexp_destroy (struct comparator *cmptr)
{
- struct regexp_comparator *rec = (struct regexp_comparator *) cmptr;
+ struct regexp_comparator *rec
+ = UP_CAST (cmptr, struct regexp_comparator, parent);
regfree (&rec->re);
}
static void
cmptr_value_destroy (struct comparator *cmptr)
{
- struct value_comparator *vc = (struct value_comparator *) cmptr;
+ struct value_comparator *vc
+ = UP_CAST (cmptr, struct value_comparator, parent);
value_destroy (&vc->pattern, var_get_width (cmptr->var));
}
#include <libpspp/getl.h>
#include <libpspp/compiler.h>
+#include <libpspp/cast.h>
#include <libpspp/str.h>
#include <stdlib.h>
gchar *text;
GtkTextIter next_line;
- struct syntax_editor_source *ses = (struct syntax_editor_source *) i;
+ struct syntax_editor_source *ses
+ = UP_CAST (i, struct syntax_editor_source, parent);
if ( gtk_text_iter_compare (&ses->i, &ses->end) >= 0)
return false;
#include <data/file-name.h>
#include <data/settings.h>
#include <language/command.h>
+#include <libpspp/cast.h>
#include <libpspp/message.h>
#include <libpspp/str.h>
#include <libpspp/version.h>
read_interactive (struct getl_interface *s,
struct string *line)
{
- struct readln_source *is =
- (struct readln_source *) s ;
+ struct readln_source *is = UP_CAST (s, struct readln_source, parent);
return is->interactive_func (line, prompt_get_style ());
}