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
175 /* Check that the version is correct up to the length of 'ver'.
176 This allows PSPP autobuilders to add a "-build#" suffix to the
177 PSPP version without causing failures here. */
178 assert (0 == strncmp (ver, bare_version, strlen (ver)));
181 msg_init (NULL, message_handler);
182 settings_init (0, 0);
186 format_value (val, var)
191 const struct fmt_spec *fmt = var_get_print_format (var);
194 make_value_from_scalar (&uv, val, var);
195 s = data_out (&uv, var_get_encoding (var), fmt);
196 value_destroy (&uv, var_get_width (var));
197 ret = newSVpv (s, fmt->w);
205 value_is_missing (val, var)
211 make_value_from_scalar (&uv, val, var);
212 ret = var_is_value_missing (var, &uv, MV_ANY);
213 value_destroy (&uv, var_get_width (var));
220 MODULE = PSPP PACKAGE = PSPP::Dict
225 RETVAL = dict_create ();
232 struct dictionary *dict
239 struct dictionary *dict
241 RETVAL = dict_get_var_cnt (dict);
246 set_label (dict, label)
247 struct dictionary *dict
250 dict_set_label (dict, label);
253 set_documents (dict, docs)
254 struct dictionary *dict
257 dict_set_documents (dict, docs);
261 add_document (dict, doc)
262 struct dictionary *dict
265 dict_add_document_line (dict, doc);
269 clear_documents (dict)
270 struct dictionary *dict
272 dict_clear_documents (dict);
276 set_weight (dict, var)
277 struct dictionary *dict
280 dict_set_weight (dict, var);
284 pxs_get_variable (dict, idx)
285 struct dictionary *dict
288 SV *errstr = get_sv("PSPP::errstr", TRUE);
289 sv_setpv (errstr, "");
290 if ( SvIV (idx) >= dict_get_var_cnt (dict))
292 sv_setpv (errstr, "The dictionary doesn't have that many variables.");
296 RETVAL = dict_get_var (dict, SvIV (idx));
302 pxs_get_var_by_name (dict, name)
303 struct dictionary *dict
306 SV *errstr = get_sv("PSPP::errstr", TRUE);
307 sv_setpv (errstr, "");
309 struct variable *var = dict_lookup_var (dict, name);
311 sv_setpv (errstr, "No such variable.");
317 MODULE = PSPP PACKAGE = PSPP::Var
321 pxs_dict_create_var (dict, name, ip_fmt)
322 struct dictionary * dict
326 SV *errstr = get_sv("PSPP::errstr", TRUE);
327 sv_setpv (errstr, "");
328 if ( ! var_is_plausible_name (name, false))
330 sv_setpv (errstr, "The variable name is not valid.");
334 struct fmt_spec op_fmt;
337 op_fmt = fmt_for_output_from_input (&ip_fmt);
338 v = dict_create_var (dict, name,
339 fmt_is_string (op_fmt.type) ? op_fmt.w : 0);
342 sv_setpv (errstr, "The variable could not be created (probably already exists).");
345 var_set_both_formats (v, &op_fmt);
346 var_set_input_format (v, ip_fmt);
353 set_missing_values (var, v1, ...)
354 struct variable *var;
361 croak ("No more than 3 missing values are permitted");
363 for (i = 0; i < items - 1; ++i)
364 scalar_to_value (&val[i], ST(i+1), var);
366 struct missing_values mv;
367 mv_init (&mv, var_get_width (var));
368 for (i = 0 ; i < items - 1; ++i )
369 mv_add_value (&mv, &val[i]);
370 var_set_missing_values (var, &mv);
374 set_label (var, label)
375 struct variable *var;
378 var_set_label (var, label);
382 clear_value_labels (var)
383 struct variable *var;
385 var_clear_value_labels (var);
388 get_write_format (var)
391 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
392 const struct fmt_spec *fmt = var_get_write_format (var);
394 hv_store (fmthash, "fmt", 3, newSVnv (fmt->type), 0);
395 hv_store (fmthash, "decimals", 8, newSVnv (fmt->d), 0);
396 hv_store (fmthash, "width", 5, newSVnv (fmt->w), 0);
398 RETVAL = newRV ((SV *) fmthash);
403 get_print_format (var)
406 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
407 const struct fmt_spec *fmt = var_get_print_format (var);
409 hv_store (fmthash, "fmt", 3, newSVnv (fmt->type), 0);
410 hv_store (fmthash, "decimals", 8, newSVnv (fmt->d), 0);
411 hv_store (fmthash, "width", 5, newSVnv (fmt->w), 0);
413 RETVAL = newRV ((SV *) fmthash);
419 pxs_set_write_format (var, fmt)
423 var_set_write_format (var, &fmt);
427 pxs_set_print_format (var, fmt)
431 var_set_print_format (var, &fmt);
434 pxs_set_output_format (var, fmt)
438 var_set_both_formats (var, &fmt);
442 add_value_label (var, key, label)
447 SV *errstr = get_sv("PSPP::errstr", TRUE);
448 sv_setpv (errstr, "");
450 union value the_value;
451 int width = var_get_width (var);
454 value_init (&the_value, width);
455 if ( var_is_numeric (var))
457 if ( ! looks_like_number (key))
459 sv_setpv (errstr, "Cannot add label with string key to a numeric variable");
460 value_destroy (&the_value, width);
463 the_value.f = SvNV (key);
467 value_copy_str_rpad (&the_value, width, SvPV_nolen(key), ' ');
469 ok = var_add_value_label (var, &the_value, label);
470 value_destroy (&the_value, width);
473 sv_setpv (errstr, "Something went wrong");
483 HV *attrhash = (HV *) sv_2mortal ((SV *) newHV());
485 struct attrset *as = var_get_attributes (var);
489 struct attrset_iterator iter;
490 struct attribute *attr;
492 for (attr = attrset_first (as, &iter);
494 attr = attrset_next (as, &iter))
497 const char *name = attribute_get_name (attr);
499 AV *values = newAV ();
501 for (i = 0 ; i < attribute_get_n_values (attr); ++i )
503 const char *value = attribute_get_value (attr, i);
504 av_push (values, newSVpv (value, 0));
507 hv_store (attrhash, name, strlen (name),
508 newRV_noinc ((SV*) values), 0);
512 RETVAL = newRV ((SV *) attrhash);
519 struct variable * var
521 RETVAL = var_get_name (var);
528 struct variable * var
530 RETVAL = var_get_label (var);
536 get_value_labels (var)
539 HV *labelhash = (HV *) sv_2mortal ((SV *) newHV());
540 const struct val_lab *vl;
541 struct val_labs_iterator *viter = NULL;
542 const struct val_labs *labels = var_get_value_labels (var);
546 for (vl = val_labs_first (labels);
548 vl = val_labs_next (labels, vl))
550 SV *sv = value_to_scalar (&vl->value, var);
552 const char *s = SvPV (sv, len);
553 hv_store (labelhash, s, len, newSVpv (val_lab_get_label (vl), 0), 0);
557 RETVAL = newRV ((SV *) labelhash);
563 MODULE = PSPP PACKAGE = PSPP::Sysfile
566 struct sysfile_info *
567 pxs_create_sysfile (name, dict_ref, opts_hr)
572 SV *dict_sv = SvRV (dict_ref);
573 struct dictionary *dict = (void *) SvIV (dict_sv);
574 struct sfm_write_options opts;
575 if (!SvROK (opts_hr))
577 opts = sfm_writer_default_options ();
581 HV *opt_h = (HV *) SvRV (opts_hr);
582 SV** readonly = hv_fetch(opt_h, "readonly", 8, 0);
583 SV** compress = hv_fetch(opt_h, "compress", 8, 0);
584 SV** version = hv_fetch(opt_h, "version", 7, 0);
586 opts.create_writeable = readonly ? ! SvIV (*readonly) : true;
587 opts.compress = compress ? SvIV (*compress) : false;
588 opts.version = version ? SvIV (*version) : 3 ;
591 struct file_handle *fh =
592 fh_create_file (NULL, name, fh_default_properties () );
593 struct sysfile_info *sfi = xmalloc (sizeof (*sfi));
594 dict_set_encoding (dict, "UTF-8");
595 sfi->writer = sfm_open_writer (fh, dict, opts);
598 sfi->dict_sv = dict_sv;
599 SvREFCNT_inc (sfi->dict_sv);
607 struct sysfile_info *sfi
609 RETVAL = sysfile_close (sfi);
615 struct sysfile_info *sfi
618 SvREFCNT_dec (sfi->dict_sv);
622 append_case (sfi, ccase)
623 struct sysfile_info *sfi
626 SV *errstr = get_sv("PSPP::errstr", TRUE);
627 sv_setpv (errstr, "");
628 if ( (!SvROK(ccase)))
634 AV *av_case = (AV*) SvRV (ccase);
636 const struct variable **vv;
641 if ( av_len (av_case) >= dict_get_var_cnt (sfi->dict))
644 c = case_create (dict_get_proto (sfi->dict));
646 dict_get_vars (sfi->dict, &vv, &nv, 1u << DC_ORDINARY | 1u << DC_SYSTEM);
648 for (sv = av_shift (av_case); SvOK (sv); sv = av_shift (av_case))
650 const struct variable *v = vv[i++];
651 const struct fmt_spec *ifmt = var_get_aux (v);
653 /* If an input format has been set, then use it.
654 Otherwise just convert the raw value.
658 struct substring ss = ss_cstr (SvPV_nolen (sv));
662 error = data_in (ss, LEGACY_NATIVE, ifmt->type,
663 case_data_rw (c, v), var_get_width (v),
664 dict_get_encoding (sfi->dict));
676 scalar_to_value (case_data_rw (c, v), sv, v);
680 /* The remaining variables must be sysmis or blank string */
681 while (i < dict_get_var_cnt (sfi->dict))
683 const struct variable *v = vv[i++];
684 union value *val = case_data_rw (c, v);
685 value_set_missing (val, var_get_width (v));
687 RETVAL = casewriter_write (sfi->writer, c);
696 MODULE = PSPP PACKAGE = PSPP::Reader
698 struct sysreader_info *
699 pxs_open_sysfile (name)
702 struct casereader *reader;
703 struct sysreader_info *sri = NULL;
704 struct file_handle *fh =
705 fh_create_file (NULL, name, fh_default_properties () );
707 sri = xmalloc (sizeof (*sri));
708 sri->reader = sfm_open_reader (fh, &sri->dict, &sri->opts);
710 if ( NULL == sri->reader)
722 pxs_get_dict (reader)
723 struct sysreader_info *reader;
725 RETVAL = reader->dict;
732 struct sysreader_info *sfr;
736 if (c = casereader_read (sfr->reader))
740 EXTEND (SP, dict_get_var_cnt (sfr->dict));
741 for (v = 0; v < dict_get_var_cnt (sfr->dict); ++v )
743 const struct variable *var = dict_get_var (sfr->dict, v);
744 const union value *val = case_data (c, var);
746 PUSHs (sv_2mortal (value_to_scalar (val, var)));