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
23 /* The Gnulib "strftime" module defines my_strftime in <config.h> for use by
24 gl/strftime.c. Perl also defines my_strftime in embed.h for some other
25 purpose. The former definition doesn't matter in this file, so suppress it
26 to avoid a compiler warning. */
36 #include <libpspp/hmap.h>
37 #include <libpspp/hash-functions.h>
38 #include <libpspp/message.h>
39 #include <libpspp/version.h>
40 #include <libpspp/i18n.h>
41 #include <gl/xalloc.h>
42 #include <data/dictionary.h>
43 #include <data/case.h>
44 #include <data/casereader.h>
45 #include <data/casewriter.h>
46 #include <data/variable.h>
47 #include <data/attributes.h>
48 #include <data/file-handle-def.h>
49 #include <data/identifier.h>
50 #include <data/settings.h>
51 #include <data/sys-file-writer.h>
52 #include <data/value.h>
53 #include <data/vardict.h>
54 #include <data/value-labels.h>
55 #include <data/format.h>
56 #include <data/data-in.h>
57 #include <data/data-out.h>
60 typedef struct fmt_spec input_format ;
61 typedef struct fmt_spec output_format ;
64 /* A thin wrapper around sfm_writer */
69 /* A pointer to the writer. The writer is owned by the struct */
70 struct casewriter *writer;
72 /* A pointer to the dictionary. Owned externally */
73 const struct pspp_dict *dict;
75 /* The scalar containing the dictionary */
80 /* A thin wrapper around sfm_reader */
83 struct any_read_info opts;
85 /* A pointer to the reader. The reader is owned by the struct */
86 struct casereader *reader;
88 /* A pointer to the dictionary. */
89 struct pspp_dict *dict;
94 struct hmap_node hmap_node; /* In struct pspp_dict's input_formats map. */
95 const struct variable *var;
96 struct fmt_spec input_format;
99 /* A thin wrapper around struct dictionary.*/
101 struct dictionary *dict;
102 struct hmap input_formats; /* Contains struct input_format. */
106 /* A message handler which writes messages to PSPP::errstr */
108 message_handler (const struct msg *m, void *aux)
110 SV *errstr = get_sv("PSPP::errstr", TRUE);
111 sv_setpv (errstr, m->text);
115 sysfile_close (struct syswriter_info *swi)
121 retval = casewriter_destroy (swi->writer);
129 scalar_to_value (union value *val, SV *scalar, const struct variable *var)
131 if ( var_is_numeric (var))
133 if ( SvNOK (scalar) || SvIOK (scalar) )
134 val->f = SvNV (scalar);
141 const char *p = SvPV (scalar, len);
142 int width = var_get_width (var);
143 value_set_missing (val, width);
144 memcpy (val->s, p, len);
150 value_to_scalar (const union value *val, const struct variable *var)
152 if ( var_is_numeric (var))
154 if ( var_is_value_missing (var, val, MV_SYSTEM))
155 return newSVpvn ("", 0);
157 return newSVnv (val->f);
161 int width = var_get_width (var);
162 return newSVpvn (val->s, width);
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;
179 hmap_init (&pspp_dict->input_formats);
183 static const struct fmt_spec *
184 find_input_format (const struct pspp_dict *dict, const struct variable *var)
186 struct input_format *input_format;
188 HMAP_FOR_EACH_IN_BUCKET (input_format, struct input_format, hmap_node,
189 hash_pointer (var, 0), &dict->input_formats)
190 if (input_format->var == var)
191 return &input_format->input_format;
199 MODULE = PSPP PACKAGE = PSPP
207 /* Check that the version is correct up to the length of 'ver'.
208 This allows PSPP autobuilders to add a "-build#" suffix to the
209 PSPP version without causing failures here. */
210 assert (0 == strncmp (ver, bare_version, strlen (ver)));
213 msg_set_handler (message_handler, NULL);
218 format_value (val, var)
223 const struct fmt_spec *fmt = var_get_print_format (var);
226 make_value_from_scalar (&uv, val, var);
227 s = data_out (&uv, var_get_encoding (var), fmt);
228 value_destroy (&uv, var_get_width (var));
229 ret = newSVpv (s, fmt->w);
237 value_is_missing (val, var)
243 make_value_from_scalar (&uv, val, var);
244 ret = var_is_value_missing (var, &uv, MV_ANY);
245 value_destroy (&uv, var_get_width (var));
252 MODULE = PSPP PACKAGE = PSPP::Dict
257 RETVAL = create_pspp_dict (dict_create ("UTF-8"));
264 struct pspp_dict *dict
268 struct input_format *input_format, *next_input_format;
270 HMAP_FOR_EACH_SAFE (input_format, next_input_format,
271 struct input_format, hmap_node, &dict->input_formats)
273 hmap_delete (&dict->input_formats, &input_format->hmap_node);
276 hmap_destroy (&dict->input_formats);
277 dict_unref (dict->dict);
283 struct pspp_dict *dict
285 RETVAL = dict_get_var_cnt (dict->dict);
290 set_label (dict, label)
291 struct pspp_dict *dict
294 dict_set_label (dict->dict, label);
297 set_documents (dict, docs)
298 struct pspp_dict *dict
301 dict_set_documents_string (dict->dict, docs);
305 add_document (dict, doc)
306 struct pspp_dict *dict
309 dict_add_document_line (dict->dict, doc, false);
313 clear_documents (dict)
314 struct pspp_dict *dict
316 dict_clear_documents (dict->dict);
320 set_weight (dict, var)
321 struct pspp_dict *dict
324 dict_set_weight (dict->dict, var);
328 pxs_get_variable (dict, idx)
329 struct pspp_dict *dict
332 SV *errstr = get_sv("PSPP::errstr", TRUE);
333 sv_setpv (errstr, "");
334 if ( SvIV (idx) >= dict_get_var_cnt (dict->dict))
336 sv_setpv (errstr, "The dictionary doesn't have that many variables.");
340 RETVAL = dict_get_var (dict->dict, SvIV (idx));
346 pxs_get_var_by_name (dict, name)
347 struct pspp_dict *dict
350 SV *errstr = get_sv("PSPP::errstr", TRUE);
351 sv_setpv (errstr, "");
353 struct variable *var = dict_lookup_var (dict->dict, name);
355 sv_setpv (errstr, "No such variable.");
361 MODULE = PSPP PACKAGE = PSPP::Var
365 pxs_dict_create_var (dict, name, ip_fmt)
366 struct pspp_dict * dict
370 SV *errstr = get_sv("PSPP::errstr", TRUE);
371 sv_setpv (errstr, "");
372 if ( ! id_is_plausible (name, false))
374 sv_setpv (errstr, "The variable name is not valid.");
378 struct fmt_spec op_fmt;
379 struct input_format *input_format;
382 op_fmt = fmt_for_output_from_input (&ip_fmt);
383 v = dict_create_var (dict->dict, name,
384 fmt_is_string (op_fmt.type) ? op_fmt.w : 0);
387 sv_setpv (errstr, "The variable could not be created (probably already exists).");
390 var_set_both_formats (v, &op_fmt);
392 input_format = xmalloc (sizeof *input_format);
393 input_format->var = v;
394 input_format->input_format = ip_fmt;
395 hmap_insert (&dict->input_formats, &input_format->hmap_node,
396 hash_pointer (v, 0));
404 set_missing_values (var, v1, ...)
405 struct variable *var;
412 croak ("No more than 3 missing values are permitted");
414 for (i = 0; i < items - 1; ++i)
415 make_value_from_scalar (&val[i], ST(i+1), var);
417 struct missing_values mv;
418 mv_init (&mv, var_get_width (var));
419 for (i = 0 ; i < items - 1; ++i )
421 mv_add_value (&mv, &val[i]);
422 value_destroy (&val[i], var_get_width (var));
424 var_set_missing_values (var, &mv);
428 set_label (var, label)
429 struct variable *var;
432 var_set_label (var, label);
436 clear_value_labels (var)
437 struct variable *var;
439 var_clear_value_labels (var);
442 get_write_format (var)
445 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
446 const struct fmt_spec *fmt = var_get_write_format (var);
448 hv_store (fmthash, "fmt", 3, newSVnv (fmt->type), 0);
449 hv_store (fmthash, "decimals", 8, newSVnv (fmt->d), 0);
450 hv_store (fmthash, "width", 5, newSVnv (fmt->w), 0);
452 RETVAL = newRV ((SV *) fmthash);
457 get_print_format (var)
460 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
461 const struct fmt_spec *fmt = var_get_print_format (var);
463 hv_store (fmthash, "fmt", 3, newSVnv (fmt->type), 0);
464 hv_store (fmthash, "decimals", 8, newSVnv (fmt->d), 0);
465 hv_store (fmthash, "width", 5, newSVnv (fmt->w), 0);
467 RETVAL = newRV ((SV *) fmthash);
473 pxs_set_write_format (var, fmt)
477 var_set_write_format (var, &fmt);
481 pxs_set_print_format (var, fmt)
485 var_set_print_format (var, &fmt);
488 pxs_set_output_format (var, fmt)
492 var_set_both_formats (var, &fmt);
496 add_value_label (var, key, label)
501 SV *errstr = get_sv("PSPP::errstr", TRUE);
502 sv_setpv (errstr, "");
504 union value the_value;
505 int width = var_get_width (var);
508 value_init (&the_value, width);
509 if ( var_is_numeric (var))
511 if ( ! looks_like_number (key))
513 sv_setpv (errstr, "Cannot add label with string key to a numeric variable");
514 value_destroy (&the_value, width);
517 the_value.f = SvNV (key);
521 value_copy_str_rpad (&the_value, width, SvPV_nolen(key), ' ');
523 ok = var_add_value_label (var, &the_value, label);
524 value_destroy (&the_value, width);
527 sv_setpv (errstr, "Something went wrong");
537 HV *attrhash = (HV *) sv_2mortal ((SV *) newHV());
539 struct attrset *as = var_get_attributes (var);
543 struct attrset_iterator iter;
544 struct attribute *attr;
546 for (attr = attrset_first (as, &iter);
548 attr = attrset_next (as, &iter))
551 const char *name = attribute_get_name (attr);
553 AV *values = newAV ();
555 for (i = 0 ; i < attribute_get_n_values (attr); ++i )
557 const char *value = attribute_get_value (attr, i);
558 av_push (values, newSVpv (value, 0));
561 hv_store (attrhash, name, strlen (name),
562 newRV_noinc ((SV*) values), 0);
566 RETVAL = newRV ((SV *) attrhash);
573 struct variable * var
575 RETVAL = var_get_name (var);
582 struct variable * var
584 RETVAL = var_get_label (var);
590 get_value_labels (var)
593 HV *labelhash = (HV *) sv_2mortal ((SV *) newHV());
594 const struct val_lab *vl;
595 const struct val_labs *labels = var_get_value_labels (var);
599 for (vl = val_labs_first (labels);
601 vl = val_labs_next (labels, vl))
603 SV *sv = value_to_scalar (&vl->value, var);
605 const char *s = SvPV (sv, len);
606 hv_store (labelhash, s, len, newSVpv (val_lab_get_label (vl), 0), 0);
610 RETVAL = newRV ((SV *) labelhash);
616 MODULE = PSPP PACKAGE = PSPP::Sysfile
619 struct syswriter_info *
620 pxs_create_sysfile (name, dict, opts_hr)
622 struct pspp_dict *dict;
626 struct sfm_write_options opts;
627 if (!SvROK (opts_hr))
629 opts = sfm_writer_default_options ();
633 HV *opt_h = (HV *) SvRV (opts_hr);
634 SV** readonly = hv_fetch(opt_h, "readonly", 8, 0);
635 SV** compress = hv_fetch(opt_h, "compress", 8, 0);
636 SV** version = hv_fetch(opt_h, "version", 7, 0);
638 opts.create_writeable = readonly ? ! SvIV (*readonly) : true;
639 opts.compression = (compress && SvIV (*compress)
642 opts.version = version ? SvIV (*version) : 3 ;
645 struct file_handle *fh =
646 fh_create_file (NULL, name, "UTF-8", fh_default_properties () );
647 struct syswriter_info *swi = xmalloc (sizeof (*swi));
648 swi->writer = sfm_open_writer (fh, dict->dict, opts);
651 swi->dict_sv = dict_sv;
652 SvREFCNT_inc (swi->dict_sv);
660 struct syswriter_info *swi
662 RETVAL = sysfile_close (swi);
668 struct syswriter_info *swi
671 SvREFCNT_dec (swi->dict_sv);
675 append_case (swi, ccase)
676 struct syswriter_info *swi
679 SV *errstr = get_sv("PSPP::errstr", TRUE);
680 sv_setpv (errstr, "");
681 if ( (!SvROK(ccase)))
687 AV *av_case = (AV*) SvRV (ccase);
689 const struct variable **vv;
694 if ( av_len (av_case) >= dict_get_var_cnt (swi->dict->dict))
697 c = case_create (dict_get_proto (swi->dict->dict));
699 dict_get_vars (swi->dict->dict, &vv, &nv,
700 1u << DC_ORDINARY | 1u << DC_SYSTEM);
702 for (sv = av_shift (av_case); SvOK (sv); sv = av_shift (av_case))
704 const struct variable *v = vv[i++];
705 const struct fmt_spec *ifmt = find_input_format (swi->dict, v);
707 /* If an input format has been set, then use it.
708 Otherwise just convert the raw value.
712 struct substring ss = ss_cstr (SvPV_nolen (sv));
716 error = data_in (ss, SvUTF8(sv) ? UTF8: "iso-8859-1", ifmt->type,
717 case_data_rw (c, v), var_get_width (v),
718 dict_get_encoding (swi->dict->dict));
730 scalar_to_value (case_data_rw (c, v), sv, v);
734 /* The remaining variables must be sysmis or blank string */
735 while (i < dict_get_var_cnt (swi->dict->dict))
737 const struct variable *v = vv[i++];
738 union value *val = case_data_rw (c, v);
739 value_set_missing (val, var_get_width (v));
741 casewriter_write (swi->writer, c);
751 MODULE = PSPP PACKAGE = PSPP::Reader
753 struct sysreader_info *
754 pxs_open_sysfile (name)
757 struct sysreader_info *sri = NULL;
758 struct file_handle *fh =
759 fh_create_file (NULL, name, "UTF-8", fh_default_properties () );
760 struct dictionary *dict;
762 sri = xmalloc (sizeof (*sri));
763 sri->reader = any_reader_open_and_decode (fh, NULL, &dict, &sri->opts);
765 sri->dict = create_pspp_dict (dict);
778 pxs_get_dict (reader)
779 struct sysreader_info *reader;
781 RETVAL = reader->dict;
787 struct sysreader_info *sfr;
790 casenumber n = casereader_get_case_cnt (sfr->reader);
791 if (n == CASENUMBER_MAX)
803 struct sysreader_info *sfr;
807 if ((c = casereader_read (sfr->reader)) != NULL)
811 EXTEND (SP, dict_get_var_cnt (sfr->dict->dict));
812 for (v = 0; v < dict_get_var_cnt (sfr->dict->dict); ++v )
814 const struct variable *var = dict_get_var (sfr->dict->dict, v);
815 const union value *val = case_data (c, var);
817 PUSHs (sv_2mortal (value_to_scalar (val, var)));