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/settings.h>
48 #include <data/sys-file-writer.h>
49 #include <data/sys-file-reader.h>
50 #include <data/value.h>
51 #include <data/vardict.h>
52 #include <data/value-labels.h>
53 #include <data/format.h>
54 #include <data/data-in.h>
55 #include <data/data-out.h>
58 typedef struct fmt_spec input_format ;
59 typedef struct fmt_spec output_format ;
62 /* A thin wrapper around sfm_writer */
67 /* A pointer to the writer. The writer is owned by the struct */
68 struct casewriter *writer;
70 /* A pointer to the dictionary. Owned externally */
71 const struct dictionary *dict;
73 /* The scalar containing the dictionary */
78 /* A thin wrapper around sfm_reader */
81 struct sfm_read_info opts;
83 /* A pointer to the reader. The reader is owned by the struct */
84 struct casereader *reader;
86 /* A pointer to the dictionary. */
87 struct dictionary *dict;
92 /* A message handler which writes messages to PSPP::errstr */
94 message_handler (const struct msg *m, void *aux)
96 SV *errstr = get_sv("PSPP::errstr", TRUE);
97 sv_setpv (errstr, m->text);
101 sysfile_close (struct sysfile_info *sfi)
107 retval = casewriter_destroy (sfi->writer);
115 scalar_to_value (union value *val, SV *scalar, const struct variable *var)
117 if ( var_is_numeric (var))
119 if ( SvNOK (scalar) || SvIOK (scalar) )
120 val->f = SvNV (scalar);
127 const char *p = SvPV (scalar, len);
128 int width = var_get_width (var);
129 value_set_missing (val, width);
130 memcpy (value_str_rw (val, width), p, len);
136 value_to_scalar (const union value *val, const struct variable *var)
138 if ( var_is_numeric (var))
140 if ( var_is_value_missing (var, val, MV_SYSTEM))
141 return newSVpvn ("", 0);
143 return newSVnv (val->f);
147 int width = var_get_width (var);
148 return newSVpvn (value_str (val, width), width);
154 var_set_input_format (struct variable *v, input_format ip_fmt)
156 struct fmt_spec *if_copy = malloc (sizeof (*if_copy));
157 memcpy (if_copy, &ip_fmt, sizeof (ip_fmt));
158 var_attach_aux (v, if_copy, var_dtor_free);
162 make_value_from_scalar (union value *uv, SV *val, const struct variable *var)
164 value_init (uv, var_get_width (var));
165 scalar_to_value (uv, val, var);
171 MODULE = PSPP PACKAGE = PSPP
179 /* Check that the version is correct up to the length of 'ver'.
180 This allows PSPP autobuilders to add a "-build#" suffix to the
181 PSPP version without causing failures here. */
182 assert (0 == strncmp (ver, bare_version, strlen (ver)));
185 msg_set_handler (message_handler, NULL);
190 format_value (val, var)
195 const struct fmt_spec *fmt = var_get_print_format (var);
198 make_value_from_scalar (&uv, val, var);
199 s = data_out (&uv, var_get_encoding (var), fmt);
200 value_destroy (&uv, var_get_width (var));
201 ret = newSVpv (s, fmt->w);
209 value_is_missing (val, var)
215 make_value_from_scalar (&uv, val, var);
216 ret = var_is_value_missing (var, &uv, MV_ANY);
217 value_destroy (&uv, var_get_width (var));
224 MODULE = PSPP PACKAGE = PSPP::Dict
229 RETVAL = dict_create ("UTF-8");
236 struct dictionary *dict
243 struct dictionary *dict
245 RETVAL = dict_get_var_cnt (dict);
250 set_label (dict, label)
251 struct dictionary *dict
254 dict_set_label (dict, label);
257 set_documents (dict, docs)
258 struct dictionary *dict
261 dict_set_documents_string (dict, docs);
265 add_document (dict, doc)
266 struct dictionary *dict
269 dict_add_document_line (dict, doc, false);
273 clear_documents (dict)
274 struct dictionary *dict
276 dict_clear_documents (dict);
280 set_weight (dict, var)
281 struct dictionary *dict
284 dict_set_weight (dict, var);
288 pxs_get_variable (dict, idx)
289 struct dictionary *dict
292 SV *errstr = get_sv("PSPP::errstr", TRUE);
293 sv_setpv (errstr, "");
294 if ( SvIV (idx) >= dict_get_var_cnt (dict))
296 sv_setpv (errstr, "The dictionary doesn't have that many variables.");
300 RETVAL = dict_get_var (dict, SvIV (idx));
306 pxs_get_var_by_name (dict, name)
307 struct dictionary *dict
310 SV *errstr = get_sv("PSPP::errstr", TRUE);
311 sv_setpv (errstr, "");
313 struct variable *var = dict_lookup_var (dict, name);
315 sv_setpv (errstr, "No such variable.");
321 MODULE = PSPP PACKAGE = PSPP::Var
325 pxs_dict_create_var (dict, name, ip_fmt)
326 struct dictionary * dict
330 SV *errstr = get_sv("PSPP::errstr", TRUE);
331 sv_setpv (errstr, "");
332 if ( ! id_is_plausible (name, false))
334 sv_setpv (errstr, "The variable name is not valid.");
338 struct fmt_spec op_fmt;
341 op_fmt = fmt_for_output_from_input (&ip_fmt);
342 v = dict_create_var (dict, name,
343 fmt_is_string (op_fmt.type) ? op_fmt.w : 0);
346 sv_setpv (errstr, "The variable could not be created (probably already exists).");
349 var_set_both_formats (v, &op_fmt);
350 var_set_input_format (v, ip_fmt);
357 set_missing_values (var, v1, ...)
358 struct variable *var;
365 croak ("No more than 3 missing values are permitted");
367 for (i = 0; i < items - 1; ++i)
368 scalar_to_value (&val[i], ST(i+1), var);
370 struct missing_values mv;
371 mv_init (&mv, var_get_width (var));
372 for (i = 0 ; i < items - 1; ++i )
373 mv_add_value (&mv, &val[i]);
374 var_set_missing_values (var, &mv);
378 set_label (var, label)
379 struct variable *var;
382 var_set_label (var, label, false);
386 clear_value_labels (var)
387 struct variable *var;
389 var_clear_value_labels (var);
392 get_write_format (var)
395 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
396 const struct fmt_spec *fmt = var_get_write_format (var);
398 hv_store (fmthash, "fmt", 3, newSVnv (fmt->type), 0);
399 hv_store (fmthash, "decimals", 8, newSVnv (fmt->d), 0);
400 hv_store (fmthash, "width", 5, newSVnv (fmt->w), 0);
402 RETVAL = newRV ((SV *) fmthash);
407 get_print_format (var)
410 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
411 const struct fmt_spec *fmt = var_get_print_format (var);
413 hv_store (fmthash, "fmt", 3, newSVnv (fmt->type), 0);
414 hv_store (fmthash, "decimals", 8, newSVnv (fmt->d), 0);
415 hv_store (fmthash, "width", 5, newSVnv (fmt->w), 0);
417 RETVAL = newRV ((SV *) fmthash);
423 pxs_set_write_format (var, fmt)
427 var_set_write_format (var, &fmt);
431 pxs_set_print_format (var, fmt)
435 var_set_print_format (var, &fmt);
438 pxs_set_output_format (var, fmt)
442 var_set_both_formats (var, &fmt);
446 add_value_label (var, key, label)
451 SV *errstr = get_sv("PSPP::errstr", TRUE);
452 sv_setpv (errstr, "");
454 union value the_value;
455 int width = var_get_width (var);
458 value_init (&the_value, width);
459 if ( var_is_numeric (var))
461 if ( ! looks_like_number (key))
463 sv_setpv (errstr, "Cannot add label with string key to a numeric variable");
464 value_destroy (&the_value, width);
467 the_value.f = SvNV (key);
471 value_copy_str_rpad (&the_value, width, SvPV_nolen(key), ' ');
473 ok = var_add_value_label (var, &the_value, label);
474 value_destroy (&the_value, width);
477 sv_setpv (errstr, "Something went wrong");
487 HV *attrhash = (HV *) sv_2mortal ((SV *) newHV());
489 struct attrset *as = var_get_attributes (var);
493 struct attrset_iterator iter;
494 struct attribute *attr;
496 for (attr = attrset_first (as, &iter);
498 attr = attrset_next (as, &iter))
501 const char *name = attribute_get_name (attr);
503 AV *values = newAV ();
505 for (i = 0 ; i < attribute_get_n_values (attr); ++i )
507 const char *value = attribute_get_value (attr, i);
508 av_push (values, newSVpv (value, 0));
511 hv_store (attrhash, name, strlen (name),
512 newRV_noinc ((SV*) values), 0);
516 RETVAL = newRV ((SV *) attrhash);
523 struct variable * var
525 RETVAL = var_get_name (var);
532 struct variable * var
534 RETVAL = var_get_label (var);
540 get_value_labels (var)
543 HV *labelhash = (HV *) sv_2mortal ((SV *) newHV());
544 const struct val_lab *vl;
545 struct val_labs_iterator *viter = NULL;
546 const struct val_labs *labels = var_get_value_labels (var);
550 for (vl = val_labs_first (labels);
552 vl = val_labs_next (labels, vl))
554 SV *sv = value_to_scalar (&vl->value, var);
556 const char *s = SvPV (sv, len);
557 hv_store (labelhash, s, len, newSVpv (val_lab_get_label (vl), 0), 0);
561 RETVAL = newRV ((SV *) labelhash);
567 MODULE = PSPP PACKAGE = PSPP::Sysfile
570 struct sysfile_info *
571 pxs_create_sysfile (name, dict_ref, opts_hr)
576 SV *dict_sv = SvRV (dict_ref);
577 struct dictionary *dict = (void *) SvIV (dict_sv);
578 struct sfm_write_options opts;
579 if (!SvROK (opts_hr))
581 opts = sfm_writer_default_options ();
585 HV *opt_h = (HV *) SvRV (opts_hr);
586 SV** readonly = hv_fetch(opt_h, "readonly", 8, 0);
587 SV** compress = hv_fetch(opt_h, "compress", 8, 0);
588 SV** version = hv_fetch(opt_h, "version", 7, 0);
590 opts.create_writeable = readonly ? ! SvIV (*readonly) : true;
591 opts.compress = compress ? SvIV (*compress) : false;
592 opts.version = version ? SvIV (*version) : 3 ;
595 struct file_handle *fh =
596 fh_create_file (NULL, name, fh_default_properties () );
597 struct sysfile_info *sfi = xmalloc (sizeof (*sfi));
598 sfi->writer = sfm_open_writer (fh, dict, opts);
601 sfi->dict_sv = dict_sv;
602 SvREFCNT_inc (sfi->dict_sv);
610 struct sysfile_info *sfi
612 RETVAL = sysfile_close (sfi);
618 struct sysfile_info *sfi
621 SvREFCNT_dec (sfi->dict_sv);
625 append_case (sfi, ccase)
626 struct sysfile_info *sfi
629 SV *errstr = get_sv("PSPP::errstr", TRUE);
630 sv_setpv (errstr, "");
631 if ( (!SvROK(ccase)))
637 AV *av_case = (AV*) SvRV (ccase);
639 const struct variable **vv;
644 if ( av_len (av_case) >= dict_get_var_cnt (sfi->dict))
647 c = case_create (dict_get_proto (sfi->dict));
649 dict_get_vars (sfi->dict, &vv, &nv, 1u << DC_ORDINARY | 1u << DC_SYSTEM);
651 for (sv = av_shift (av_case); SvOK (sv); sv = av_shift (av_case))
653 const struct variable *v = vv[i++];
654 const struct fmt_spec *ifmt = var_get_aux (v);
656 /* If an input format has been set, then use it.
657 Otherwise just convert the raw value.
661 struct substring ss = ss_cstr (SvPV_nolen (sv));
665 error = data_in (ss, SvUTF8(sv) ? UTF8: "iso-8859-1", ifmt->type,
666 case_data_rw (c, v), var_get_width (v),
667 dict_get_encoding (sfi->dict));
679 scalar_to_value (case_data_rw (c, v), sv, v);
683 /* The remaining variables must be sysmis or blank string */
684 while (i < dict_get_var_cnt (sfi->dict))
686 const struct variable *v = vv[i++];
687 union value *val = case_data_rw (c, v);
688 value_set_missing (val, var_get_width (v));
690 casewriter_write (sfi->writer, c);
700 MODULE = PSPP PACKAGE = PSPP::Reader
702 struct sysreader_info *
703 pxs_open_sysfile (name)
706 struct casereader *reader;
707 struct sysreader_info *sri = NULL;
708 struct file_handle *fh =
709 fh_create_file (NULL, name, fh_default_properties () );
711 sri = xmalloc (sizeof (*sri));
712 sri->reader = sfm_open_reader (fh, &sri->dict, &sri->opts);
714 if ( NULL == sri->reader)
726 pxs_get_dict (reader)
727 struct sysreader_info *reader;
729 RETVAL = reader->dict;
735 struct sysreader_info *sfr;
738 casenumber n = casereader_get_case_cnt (sfr->reader);
739 if (n == CASENUMBER_MAX)
751 struct sysreader_info *sfr;
755 if ((c = casereader_read (sfr->reader)) != NULL)
759 EXTEND (SP, dict_get_var_cnt (sfr->dict));
760 for (v = 0; v < dict_get_var_cnt (sfr->dict); ++v )
762 const struct variable *var = dict_get_var (sfr->dict, v);
763 const union value *val = case_data (c, var);
765 PUSHs (sv_2mortal (value_to_scalar (val, var)));