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 (value_str_rw (val, width), 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 (value_str (val, width), 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_destroy (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 scalar_to_value (&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 )
420 mv_add_value (&mv, &val[i]);
421 var_set_missing_values (var, &mv);
425 set_label (var, label)
426 struct variable *var;
429 var_set_label (var, label);
433 clear_value_labels (var)
434 struct variable *var;
436 var_clear_value_labels (var);
439 get_write_format (var)
442 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
443 const struct fmt_spec *fmt = var_get_write_format (var);
445 hv_store (fmthash, "fmt", 3, newSVnv (fmt->type), 0);
446 hv_store (fmthash, "decimals", 8, newSVnv (fmt->d), 0);
447 hv_store (fmthash, "width", 5, newSVnv (fmt->w), 0);
449 RETVAL = newRV ((SV *) fmthash);
454 get_print_format (var)
457 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
458 const struct fmt_spec *fmt = var_get_print_format (var);
460 hv_store (fmthash, "fmt", 3, newSVnv (fmt->type), 0);
461 hv_store (fmthash, "decimals", 8, newSVnv (fmt->d), 0);
462 hv_store (fmthash, "width", 5, newSVnv (fmt->w), 0);
464 RETVAL = newRV ((SV *) fmthash);
470 pxs_set_write_format (var, fmt)
474 var_set_write_format (var, &fmt);
478 pxs_set_print_format (var, fmt)
482 var_set_print_format (var, &fmt);
485 pxs_set_output_format (var, fmt)
489 var_set_both_formats (var, &fmt);
493 add_value_label (var, key, label)
498 SV *errstr = get_sv("PSPP::errstr", TRUE);
499 sv_setpv (errstr, "");
501 union value the_value;
502 int width = var_get_width (var);
505 value_init (&the_value, width);
506 if ( var_is_numeric (var))
508 if ( ! looks_like_number (key))
510 sv_setpv (errstr, "Cannot add label with string key to a numeric variable");
511 value_destroy (&the_value, width);
514 the_value.f = SvNV (key);
518 value_copy_str_rpad (&the_value, width, SvPV_nolen(key), ' ');
520 ok = var_add_value_label (var, &the_value, label);
521 value_destroy (&the_value, width);
524 sv_setpv (errstr, "Something went wrong");
534 HV *attrhash = (HV *) sv_2mortal ((SV *) newHV());
536 struct attrset *as = var_get_attributes (var);
540 struct attrset_iterator iter;
541 struct attribute *attr;
543 for (attr = attrset_first (as, &iter);
545 attr = attrset_next (as, &iter))
548 const char *name = attribute_get_name (attr);
550 AV *values = newAV ();
552 for (i = 0 ; i < attribute_get_n_values (attr); ++i )
554 const char *value = attribute_get_value (attr, i);
555 av_push (values, newSVpv (value, 0));
558 hv_store (attrhash, name, strlen (name),
559 newRV_noinc ((SV*) values), 0);
563 RETVAL = newRV ((SV *) attrhash);
570 struct variable * var
572 RETVAL = var_get_name (var);
579 struct variable * var
581 RETVAL = var_get_label (var);
587 get_value_labels (var)
590 HV *labelhash = (HV *) sv_2mortal ((SV *) newHV());
591 const struct val_lab *vl;
592 struct val_labs_iterator *viter = NULL;
593 const struct val_labs *labels = var_get_value_labels (var);
597 for (vl = val_labs_first (labels);
599 vl = val_labs_next (labels, vl))
601 SV *sv = value_to_scalar (&vl->value, var);
603 const char *s = SvPV (sv, len);
604 hv_store (labelhash, s, len, newSVpv (val_lab_get_label (vl), 0), 0);
608 RETVAL = newRV ((SV *) labelhash);
614 MODULE = PSPP PACKAGE = PSPP::Sysfile
617 struct syswriter_info *
618 pxs_create_sysfile (name, dict, opts_hr)
620 struct pspp_dict *dict;
624 struct sfm_write_options opts;
625 if (!SvROK (opts_hr))
627 opts = sfm_writer_default_options ();
631 HV *opt_h = (HV *) SvRV (opts_hr);
632 SV** readonly = hv_fetch(opt_h, "readonly", 8, 0);
633 SV** compress = hv_fetch(opt_h, "compress", 8, 0);
634 SV** version = hv_fetch(opt_h, "version", 7, 0);
636 opts.create_writeable = readonly ? ! SvIV (*readonly) : true;
637 opts.compression = (compress && SvIV (*compress)
640 opts.version = version ? SvIV (*version) : 3 ;
643 struct file_handle *fh =
644 fh_create_file (NULL, name, "UTF-8", fh_default_properties () );
645 struct syswriter_info *swi = xmalloc (sizeof (*swi));
646 swi->writer = sfm_open_writer (fh, dict->dict, opts);
649 swi->dict_sv = dict_sv;
650 SvREFCNT_inc (swi->dict_sv);
658 struct syswriter_info *swi
660 RETVAL = sysfile_close (swi);
666 struct syswriter_info *swi
669 SvREFCNT_dec (swi->dict_sv);
673 append_case (swi, ccase)
674 struct syswriter_info *swi
677 SV *errstr = get_sv("PSPP::errstr", TRUE);
678 sv_setpv (errstr, "");
679 if ( (!SvROK(ccase)))
685 AV *av_case = (AV*) SvRV (ccase);
687 const struct variable **vv;
692 if ( av_len (av_case) >= dict_get_var_cnt (swi->dict->dict))
695 c = case_create (dict_get_proto (swi->dict->dict));
697 dict_get_vars (swi->dict->dict, &vv, &nv,
698 1u << DC_ORDINARY | 1u << DC_SYSTEM);
700 for (sv = av_shift (av_case); SvOK (sv); sv = av_shift (av_case))
702 const struct variable *v = vv[i++];
703 const struct fmt_spec *ifmt = find_input_format (swi->dict, v);
705 /* If an input format has been set, then use it.
706 Otherwise just convert the raw value.
710 struct substring ss = ss_cstr (SvPV_nolen (sv));
714 error = data_in (ss, SvUTF8(sv) ? UTF8: "iso-8859-1", ifmt->type,
715 case_data_rw (c, v), var_get_width (v),
716 dict_get_encoding (swi->dict->dict));
728 scalar_to_value (case_data_rw (c, v), sv, v);
732 /* The remaining variables must be sysmis or blank string */
733 while (i < dict_get_var_cnt (swi->dict->dict))
735 const struct variable *v = vv[i++];
736 union value *val = case_data_rw (c, v);
737 value_set_missing (val, var_get_width (v));
739 casewriter_write (swi->writer, c);
749 MODULE = PSPP PACKAGE = PSPP::Reader
751 struct sysreader_info *
752 pxs_open_sysfile (name)
755 struct casereader *reader;
756 struct sysreader_info *sri = NULL;
757 struct file_handle *fh =
758 fh_create_file (NULL, name, "UTF-8", fh_default_properties () );
759 struct dictionary *dict;
761 sri = xmalloc (sizeof (*sri));
762 sri->reader = any_reader_open_and_decode (fh, NULL, &dict, &sri->opts);
764 sri->dict = create_pspp_dict (dict);
777 pxs_get_dict (reader)
778 struct sysreader_info *reader;
780 RETVAL = reader->dict;
786 struct sysreader_info *sfr;
789 casenumber n = casereader_get_case_cnt (sfr->reader);
790 if (n == CASENUMBER_MAX)
802 struct sysreader_info *sfr;
806 if ((c = casereader_read (sfr->reader)) != NULL)
810 EXTEND (SP, dict_get_var_cnt (sfr->dict->dict));
811 for (v = 0; v < dict_get_var_cnt (sfr->dict->dict); ++v )
813 const struct variable *var = dict_get_var (sfr->dict->dict, v);
814 const union value *val = case_data (c, var);
816 PUSHs (sv_2mortal (value_to_scalar (val, var)));