1 /* PSPP - computes sample statistics.
2 Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 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/sys-file-writer.h>
48 #include <data/sys-file-reader.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 dictionary *dict;
72 /* The scalar containing the dictionary */
77 /* A thin wrapper around sfm_reader */
80 struct sfm_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 dictionary *dict;
91 /* A message handler which writes messages to PSPP::errstr */
93 message_handler (const struct msg *m, void *aux)
95 SV *errstr = get_sv("PSPP::errstr", TRUE);
96 sv_setpv (errstr, m->text);
100 sysfile_close (struct sysfile_info *sfi)
106 retval = casewriter_destroy (sfi->writer);
114 scalar_to_value (union value *val, SV *scalar, const struct variable *var)
116 if ( var_is_numeric (var))
118 if ( SvNOK (scalar) || SvIOK (scalar) )
119 val->f = SvNV (scalar);
126 const char *p = SvPV (scalar, len);
127 int width = var_get_width (var);
128 value_set_missing (val, width);
129 memcpy (value_str_rw (val, width), p, len);
135 value_to_scalar (const union value *val, const struct variable *var)
137 if ( var_is_numeric (var))
139 if ( var_is_value_missing (var, val, MV_SYSTEM))
140 return newSVpvn ("", 0);
142 return newSVnv (val->f);
146 int width = var_get_width (var);
147 return newSVpvn (value_str (val, width), width);
153 var_set_input_format (struct variable *v, input_format ip_fmt)
155 struct fmt_spec *if_copy = malloc (sizeof (*if_copy));
156 memcpy (if_copy, &ip_fmt, sizeof (ip_fmt));
157 var_attach_aux (v, if_copy, var_dtor_free);
161 make_value_from_scalar (union value *uv, SV *val, const struct variable *var)
163 value_init (uv, var_get_width (var));
164 scalar_to_value (uv, val, var);
170 MODULE = PSPP PACKAGE = PSPP
178 /* Check that the version is correct up to the length of 'ver'.
179 This allows PSPP autobuilders to add a "-build#" suffix to the
180 PSPP version without causing failures here. */
181 assert (0 == strncmp (ver, bare_version, strlen (ver)));
184 msg_set_handler (message_handler, NULL);
185 settings_init (0, 0);
189 format_value (val, var)
194 const struct fmt_spec *fmt = var_get_print_format (var);
197 make_value_from_scalar (&uv, val, var);
198 s = data_out (&uv, var_get_encoding (var), fmt);
199 value_destroy (&uv, var_get_width (var));
200 ret = newSVpv (s, fmt->w);
208 value_is_missing (val, var)
214 make_value_from_scalar (&uv, val, var);
215 ret = var_is_value_missing (var, &uv, MV_ANY);
216 value_destroy (&uv, var_get_width (var));
223 MODULE = PSPP PACKAGE = PSPP::Dict
228 RETVAL = dict_create ("UTF-8");
235 struct dictionary *dict
242 struct dictionary *dict
244 RETVAL = dict_get_var_cnt (dict);
249 set_label (dict, label)
250 struct dictionary *dict
253 dict_set_label (dict, label);
256 set_documents (dict, docs)
257 struct dictionary *dict
260 dict_set_documents_string (dict, docs);
264 add_document (dict, doc)
265 struct dictionary *dict
268 dict_add_document_line (dict, doc, false);
272 clear_documents (dict)
273 struct dictionary *dict
275 dict_clear_documents (dict);
279 set_weight (dict, var)
280 struct dictionary *dict
283 dict_set_weight (dict, var);
287 pxs_get_variable (dict, idx)
288 struct dictionary *dict
291 SV *errstr = get_sv("PSPP::errstr", TRUE);
292 sv_setpv (errstr, "");
293 if ( SvIV (idx) >= dict_get_var_cnt (dict))
295 sv_setpv (errstr, "The dictionary doesn't have that many variables.");
299 RETVAL = dict_get_var (dict, SvIV (idx));
305 pxs_get_var_by_name (dict, name)
306 struct dictionary *dict
309 SV *errstr = get_sv("PSPP::errstr", TRUE);
310 sv_setpv (errstr, "");
312 struct variable *var = dict_lookup_var (dict, name);
314 sv_setpv (errstr, "No such variable.");
320 MODULE = PSPP PACKAGE = PSPP::Var
324 pxs_dict_create_var (dict, name, ip_fmt)
325 struct dictionary * dict
329 SV *errstr = get_sv("PSPP::errstr", TRUE);
330 sv_setpv (errstr, "");
331 if ( ! id_is_plausible (name, false))
333 sv_setpv (errstr, "The variable name is not valid.");
337 struct fmt_spec op_fmt;
340 op_fmt = fmt_for_output_from_input (&ip_fmt);
341 v = dict_create_var (dict, name,
342 fmt_is_string (op_fmt.type) ? op_fmt.w : 0);
345 sv_setpv (errstr, "The variable could not be created (probably already exists).");
348 var_set_both_formats (v, &op_fmt);
349 var_set_input_format (v, ip_fmt);
356 set_missing_values (var, v1, ...)
357 struct variable *var;
364 croak ("No more than 3 missing values are permitted");
366 for (i = 0; i < items - 1; ++i)
367 scalar_to_value (&val[i], ST(i+1), var);
369 struct missing_values mv;
370 mv_init (&mv, var_get_width (var));
371 for (i = 0 ; i < items - 1; ++i )
372 mv_add_value (&mv, &val[i]);
373 var_set_missing_values (var, &mv);
377 set_label (var, label)
378 struct variable *var;
381 var_set_label (var, label, false);
385 clear_value_labels (var)
386 struct variable *var;
388 var_clear_value_labels (var);
391 get_write_format (var)
394 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
395 const struct fmt_spec *fmt = var_get_write_format (var);
397 hv_store (fmthash, "fmt", 3, newSVnv (fmt->type), 0);
398 hv_store (fmthash, "decimals", 8, newSVnv (fmt->d), 0);
399 hv_store (fmthash, "width", 5, newSVnv (fmt->w), 0);
401 RETVAL = newRV ((SV *) fmthash);
406 get_print_format (var)
409 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
410 const struct fmt_spec *fmt = var_get_print_format (var);
412 hv_store (fmthash, "fmt", 3, newSVnv (fmt->type), 0);
413 hv_store (fmthash, "decimals", 8, newSVnv (fmt->d), 0);
414 hv_store (fmthash, "width", 5, newSVnv (fmt->w), 0);
416 RETVAL = newRV ((SV *) fmthash);
422 pxs_set_write_format (var, fmt)
426 var_set_write_format (var, &fmt);
430 pxs_set_print_format (var, fmt)
434 var_set_print_format (var, &fmt);
437 pxs_set_output_format (var, fmt)
441 var_set_both_formats (var, &fmt);
445 add_value_label (var, key, label)
450 SV *errstr = get_sv("PSPP::errstr", TRUE);
451 sv_setpv (errstr, "");
453 union value the_value;
454 int width = var_get_width (var);
457 value_init (&the_value, width);
458 if ( var_is_numeric (var))
460 if ( ! looks_like_number (key))
462 sv_setpv (errstr, "Cannot add label with string key to a numeric variable");
463 value_destroy (&the_value, width);
466 the_value.f = SvNV (key);
470 value_copy_str_rpad (&the_value, width, SvPV_nolen(key), ' ');
472 ok = var_add_value_label (var, &the_value, label);
473 value_destroy (&the_value, width);
476 sv_setpv (errstr, "Something went wrong");
486 HV *attrhash = (HV *) sv_2mortal ((SV *) newHV());
488 struct attrset *as = var_get_attributes (var);
492 struct attrset_iterator iter;
493 struct attribute *attr;
495 for (attr = attrset_first (as, &iter);
497 attr = attrset_next (as, &iter))
500 const char *name = attribute_get_name (attr);
502 AV *values = newAV ();
504 for (i = 0 ; i < attribute_get_n_values (attr); ++i )
506 const char *value = attribute_get_value (attr, i);
507 av_push (values, newSVpv (value, 0));
510 hv_store (attrhash, name, strlen (name),
511 newRV_noinc ((SV*) values), 0);
515 RETVAL = newRV ((SV *) attrhash);
522 struct variable * var
524 RETVAL = var_get_name (var);
531 struct variable * var
533 RETVAL = var_get_label (var);
539 get_value_labels (var)
542 HV *labelhash = (HV *) sv_2mortal ((SV *) newHV());
543 const struct val_lab *vl;
544 struct val_labs_iterator *viter = NULL;
545 const struct val_labs *labels = var_get_value_labels (var);
549 for (vl = val_labs_first (labels);
551 vl = val_labs_next (labels, vl))
553 SV *sv = value_to_scalar (&vl->value, var);
555 const char *s = SvPV (sv, len);
556 hv_store (labelhash, s, len, newSVpv (val_lab_get_label (vl), 0), 0);
560 RETVAL = newRV ((SV *) labelhash);
566 MODULE = PSPP PACKAGE = PSPP::Sysfile
569 struct sysfile_info *
570 pxs_create_sysfile (name, dict_ref, opts_hr)
575 SV *dict_sv = SvRV (dict_ref);
576 struct dictionary *dict = (void *) SvIV (dict_sv);
577 struct sfm_write_options opts;
578 if (!SvROK (opts_hr))
580 opts = sfm_writer_default_options ();
584 HV *opt_h = (HV *) SvRV (opts_hr);
585 SV** readonly = hv_fetch(opt_h, "readonly", 8, 0);
586 SV** compress = hv_fetch(opt_h, "compress", 8, 0);
587 SV** version = hv_fetch(opt_h, "version", 7, 0);
589 opts.create_writeable = readonly ? ! SvIV (*readonly) : true;
590 opts.compress = compress ? SvIV (*compress) : false;
591 opts.version = version ? SvIV (*version) : 3 ;
594 struct file_handle *fh =
595 fh_create_file (NULL, name, fh_default_properties () );
596 struct sysfile_info *sfi = xmalloc (sizeof (*sfi));
597 sfi->writer = sfm_open_writer (fh, dict, opts);
600 sfi->dict_sv = dict_sv;
601 SvREFCNT_inc (sfi->dict_sv);
609 struct sysfile_info *sfi
611 RETVAL = sysfile_close (sfi);
617 struct sysfile_info *sfi
620 SvREFCNT_dec (sfi->dict_sv);
624 append_case (sfi, ccase)
625 struct sysfile_info *sfi
628 SV *errstr = get_sv("PSPP::errstr", TRUE);
629 sv_setpv (errstr, "");
630 if ( (!SvROK(ccase)))
636 AV *av_case = (AV*) SvRV (ccase);
638 const struct variable **vv;
643 if ( av_len (av_case) >= dict_get_var_cnt (sfi->dict))
646 c = case_create (dict_get_proto (sfi->dict));
648 dict_get_vars (sfi->dict, &vv, &nv, 1u << DC_ORDINARY | 1u << DC_SYSTEM);
650 for (sv = av_shift (av_case); SvOK (sv); sv = av_shift (av_case))
652 const struct variable *v = vv[i++];
653 const struct fmt_spec *ifmt = var_get_aux (v);
655 /* If an input format has been set, then use it.
656 Otherwise just convert the raw value.
660 struct substring ss = ss_cstr (SvPV_nolen (sv));
664 error = data_in (ss, SvUTF8(sv) ? UTF8: "iso-8859-1", ifmt->type,
665 case_data_rw (c, v), var_get_width (v),
666 dict_get_encoding (sfi->dict));
678 scalar_to_value (case_data_rw (c, v), sv, v);
682 /* The remaining variables must be sysmis or blank string */
683 while (i < dict_get_var_cnt (sfi->dict))
685 const struct variable *v = vv[i++];
686 union value *val = case_data_rw (c, v);
687 value_set_missing (val, var_get_width (v));
689 casewriter_write (sfi->writer, c);
699 MODULE = PSPP PACKAGE = PSPP::Reader
701 struct sysreader_info *
702 pxs_open_sysfile (name)
705 struct casereader *reader;
706 struct sysreader_info *sri = NULL;
707 struct file_handle *fh =
708 fh_create_file (NULL, name, fh_default_properties () );
710 sri = xmalloc (sizeof (*sri));
711 sri->reader = sfm_open_reader (fh, &sri->dict, &sri->opts);
713 if ( NULL == sri->reader)
725 pxs_get_dict (reader)
726 struct sysreader_info *reader;
728 RETVAL = reader->dict;
734 struct sysreader_info *sfr;
737 casenumber n = casereader_get_case_cnt (sfr->reader);
738 if (n == CASENUMBER_MAX)
750 struct sysreader_info *sfr;
754 if ((c = casereader_read (sfr->reader)) != NULL)
758 EXTEND (SP, dict_get_var_cnt (sfr->dict));
759 for (v = 0; v < dict_get_var_cnt (sfr->dict); ++v )
761 const struct variable *var = dict_get_var (sfr->dict, v);
762 const union value *val = case_data (c, var);
764 PUSHs (sv_2mortal (value_to_scalar (val, var)));