1 /* PSPP - computes sample statistics.
2 Copyright (C) 2007, 2008, 2009 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
29 #include <libpspp/message.h>
30 #include <libpspp/version.h>
31 #include <gl/xalloc.h>
32 #include <data/dictionary.h>
33 #include <data/case.h>
34 #include <data/casereader.h>
35 #include <data/variable.h>
36 #include <data/attributes.h>
37 #include <data/file-handle-def.h>
38 #include <data/sys-file-writer.h>
39 #include <data/sys-file-reader.h>
40 #include <data/value.h>
41 #include <data/vardict.h>
42 #include <data/value-labels.h>
43 #include <data/format.h>
44 #include <data/data-in.h>
45 #include <data/data-out.h>
48 typedef struct fmt_spec input_format ;
49 typedef struct fmt_spec output_format ;
52 /* A thin wrapper around sfm_writer */
57 /* A pointer to the writer. The writer is owned by the struct */
58 struct casewriter *writer;
60 /* A pointer to the dictionary. Owned externally */
61 const struct dictionary *dict;
63 /* The scalar containing the dictionary */
68 /* A thin wrapper around sfm_reader */
71 struct sfm_read_info opts;
73 /* A pointer to the reader. The reader is owned by the struct */
74 struct casereader *reader;
76 /* A pointer to the dictionary. */
77 struct dictionary *dict;
82 /* A message handler which writes messages to PSPP::errstr */
84 message_handler (const struct msg *m)
86 SV *errstr = get_sv("PSPP::errstr", TRUE);
87 sv_setpv (errstr, m->text);
91 sysfile_close (struct sysfile_info *sfi)
97 retval = casewriter_destroy (sfi->writer);
105 scalar_to_value (union value *val, SV *scalar, const struct variable *var)
107 if ( var_is_numeric (var))
109 if ( SvNOK (scalar) || SvIOK (scalar) )
110 val->f = SvNV (scalar);
117 const char *p = SvPV (scalar, len);
118 int width = var_get_width (var);
119 value_set_missing (val, width);
120 memcpy (value_str_rw (val, width), p, len);
126 value_to_scalar (const union value *val, const struct variable *var)
128 if ( var_is_numeric (var))
130 if ( var_is_value_missing (var, val, MV_SYSTEM))
131 return newSVpvn ("", 0);
133 return newSVnv (val->f);
137 int width = var_get_width (var);
138 return newSVpvn (value_str (val, width), width);
144 var_set_input_format (struct variable *v, input_format ip_fmt)
146 struct fmt_spec *if_copy = malloc (sizeof (*if_copy));
147 memcpy (if_copy, &ip_fmt, sizeof (ip_fmt));
148 var_attach_aux (v, if_copy, var_dtor_free);
152 make_value_from_scalar (union value *uv, SV *val, const struct variable *var)
154 value_init (uv, var_get_width (var));
155 scalar_to_value (uv, val, var);
161 MODULE = PSPP PACKAGE = PSPP
167 assert (0 == strcmp (ver, bare_version));
169 msg_init (NULL, message_handler);
170 settings_init (0, 0);
174 format_value (val, var)
179 const struct fmt_spec *fmt = var_get_print_format (var);
180 const struct dictionary *dict = var_get_vardict (var)->dict;
183 make_value_from_scalar (&uv, val, var);
184 s = data_out (&uv, dict_get_encoding (dict), fmt);
185 value_destroy (&uv, var_get_width (var));
186 ret = newSVpv (s, fmt->w);
194 value_is_missing (val, var)
200 make_value_from_scalar (&uv, val, var);
201 ret = var_is_value_missing (var, &uv, MV_ANY);
202 value_destroy (&uv, var_get_width (var));
209 MODULE = PSPP PACKAGE = PSPP::Dict
214 RETVAL = dict_create ();
221 struct dictionary *dict
228 struct dictionary *dict
230 RETVAL = dict_get_var_cnt (dict);
235 set_label (dict, label)
236 struct dictionary *dict
239 dict_set_label (dict, label);
242 set_documents (dict, docs)
243 struct dictionary *dict
246 dict_set_documents (dict, docs);
250 add_document (dict, doc)
251 struct dictionary *dict
254 dict_add_document_line (dict, doc);
258 clear_documents (dict)
259 struct dictionary *dict
261 dict_clear_documents (dict);
265 set_weight (dict, var)
266 struct dictionary *dict
269 dict_set_weight (dict, var);
273 pxs_get_variable (dict, idx)
274 struct dictionary *dict
277 SV *errstr = get_sv("PSPP::errstr", TRUE);
278 sv_setpv (errstr, "");
279 if ( SvIV (idx) >= dict_get_var_cnt (dict))
281 sv_setpv (errstr, "The dictionary doesn't have that many variables.");
285 RETVAL = dict_get_var (dict, SvIV (idx));
291 pxs_get_var_by_name (dict, name)
292 struct dictionary *dict
295 SV *errstr = get_sv("PSPP::errstr", TRUE);
296 sv_setpv (errstr, "");
298 struct variable *var = dict_lookup_var (dict, name);
300 sv_setpv (errstr, "No such variable.");
306 MODULE = PSPP PACKAGE = PSPP::Var
310 pxs_dict_create_var (dict, name, ip_fmt)
311 struct dictionary * dict
315 SV *errstr = get_sv("PSPP::errstr", TRUE);
316 sv_setpv (errstr, "");
317 if ( ! var_is_plausible_name (name, false))
319 sv_setpv (errstr, "The variable name is not valid.");
323 struct fmt_spec op_fmt;
326 op_fmt = fmt_for_output_from_input (&ip_fmt);
327 v = dict_create_var (dict, name,
328 fmt_is_string (op_fmt.type) ? op_fmt.w : 0);
331 sv_setpv (errstr, "The variable could not be created (probably already exists).");
334 var_set_both_formats (v, &op_fmt);
335 var_set_input_format (v, ip_fmt);
342 set_missing_values (var, v1, ...)
343 struct variable *var;
350 croak ("No more than 3 missing values are permitted");
352 for (i = 0; i < items - 1; ++i)
353 scalar_to_value (&val[i], ST(i+1), var);
355 struct missing_values mv;
356 mv_init (&mv, var_get_width (var));
357 for (i = 0 ; i < items - 1; ++i )
358 mv_add_value (&mv, &val[i]);
359 var_set_missing_values (var, &mv);
363 set_label (var, label)
364 struct variable *var;
367 var_set_label (var, label);
371 clear_value_labels (var)
372 struct variable *var;
374 var_clear_value_labels (var);
377 get_write_format (var)
380 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
381 const struct fmt_spec *fmt = var_get_write_format (var);
383 hv_store (fmthash, "fmt", 3, newSVnv (fmt->type), 0);
384 hv_store (fmthash, "decimals", 8, newSVnv (fmt->d), 0);
385 hv_store (fmthash, "width", 5, newSVnv (fmt->w), 0);
387 RETVAL = newRV ((SV *) fmthash);
392 get_print_format (var)
395 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
396 const struct fmt_spec *fmt = var_get_print_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);
408 pxs_set_write_format (var, fmt)
412 var_set_write_format (var, &fmt);
416 pxs_set_print_format (var, fmt)
420 var_set_print_format (var, &fmt);
423 pxs_set_output_format (var, fmt)
427 var_set_both_formats (var, &fmt);
431 add_value_label (var, key, label)
436 SV *errstr = get_sv("PSPP::errstr", TRUE);
437 sv_setpv (errstr, "");
439 union value the_value;
440 int width = var_get_width (var);
443 value_init (&the_value, width);
444 if ( var_is_numeric (var))
446 if ( ! looks_like_number (key))
448 sv_setpv (errstr, "Cannot add label with string key to a numeric variable");
449 value_destroy (&the_value, width);
452 the_value.f = SvNV (key);
456 value_copy_str_rpad (&the_value, width, SvPV_nolen(key), ' ');
458 ok = var_add_value_label (var, &the_value, label);
459 value_destroy (&the_value, width);
462 sv_setpv (errstr, "Something went wrong");
472 HV *attrhash = (HV *) sv_2mortal ((SV *) newHV());
474 struct attrset *as = var_get_attributes (var);
478 struct attrset_iterator iter;
479 struct attribute *attr;
481 for (attr = attrset_first (as, &iter);
483 attr = attrset_next (as, &iter))
486 const char *name = attribute_get_name (attr);
488 AV *values = newAV ();
490 for (i = 0 ; i < attribute_get_n_values (attr); ++i )
492 const char *value = attribute_get_value (attr, i);
493 av_push (values, newSVpv (value, 0));
496 hv_store (attrhash, name, strlen (name),
497 newRV_noinc ((SV*) values), 0);
501 RETVAL = newRV ((SV *) attrhash);
508 struct variable * var
510 RETVAL = var_get_name (var);
517 struct variable * var
519 RETVAL = var_get_label (var);
525 get_value_labels (var)
528 HV *labelhash = (HV *) sv_2mortal ((SV *) newHV());
529 const struct val_lab *vl;
530 struct val_labs_iterator *viter = NULL;
531 const struct val_labs *labels = var_get_value_labels (var);
535 for (vl = val_labs_first (labels);
537 vl = val_labs_next (labels, vl))
539 SV *sv = value_to_scalar (&vl->value, var);
541 const char *s = SvPV (sv, len);
542 hv_store (labelhash, s, len, newSVpv (val_lab_get_label (vl), 0), 0);
546 RETVAL = newRV ((SV *) labelhash);
552 MODULE = PSPP PACKAGE = PSPP::Sysfile
555 struct sysfile_info *
556 pxs_create_sysfile (name, dict_ref, opts_hr)
561 SV *dict_sv = SvRV (dict_ref);
562 struct dictionary *dict = (void *) SvIV (dict_sv);
563 struct sfm_write_options opts;
564 if (!SvROK (opts_hr))
566 opts = sfm_writer_default_options ();
570 HV *opt_h = (HV *) SvRV (opts_hr);
571 SV** readonly = hv_fetch(opt_h, "readonly", 8, 0);
572 SV** compress = hv_fetch(opt_h, "compress", 8, 0);
573 SV** version = hv_fetch(opt_h, "version", 7, 0);
575 opts.create_writeable = readonly ? ! SvIV (*readonly) : true;
576 opts.compress = compress ? SvIV (*compress) : false;
577 opts.version = version ? SvIV (*version) : 3 ;
580 struct file_handle *fh =
581 fh_create_file (NULL, name, fh_default_properties () );
582 struct sysfile_info *sfi = xmalloc (sizeof (*sfi));
583 sfi->writer = sfm_open_writer (fh, dict, opts);
586 sfi->dict_sv = dict_sv;
587 SvREFCNT_inc (sfi->dict_sv);
595 struct sysfile_info *sfi
597 RETVAL = sysfile_close (sfi);
603 struct sysfile_info *sfi
606 SvREFCNT_dec (sfi->dict_sv);
610 append_case (sfi, ccase)
611 struct sysfile_info *sfi
614 SV *errstr = get_sv("PSPP::errstr", TRUE);
615 sv_setpv (errstr, "");
616 if ( (!SvROK(ccase)))
622 AV *av_case = (AV*) SvRV (ccase);
624 const struct variable **vv;
629 if ( av_len (av_case) >= dict_get_var_cnt (sfi->dict))
632 c = case_create (dict_get_proto (sfi->dict));
634 dict_get_vars (sfi->dict, &vv, &nv, 1u << DC_ORDINARY | 1u << DC_SYSTEM);
636 for (sv = av_shift (av_case); SvOK (sv); sv = av_shift (av_case))
638 const struct variable *v = vv[i++];
639 const struct fmt_spec *ifmt = var_get_aux (v);
641 /* If an input format has been set, then use it.
642 Otherwise just convert the raw value.
646 struct substring ss = ss_cstr (SvPV_nolen (sv));
647 if ( ! data_in (ss, LEGACY_NATIVE, ifmt->type, 0, 0, 0,
658 scalar_to_value (case_data_rw (c, v), sv, v);
662 /* The remaining variables must be sysmis or blank string */
663 while (i < dict_get_var_cnt (sfi->dict))
665 const struct variable *v = vv[i++];
666 union value *val = case_data_rw (c, v);
667 value_set_missing (val, var_get_width (v));
669 RETVAL = casewriter_write (sfi->writer, c);
678 MODULE = PSPP PACKAGE = PSPP::Reader
680 struct sysreader_info *
681 pxs_open_sysfile (name)
684 struct casereader *reader;
685 struct sysreader_info *sri = NULL;
686 struct file_handle *fh =
687 fh_create_file (NULL, name, fh_default_properties () );
689 sri = xmalloc (sizeof (*sri));
690 sri->reader = sfm_open_reader (fh, &sri->dict, &sri->opts);
692 if ( NULL == sri->reader)
704 pxs_get_dict (reader)
705 struct sysreader_info *reader;
707 RETVAL = reader->dict;
714 struct sysreader_info *sfr;
718 if (c = casereader_read (sfr->reader))
722 EXTEND (SP, dict_get_var_cnt (sfr->dict));
723 for (v = 0; v < dict_get_var_cnt (sfr->dict); ++v )
725 const struct variable *var = dict_get_var (sfr->dict, v);
726 const union value *val = case_data (c, var);
728 PUSHs (sv_2mortal (value_to_scalar (val, var)));