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/value.h>
50 #include <data/vardict.h>
51 #include <data/value-labels.h>
52 #include <data/format.h>
53 #include <data/data-in.h>
54 #include <data/data-out.h>
57 typedef struct fmt_spec input_format ;
58 typedef struct fmt_spec output_format ;
61 /* A thin wrapper around sfm_writer */
66 /* A pointer to the writer. The writer is owned by the struct */
67 struct casewriter *writer;
69 /* A pointer to the dictionary. Owned externally */
70 const struct pspp_dict *dict;
72 /* The scalar containing the dictionary */
77 /* A thin wrapper around sfm_reader */
80 struct any_read_info opts;
82 /* A pointer to the reader. The reader is owned by the struct */
83 struct casereader *reader;
85 /* A pointer to the dictionary. */
86 struct pspp_dict *dict;
91 struct hmap_node hmap_node; /* In struct pspp_dict's input_formats map. */
92 const struct variable *var;
93 struct fmt_spec input_format;
96 /* A thin wrapper around struct dictionary.*/
98 struct dictionary *dict;
99 struct hmap input_formats; /* Contains struct input_format. */
103 /* A message handler which writes messages to PSPP::errstr */
105 message_handler (const struct msg *m, void *aux)
107 SV *errstr = get_sv("PSPP::errstr", TRUE);
108 sv_setpv (errstr, m->text);
112 sysfile_close (struct syswriter_info *swi)
118 retval = casewriter_destroy (swi->writer);
126 scalar_to_value (union value *val, SV *scalar, const struct variable *var)
128 if ( var_is_numeric (var))
130 if ( SvNOK (scalar) || SvIOK (scalar) )
131 val->f = SvNV (scalar);
138 const char *p = SvPV (scalar, len);
139 int width = var_get_width (var);
140 value_set_missing (val, width);
141 memcpy (value_str_rw (val, width), p, len);
147 value_to_scalar (const union value *val, const struct variable *var)
149 if ( var_is_numeric (var))
151 if ( var_is_value_missing (var, val, MV_SYSTEM))
152 return newSVpvn ("", 0);
154 return newSVnv (val->f);
158 int width = var_get_width (var);
159 return newSVpvn (value_str (val, width), width);
165 make_value_from_scalar (union value *uv, SV *val, const struct variable *var)
167 value_init (uv, var_get_width (var));
168 scalar_to_value (uv, val, var);
171 static struct pspp_dict *
172 create_pspp_dict (struct dictionary *dict)
174 struct pspp_dict *pspp_dict = xmalloc (sizeof *pspp_dict);
175 pspp_dict->dict = dict;
176 hmap_init (&pspp_dict->input_formats);
180 static const struct fmt_spec *
181 find_input_format (const struct pspp_dict *dict, const struct variable *var)
183 struct input_format *input_format;
185 HMAP_FOR_EACH_IN_BUCKET (input_format, struct input_format, hmap_node,
186 hash_pointer (var, 0), &dict->input_formats)
187 if (input_format->var == var)
188 return &input_format->input_format;
196 MODULE = PSPP PACKAGE = PSPP
204 /* Check that the version is correct up to the length of 'ver'.
205 This allows PSPP autobuilders to add a "-build#" suffix to the
206 PSPP version without causing failures here. */
207 assert (0 == strncmp (ver, bare_version, strlen (ver)));
210 msg_set_handler (message_handler, NULL);
215 format_value (val, var)
220 const struct fmt_spec *fmt = var_get_print_format (var);
223 make_value_from_scalar (&uv, val, var);
224 s = data_out (&uv, var_get_encoding (var), fmt);
225 value_destroy (&uv, var_get_width (var));
226 ret = newSVpv (s, fmt->w);
234 value_is_missing (val, var)
240 make_value_from_scalar (&uv, val, var);
241 ret = var_is_value_missing (var, &uv, MV_ANY);
242 value_destroy (&uv, var_get_width (var));
249 MODULE = PSPP PACKAGE = PSPP::Dict
254 RETVAL = create_pspp_dict (dict_create ("UTF-8"));
261 struct pspp_dict *dict
265 struct input_format *input_format, *next_input_format;
267 HMAP_FOR_EACH_SAFE (input_format, next_input_format,
268 struct input_format, hmap_node, &dict->input_formats)
270 hmap_delete (&dict->input_formats, &input_format->hmap_node);
273 hmap_destroy (&dict->input_formats);
274 dict_destroy (dict->dict);
280 struct pspp_dict *dict
282 RETVAL = dict_get_var_cnt (dict->dict);
287 set_label (dict, label)
288 struct pspp_dict *dict
291 dict_set_label (dict->dict, label);
294 set_documents (dict, docs)
295 struct pspp_dict *dict
298 dict_set_documents_string (dict->dict, docs);
302 add_document (dict, doc)
303 struct pspp_dict *dict
306 dict_add_document_line (dict->dict, doc, false);
310 clear_documents (dict)
311 struct pspp_dict *dict
313 dict_clear_documents (dict->dict);
317 set_weight (dict, var)
318 struct pspp_dict *dict
321 dict_set_weight (dict->dict, var);
325 pxs_get_variable (dict, idx)
326 struct pspp_dict *dict
329 SV *errstr = get_sv("PSPP::errstr", TRUE);
330 sv_setpv (errstr, "");
331 if ( SvIV (idx) >= dict_get_var_cnt (dict->dict))
333 sv_setpv (errstr, "The dictionary doesn't have that many variables.");
337 RETVAL = dict_get_var (dict->dict, SvIV (idx));
343 pxs_get_var_by_name (dict, name)
344 struct pspp_dict *dict
347 SV *errstr = get_sv("PSPP::errstr", TRUE);
348 sv_setpv (errstr, "");
350 struct variable *var = dict_lookup_var (dict->dict, name);
352 sv_setpv (errstr, "No such variable.");
358 MODULE = PSPP PACKAGE = PSPP::Var
362 pxs_dict_create_var (dict, name, ip_fmt)
363 struct pspp_dict * dict
367 SV *errstr = get_sv("PSPP::errstr", TRUE);
368 sv_setpv (errstr, "");
369 if ( ! id_is_plausible (name, false))
371 sv_setpv (errstr, "The variable name is not valid.");
375 struct fmt_spec op_fmt;
376 struct input_format *input_format;
379 op_fmt = fmt_for_output_from_input (&ip_fmt);
380 v = dict_create_var (dict->dict, name,
381 fmt_is_string (op_fmt.type) ? op_fmt.w : 0);
384 sv_setpv (errstr, "The variable could not be created (probably already exists).");
387 var_set_both_formats (v, &op_fmt);
389 input_format = xmalloc (sizeof *input_format);
390 input_format->var = v;
391 input_format->input_format = ip_fmt;
392 hmap_insert (&dict->input_formats, &input_format->hmap_node,
393 hash_pointer (v, 0));
401 set_missing_values (var, v1, ...)
402 struct variable *var;
409 croak ("No more than 3 missing values are permitted");
411 for (i = 0; i < items - 1; ++i)
412 scalar_to_value (&val[i], ST(i+1), var);
414 struct missing_values mv;
415 mv_init (&mv, var_get_width (var));
416 for (i = 0 ; i < items - 1; ++i )
417 mv_add_value (&mv, &val[i]);
418 var_set_missing_values (var, &mv);
422 set_label (var, label)
423 struct variable *var;
426 var_set_label (var, label);
430 clear_value_labels (var)
431 struct variable *var;
433 var_clear_value_labels (var);
436 get_write_format (var)
439 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
440 const struct fmt_spec *fmt = var_get_write_format (var);
442 hv_store (fmthash, "fmt", 3, newSVnv (fmt->type), 0);
443 hv_store (fmthash, "decimals", 8, newSVnv (fmt->d), 0);
444 hv_store (fmthash, "width", 5, newSVnv (fmt->w), 0);
446 RETVAL = newRV ((SV *) fmthash);
451 get_print_format (var)
454 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
455 const struct fmt_spec *fmt = var_get_print_format (var);
457 hv_store (fmthash, "fmt", 3, newSVnv (fmt->type), 0);
458 hv_store (fmthash, "decimals", 8, newSVnv (fmt->d), 0);
459 hv_store (fmthash, "width", 5, newSVnv (fmt->w), 0);
461 RETVAL = newRV ((SV *) fmthash);
467 pxs_set_write_format (var, fmt)
471 var_set_write_format (var, &fmt);
475 pxs_set_print_format (var, fmt)
479 var_set_print_format (var, &fmt);
482 pxs_set_output_format (var, fmt)
486 var_set_both_formats (var, &fmt);
490 add_value_label (var, key, label)
495 SV *errstr = get_sv("PSPP::errstr", TRUE);
496 sv_setpv (errstr, "");
498 union value the_value;
499 int width = var_get_width (var);
502 value_init (&the_value, width);
503 if ( var_is_numeric (var))
505 if ( ! looks_like_number (key))
507 sv_setpv (errstr, "Cannot add label with string key to a numeric variable");
508 value_destroy (&the_value, width);
511 the_value.f = SvNV (key);
515 value_copy_str_rpad (&the_value, width, SvPV_nolen(key), ' ');
517 ok = var_add_value_label (var, &the_value, label);
518 value_destroy (&the_value, width);
521 sv_setpv (errstr, "Something went wrong");
531 HV *attrhash = (HV *) sv_2mortal ((SV *) newHV());
533 struct attrset *as = var_get_attributes (var);
537 struct attrset_iterator iter;
538 struct attribute *attr;
540 for (attr = attrset_first (as, &iter);
542 attr = attrset_next (as, &iter))
545 const char *name = attribute_get_name (attr);
547 AV *values = newAV ();
549 for (i = 0 ; i < attribute_get_n_values (attr); ++i )
551 const char *value = attribute_get_value (attr, i);
552 av_push (values, newSVpv (value, 0));
555 hv_store (attrhash, name, strlen (name),
556 newRV_noinc ((SV*) values), 0);
560 RETVAL = newRV ((SV *) attrhash);
567 struct variable * var
569 RETVAL = var_get_name (var);
576 struct variable * var
578 RETVAL = var_get_label (var);
584 get_value_labels (var)
587 HV *labelhash = (HV *) sv_2mortal ((SV *) newHV());
588 const struct val_lab *vl;
589 struct val_labs_iterator *viter = NULL;
590 const struct val_labs *labels = var_get_value_labels (var);
594 for (vl = val_labs_first (labels);
596 vl = val_labs_next (labels, vl))
598 SV *sv = value_to_scalar (&vl->value, var);
600 const char *s = SvPV (sv, len);
601 hv_store (labelhash, s, len, newSVpv (val_lab_get_label (vl), 0), 0);
605 RETVAL = newRV ((SV *) labelhash);
611 MODULE = PSPP PACKAGE = PSPP::Sysfile
614 struct syswriter_info *
615 pxs_create_sysfile (name, dict, opts_hr)
617 struct pspp_dict *dict;
621 struct sfm_write_options opts;
622 if (!SvROK (opts_hr))
624 opts = sfm_writer_default_options ();
628 HV *opt_h = (HV *) SvRV (opts_hr);
629 SV** readonly = hv_fetch(opt_h, "readonly", 8, 0);
630 SV** compress = hv_fetch(opt_h, "compress", 8, 0);
631 SV** version = hv_fetch(opt_h, "version", 7, 0);
633 opts.create_writeable = readonly ? ! SvIV (*readonly) : true;
634 opts.compression = (compress && SvIV (*compress)
637 opts.version = version ? SvIV (*version) : 3 ;
640 struct file_handle *fh =
641 fh_create_file (NULL, name, fh_default_properties () );
642 struct syswriter_info *swi = xmalloc (sizeof (*swi));
643 swi->writer = sfm_open_writer (fh, dict->dict, opts);
646 swi->dict_sv = dict_sv;
647 SvREFCNT_inc (swi->dict_sv);
655 struct syswriter_info *swi
657 RETVAL = sysfile_close (swi);
663 struct syswriter_info *swi
666 SvREFCNT_dec (swi->dict_sv);
670 append_case (swi, ccase)
671 struct syswriter_info *swi
674 SV *errstr = get_sv("PSPP::errstr", TRUE);
675 sv_setpv (errstr, "");
676 if ( (!SvROK(ccase)))
682 AV *av_case = (AV*) SvRV (ccase);
684 const struct variable **vv;
689 if ( av_len (av_case) >= dict_get_var_cnt (swi->dict->dict))
692 c = case_create (dict_get_proto (swi->dict->dict));
694 dict_get_vars (swi->dict->dict, &vv, &nv,
695 1u << DC_ORDINARY | 1u << DC_SYSTEM);
697 for (sv = av_shift (av_case); SvOK (sv); sv = av_shift (av_case))
699 const struct variable *v = vv[i++];
700 const struct fmt_spec *ifmt = find_input_format (swi->dict, v);
702 /* If an input format has been set, then use it.
703 Otherwise just convert the raw value.
707 struct substring ss = ss_cstr (SvPV_nolen (sv));
711 error = data_in (ss, SvUTF8(sv) ? UTF8: "iso-8859-1", ifmt->type,
712 case_data_rw (c, v), var_get_width (v),
713 dict_get_encoding (swi->dict->dict));
725 scalar_to_value (case_data_rw (c, v), sv, v);
729 /* The remaining variables must be sysmis or blank string */
730 while (i < dict_get_var_cnt (swi->dict->dict))
732 const struct variable *v = vv[i++];
733 union value *val = case_data_rw (c, v);
734 value_set_missing (val, var_get_width (v));
736 casewriter_write (swi->writer, c);
746 MODULE = PSPP PACKAGE = PSPP::Reader
748 struct sysreader_info *
749 pxs_open_sysfile (name)
752 struct casereader *reader;
753 struct sysreader_info *sri = NULL;
754 struct file_handle *fh =
755 fh_create_file (NULL, name, fh_default_properties () );
756 struct dictionary *dict;
758 sri = xmalloc (sizeof (*sri));
759 sri->reader = any_reader_open_and_decode (fh, NULL, &dict, &sri->opts);
761 sri->dict = create_pspp_dict (dict);
774 pxs_get_dict (reader)
775 struct sysreader_info *reader;
777 RETVAL = reader->dict;
783 struct sysreader_info *sfr;
786 casenumber n = casereader_get_case_cnt (sfr->reader);
787 if (n == CASENUMBER_MAX)
799 struct sysreader_info *sfr;
803 if ((c = casereader_read (sfr->reader)) != NULL)
807 EXTEND (SP, dict_get_var_cnt (sfr->dict->dict));
808 for (v = 0; v < dict_get_var_cnt (sfr->dict->dict); ++v )
810 const struct variable *var = dict_get_var (sfr->dict->dict, v);
811 const union value *val = case_data (c, var);
813 PUSHs (sv_2mortal (value_to_scalar (val, var)));