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/value-labels.h>
42 #include <data/format.h>
43 #include <data/data-in.h>
46 typedef struct fmt_spec input_format ;
47 typedef struct fmt_spec output_format ;
50 /* A thin wrapper around sfm_writer */
55 /* A pointer to the writer. The writer is owned by the struct */
56 struct casewriter *writer;
58 /* A pointer to the dictionary. Owned externally */
59 const struct dictionary *dict;
61 /* The scalar containing the dictionary */
66 /* A thin wrapper around sfm_reader */
69 struct sfm_read_info opts;
71 /* A pointer to the reader. The reader is owned by the struct */
72 struct casereader *reader;
74 /* A pointer to the dictionary. */
75 struct dictionary *dict;
80 /* A message handler which writes messages to PSPP::errstr */
82 message_handler (const struct msg *m)
84 SV *errstr = get_sv("PSPP::errstr", TRUE);
85 sv_setpv (errstr, m->text);
89 sysfile_close (struct sysfile_info *sfi)
95 retval = casewriter_destroy (sfi->writer);
103 scalar_to_value (union value *val, SV *scalar, const struct variable *var)
105 if ( var_is_numeric (var))
107 if ( SvNOK (scalar) || SvIOK (scalar) )
108 val->f = SvNV (scalar);
115 const char *p = SvPV (scalar, len);
116 memset (val->s, ' ', var_get_width (var));
117 memcpy (val->s, p, len);
123 value_to_scalar (const union value *val, const struct variable *var)
125 if ( var_is_numeric (var))
127 if ( var_is_value_missing (var, val, MV_SYSTEM))
128 return newSVpvn ("", 0);
130 return newSVnv (val->f);
133 return newSVpvn (val->s, var_get_width (var));
138 var_set_input_format (struct variable *v, input_format ip_fmt)
140 struct fmt_spec *if_copy = malloc (sizeof (*if_copy));
141 memcpy (if_copy, &ip_fmt, sizeof (ip_fmt));
142 var_attach_aux (v, if_copy, var_dtor_free);
146 make_value_from_scalar (SV *val, const struct variable *var)
148 union value *uv = value_create (var_get_width (var));
149 scalar_to_value (uv, val, var);
156 MODULE = PSPP PACKAGE = PSPP
162 assert (0 == strcmp (ver, bare_version));
163 msg_init (NULL, message_handler);
164 settings_init (0, 0);
168 format_value (val, var)
173 const struct fmt_spec *fmt = var_get_print_format (var);
174 union value *uv = make_value_from_scalar (val, var);
177 memset (s, '\0', fmt->w);
178 data_out (uv, fmt, s);
180 ret = newSVpv (s, fmt->w);
188 value_is_missing (val, var)
192 union value *uv = make_value_from_scalar (val, var);
193 int ret = var_is_value_missing (var, uv, MV_ANY);
201 MODULE = PSPP PACKAGE = PSPP::Dict
206 RETVAL = dict_create ();
213 struct dictionary *dict
220 struct dictionary *dict
222 RETVAL = dict_get_var_cnt (dict);
227 set_label (dict, label)
228 struct dictionary *dict
231 dict_set_label (dict, label);
234 set_documents (dict, docs)
235 struct dictionary *dict
238 dict_set_documents (dict, docs);
242 add_document (dict, doc)
243 struct dictionary *dict
246 dict_add_document_line (dict, doc);
250 clear_documents (dict)
251 struct dictionary *dict
253 dict_clear_documents (dict);
257 set_weight (dict, var)
258 struct dictionary *dict
261 dict_set_weight (dict, var);
265 pxs_get_variable (dict, idx)
266 struct dictionary *dict
269 SV *errstr = get_sv("PSPP::errstr", TRUE);
270 sv_setpv (errstr, "");
271 if ( SvIV (idx) >= dict_get_var_cnt (dict))
273 sv_setpv (errstr, "The dictionary doesn't have that many variables.");
277 RETVAL = dict_get_var (dict, SvIV (idx));
283 pxs_get_var_by_name (dict, name)
284 struct dictionary *dict
287 SV *errstr = get_sv("PSPP::errstr", TRUE);
288 sv_setpv (errstr, "");
290 struct variable *var = dict_lookup_var (dict, name);
292 sv_setpv (errstr, "No such variable.");
298 MODULE = PSPP PACKAGE = PSPP::Var
302 pxs_dict_create_var (dict, name, ip_fmt)
303 struct dictionary * dict
307 SV *errstr = get_sv("PSPP::errstr", TRUE);
308 sv_setpv (errstr, "");
309 if ( ! var_is_plausible_name (name, false))
311 sv_setpv (errstr, "The variable name is not valid.");
315 struct fmt_spec op_fmt;
318 op_fmt = fmt_for_output_from_input (&ip_fmt);
319 v = dict_create_var (dict, name,
320 fmt_is_string (op_fmt.type) ? op_fmt.w : 0);
323 sv_setpv (errstr, "The variable could not be created (probably already exists).");
326 var_set_both_formats (v, &op_fmt);
327 var_set_input_format (v, ip_fmt);
334 set_missing_values (var, v1, ...)
335 struct variable *var;
342 croak ("No more than 3 missing values are permitted");
344 for (i = 0; i < items - 1; ++i)
345 scalar_to_value (&val[i], ST(i+1), var);
347 struct missing_values mv;
348 mv_init (&mv, var_get_width (var));
349 for (i = 0 ; i < items - 1; ++i )
350 mv_add_value (&mv, &val[i]);
351 var_set_missing_values (var, &mv);
355 set_label (var, label)
356 struct variable *var;
359 var_set_label (var, label);
363 clear_value_labels (var)
364 struct variable *var;
366 var_clear_value_labels (var);
369 pxs_set_write_format (var, fmt)
373 var_set_write_format (var, &fmt);
377 pxs_set_print_format (var, fmt)
381 var_set_print_format (var, &fmt);
384 pxs_set_output_format (var, fmt)
388 var_set_both_formats (var, &fmt);
392 add_value_label (var, key, label)
397 SV *errstr = get_sv("PSPP::errstr", TRUE);
398 sv_setpv (errstr, "");
400 union value the_value;
402 if ( var_is_numeric (var))
404 if ( ! looks_like_number (key))
406 sv_setpv (errstr, "Cannot add label with string key to a numeric variable");
409 the_value.f = SvNV (key);
413 if ( var_is_long_string (var) )
415 sv_setpv (errstr, "Cannot add label to a long string variable");
418 strncpy (the_value.s, SvPV_nolen(key), MAX_SHORT_STRING);
420 if (! var_add_value_label (var, &the_value, label) )
422 sv_setpv (errstr, "Something went wrong");
432 HV *attrhash = (HV *) sv_2mortal ((SV *) newHV());
434 struct attrset *as = var_get_attributes (var);
438 struct attrset_iterator iter;
439 struct attribute *attr;
441 for (attr = attrset_first (as, &iter);
443 attr = attrset_next (as, &iter))
446 const char *name = attribute_get_name (attr);
448 AV *values = newAV ();
450 for (i = 0 ; i < attribute_get_n_values (attr); ++i )
452 const char *value = attribute_get_value (attr, i);
453 av_push (values, newSVpv (value, 0));
456 hv_store (attrhash, name, strlen (name),
457 newRV_noinc ((SV*) values), 0);
461 RETVAL = newRV ((SV *) attrhash);
468 struct variable * var
470 RETVAL = var_get_name (var);
477 struct variable * var
479 RETVAL = var_get_label (var);
485 get_value_labels (var)
488 HV *labelhash = (HV *) sv_2mortal ((SV *) newHV());
490 struct val_labs_iterator *viter = NULL;
491 const struct val_labs *labels = var_get_value_labels (var);
495 for (vl = val_labs_first (labels, &viter);
497 vl = val_labs_next (labels, &viter))
499 SV *sv = value_to_scalar (&vl->value, var);
501 const char *s = SvPV (sv, len);
502 hv_store (labelhash, s, len, newSVpv (vl->label, 0), 0);
506 RETVAL = newRV ((SV *) labelhash);
512 MODULE = PSPP PACKAGE = PSPP::Sysfile
515 struct sysfile_info *
516 pxs_create_sysfile (name, dict_ref, opts_hr)
521 SV *dict_sv = SvRV (dict_ref);
522 struct dictionary *dict = (void *) SvIV (dict_sv);
523 struct sfm_write_options opts;
524 if (!SvROK (opts_hr))
526 opts = sfm_writer_default_options ();
530 HV *opt_h = (HV *) SvRV (opts_hr);
531 SV** readonly = hv_fetch(opt_h, "readonly", 8, 0);
532 SV** compress = hv_fetch(opt_h, "compress", 8, 0);
533 SV** version = hv_fetch(opt_h, "version", 7, 0);
535 opts.create_writeable = readonly ? ! SvIV (*readonly) : true;
536 opts.compress = compress ? SvIV (*compress) : false;
537 opts.version = version ? SvIV (*version) : 3 ;
540 struct file_handle *fh =
541 fh_create_file (NULL, name, fh_default_properties () );
542 struct sysfile_info *sfi = xmalloc (sizeof (*sfi));
543 sfi->writer = sfm_open_writer (fh, dict, opts);
546 sfi->dict_sv = dict_sv;
547 SvREFCNT_inc (sfi->dict_sv);
555 struct sysfile_info *sfi
557 RETVAL = sysfile_close (sfi);
563 struct sysfile_info *sfi
566 SvREFCNT_dec (sfi->dict_sv);
570 append_case (sfi, ccase)
571 struct sysfile_info *sfi
574 SV *errstr = get_sv("PSPP::errstr", TRUE);
575 sv_setpv (errstr, "");
576 if ( (!SvROK(ccase)))
582 AV *av_case = (AV*) SvRV (ccase);
584 const struct variable **vv;
589 if ( av_len (av_case) >= dict_get_var_cnt (sfi->dict))
592 c = case_create (dict_get_next_value_idx (sfi->dict));
594 dict_get_vars (sfi->dict, &vv, &nv, 1u << DC_ORDINARY | 1u << DC_SYSTEM);
596 for (sv = av_shift (av_case); SvOK (sv); sv = av_shift (av_case))
598 const struct variable *v = vv[i++];
599 const struct fmt_spec *ifmt = var_get_aux (v);
601 /* If an input format has been set, then use it.
602 Otherwise just convert the raw value.
606 struct substring ss = ss_cstr (SvPV_nolen (sv));
607 if ( ! data_in (ss, LEGACY_NATIVE, ifmt->type, 0, 0, 0,
617 scalar_to_value (case_data_rw (c, v), sv, v);
621 /* The remaining variables must be sysmis or blank string */
622 while (i < dict_get_var_cnt (sfi->dict))
624 const struct variable *v = vv[i++];
625 union value *val = case_data_rw (c, v);
626 if ( var_is_numeric (v))
629 memset (val->s, ' ', var_get_width (v));
631 RETVAL = casewriter_write (sfi->writer, c);
640 MODULE = PSPP PACKAGE = PSPP::Reader
642 struct sysreader_info *
643 pxs_open_sysfile (name)
646 struct casereader *reader;
647 struct sysreader_info *sri = NULL;
648 struct file_handle *fh =
649 fh_create_file (NULL, name, fh_default_properties () );
651 sri = xmalloc (sizeof (*sri));
652 sri->reader = sfm_open_reader (fh, &sri->dict, &sri->opts);
654 if ( NULL == sri->reader)
666 pxs_get_dict (reader)
667 struct sysreader_info *reader;
669 RETVAL = reader->dict;
676 struct sysreader_info *sfr;
680 if (c = casereader_read (sfr->reader))
684 EXTEND (SP, dict_get_var_cnt (sfr->dict));
685 for (v = 0; v < dict_get_var_cnt (sfr->dict); ++v )
687 const struct variable *var = dict_get_var (sfr->dict, v);
688 const union value *val = case_data (c, var);
690 PUSHs (sv_2mortal (value_to_scalar (val, var)));