1 /* PSPP - computes sample statistics.
2 Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013 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 /* A thin wrapper around struct dictionary.*/
94 struct dictionary *dict;
98 /* A message handler which writes messages to PSPP::errstr */
100 message_handler (const struct msg *m, void *aux)
102 SV *errstr = get_sv("PSPP::errstr", TRUE);
103 sv_setpv (errstr, m->text);
107 sysfile_close (struct syswriter_info *swi)
113 retval = casewriter_destroy (swi->writer);
121 scalar_to_value (union value *val, SV *scalar, const struct variable *var)
123 if ( var_is_numeric (var))
125 if ( SvNOK (scalar) || SvIOK (scalar) )
126 val->f = SvNV (scalar);
133 const char *p = SvPV (scalar, len);
134 int width = var_get_width (var);
135 value_set_missing (val, width);
136 memcpy (value_str_rw (val, width), p, len);
142 value_to_scalar (const union value *val, const struct variable *var)
144 if ( var_is_numeric (var))
146 if ( var_is_value_missing (var, val, MV_SYSTEM))
147 return newSVpvn ("", 0);
149 return newSVnv (val->f);
153 int width = var_get_width (var);
154 return newSVpvn (value_str (val, width), width);
160 var_set_input_format (struct variable *v, input_format ip_fmt)
162 struct fmt_spec *if_copy = malloc (sizeof (*if_copy));
163 memcpy (if_copy, &ip_fmt, sizeof (ip_fmt));
164 var_attach_aux (v, if_copy, var_dtor_free);
168 make_value_from_scalar (union value *uv, SV *val, const struct variable *var)
170 value_init (uv, var_get_width (var));
171 scalar_to_value (uv, val, var);
174 static struct pspp_dict *
175 create_pspp_dict (struct dictionary *dict)
177 struct pspp_dict *pspp_dict = xmalloc (sizeof *pspp_dict);
178 pspp_dict->dict = dict;
185 MODULE = PSPP PACKAGE = PSPP
193 /* Check that the version is correct up to the length of 'ver'.
194 This allows PSPP autobuilders to add a "-build#" suffix to the
195 PSPP version without causing failures here. */
196 assert (0 == strncmp (ver, bare_version, strlen (ver)));
199 msg_set_handler (message_handler, NULL);
204 format_value (val, var)
209 const struct fmt_spec *fmt = var_get_print_format (var);
212 make_value_from_scalar (&uv, val, var);
213 s = data_out (&uv, var_get_encoding (var), fmt);
214 value_destroy (&uv, var_get_width (var));
215 ret = newSVpv (s, fmt->w);
223 value_is_missing (val, var)
229 make_value_from_scalar (&uv, val, var);
230 ret = var_is_value_missing (var, &uv, MV_ANY);
231 value_destroy (&uv, var_get_width (var));
238 MODULE = PSPP PACKAGE = PSPP::Dict
243 RETVAL = create_pspp_dict (dict_create ("UTF-8"));
250 struct pspp_dict *dict
254 dict_destroy (dict->dict);
260 struct pspp_dict *dict
262 RETVAL = dict_get_var_cnt (dict->dict);
267 set_label (dict, label)
268 struct pspp_dict *dict
271 dict_set_label (dict->dict, label);
274 set_documents (dict, docs)
275 struct pspp_dict *dict
278 dict_set_documents_string (dict->dict, docs);
282 add_document (dict, doc)
283 struct pspp_dict *dict
286 dict_add_document_line (dict->dict, doc, false);
290 clear_documents (dict)
291 struct pspp_dict *dict
293 dict_clear_documents (dict->dict);
297 set_weight (dict, var)
298 struct pspp_dict *dict
301 dict_set_weight (dict->dict, var);
305 pxs_get_variable (dict, idx)
306 struct pspp_dict *dict
309 SV *errstr = get_sv("PSPP::errstr", TRUE);
310 sv_setpv (errstr, "");
311 if ( SvIV (idx) >= dict_get_var_cnt (dict->dict))
313 sv_setpv (errstr, "The dictionary doesn't have that many variables.");
317 RETVAL = dict_get_var (dict->dict, SvIV (idx));
323 pxs_get_var_by_name (dict, name)
324 struct pspp_dict *dict
327 SV *errstr = get_sv("PSPP::errstr", TRUE);
328 sv_setpv (errstr, "");
330 struct variable *var = dict_lookup_var (dict->dict, name);
332 sv_setpv (errstr, "No such variable.");
338 MODULE = PSPP PACKAGE = PSPP::Var
342 pxs_dict_create_var (dict, name, ip_fmt)
343 struct pspp_dict * dict
347 SV *errstr = get_sv("PSPP::errstr", TRUE);
348 sv_setpv (errstr, "");
349 if ( ! id_is_plausible (name, false))
351 sv_setpv (errstr, "The variable name is not valid.");
355 struct fmt_spec op_fmt;
358 op_fmt = fmt_for_output_from_input (&ip_fmt);
359 v = dict_create_var (dict->dict, name,
360 fmt_is_string (op_fmt.type) ? op_fmt.w : 0);
363 sv_setpv (errstr, "The variable could not be created (probably already exists).");
366 var_set_both_formats (v, &op_fmt);
367 var_set_input_format (v, ip_fmt);
374 set_missing_values (var, v1, ...)
375 struct variable *var;
382 croak ("No more than 3 missing values are permitted");
384 for (i = 0; i < items - 1; ++i)
385 scalar_to_value (&val[i], ST(i+1), var);
387 struct missing_values mv;
388 mv_init (&mv, var_get_width (var));
389 for (i = 0 ; i < items - 1; ++i )
390 mv_add_value (&mv, &val[i]);
391 var_set_missing_values (var, &mv);
395 set_label (var, label)
396 struct variable *var;
399 var_set_label (var, label, false);
403 clear_value_labels (var)
404 struct variable *var;
406 var_clear_value_labels (var);
409 get_write_format (var)
412 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
413 const struct fmt_spec *fmt = var_get_write_format (var);
415 hv_store (fmthash, "fmt", 3, newSVnv (fmt->type), 0);
416 hv_store (fmthash, "decimals", 8, newSVnv (fmt->d), 0);
417 hv_store (fmthash, "width", 5, newSVnv (fmt->w), 0);
419 RETVAL = newRV ((SV *) fmthash);
424 get_print_format (var)
427 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
428 const struct fmt_spec *fmt = var_get_print_format (var);
430 hv_store (fmthash, "fmt", 3, newSVnv (fmt->type), 0);
431 hv_store (fmthash, "decimals", 8, newSVnv (fmt->d), 0);
432 hv_store (fmthash, "width", 5, newSVnv (fmt->w), 0);
434 RETVAL = newRV ((SV *) fmthash);
440 pxs_set_write_format (var, fmt)
444 var_set_write_format (var, &fmt);
448 pxs_set_print_format (var, fmt)
452 var_set_print_format (var, &fmt);
455 pxs_set_output_format (var, fmt)
459 var_set_both_formats (var, &fmt);
463 add_value_label (var, key, label)
468 SV *errstr = get_sv("PSPP::errstr", TRUE);
469 sv_setpv (errstr, "");
471 union value the_value;
472 int width = var_get_width (var);
475 value_init (&the_value, width);
476 if ( var_is_numeric (var))
478 if ( ! looks_like_number (key))
480 sv_setpv (errstr, "Cannot add label with string key to a numeric variable");
481 value_destroy (&the_value, width);
484 the_value.f = SvNV (key);
488 value_copy_str_rpad (&the_value, width, SvPV_nolen(key), ' ');
490 ok = var_add_value_label (var, &the_value, label);
491 value_destroy (&the_value, width);
494 sv_setpv (errstr, "Something went wrong");
504 HV *attrhash = (HV *) sv_2mortal ((SV *) newHV());
506 struct attrset *as = var_get_attributes (var);
510 struct attrset_iterator iter;
511 struct attribute *attr;
513 for (attr = attrset_first (as, &iter);
515 attr = attrset_next (as, &iter))
518 const char *name = attribute_get_name (attr);
520 AV *values = newAV ();
522 for (i = 0 ; i < attribute_get_n_values (attr); ++i )
524 const char *value = attribute_get_value (attr, i);
525 av_push (values, newSVpv (value, 0));
528 hv_store (attrhash, name, strlen (name),
529 newRV_noinc ((SV*) values), 0);
533 RETVAL = newRV ((SV *) attrhash);
540 struct variable * var
542 RETVAL = var_get_name (var);
549 struct variable * var
551 RETVAL = var_get_label (var);
557 get_value_labels (var)
560 HV *labelhash = (HV *) sv_2mortal ((SV *) newHV());
561 const struct val_lab *vl;
562 struct val_labs_iterator *viter = NULL;
563 const struct val_labs *labels = var_get_value_labels (var);
567 for (vl = val_labs_first (labels);
569 vl = val_labs_next (labels, vl))
571 SV *sv = value_to_scalar (&vl->value, var);
573 const char *s = SvPV (sv, len);
574 hv_store (labelhash, s, len, newSVpv (val_lab_get_label (vl), 0), 0);
578 RETVAL = newRV ((SV *) labelhash);
584 MODULE = PSPP PACKAGE = PSPP::Sysfile
587 struct syswriter_info *
588 pxs_create_sysfile (name, dict, opts_hr)
590 struct pspp_dict *dict;
594 struct sfm_write_options opts;
595 if (!SvROK (opts_hr))
597 opts = sfm_writer_default_options ();
601 HV *opt_h = (HV *) SvRV (opts_hr);
602 SV** readonly = hv_fetch(opt_h, "readonly", 8, 0);
603 SV** compress = hv_fetch(opt_h, "compress", 8, 0);
604 SV** version = hv_fetch(opt_h, "version", 7, 0);
606 opts.create_writeable = readonly ? ! SvIV (*readonly) : true;
607 opts.compress = compress ? SvIV (*compress) : false;
608 opts.version = version ? SvIV (*version) : 3 ;
611 struct file_handle *fh =
612 fh_create_file (NULL, name, fh_default_properties () );
613 struct syswriter_info *swi = xmalloc (sizeof (*swi));
614 swi->writer = sfm_open_writer (fh, dict->dict, opts);
617 swi->dict_sv = dict_sv;
618 SvREFCNT_inc (swi->dict_sv);
626 struct syswriter_info *swi
628 RETVAL = sysfile_close (swi);
634 struct syswriter_info *swi
637 SvREFCNT_dec (swi->dict_sv);
641 append_case (swi, ccase)
642 struct syswriter_info *swi
645 SV *errstr = get_sv("PSPP::errstr", TRUE);
646 sv_setpv (errstr, "");
647 if ( (!SvROK(ccase)))
653 AV *av_case = (AV*) SvRV (ccase);
655 const struct variable **vv;
660 if ( av_len (av_case) >= dict_get_var_cnt (swi->dict->dict))
663 c = case_create (dict_get_proto (swi->dict->dict));
665 dict_get_vars (swi->dict->dict, &vv, &nv,
666 1u << DC_ORDINARY | 1u << DC_SYSTEM);
668 for (sv = av_shift (av_case); SvOK (sv); sv = av_shift (av_case))
670 const struct variable *v = vv[i++];
671 const struct fmt_spec *ifmt = var_get_aux (v);
673 /* If an input format has been set, then use it.
674 Otherwise just convert the raw value.
678 struct substring ss = ss_cstr (SvPV_nolen (sv));
682 error = data_in (ss, SvUTF8(sv) ? UTF8: "iso-8859-1", ifmt->type,
683 case_data_rw (c, v), var_get_width (v),
684 dict_get_encoding (swi->dict->dict));
696 scalar_to_value (case_data_rw (c, v), sv, v);
700 /* The remaining variables must be sysmis or blank string */
701 while (i < dict_get_var_cnt (swi->dict->dict))
703 const struct variable *v = vv[i++];
704 union value *val = case_data_rw (c, v);
705 value_set_missing (val, var_get_width (v));
707 casewriter_write (swi->writer, c);
717 MODULE = PSPP PACKAGE = PSPP::Reader
719 struct sysreader_info *
720 pxs_open_sysfile (name)
723 struct casereader *reader;
724 struct sysreader_info *sri = NULL;
725 struct file_handle *fh =
726 fh_create_file (NULL, name, fh_default_properties () );
727 struct dictionary *dict;
729 sri = xmalloc (sizeof (*sri));
730 sri->reader = sfm_open_reader (fh, NULL, &dict, &sri->opts);
732 if ( sri->reader != NULL)
733 sri->dict = create_pspp_dict (dict);
746 pxs_get_dict (reader)
747 struct sysreader_info *reader;
749 RETVAL = reader->dict;
755 struct sysreader_info *sfr;
758 casenumber n = casereader_get_case_cnt (sfr->reader);
759 if (n == CASENUMBER_MAX)
771 struct sysreader_info *sfr;
775 if ((c = casereader_read (sfr->reader)) != NULL)
779 EXTEND (SP, dict_get_var_cnt (sfr->dict->dict));
780 for (v = 0; v < dict_get_var_cnt (sfr->dict->dict); ++v )
782 const struct variable *var = dict_get_var (sfr->dict->dict, v);
783 const union value *val = case_data (c, var);
785 PUSHs (sv_2mortal (value_to_scalar (val, var)));