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/message.h>
36 #include <libpspp/version.h>
37 #include <libpspp/i18n.h>
38 #include <gl/xalloc.h>
39 #include <data/dictionary.h>
40 #include <data/case.h>
41 #include <data/casereader.h>
42 #include <data/casewriter.h>
43 #include <data/variable.h>
44 #include <data/attributes.h>
45 #include <data/file-handle-def.h>
46 #include <data/identifier.h>
47 #include <data/settings.h>
48 #include <data/sys-file-writer.h>
49 #include <data/sys-file-reader.h>
50 #include <data/value.h>
51 #include <data/vardict.h>
52 #include <data/value-labels.h>
53 #include <data/format.h>
54 #include <data/data-in.h>
55 #include <data/data-out.h>
58 typedef struct fmt_spec input_format ;
59 typedef struct fmt_spec output_format ;
62 /* A thin wrapper around sfm_writer */
67 /* A pointer to the writer. The writer is owned by the struct */
68 struct casewriter *writer;
70 /* A pointer to the dictionary. Owned externally */
71 const struct pspp_dict *dict;
73 /* The scalar containing the dictionary */
78 /* A thin wrapper around sfm_reader */
81 struct sfm_read_info opts;
83 /* A pointer to the reader. The reader is owned by the struct */
84 struct casereader *reader;
86 /* A pointer to the dictionary. */
87 struct pspp_dict *dict;
92 struct hmap_node hmap_node; /* In struct pspp_dict's input_formats map. */
93 const struct variable *var;
94 struct fmt_spec input_format;
97 /* A thin wrapper around struct dictionary.*/
99 struct dictionary *dict;
100 struct hmap input_formats; /* Contains struct input_format. */
104 /* A message handler which writes messages to PSPP::errstr */
106 message_handler (const struct msg *m, void *aux)
108 SV *errstr = get_sv("PSPP::errstr", TRUE);
109 sv_setpv (errstr, m->text);
113 sysfile_close (struct syswriter_info *swi)
119 retval = casewriter_destroy (swi->writer);
127 scalar_to_value (union value *val, SV *scalar, const struct variable *var)
129 if ( var_is_numeric (var))
131 if ( SvNOK (scalar) || SvIOK (scalar) )
132 val->f = SvNV (scalar);
139 const char *p = SvPV (scalar, len);
140 int width = var_get_width (var);
141 value_set_missing (val, width);
142 memcpy (value_str_rw (val, width), p, len);
148 value_to_scalar (const union value *val, const struct variable *var)
150 if ( var_is_numeric (var))
152 if ( var_is_value_missing (var, val, MV_SYSTEM))
153 return newSVpvn ("", 0);
155 return newSVnv (val->f);
159 int width = var_get_width (var);
160 return newSVpvn (value_str (val, width), width);
166 make_value_from_scalar (union value *uv, SV *val, const struct variable *var)
168 value_init (uv, var_get_width (var));
169 scalar_to_value (uv, val, var);
172 static struct pspp_dict *
173 create_pspp_dict (struct dictionary *dict)
175 struct pspp_dict *pspp_dict = xmalloc (sizeof *pspp_dict);
176 pspp_dict->dict = dict;
177 hmap_init (&pspp_dict->input_formats);
181 static const struct fmt_spec *
182 find_input_format (const struct pspp_dict *dict, const struct variable *var)
184 struct input_format *input_format;
186 HMAP_FOR_EACH_IN_BUCKET (input_format, struct input_format, hmap_node,
187 hash_pointer (var, 0), &dict->input_formats)
188 if (input_format->var == var)
189 return &input_format->input_format;
197 MODULE = PSPP PACKAGE = PSPP
205 /* Check that the version is correct up to the length of 'ver'.
206 This allows PSPP autobuilders to add a "-build#" suffix to the
207 PSPP version without causing failures here. */
208 assert (0 == strncmp (ver, bare_version, strlen (ver)));
211 msg_set_handler (message_handler, NULL);
216 format_value (val, var)
221 const struct fmt_spec *fmt = var_get_print_format (var);
224 make_value_from_scalar (&uv, val, var);
225 s = data_out (&uv, var_get_encoding (var), fmt);
226 value_destroy (&uv, var_get_width (var));
227 ret = newSVpv (s, fmt->w);
235 value_is_missing (val, var)
241 make_value_from_scalar (&uv, val, var);
242 ret = var_is_value_missing (var, &uv, MV_ANY);
243 value_destroy (&uv, var_get_width (var));
250 MODULE = PSPP PACKAGE = PSPP::Dict
255 RETVAL = create_pspp_dict (dict_create ("UTF-8"));
262 struct pspp_dict *dict
266 struct input_format *input_format, *next_input_format;
268 HMAP_FOR_EACH_SAFE (input_format, next_input_format,
269 struct input_format, hmap_node, &dict->input_formats)
271 hmap_delete (&dict->input_formats, &input_format->hmap_node);
274 hmap_destroy (&dict->input_formats);
275 dict_destroy (dict->dict);
281 struct pspp_dict *dict
283 RETVAL = dict_get_var_cnt (dict->dict);
288 set_label (dict, label)
289 struct pspp_dict *dict
292 dict_set_label (dict->dict, label);
295 set_documents (dict, docs)
296 struct pspp_dict *dict
299 dict_set_documents_string (dict->dict, docs);
303 add_document (dict, doc)
304 struct pspp_dict *dict
307 dict_add_document_line (dict->dict, doc, false);
311 clear_documents (dict)
312 struct pspp_dict *dict
314 dict_clear_documents (dict->dict);
318 set_weight (dict, var)
319 struct pspp_dict *dict
322 dict_set_weight (dict->dict, var);
326 pxs_get_variable (dict, idx)
327 struct pspp_dict *dict
330 SV *errstr = get_sv("PSPP::errstr", TRUE);
331 sv_setpv (errstr, "");
332 if ( SvIV (idx) >= dict_get_var_cnt (dict->dict))
334 sv_setpv (errstr, "The dictionary doesn't have that many variables.");
338 RETVAL = dict_get_var (dict->dict, SvIV (idx));
344 pxs_get_var_by_name (dict, name)
345 struct pspp_dict *dict
348 SV *errstr = get_sv("PSPP::errstr", TRUE);
349 sv_setpv (errstr, "");
351 struct variable *var = dict_lookup_var (dict->dict, name);
353 sv_setpv (errstr, "No such variable.");
359 MODULE = PSPP PACKAGE = PSPP::Var
363 pxs_dict_create_var (dict, name, ip_fmt)
364 struct pspp_dict * dict
368 SV *errstr = get_sv("PSPP::errstr", TRUE);
369 sv_setpv (errstr, "");
370 if ( ! id_is_plausible (name, false))
372 sv_setpv (errstr, "The variable name is not valid.");
376 struct fmt_spec op_fmt;
377 struct input_format *input_format;
380 op_fmt = fmt_for_output_from_input (&ip_fmt);
381 v = dict_create_var (dict->dict, name,
382 fmt_is_string (op_fmt.type) ? op_fmt.w : 0);
385 sv_setpv (errstr, "The variable could not be created (probably already exists).");
388 var_set_both_formats (v, &op_fmt);
390 input_format = xmalloc (sizeof *input_format);
391 input_format->var = v;
392 input_format->input_format = ip_fmt;
393 hmap_insert (&dict->input_formats, &input_format->hmap_node,
394 hash_pointer (v, 0));
402 set_missing_values (var, v1, ...)
403 struct variable *var;
410 croak ("No more than 3 missing values are permitted");
412 for (i = 0; i < items - 1; ++i)
413 scalar_to_value (&val[i], ST(i+1), var);
415 struct missing_values mv;
416 mv_init (&mv, var_get_width (var));
417 for (i = 0 ; i < items - 1; ++i )
418 mv_add_value (&mv, &val[i]);
419 var_set_missing_values (var, &mv);
423 set_label (var, label)
424 struct variable *var;
427 var_set_label (var, label);
431 clear_value_labels (var)
432 struct variable *var;
434 var_clear_value_labels (var);
437 get_write_format (var)
440 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
441 const struct fmt_spec *fmt = var_get_write_format (var);
443 hv_store (fmthash, "fmt", 3, newSVnv (fmt->type), 0);
444 hv_store (fmthash, "decimals", 8, newSVnv (fmt->d), 0);
445 hv_store (fmthash, "width", 5, newSVnv (fmt->w), 0);
447 RETVAL = newRV ((SV *) fmthash);
452 get_print_format (var)
455 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
456 const struct fmt_spec *fmt = var_get_print_format (var);
458 hv_store (fmthash, "fmt", 3, newSVnv (fmt->type), 0);
459 hv_store (fmthash, "decimals", 8, newSVnv (fmt->d), 0);
460 hv_store (fmthash, "width", 5, newSVnv (fmt->w), 0);
462 RETVAL = newRV ((SV *) fmthash);
468 pxs_set_write_format (var, fmt)
472 var_set_write_format (var, &fmt);
476 pxs_set_print_format (var, fmt)
480 var_set_print_format (var, &fmt);
483 pxs_set_output_format (var, fmt)
487 var_set_both_formats (var, &fmt);
491 add_value_label (var, key, label)
496 SV *errstr = get_sv("PSPP::errstr", TRUE);
497 sv_setpv (errstr, "");
499 union value the_value;
500 int width = var_get_width (var);
503 value_init (&the_value, width);
504 if ( var_is_numeric (var))
506 if ( ! looks_like_number (key))
508 sv_setpv (errstr, "Cannot add label with string key to a numeric variable");
509 value_destroy (&the_value, width);
512 the_value.f = SvNV (key);
516 value_copy_str_rpad (&the_value, width, SvPV_nolen(key), ' ');
518 ok = var_add_value_label (var, &the_value, label);
519 value_destroy (&the_value, width);
522 sv_setpv (errstr, "Something went wrong");
532 HV *attrhash = (HV *) sv_2mortal ((SV *) newHV());
534 struct attrset *as = var_get_attributes (var);
538 struct attrset_iterator iter;
539 struct attribute *attr;
541 for (attr = attrset_first (as, &iter);
543 attr = attrset_next (as, &iter))
546 const char *name = attribute_get_name (attr);
548 AV *values = newAV ();
550 for (i = 0 ; i < attribute_get_n_values (attr); ++i )
552 const char *value = attribute_get_value (attr, i);
553 av_push (values, newSVpv (value, 0));
556 hv_store (attrhash, name, strlen (name),
557 newRV_noinc ((SV*) values), 0);
561 RETVAL = newRV ((SV *) attrhash);
568 struct variable * var
570 RETVAL = var_get_name (var);
577 struct variable * var
579 RETVAL = var_get_label (var);
585 get_value_labels (var)
588 HV *labelhash = (HV *) sv_2mortal ((SV *) newHV());
589 const struct val_lab *vl;
590 struct val_labs_iterator *viter = NULL;
591 const struct val_labs *labels = var_get_value_labels (var);
595 for (vl = val_labs_first (labels);
597 vl = val_labs_next (labels, vl))
599 SV *sv = value_to_scalar (&vl->value, var);
601 const char *s = SvPV (sv, len);
602 hv_store (labelhash, s, len, newSVpv (val_lab_get_label (vl), 0), 0);
606 RETVAL = newRV ((SV *) labelhash);
612 MODULE = PSPP PACKAGE = PSPP::Sysfile
615 struct syswriter_info *
616 pxs_create_sysfile (name, dict, opts_hr)
618 struct pspp_dict *dict;
622 struct sfm_write_options opts;
623 if (!SvROK (opts_hr))
625 opts = sfm_writer_default_options ();
629 HV *opt_h = (HV *) SvRV (opts_hr);
630 SV** readonly = hv_fetch(opt_h, "readonly", 8, 0);
631 SV** compress = hv_fetch(opt_h, "compress", 8, 0);
632 SV** version = hv_fetch(opt_h, "version", 7, 0);
634 opts.create_writeable = readonly ? ! SvIV (*readonly) : true;
635 opts.compression = (compress && SvIV (*compress)
638 opts.version = version ? SvIV (*version) : 3 ;
641 struct file_handle *fh =
642 fh_create_file (NULL, name, fh_default_properties () );
643 struct syswriter_info *swi = xmalloc (sizeof (*swi));
644 swi->writer = sfm_open_writer (fh, dict->dict, opts);
647 swi->dict_sv = dict_sv;
648 SvREFCNT_inc (swi->dict_sv);
656 struct syswriter_info *swi
658 RETVAL = sysfile_close (swi);
664 struct syswriter_info *swi
667 SvREFCNT_dec (swi->dict_sv);
671 append_case (swi, ccase)
672 struct syswriter_info *swi
675 SV *errstr = get_sv("PSPP::errstr", TRUE);
676 sv_setpv (errstr, "");
677 if ( (!SvROK(ccase)))
683 AV *av_case = (AV*) SvRV (ccase);
685 const struct variable **vv;
690 if ( av_len (av_case) >= dict_get_var_cnt (swi->dict->dict))
693 c = case_create (dict_get_proto (swi->dict->dict));
695 dict_get_vars (swi->dict->dict, &vv, &nv,
696 1u << DC_ORDINARY | 1u << DC_SYSTEM);
698 for (sv = av_shift (av_case); SvOK (sv); sv = av_shift (av_case))
700 const struct variable *v = vv[i++];
701 const struct fmt_spec *ifmt = find_input_format (swi->dict, v);
703 /* If an input format has been set, then use it.
704 Otherwise just convert the raw value.
708 struct substring ss = ss_cstr (SvPV_nolen (sv));
712 error = data_in (ss, SvUTF8(sv) ? UTF8: "iso-8859-1", ifmt->type,
713 case_data_rw (c, v), var_get_width (v),
714 dict_get_encoding (swi->dict->dict));
726 scalar_to_value (case_data_rw (c, v), sv, v);
730 /* The remaining variables must be sysmis or blank string */
731 while (i < dict_get_var_cnt (swi->dict->dict))
733 const struct variable *v = vv[i++];
734 union value *val = case_data_rw (c, v);
735 value_set_missing (val, var_get_width (v));
737 casewriter_write (swi->writer, c);
747 MODULE = PSPP PACKAGE = PSPP::Reader
749 struct sysreader_info *
750 pxs_open_sysfile (name)
753 struct casereader *reader;
754 struct sysreader_info *sri = NULL;
755 struct file_handle *fh =
756 fh_create_file (NULL, name, fh_default_properties () );
757 struct dictionary *dict;
758 struct sfm_reader *r;
760 sri = xmalloc (sizeof (*sri));
764 sri->reader = sfm_decode (r, NULL, &dict, &sri->opts);
766 sri->dict = create_pspp_dict (dict);
785 pxs_get_dict (reader)
786 struct sysreader_info *reader;
788 RETVAL = reader->dict;
794 struct sysreader_info *sfr;
797 casenumber n = casereader_get_case_cnt (sfr->reader);
798 if (n == CASENUMBER_MAX)
810 struct sysreader_info *sfr;
814 if ((c = casereader_read (sfr->reader)) != NULL)
818 EXTEND (SP, dict_get_var_cnt (sfr->dict->dict));
819 for (v = 0; v < dict_get_var_cnt (sfr->dict->dict); ++v )
821 const struct variable *var = dict_get_var (sfr->dict->dict, v);
822 const union value *val = case_data (c, var);
824 PUSHs (sv_2mortal (value_to_scalar (val, var)));