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 modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU 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, see <http://www.gnu.org/licenses/>. */
20 /* The Gnulib "strftime" module defines my_strftime in <config.h> for use by
21 gl/strftime.c. Perl also defines my_strftime in embed.h for some other
22 purpose. The former definition doesn't matter in this file, so suppress it
23 to avoid a compiler warning. */
33 #include <libpspp/hmap.h>
34 #include <libpspp/hash-functions.h>
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 (val->s, 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 (val->s, 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_unref (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 make_value_from_scalar (&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 )
418 mv_add_value (&mv, &val[i]);
419 value_destroy (&val[i], var_get_width (var));
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 const struct val_labs *labels = var_get_value_labels (var);
596 for (vl = val_labs_first (labels);
598 vl = val_labs_next (labels, vl))
600 SV *sv = value_to_scalar (&vl->value, var);
602 const char *s = SvPV (sv, len);
603 hv_store (labelhash, s, len, newSVpv (val_lab_get_label (vl), 0), 0);
607 RETVAL = newRV ((SV *) labelhash);
613 MODULE = PSPP PACKAGE = PSPP::Sysfile
616 struct syswriter_info *
617 pxs_create_sysfile (name, dict, opts_hr)
619 struct pspp_dict *dict;
623 struct sfm_write_options opts;
624 if (!SvROK (opts_hr))
626 opts = sfm_writer_default_options ();
630 HV *opt_h = (HV *) SvRV (opts_hr);
631 SV** readonly = hv_fetch(opt_h, "readonly", 8, 0);
632 SV** compress = hv_fetch(opt_h, "compress", 8, 0);
633 SV** version = hv_fetch(opt_h, "version", 7, 0);
635 opts.create_writeable = readonly ? ! SvIV (*readonly) : true;
636 opts.compression = (compress && SvIV (*compress)
639 opts.version = version ? SvIV (*version) : 3 ;
642 struct file_handle *fh =
643 fh_create_file (NULL, name, "UTF-8", fh_default_properties () );
644 struct syswriter_info *swi = xmalloc (sizeof (*swi));
645 swi->writer = sfm_open_writer (fh, dict->dict, opts);
648 swi->dict_sv = dict_sv;
649 SvREFCNT_inc (swi->dict_sv);
657 struct syswriter_info *swi
659 RETVAL = sysfile_close (swi);
665 struct syswriter_info *swi
668 SvREFCNT_dec (swi->dict_sv);
672 append_case (swi, ccase)
673 struct syswriter_info *swi
676 SV *errstr = get_sv("PSPP::errstr", TRUE);
677 sv_setpv (errstr, "");
678 if ( (!SvROK(ccase)))
684 AV *av_case = (AV*) SvRV (ccase);
686 const struct variable **vv;
691 if ( av_len (av_case) >= dict_get_var_cnt (swi->dict->dict))
694 c = case_create (dict_get_proto (swi->dict->dict));
696 dict_get_vars (swi->dict->dict, &vv, &nv,
697 1u << DC_ORDINARY | 1u << DC_SYSTEM);
699 for (sv = av_shift (av_case); SvOK (sv); sv = av_shift (av_case))
701 const struct variable *v = vv[i++];
702 const struct fmt_spec *ifmt = find_input_format (swi->dict, v);
704 /* If an input format has been set, then use it.
705 Otherwise just convert the raw value.
709 struct substring ss = ss_cstr (SvPV_nolen (sv));
713 error = data_in (ss, SvUTF8(sv) ? UTF8: "iso-8859-1", ifmt->type,
714 case_data_rw (c, v), var_get_width (v),
715 dict_get_encoding (swi->dict->dict));
727 scalar_to_value (case_data_rw (c, v), sv, v);
731 /* The remaining variables must be sysmis or blank string */
732 while (i < dict_get_var_cnt (swi->dict->dict))
734 const struct variable *v = vv[i++];
735 union value *val = case_data_rw (c, v);
736 value_set_missing (val, var_get_width (v));
738 casewriter_write (swi->writer, c);
748 MODULE = PSPP PACKAGE = PSPP::Reader
750 struct sysreader_info *
751 pxs_open_sysfile (name)
754 struct sysreader_info *sri = NULL;
755 struct file_handle *fh =
756 fh_create_file (NULL, name, "UTF-8", fh_default_properties () );
757 struct dictionary *dict;
759 sri = xmalloc (sizeof (*sri));
760 sri->reader = any_reader_open_and_decode (fh, NULL, &dict, &sri->opts);
762 sri->dict = create_pspp_dict (dict);
775 pxs_get_dict (reader)
776 struct sysreader_info *reader;
778 RETVAL = reader->dict;
784 struct sysreader_info *sfr;
787 casenumber n = casereader_get_case_cnt (sfr->reader);
788 if (n == CASENUMBER_MAX)
800 struct sysreader_info *sfr;
804 if ((c = casereader_read (sfr->reader)) != NULL)
808 EXTEND (SP, dict_get_var_cnt (sfr->dict->dict));
809 for (v = 0; v < dict_get_var_cnt (sfr->dict->dict); ++v )
811 const struct variable *var = dict_get_var (sfr->dict->dict, v);
812 const union value *val = case_data (c, var);
814 PUSHs (sv_2mortal (value_to_scalar (val, var)));