1 /* PSPP - computes sample statistics.
2 Copyright (C) 2007, 2008, 2009, 2010 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 <gl/xalloc.h>
38 #include <data/dictionary.h>
39 #include <data/case.h>
40 #include <data/casereader.h>
41 #include <data/variable.h>
42 #include <data/attributes.h>
43 #include <data/file-handle-def.h>
44 #include <data/sys-file-writer.h>
45 #include <data/sys-file-reader.h>
46 #include <data/value.h>
47 #include <data/vardict.h>
48 #include <data/value-labels.h>
49 #include <data/format.h>
50 #include <data/data-in.h>
51 #include <data/data-out.h>
54 typedef struct fmt_spec input_format ;
55 typedef struct fmt_spec output_format ;
58 /* A thin wrapper around sfm_writer */
63 /* A pointer to the writer. The writer is owned by the struct */
64 struct casewriter *writer;
66 /* A pointer to the dictionary. Owned externally */
67 const struct dictionary *dict;
69 /* The scalar containing the dictionary */
74 /* A thin wrapper around sfm_reader */
77 struct sfm_read_info opts;
79 /* A pointer to the reader. The reader is owned by the struct */
80 struct casereader *reader;
82 /* A pointer to the dictionary. */
83 struct dictionary *dict;
88 /* A message handler which writes messages to PSPP::errstr */
90 message_handler (const struct msg *m)
92 SV *errstr = get_sv("PSPP::errstr", TRUE);
93 sv_setpv (errstr, m->text);
97 sysfile_close (struct sysfile_info *sfi)
103 retval = casewriter_destroy (sfi->writer);
111 scalar_to_value (union value *val, SV *scalar, const struct variable *var)
113 if ( var_is_numeric (var))
115 if ( SvNOK (scalar) || SvIOK (scalar) )
116 val->f = SvNV (scalar);
123 const char *p = SvPV (scalar, len);
124 int width = var_get_width (var);
125 value_set_missing (val, width);
126 memcpy (value_str_rw (val, width), p, len);
132 value_to_scalar (const union value *val, const struct variable *var)
134 if ( var_is_numeric (var))
136 if ( var_is_value_missing (var, val, MV_SYSTEM))
137 return newSVpvn ("", 0);
139 return newSVnv (val->f);
143 int width = var_get_width (var);
144 return newSVpvn (value_str (val, width), width);
150 var_set_input_format (struct variable *v, input_format ip_fmt)
152 struct fmt_spec *if_copy = malloc (sizeof (*if_copy));
153 memcpy (if_copy, &ip_fmt, sizeof (ip_fmt));
154 var_attach_aux (v, if_copy, var_dtor_free);
158 make_value_from_scalar (union value *uv, SV *val, const struct variable *var)
160 value_init (uv, var_get_width (var));
161 scalar_to_value (uv, val, var);
167 MODULE = PSPP PACKAGE = PSPP
173 /* Check that the version is correct up to the length of 'ver'.
174 This allows PSPP autobuilders to add a "-build#" suffix to the
175 PSPP version without causing failures here. */
176 assert (0 == strncmp (ver, bare_version, strlen (ver)));
179 msg_init (NULL, message_handler);
180 settings_init (0, 0);
184 format_value (val, var)
189 const struct fmt_spec *fmt = var_get_print_format (var);
192 make_value_from_scalar (&uv, val, var);
193 s = data_out (&uv, var_get_encoding (var), fmt);
194 value_destroy (&uv, var_get_width (var));
195 ret = newSVpv (s, fmt->w);
203 value_is_missing (val, var)
209 make_value_from_scalar (&uv, val, var);
210 ret = var_is_value_missing (var, &uv, MV_ANY);
211 value_destroy (&uv, var_get_width (var));
218 MODULE = PSPP PACKAGE = PSPP::Dict
223 RETVAL = dict_create ();
230 struct dictionary *dict
237 struct dictionary *dict
239 RETVAL = dict_get_var_cnt (dict);
244 set_label (dict, label)
245 struct dictionary *dict
248 dict_set_label (dict, label);
251 set_documents (dict, docs)
252 struct dictionary *dict
255 dict_set_documents (dict, docs);
259 add_document (dict, doc)
260 struct dictionary *dict
263 dict_add_document_line (dict, doc);
267 clear_documents (dict)
268 struct dictionary *dict
270 dict_clear_documents (dict);
274 set_weight (dict, var)
275 struct dictionary *dict
278 dict_set_weight (dict, var);
282 pxs_get_variable (dict, idx)
283 struct dictionary *dict
286 SV *errstr = get_sv("PSPP::errstr", TRUE);
287 sv_setpv (errstr, "");
288 if ( SvIV (idx) >= dict_get_var_cnt (dict))
290 sv_setpv (errstr, "The dictionary doesn't have that many variables.");
294 RETVAL = dict_get_var (dict, SvIV (idx));
300 pxs_get_var_by_name (dict, name)
301 struct dictionary *dict
304 SV *errstr = get_sv("PSPP::errstr", TRUE);
305 sv_setpv (errstr, "");
307 struct variable *var = dict_lookup_var (dict, name);
309 sv_setpv (errstr, "No such variable.");
315 MODULE = PSPP PACKAGE = PSPP::Var
319 pxs_dict_create_var (dict, name, ip_fmt)
320 struct dictionary * dict
324 SV *errstr = get_sv("PSPP::errstr", TRUE);
325 sv_setpv (errstr, "");
326 if ( ! var_is_plausible_name (name, false))
328 sv_setpv (errstr, "The variable name is not valid.");
332 struct fmt_spec op_fmt;
335 op_fmt = fmt_for_output_from_input (&ip_fmt);
336 v = dict_create_var (dict, name,
337 fmt_is_string (op_fmt.type) ? op_fmt.w : 0);
340 sv_setpv (errstr, "The variable could not be created (probably already exists).");
343 var_set_both_formats (v, &op_fmt);
344 var_set_input_format (v, ip_fmt);
351 set_missing_values (var, v1, ...)
352 struct variable *var;
359 croak ("No more than 3 missing values are permitted");
361 for (i = 0; i < items - 1; ++i)
362 scalar_to_value (&val[i], ST(i+1), var);
364 struct missing_values mv;
365 mv_init (&mv, var_get_width (var));
366 for (i = 0 ; i < items - 1; ++i )
367 mv_add_value (&mv, &val[i]);
368 var_set_missing_values (var, &mv);
372 set_label (var, label)
373 struct variable *var;
376 var_set_label (var, label);
380 clear_value_labels (var)
381 struct variable *var;
383 var_clear_value_labels (var);
386 get_write_format (var)
389 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
390 const struct fmt_spec *fmt = var_get_write_format (var);
392 hv_store (fmthash, "fmt", 3, newSVnv (fmt->type), 0);
393 hv_store (fmthash, "decimals", 8, newSVnv (fmt->d), 0);
394 hv_store (fmthash, "width", 5, newSVnv (fmt->w), 0);
396 RETVAL = newRV ((SV *) fmthash);
401 get_print_format (var)
404 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
405 const struct fmt_spec *fmt = var_get_print_format (var);
407 hv_store (fmthash, "fmt", 3, newSVnv (fmt->type), 0);
408 hv_store (fmthash, "decimals", 8, newSVnv (fmt->d), 0);
409 hv_store (fmthash, "width", 5, newSVnv (fmt->w), 0);
411 RETVAL = newRV ((SV *) fmthash);
417 pxs_set_write_format (var, fmt)
421 var_set_write_format (var, &fmt);
425 pxs_set_print_format (var, fmt)
429 var_set_print_format (var, &fmt);
432 pxs_set_output_format (var, fmt)
436 var_set_both_formats (var, &fmt);
440 add_value_label (var, key, label)
445 SV *errstr = get_sv("PSPP::errstr", TRUE);
446 sv_setpv (errstr, "");
448 union value the_value;
449 int width = var_get_width (var);
452 value_init (&the_value, width);
453 if ( var_is_numeric (var))
455 if ( ! looks_like_number (key))
457 sv_setpv (errstr, "Cannot add label with string key to a numeric variable");
458 value_destroy (&the_value, width);
461 the_value.f = SvNV (key);
465 value_copy_str_rpad (&the_value, width, SvPV_nolen(key), ' ');
467 ok = var_add_value_label (var, &the_value, label);
468 value_destroy (&the_value, width);
471 sv_setpv (errstr, "Something went wrong");
481 HV *attrhash = (HV *) sv_2mortal ((SV *) newHV());
483 struct attrset *as = var_get_attributes (var);
487 struct attrset_iterator iter;
488 struct attribute *attr;
490 for (attr = attrset_first (as, &iter);
492 attr = attrset_next (as, &iter))
495 const char *name = attribute_get_name (attr);
497 AV *values = newAV ();
499 for (i = 0 ; i < attribute_get_n_values (attr); ++i )
501 const char *value = attribute_get_value (attr, i);
502 av_push (values, newSVpv (value, 0));
505 hv_store (attrhash, name, strlen (name),
506 newRV_noinc ((SV*) values), 0);
510 RETVAL = newRV ((SV *) attrhash);
517 struct variable * var
519 RETVAL = var_get_name (var);
526 struct variable * var
528 RETVAL = var_get_label (var);
534 get_value_labels (var)
537 HV *labelhash = (HV *) sv_2mortal ((SV *) newHV());
538 const struct val_lab *vl;
539 struct val_labs_iterator *viter = NULL;
540 const struct val_labs *labels = var_get_value_labels (var);
544 for (vl = val_labs_first (labels);
546 vl = val_labs_next (labels, vl))
548 SV *sv = value_to_scalar (&vl->value, var);
550 const char *s = SvPV (sv, len);
551 hv_store (labelhash, s, len, newSVpv (val_lab_get_label (vl), 0), 0);
555 RETVAL = newRV ((SV *) labelhash);
561 MODULE = PSPP PACKAGE = PSPP::Sysfile
564 struct sysfile_info *
565 pxs_create_sysfile (name, dict_ref, opts_hr)
570 SV *dict_sv = SvRV (dict_ref);
571 struct dictionary *dict = (void *) SvIV (dict_sv);
572 struct sfm_write_options opts;
573 if (!SvROK (opts_hr))
575 opts = sfm_writer_default_options ();
579 HV *opt_h = (HV *) SvRV (opts_hr);
580 SV** readonly = hv_fetch(opt_h, "readonly", 8, 0);
581 SV** compress = hv_fetch(opt_h, "compress", 8, 0);
582 SV** version = hv_fetch(opt_h, "version", 7, 0);
584 opts.create_writeable = readonly ? ! SvIV (*readonly) : true;
585 opts.compress = compress ? SvIV (*compress) : false;
586 opts.version = version ? SvIV (*version) : 3 ;
589 struct file_handle *fh =
590 fh_create_file (NULL, name, fh_default_properties () );
591 struct sysfile_info *sfi = xmalloc (sizeof (*sfi));
592 dict_set_encoding (dict, "UTF-8");
593 sfi->writer = sfm_open_writer (fh, dict, opts);
596 sfi->dict_sv = dict_sv;
597 SvREFCNT_inc (sfi->dict_sv);
605 struct sysfile_info *sfi
607 RETVAL = sysfile_close (sfi);
613 struct sysfile_info *sfi
616 SvREFCNT_dec (sfi->dict_sv);
620 append_case (sfi, ccase)
621 struct sysfile_info *sfi
624 SV *errstr = get_sv("PSPP::errstr", TRUE);
625 sv_setpv (errstr, "");
626 if ( (!SvROK(ccase)))
632 AV *av_case = (AV*) SvRV (ccase);
634 const struct variable **vv;
639 if ( av_len (av_case) >= dict_get_var_cnt (sfi->dict))
642 c = case_create (dict_get_proto (sfi->dict));
644 dict_get_vars (sfi->dict, &vv, &nv, 1u << DC_ORDINARY | 1u << DC_SYSTEM);
646 for (sv = av_shift (av_case); SvOK (sv); sv = av_shift (av_case))
648 const struct variable *v = vv[i++];
649 const struct fmt_spec *ifmt = var_get_aux (v);
651 /* If an input format has been set, then use it.
652 Otherwise just convert the raw value.
656 struct substring ss = ss_cstr (SvPV_nolen (sv));
657 if ( ! data_in (ss, LEGACY_NATIVE, ifmt->type, 0, 0, 0,
668 scalar_to_value (case_data_rw (c, v), sv, v);
672 /* The remaining variables must be sysmis or blank string */
673 while (i < dict_get_var_cnt (sfi->dict))
675 const struct variable *v = vv[i++];
676 union value *val = case_data_rw (c, v);
677 value_set_missing (val, var_get_width (v));
679 RETVAL = casewriter_write (sfi->writer, c);
688 MODULE = PSPP PACKAGE = PSPP::Reader
690 struct sysreader_info *
691 pxs_open_sysfile (name)
694 struct casereader *reader;
695 struct sysreader_info *sri = NULL;
696 struct file_handle *fh =
697 fh_create_file (NULL, name, fh_default_properties () );
699 sri = xmalloc (sizeof (*sri));
700 sri->reader = sfm_open_reader (fh, &sri->dict, &sri->opts);
702 if ( NULL == sri->reader)
714 pxs_get_dict (reader)
715 struct sysreader_info *reader;
717 RETVAL = reader->dict;
724 struct sysreader_info *sfr;
728 if (c = casereader_read (sfr->reader))
732 EXTEND (SP, dict_get_var_cnt (sfr->dict));
733 for (v = 0; v < dict_get_var_cnt (sfr->dict); ++v )
735 const struct variable *var = dict_get_var (sfr->dict, v);
736 const union value *val = case_data (c, var);
738 PUSHs (sv_2mortal (value_to_scalar (val, var)));