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 /* Check that the version is correct up to the length of 'ver'.
168 This allows PSPP autobuilders to add a "-build#" suffix to the
169 PSPP version without causing failures here. */
170 assert (0 == strncmp (ver, bare_version, strlen (ver)));
173 msg_init (NULL, message_handler);
174 settings_init (0, 0);
178 format_value (val, var)
183 const struct fmt_spec *fmt = var_get_print_format (var);
184 const struct dictionary *dict = var_get_vardict (var)->dict;
187 make_value_from_scalar (&uv, val, var);
188 s = data_out (&uv, dict_get_encoding (dict), fmt);
189 value_destroy (&uv, var_get_width (var));
190 ret = newSVpv (s, fmt->w);
198 value_is_missing (val, var)
204 make_value_from_scalar (&uv, val, var);
205 ret = var_is_value_missing (var, &uv, MV_ANY);
206 value_destroy (&uv, var_get_width (var));
213 MODULE = PSPP PACKAGE = PSPP::Dict
218 RETVAL = dict_create ();
225 struct dictionary *dict
232 struct dictionary *dict
234 RETVAL = dict_get_var_cnt (dict);
239 set_label (dict, label)
240 struct dictionary *dict
243 dict_set_label (dict, label);
246 set_documents (dict, docs)
247 struct dictionary *dict
250 dict_set_documents (dict, docs);
254 add_document (dict, doc)
255 struct dictionary *dict
258 dict_add_document_line (dict, doc);
262 clear_documents (dict)
263 struct dictionary *dict
265 dict_clear_documents (dict);
269 set_weight (dict, var)
270 struct dictionary *dict
273 dict_set_weight (dict, var);
277 pxs_get_variable (dict, idx)
278 struct dictionary *dict
281 SV *errstr = get_sv("PSPP::errstr", TRUE);
282 sv_setpv (errstr, "");
283 if ( SvIV (idx) >= dict_get_var_cnt (dict))
285 sv_setpv (errstr, "The dictionary doesn't have that many variables.");
289 RETVAL = dict_get_var (dict, SvIV (idx));
295 pxs_get_var_by_name (dict, name)
296 struct dictionary *dict
299 SV *errstr = get_sv("PSPP::errstr", TRUE);
300 sv_setpv (errstr, "");
302 struct variable *var = dict_lookup_var (dict, name);
304 sv_setpv (errstr, "No such variable.");
310 MODULE = PSPP PACKAGE = PSPP::Var
314 pxs_dict_create_var (dict, name, ip_fmt)
315 struct dictionary * dict
319 SV *errstr = get_sv("PSPP::errstr", TRUE);
320 sv_setpv (errstr, "");
321 if ( ! var_is_plausible_name (name, false))
323 sv_setpv (errstr, "The variable name is not valid.");
327 struct fmt_spec op_fmt;
330 op_fmt = fmt_for_output_from_input (&ip_fmt);
331 v = dict_create_var (dict, name,
332 fmt_is_string (op_fmt.type) ? op_fmt.w : 0);
335 sv_setpv (errstr, "The variable could not be created (probably already exists).");
338 var_set_both_formats (v, &op_fmt);
339 var_set_input_format (v, ip_fmt);
346 set_missing_values (var, v1, ...)
347 struct variable *var;
354 croak ("No more than 3 missing values are permitted");
356 for (i = 0; i < items - 1; ++i)
357 scalar_to_value (&val[i], ST(i+1), var);
359 struct missing_values mv;
360 mv_init (&mv, var_get_width (var));
361 for (i = 0 ; i < items - 1; ++i )
362 mv_add_value (&mv, &val[i]);
363 var_set_missing_values (var, &mv);
367 set_label (var, label)
368 struct variable *var;
371 var_set_label (var, label);
375 clear_value_labels (var)
376 struct variable *var;
378 var_clear_value_labels (var);
381 get_write_format (var)
384 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
385 const struct fmt_spec *fmt = var_get_write_format (var);
387 hv_store (fmthash, "fmt", 3, newSVnv (fmt->type), 0);
388 hv_store (fmthash, "decimals", 8, newSVnv (fmt->d), 0);
389 hv_store (fmthash, "width", 5, newSVnv (fmt->w), 0);
391 RETVAL = newRV ((SV *) fmthash);
396 get_print_format (var)
399 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
400 const struct fmt_spec *fmt = var_get_print_format (var);
402 hv_store (fmthash, "fmt", 3, newSVnv (fmt->type), 0);
403 hv_store (fmthash, "decimals", 8, newSVnv (fmt->d), 0);
404 hv_store (fmthash, "width", 5, newSVnv (fmt->w), 0);
406 RETVAL = newRV ((SV *) fmthash);
412 pxs_set_write_format (var, fmt)
416 var_set_write_format (var, &fmt);
420 pxs_set_print_format (var, fmt)
424 var_set_print_format (var, &fmt);
427 pxs_set_output_format (var, fmt)
431 var_set_both_formats (var, &fmt);
435 add_value_label (var, key, label)
440 SV *errstr = get_sv("PSPP::errstr", TRUE);
441 sv_setpv (errstr, "");
443 union value the_value;
444 int width = var_get_width (var);
447 value_init (&the_value, width);
448 if ( var_is_numeric (var))
450 if ( ! looks_like_number (key))
452 sv_setpv (errstr, "Cannot add label with string key to a numeric variable");
453 value_destroy (&the_value, width);
456 the_value.f = SvNV (key);
460 value_copy_str_rpad (&the_value, width, SvPV_nolen(key), ' ');
462 ok = var_add_value_label (var, &the_value, label);
463 value_destroy (&the_value, width);
466 sv_setpv (errstr, "Something went wrong");
476 HV *attrhash = (HV *) sv_2mortal ((SV *) newHV());
478 struct attrset *as = var_get_attributes (var);
482 struct attrset_iterator iter;
483 struct attribute *attr;
485 for (attr = attrset_first (as, &iter);
487 attr = attrset_next (as, &iter))
490 const char *name = attribute_get_name (attr);
492 AV *values = newAV ();
494 for (i = 0 ; i < attribute_get_n_values (attr); ++i )
496 const char *value = attribute_get_value (attr, i);
497 av_push (values, newSVpv (value, 0));
500 hv_store (attrhash, name, strlen (name),
501 newRV_noinc ((SV*) values), 0);
505 RETVAL = newRV ((SV *) attrhash);
512 struct variable * var
514 RETVAL = var_get_name (var);
521 struct variable * var
523 RETVAL = var_get_label (var);
529 get_value_labels (var)
532 HV *labelhash = (HV *) sv_2mortal ((SV *) newHV());
533 const struct val_lab *vl;
534 struct val_labs_iterator *viter = NULL;
535 const struct val_labs *labels = var_get_value_labels (var);
539 for (vl = val_labs_first (labels);
541 vl = val_labs_next (labels, vl))
543 SV *sv = value_to_scalar (&vl->value, var);
545 const char *s = SvPV (sv, len);
546 hv_store (labelhash, s, len, newSVpv (val_lab_get_label (vl), 0), 0);
550 RETVAL = newRV ((SV *) labelhash);
556 MODULE = PSPP PACKAGE = PSPP::Sysfile
559 struct sysfile_info *
560 pxs_create_sysfile (name, dict_ref, opts_hr)
565 SV *dict_sv = SvRV (dict_ref);
566 struct dictionary *dict = (void *) SvIV (dict_sv);
567 struct sfm_write_options opts;
568 if (!SvROK (opts_hr))
570 opts = sfm_writer_default_options ();
574 HV *opt_h = (HV *) SvRV (opts_hr);
575 SV** readonly = hv_fetch(opt_h, "readonly", 8, 0);
576 SV** compress = hv_fetch(opt_h, "compress", 8, 0);
577 SV** version = hv_fetch(opt_h, "version", 7, 0);
579 opts.create_writeable = readonly ? ! SvIV (*readonly) : true;
580 opts.compress = compress ? SvIV (*compress) : false;
581 opts.version = version ? SvIV (*version) : 3 ;
584 struct file_handle *fh =
585 fh_create_file (NULL, name, fh_default_properties () );
586 struct sysfile_info *sfi = xmalloc (sizeof (*sfi));
587 dict_set_encoding (dict, "UTF-8");
588 sfi->writer = sfm_open_writer (fh, dict, opts);
591 sfi->dict_sv = dict_sv;
592 SvREFCNT_inc (sfi->dict_sv);
600 struct sysfile_info *sfi
602 RETVAL = sysfile_close (sfi);
608 struct sysfile_info *sfi
611 SvREFCNT_dec (sfi->dict_sv);
615 append_case (sfi, ccase)
616 struct sysfile_info *sfi
619 SV *errstr = get_sv("PSPP::errstr", TRUE);
620 sv_setpv (errstr, "");
621 if ( (!SvROK(ccase)))
627 AV *av_case = (AV*) SvRV (ccase);
629 const struct variable **vv;
634 if ( av_len (av_case) >= dict_get_var_cnt (sfi->dict))
637 c = case_create (dict_get_proto (sfi->dict));
639 dict_get_vars (sfi->dict, &vv, &nv, 1u << DC_ORDINARY | 1u << DC_SYSTEM);
641 for (sv = av_shift (av_case); SvOK (sv); sv = av_shift (av_case))
643 const struct variable *v = vv[i++];
644 const struct fmt_spec *ifmt = var_get_aux (v);
646 /* If an input format has been set, then use it.
647 Otherwise just convert the raw value.
651 struct substring ss = ss_cstr (SvPV_nolen (sv));
652 if ( ! data_in (ss, LEGACY_NATIVE, ifmt->type, 0, 0, 0,
663 scalar_to_value (case_data_rw (c, v), sv, v);
667 /* The remaining variables must be sysmis or blank string */
668 while (i < dict_get_var_cnt (sfi->dict))
670 const struct variable *v = vv[i++];
671 union value *val = case_data_rw (c, v);
672 value_set_missing (val, var_get_width (v));
674 RETVAL = casewriter_write (sfi->writer, c);
683 MODULE = PSPP PACKAGE = PSPP::Reader
685 struct sysreader_info *
686 pxs_open_sysfile (name)
689 struct casereader *reader;
690 struct sysreader_info *sri = NULL;
691 struct file_handle *fh =
692 fh_create_file (NULL, name, fh_default_properties () );
694 sri = xmalloc (sizeof (*sri));
695 sri->reader = sfm_open_reader (fh, &sri->dict, &sri->opts);
697 if ( NULL == sri->reader)
709 pxs_get_dict (reader)
710 struct sysreader_info *reader;
712 RETVAL = reader->dict;
719 struct sysreader_info *sfr;
723 if (c = casereader_read (sfr->reader))
727 EXTEND (SP, dict_get_var_cnt (sfr->dict));
728 for (v = 0; v < dict_get_var_cnt (sfr->dict); ++v )
730 const struct variable *var = dict_get_var (sfr->dict, v);
731 const union value *val = case_data (c, var);
733 PUSHs (sv_2mortal (value_to_scalar (val, var)));