1 /* PSPP - computes sample statistics.
2 Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 /* The Gnulib "strftime" module defines my_strftime in <config.h> for use by
23 gl/strftime.c. Perl also defines my_strftime in embed.h for some other
24 purpose. The former definition doesn't matter in this file, so suppress it
25 to avoid a compiler warning. */
35 #include <libpspp/hmap.h>
36 #include <libpspp/hash-functions.h>
37 #include <libpspp/message.h>
38 #include <libpspp/version.h>
39 #include <libpspp/i18n.h>
40 #include <gl/xalloc.h>
41 #include <data/dictionary.h>
42 #include <data/case.h>
43 #include <data/casereader.h>
44 #include <data/casewriter.h>
45 #include <data/variable.h>
46 #include <data/attributes.h>
47 #include <data/file-handle-def.h>
48 #include <data/identifier.h>
49 #include <data/settings.h>
50 #include <data/sys-file-writer.h>
51 #include <data/value.h>
52 #include <data/vardict.h>
53 #include <data/value-labels.h>
54 #include <data/format.h>
55 #include <data/data-in.h>
56 #include <data/data-out.h>
59 typedef struct fmt_spec input_format ;
60 typedef struct fmt_spec output_format ;
63 /* A thin wrapper around sfm_writer */
68 /* A pointer to the writer. The writer is owned by the struct */
69 struct casewriter *writer;
71 /* A pointer to the dictionary. Owned externally */
72 const struct pspp_dict *dict;
74 /* The scalar containing the dictionary */
79 /* A thin wrapper around sfm_reader */
82 struct any_read_info opts;
84 /* A pointer to the reader. The reader is owned by the struct */
85 struct casereader *reader;
87 /* A pointer to the dictionary. */
88 struct pspp_dict *dict;
93 struct hmap_node hmap_node; /* In struct pspp_dict's input_formats map. */
94 const struct variable *var;
95 struct fmt_spec input_format;
98 /* A thin wrapper around struct dictionary.*/
100 struct dictionary *dict;
101 struct hmap input_formats; /* Contains struct input_format. */
105 /* A message handler which writes messages to PSPP::errstr */
107 message_handler (const struct msg *m, void *aux)
109 SV *errstr = get_sv("PSPP::errstr", TRUE);
110 sv_setpv (errstr, m->text);
114 sysfile_close (struct syswriter_info *swi)
120 retval = casewriter_destroy (swi->writer);
128 scalar_to_value (union value *val, SV *scalar, const struct variable *var)
130 if ( var_is_numeric (var))
132 if ( SvNOK (scalar) || SvIOK (scalar) )
133 val->f = SvNV (scalar);
140 const char *p = SvPV (scalar, len);
141 int width = var_get_width (var);
142 value_set_missing (val, width);
143 memcpy (value_str_rw (val, width), p, len);
149 value_to_scalar (const union value *val, const struct variable *var)
151 if ( var_is_numeric (var))
153 if ( var_is_value_missing (var, val, MV_SYSTEM))
154 return newSVpvn ("", 0);
156 return newSVnv (val->f);
160 int width = var_get_width (var);
161 return newSVpvn (value_str (val, width), width);
167 make_value_from_scalar (union value *uv, SV *val, const struct variable *var)
169 value_init (uv, var_get_width (var));
170 scalar_to_value (uv, val, var);
173 static struct pspp_dict *
174 create_pspp_dict (struct dictionary *dict)
176 struct pspp_dict *pspp_dict = xmalloc (sizeof *pspp_dict);
177 pspp_dict->dict = dict;
178 hmap_init (&pspp_dict->input_formats);
182 static const struct fmt_spec *
183 find_input_format (const struct pspp_dict *dict, const struct variable *var)
185 struct input_format *input_format;
187 HMAP_FOR_EACH_IN_BUCKET (input_format, struct input_format, hmap_node,
188 hash_pointer (var, 0), &dict->input_formats)
189 if (input_format->var == var)
190 return &input_format->input_format;
198 MODULE = PSPP PACKAGE = PSPP
206 /* Check that the version is correct up to the length of 'ver'.
207 This allows PSPP autobuilders to add a "-build#" suffix to the
208 PSPP version without causing failures here. */
209 assert (0 == strncmp (ver, bare_version, strlen (ver)));
212 msg_set_handler (message_handler, NULL);
217 format_value (val, var)
222 const struct fmt_spec *fmt = var_get_print_format (var);
225 make_value_from_scalar (&uv, val, var);
226 s = data_out (&uv, var_get_encoding (var), fmt);
227 value_destroy (&uv, var_get_width (var));
228 ret = newSVpv (s, fmt->w);
236 value_is_missing (val, var)
242 make_value_from_scalar (&uv, val, var);
243 ret = var_is_value_missing (var, &uv, MV_ANY);
244 value_destroy (&uv, var_get_width (var));
251 MODULE = PSPP PACKAGE = PSPP::Dict
256 RETVAL = create_pspp_dict (dict_create ("UTF-8"));
263 struct pspp_dict *dict
267 struct input_format *input_format, *next_input_format;
269 HMAP_FOR_EACH_SAFE (input_format, next_input_format,
270 struct input_format, hmap_node, &dict->input_formats)
272 hmap_delete (&dict->input_formats, &input_format->hmap_node);
275 hmap_destroy (&dict->input_formats);
276 dict_destroy (dict->dict);
282 struct pspp_dict *dict
284 RETVAL = dict_get_var_cnt (dict->dict);
289 set_label (dict, label)
290 struct pspp_dict *dict
293 dict_set_label (dict->dict, label);
296 set_documents (dict, docs)
297 struct pspp_dict *dict
300 dict_set_documents_string (dict->dict, docs);
304 add_document (dict, doc)
305 struct pspp_dict *dict
308 dict_add_document_line (dict->dict, doc, false);
312 clear_documents (dict)
313 struct pspp_dict *dict
315 dict_clear_documents (dict->dict);
319 set_weight (dict, var)
320 struct pspp_dict *dict
323 dict_set_weight (dict->dict, var);
327 pxs_get_variable (dict, idx)
328 struct pspp_dict *dict
331 SV *errstr = get_sv("PSPP::errstr", TRUE);
332 sv_setpv (errstr, "");
333 if ( SvIV (idx) >= dict_get_var_cnt (dict->dict))
335 sv_setpv (errstr, "The dictionary doesn't have that many variables.");
339 RETVAL = dict_get_var (dict->dict, SvIV (idx));
345 pxs_get_var_by_name (dict, name)
346 struct pspp_dict *dict
349 SV *errstr = get_sv("PSPP::errstr", TRUE);
350 sv_setpv (errstr, "");
352 struct variable *var = dict_lookup_var (dict->dict, name);
354 sv_setpv (errstr, "No such variable.");
360 MODULE = PSPP PACKAGE = PSPP::Var
364 pxs_dict_create_var (dict, name, ip_fmt)
365 struct pspp_dict * dict
369 SV *errstr = get_sv("PSPP::errstr", TRUE);
370 sv_setpv (errstr, "");
371 if ( ! id_is_plausible (name, false))
373 sv_setpv (errstr, "The variable name is not valid.");
377 struct fmt_spec op_fmt;
378 struct input_format *input_format;
381 op_fmt = fmt_for_output_from_input (&ip_fmt);
382 v = dict_create_var (dict->dict, name,
383 fmt_is_string (op_fmt.type) ? op_fmt.w : 0);
386 sv_setpv (errstr, "The variable could not be created (probably already exists).");
389 var_set_both_formats (v, &op_fmt);
391 input_format = xmalloc (sizeof *input_format);
392 input_format->var = v;
393 input_format->input_format = ip_fmt;
394 hmap_insert (&dict->input_formats, &input_format->hmap_node,
395 hash_pointer (v, 0));
403 set_missing_values (var, v1, ...)
404 struct variable *var;
411 croak ("No more than 3 missing values are permitted");
413 for (i = 0; i < items - 1; ++i)
414 scalar_to_value (&val[i], ST(i+1), var);
416 struct missing_values mv;
417 mv_init (&mv, var_get_width (var));
418 for (i = 0 ; i < items - 1; ++i )
419 mv_add_value (&mv, &val[i]);
420 var_set_missing_values (var, &mv);
424 set_label (var, label)
425 struct variable *var;
428 var_set_label (var, label);
432 clear_value_labels (var)
433 struct variable *var;
435 var_clear_value_labels (var);
438 get_write_format (var)
441 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
442 const struct fmt_spec *fmt = var_get_write_format (var);
444 hv_store (fmthash, "fmt", 3, newSVnv (fmt->type), 0);
445 hv_store (fmthash, "decimals", 8, newSVnv (fmt->d), 0);
446 hv_store (fmthash, "width", 5, newSVnv (fmt->w), 0);
448 RETVAL = newRV ((SV *) fmthash);
453 get_print_format (var)
456 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
457 const struct fmt_spec *fmt = var_get_print_format (var);
459 hv_store (fmthash, "fmt", 3, newSVnv (fmt->type), 0);
460 hv_store (fmthash, "decimals", 8, newSVnv (fmt->d), 0);
461 hv_store (fmthash, "width", 5, newSVnv (fmt->w), 0);
463 RETVAL = newRV ((SV *) fmthash);
469 pxs_set_write_format (var, fmt)
473 var_set_write_format (var, &fmt);
477 pxs_set_print_format (var, fmt)
481 var_set_print_format (var, &fmt);
484 pxs_set_output_format (var, fmt)
488 var_set_both_formats (var, &fmt);
492 add_value_label (var, key, label)
497 SV *errstr = get_sv("PSPP::errstr", TRUE);
498 sv_setpv (errstr, "");
500 union value the_value;
501 int width = var_get_width (var);
504 value_init (&the_value, width);
505 if ( var_is_numeric (var))
507 if ( ! looks_like_number (key))
509 sv_setpv (errstr, "Cannot add label with string key to a numeric variable");
510 value_destroy (&the_value, width);
513 the_value.f = SvNV (key);
517 value_copy_str_rpad (&the_value, width, SvPV_nolen(key), ' ');
519 ok = var_add_value_label (var, &the_value, label);
520 value_destroy (&the_value, width);
523 sv_setpv (errstr, "Something went wrong");
533 HV *attrhash = (HV *) sv_2mortal ((SV *) newHV());
535 struct attrset *as = var_get_attributes (var);
539 struct attrset_iterator iter;
540 struct attribute *attr;
542 for (attr = attrset_first (as, &iter);
544 attr = attrset_next (as, &iter))
547 const char *name = attribute_get_name (attr);
549 AV *values = newAV ();
551 for (i = 0 ; i < attribute_get_n_values (attr); ++i )
553 const char *value = attribute_get_value (attr, i);
554 av_push (values, newSVpv (value, 0));
557 hv_store (attrhash, name, strlen (name),
558 newRV_noinc ((SV*) values), 0);
562 RETVAL = newRV ((SV *) attrhash);
569 struct variable * var
571 RETVAL = var_get_name (var);
578 struct variable * var
580 RETVAL = var_get_label (var);
586 get_value_labels (var)
589 HV *labelhash = (HV *) sv_2mortal ((SV *) newHV());
590 const struct val_lab *vl;
591 struct val_labs_iterator *viter = NULL;
592 const struct val_labs *labels = var_get_value_labels (var);
596 for (vl = val_labs_first (labels);
598 vl = val_labs_next (labels, vl))
600 SV *sv = value_to_scalar (&vl->value, var);
602 const char *s = SvPV (sv, len);
603 hv_store (labelhash, s, len, newSVpv (val_lab_get_label (vl), 0), 0);
607 RETVAL = newRV ((SV *) labelhash);
613 MODULE = PSPP PACKAGE = PSPP::Sysfile
616 struct syswriter_info *
617 pxs_create_sysfile (name, dict, opts_hr)
619 struct pspp_dict *dict;
623 struct sfm_write_options opts;
624 if (!SvROK (opts_hr))
626 opts = sfm_writer_default_options ();
630 HV *opt_h = (HV *) SvRV (opts_hr);
631 SV** readonly = hv_fetch(opt_h, "readonly", 8, 0);
632 SV** compress = hv_fetch(opt_h, "compress", 8, 0);
633 SV** version = hv_fetch(opt_h, "version", 7, 0);
635 opts.create_writeable = readonly ? ! SvIV (*readonly) : true;
636 opts.compression = (compress && SvIV (*compress)
639 opts.version = version ? SvIV (*version) : 3 ;
642 struct file_handle *fh =
643 fh_create_file (NULL, name, "UTF-8", fh_default_properties () );
644 struct syswriter_info *swi = xmalloc (sizeof (*swi));
645 swi->writer = sfm_open_writer (fh, dict->dict, opts);
648 swi->dict_sv = dict_sv;
649 SvREFCNT_inc (swi->dict_sv);
657 struct syswriter_info *swi
659 RETVAL = sysfile_close (swi);
665 struct syswriter_info *swi
668 SvREFCNT_dec (swi->dict_sv);
672 append_case (swi, ccase)
673 struct syswriter_info *swi
676 SV *errstr = get_sv("PSPP::errstr", TRUE);
677 sv_setpv (errstr, "");
678 if ( (!SvROK(ccase)))
684 AV *av_case = (AV*) SvRV (ccase);
686 const struct variable **vv;
691 if ( av_len (av_case) >= dict_get_var_cnt (swi->dict->dict))
694 c = case_create (dict_get_proto (swi->dict->dict));
696 dict_get_vars (swi->dict->dict, &vv, &nv,
697 1u << DC_ORDINARY | 1u << DC_SYSTEM);
699 for (sv = av_shift (av_case); SvOK (sv); sv = av_shift (av_case))
701 const struct variable *v = vv[i++];
702 const struct fmt_spec *ifmt = find_input_format (swi->dict, v);
704 /* If an input format has been set, then use it.
705 Otherwise just convert the raw value.
709 struct substring ss = ss_cstr (SvPV_nolen (sv));
713 error = data_in (ss, SvUTF8(sv) ? UTF8: "iso-8859-1", ifmt->type,
714 case_data_rw (c, v), var_get_width (v),
715 dict_get_encoding (swi->dict->dict));
727 scalar_to_value (case_data_rw (c, v), sv, v);
731 /* The remaining variables must be sysmis or blank string */
732 while (i < dict_get_var_cnt (swi->dict->dict))
734 const struct variable *v = vv[i++];
735 union value *val = case_data_rw (c, v);
736 value_set_missing (val, var_get_width (v));
738 casewriter_write (swi->writer, c);
748 MODULE = PSPP PACKAGE = PSPP::Reader
750 struct sysreader_info *
751 pxs_open_sysfile (name)
754 struct casereader *reader;
755 struct sysreader_info *sri = NULL;
756 struct file_handle *fh =
757 fh_create_file (NULL, name, "UTF-8", fh_default_properties () );
758 struct dictionary *dict;
760 sri = xmalloc (sizeof (*sri));
761 sri->reader = any_reader_open_and_decode (fh, NULL, &dict, &sri->opts);
763 sri->dict = create_pspp_dict (dict);
776 pxs_get_dict (reader)
777 struct sysreader_info *reader;
779 RETVAL = reader->dict;
785 struct sysreader_info *sfr;
788 casenumber n = casereader_get_case_cnt (sfr->reader);
789 if (n == CASENUMBER_MAX)
801 struct sysreader_info *sfr;
805 if ((c = casereader_read (sfr->reader)) != NULL)
809 EXTEND (SP, dict_get_var_cnt (sfr->dict->dict));
810 for (v = 0; v < dict_get_var_cnt (sfr->dict->dict); ++v )
812 const struct variable *var = dict_get_var (sfr->dict->dict, v);
813 const union value *val = case_data (c, var);
815 PUSHs (sv_2mortal (value_to_scalar (val, var)));