1 /* PSPP - computes sample statistics.
2 Copyright (C) 2007, 2008, 2009, 2010, 2011 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 <libpspp/i18n.h>
38 #include <gl/xalloc.h>
39 #include <data/dictionary.h>
40 #include <data/case.h>
41 #include <data/casereader.h>
42 #include <data/variable.h>
43 #include <data/attributes.h>
44 #include <data/file-handle-def.h>
45 #include <data/sys-file-writer.h>
46 #include <data/sys-file-reader.h>
47 #include <data/value.h>
48 #include <data/vardict.h>
49 #include <data/value-labels.h>
50 #include <data/format.h>
51 #include <data/data-in.h>
52 #include <data/data-out.h>
55 typedef struct fmt_spec input_format ;
56 typedef struct fmt_spec output_format ;
59 /* A thin wrapper around sfm_writer */
64 /* A pointer to the writer. The writer is owned by the struct */
65 struct casewriter *writer;
67 /* A pointer to the dictionary. Owned externally */
68 const struct dictionary *dict;
70 /* The scalar containing the dictionary */
75 /* A thin wrapper around sfm_reader */
78 struct sfm_read_info opts;
80 /* A pointer to the reader. The reader is owned by the struct */
81 struct casereader *reader;
83 /* A pointer to the dictionary. */
84 struct dictionary *dict;
89 /* A message handler which writes messages to PSPP::errstr */
91 message_handler (const struct msg *m, void *aux)
93 SV *errstr = get_sv("PSPP::errstr", TRUE);
94 sv_setpv (errstr, m->text);
98 sysfile_close (struct sysfile_info *sfi)
104 retval = casewriter_destroy (sfi->writer);
112 scalar_to_value (union value *val, SV *scalar, const struct variable *var)
114 if ( var_is_numeric (var))
116 if ( SvNOK (scalar) || SvIOK (scalar) )
117 val->f = SvNV (scalar);
124 const char *p = SvPV (scalar, len);
125 int width = var_get_width (var);
126 value_set_missing (val, width);
127 memcpy (value_str_rw (val, width), p, len);
133 value_to_scalar (const union value *val, const struct variable *var)
135 if ( var_is_numeric (var))
137 if ( var_is_value_missing (var, val, MV_SYSTEM))
138 return newSVpvn ("", 0);
140 return newSVnv (val->f);
144 int width = var_get_width (var);
145 return newSVpvn (value_str (val, width), width);
151 var_set_input_format (struct variable *v, input_format ip_fmt)
153 struct fmt_spec *if_copy = malloc (sizeof (*if_copy));
154 memcpy (if_copy, &ip_fmt, sizeof (ip_fmt));
155 var_attach_aux (v, if_copy, var_dtor_free);
159 make_value_from_scalar (union value *uv, SV *val, const struct variable *var)
161 value_init (uv, var_get_width (var));
162 scalar_to_value (uv, val, var);
168 MODULE = PSPP PACKAGE = PSPP
176 /* Check that the version is correct up to the length of 'ver'.
177 This allows PSPP autobuilders to add a "-build#" suffix to the
178 PSPP version without causing failures here. */
179 assert (0 == strncmp (ver, bare_version, strlen (ver)));
182 msg_set_handler (message_handler, NULL);
183 settings_init (0, 0);
187 format_value (val, var)
192 const struct fmt_spec *fmt = var_get_print_format (var);
195 make_value_from_scalar (&uv, val, var);
196 s = data_out (&uv, var_get_encoding (var), fmt);
197 value_destroy (&uv, var_get_width (var));
198 ret = newSVpv (s, fmt->w);
206 value_is_missing (val, var)
212 make_value_from_scalar (&uv, val, var);
213 ret = var_is_value_missing (var, &uv, MV_ANY);
214 value_destroy (&uv, var_get_width (var));
221 MODULE = PSPP PACKAGE = PSPP::Dict
226 RETVAL = dict_create ();
233 struct dictionary *dict
240 struct dictionary *dict
242 RETVAL = dict_get_var_cnt (dict);
247 set_label (dict, label)
248 struct dictionary *dict
251 dict_set_label (dict, label);
254 set_documents (dict, docs)
255 struct dictionary *dict
258 dict_set_documents_string (dict, docs);
262 add_document (dict, doc)
263 struct dictionary *dict
266 dict_add_document_line (dict, doc, false);
270 clear_documents (dict)
271 struct dictionary *dict
273 dict_clear_documents (dict);
277 set_weight (dict, var)
278 struct dictionary *dict
281 dict_set_weight (dict, var);
285 pxs_get_variable (dict, idx)
286 struct dictionary *dict
289 SV *errstr = get_sv("PSPP::errstr", TRUE);
290 sv_setpv (errstr, "");
291 if ( SvIV (idx) >= dict_get_var_cnt (dict))
293 sv_setpv (errstr, "The dictionary doesn't have that many variables.");
297 RETVAL = dict_get_var (dict, SvIV (idx));
303 pxs_get_var_by_name (dict, name)
304 struct dictionary *dict
307 SV *errstr = get_sv("PSPP::errstr", TRUE);
308 sv_setpv (errstr, "");
310 struct variable *var = dict_lookup_var (dict, name);
312 sv_setpv (errstr, "No such variable.");
318 MODULE = PSPP PACKAGE = PSPP::Var
322 pxs_dict_create_var (dict, name, ip_fmt)
323 struct dictionary * dict
327 SV *errstr = get_sv("PSPP::errstr", TRUE);
328 sv_setpv (errstr, "");
329 if ( ! id_is_plausible (name, false))
331 sv_setpv (errstr, "The variable name is not valid.");
335 struct fmt_spec op_fmt;
338 op_fmt = fmt_for_output_from_input (&ip_fmt);
339 v = dict_create_var (dict, name,
340 fmt_is_string (op_fmt.type) ? op_fmt.w : 0);
343 sv_setpv (errstr, "The variable could not be created (probably already exists).");
346 var_set_both_formats (v, &op_fmt);
347 var_set_input_format (v, ip_fmt);
354 set_missing_values (var, v1, ...)
355 struct variable *var;
362 croak ("No more than 3 missing values are permitted");
364 for (i = 0; i < items - 1; ++i)
365 scalar_to_value (&val[i], ST(i+1), var);
367 struct missing_values mv;
368 mv_init (&mv, var_get_width (var));
369 for (i = 0 ; i < items - 1; ++i )
370 mv_add_value (&mv, &val[i]);
371 var_set_missing_values (var, &mv);
375 set_label (var, label)
376 struct variable *var;
379 var_set_label (var, label, NULL, false);
383 clear_value_labels (var)
384 struct variable *var;
386 var_clear_value_labels (var);
389 get_write_format (var)
392 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
393 const struct fmt_spec *fmt = var_get_write_format (var);
395 hv_store (fmthash, "fmt", 3, newSVnv (fmt->type), 0);
396 hv_store (fmthash, "decimals", 8, newSVnv (fmt->d), 0);
397 hv_store (fmthash, "width", 5, newSVnv (fmt->w), 0);
399 RETVAL = newRV ((SV *) fmthash);
404 get_print_format (var)
407 HV *fmthash = (HV *) sv_2mortal ((SV *) newHV());
408 const struct fmt_spec *fmt = var_get_print_format (var);
410 hv_store (fmthash, "fmt", 3, newSVnv (fmt->type), 0);
411 hv_store (fmthash, "decimals", 8, newSVnv (fmt->d), 0);
412 hv_store (fmthash, "width", 5, newSVnv (fmt->w), 0);
414 RETVAL = newRV ((SV *) fmthash);
420 pxs_set_write_format (var, fmt)
424 var_set_write_format (var, &fmt);
428 pxs_set_print_format (var, fmt)
432 var_set_print_format (var, &fmt);
435 pxs_set_output_format (var, fmt)
439 var_set_both_formats (var, &fmt);
443 add_value_label (var, key, label)
448 SV *errstr = get_sv("PSPP::errstr", TRUE);
449 sv_setpv (errstr, "");
451 union value the_value;
452 int width = var_get_width (var);
455 value_init (&the_value, width);
456 if ( var_is_numeric (var))
458 if ( ! looks_like_number (key))
460 sv_setpv (errstr, "Cannot add label with string key to a numeric variable");
461 value_destroy (&the_value, width);
464 the_value.f = SvNV (key);
468 value_copy_str_rpad (&the_value, width, SvPV_nolen(key), ' ');
470 ok = var_add_value_label (var, &the_value, label);
471 value_destroy (&the_value, width);
474 sv_setpv (errstr, "Something went wrong");
484 HV *attrhash = (HV *) sv_2mortal ((SV *) newHV());
486 struct attrset *as = var_get_attributes (var);
490 struct attrset_iterator iter;
491 struct attribute *attr;
493 for (attr = attrset_first (as, &iter);
495 attr = attrset_next (as, &iter))
498 const char *name = attribute_get_name (attr);
500 AV *values = newAV ();
502 for (i = 0 ; i < attribute_get_n_values (attr); ++i )
504 const char *value = attribute_get_value (attr, i);
505 av_push (values, newSVpv (value, 0));
508 hv_store (attrhash, name, strlen (name),
509 newRV_noinc ((SV*) values), 0);
513 RETVAL = newRV ((SV *) attrhash);
520 struct variable * var
522 RETVAL = var_get_name (var);
529 struct variable * var
531 RETVAL = var_get_label (var);
537 get_value_labels (var)
540 HV *labelhash = (HV *) sv_2mortal ((SV *) newHV());
541 const struct val_lab *vl;
542 struct val_labs_iterator *viter = NULL;
543 const struct val_labs *labels = var_get_value_labels (var);
547 for (vl = val_labs_first (labels);
549 vl = val_labs_next (labels, vl))
551 SV *sv = value_to_scalar (&vl->value, var);
553 const char *s = SvPV (sv, len);
554 hv_store (labelhash, s, len, newSVpv (val_lab_get_label (vl), 0), 0);
558 RETVAL = newRV ((SV *) labelhash);
564 MODULE = PSPP PACKAGE = PSPP::Sysfile
567 struct sysfile_info *
568 pxs_create_sysfile (name, dict_ref, opts_hr)
573 SV *dict_sv = SvRV (dict_ref);
574 struct dictionary *dict = (void *) SvIV (dict_sv);
575 struct sfm_write_options opts;
576 if (!SvROK (opts_hr))
578 opts = sfm_writer_default_options ();
582 HV *opt_h = (HV *) SvRV (opts_hr);
583 SV** readonly = hv_fetch(opt_h, "readonly", 8, 0);
584 SV** compress = hv_fetch(opt_h, "compress", 8, 0);
585 SV** version = hv_fetch(opt_h, "version", 7, 0);
587 opts.create_writeable = readonly ? ! SvIV (*readonly) : true;
588 opts.compress = compress ? SvIV (*compress) : false;
589 opts.version = version ? SvIV (*version) : 3 ;
592 struct file_handle *fh =
593 fh_create_file (NULL, name, fh_default_properties () );
594 struct sysfile_info *sfi = xmalloc (sizeof (*sfi));
595 dict_set_encoding (dict, UTF8);
596 sfi->writer = sfm_open_writer (fh, dict, opts);
599 sfi->dict_sv = dict_sv;
600 SvREFCNT_inc (sfi->dict_sv);
608 struct sysfile_info *sfi
610 RETVAL = sysfile_close (sfi);
616 struct sysfile_info *sfi
619 SvREFCNT_dec (sfi->dict_sv);
623 append_case (sfi, ccase)
624 struct sysfile_info *sfi
627 SV *errstr = get_sv("PSPP::errstr", TRUE);
628 sv_setpv (errstr, "");
629 if ( (!SvROK(ccase)))
635 AV *av_case = (AV*) SvRV (ccase);
637 const struct variable **vv;
642 if ( av_len (av_case) >= dict_get_var_cnt (sfi->dict))
645 c = case_create (dict_get_proto (sfi->dict));
647 dict_get_vars (sfi->dict, &vv, &nv, 1u << DC_ORDINARY | 1u << DC_SYSTEM);
649 for (sv = av_shift (av_case); SvOK (sv); sv = av_shift (av_case))
651 const struct variable *v = vv[i++];
652 const struct fmt_spec *ifmt = var_get_aux (v);
654 /* If an input format has been set, then use it.
655 Otherwise just convert the raw value.
659 struct substring ss = ss_cstr (SvPV_nolen (sv));
663 error = data_in (ss, SvUTF8(sv) ? UTF8: "iso-8859-1", ifmt->type,
664 case_data_rw (c, v), var_get_width (v),
665 dict_get_encoding (sfi->dict));
677 scalar_to_value (case_data_rw (c, v), sv, v);
681 /* The remaining variables must be sysmis or blank string */
682 while (i < dict_get_var_cnt (sfi->dict))
684 const struct variable *v = vv[i++];
685 union value *val = case_data_rw (c, v);
686 value_set_missing (val, var_get_width (v));
688 casewriter_write (sfi->writer, c);
698 MODULE = PSPP PACKAGE = PSPP::Reader
700 struct sysreader_info *
701 pxs_open_sysfile (name)
704 struct casereader *reader;
705 struct sysreader_info *sri = NULL;
706 struct file_handle *fh =
707 fh_create_file (NULL, name, fh_default_properties () );
709 sri = xmalloc (sizeof (*sri));
710 sri->reader = sfm_open_reader (fh, &sri->dict, &sri->opts);
712 if ( NULL == sri->reader)
724 pxs_get_dict (reader)
725 struct sysreader_info *reader;
727 RETVAL = reader->dict;
734 struct sysreader_info *sfr;
738 if (c = casereader_read (sfr->reader))
742 EXTEND (SP, dict_get_var_cnt (sfr->dict));
743 for (v = 0; v < dict_get_var_cnt (sfr->dict); ++v )
745 const struct variable *var = dict_get_var (sfr->dict, v);
746 const union value *val = case_data (c, var);
748 PUSHs (sv_2mortal (value_to_scalar (val, var)));