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 int width = var_get_width (var);
117 value_set_missing (val, width);
118 memcpy (value_str_rw (val, width), p, len);
124 value_to_scalar (const union value *val, const struct variable *var)
126 if ( var_is_numeric (var))
128 if ( var_is_value_missing (var, val, MV_SYSTEM))
129 return newSVpvn ("", 0);
131 return newSVnv (val->f);
135 int width = var_get_width (var);
136 return newSVpvn (value_str (val, width), width);
142 var_set_input_format (struct variable *v, input_format ip_fmt)
144 struct fmt_spec *if_copy = malloc (sizeof (*if_copy));
145 memcpy (if_copy, &ip_fmt, sizeof (ip_fmt));
146 var_attach_aux (v, if_copy, var_dtor_free);
150 make_value_from_scalar (union value *uv, SV *val, const struct variable *var)
152 value_init (uv, var_get_width (var));
153 scalar_to_value (uv, val, var);
159 MODULE = PSPP PACKAGE = PSPP
165 assert (0 == strcmp (ver, bare_version));
166 msg_init (NULL, message_handler);
167 settings_init (0, 0);
171 format_value (val, var)
176 const struct fmt_spec *fmt = var_get_print_format (var);
179 make_value_from_scalar (&uv, val, var);
181 memset (s, '\0', fmt->w);
182 data_out (&uv, fmt, s);
183 value_destroy (&uv, var_get_width (var));
184 ret = newSVpv (s, fmt->w);
192 value_is_missing (val, var)
198 make_value_from_scalar (&uv, val, var);
199 ret = var_is_value_missing (var, &uv, MV_ANY);
200 value_destroy (&uv, var_get_width (var));
207 MODULE = PSPP PACKAGE = PSPP::Dict
212 RETVAL = dict_create ();
219 struct dictionary *dict
226 struct dictionary *dict
228 RETVAL = dict_get_var_cnt (dict);
233 set_label (dict, label)
234 struct dictionary *dict
237 dict_set_label (dict, label);
240 set_documents (dict, docs)
241 struct dictionary *dict
244 dict_set_documents (dict, docs);
248 add_document (dict, doc)
249 struct dictionary *dict
252 dict_add_document_line (dict, doc);
256 clear_documents (dict)
257 struct dictionary *dict
259 dict_clear_documents (dict);
263 set_weight (dict, var)
264 struct dictionary *dict
267 dict_set_weight (dict, var);
271 pxs_get_variable (dict, idx)
272 struct dictionary *dict
275 SV *errstr = get_sv("PSPP::errstr", TRUE);
276 sv_setpv (errstr, "");
277 if ( SvIV (idx) >= dict_get_var_cnt (dict))
279 sv_setpv (errstr, "The dictionary doesn't have that many variables.");
283 RETVAL = dict_get_var (dict, SvIV (idx));
289 pxs_get_var_by_name (dict, name)
290 struct dictionary *dict
293 SV *errstr = get_sv("PSPP::errstr", TRUE);
294 sv_setpv (errstr, "");
296 struct variable *var = dict_lookup_var (dict, name);
298 sv_setpv (errstr, "No such variable.");
304 MODULE = PSPP PACKAGE = PSPP::Var
308 pxs_dict_create_var (dict, name, ip_fmt)
309 struct dictionary * dict
313 SV *errstr = get_sv("PSPP::errstr", TRUE);
314 sv_setpv (errstr, "");
315 if ( ! var_is_plausible_name (name, false))
317 sv_setpv (errstr, "The variable name is not valid.");
321 struct fmt_spec op_fmt;
324 op_fmt = fmt_for_output_from_input (&ip_fmt);
325 v = dict_create_var (dict, name,
326 fmt_is_string (op_fmt.type) ? op_fmt.w : 0);
329 sv_setpv (errstr, "The variable could not be created (probably already exists).");
332 var_set_both_formats (v, &op_fmt);
333 var_set_input_format (v, ip_fmt);
340 set_missing_values (var, v1, ...)
341 struct variable *var;
348 croak ("No more than 3 missing values are permitted");
350 for (i = 0; i < items - 1; ++i)
351 scalar_to_value (&val[i], ST(i+1), var);
353 struct missing_values mv;
354 mv_init (&mv, var_get_width (var));
355 for (i = 0 ; i < items - 1; ++i )
356 mv_add_value (&mv, &val[i]);
357 var_set_missing_values (var, &mv);
361 set_label (var, label)
362 struct variable *var;
365 var_set_label (var, label);
369 clear_value_labels (var)
370 struct variable *var;
372 var_clear_value_labels (var);
375 pxs_set_write_format (var, fmt)
379 var_set_write_format (var, &fmt);
383 pxs_set_print_format (var, fmt)
387 var_set_print_format (var, &fmt);
390 pxs_set_output_format (var, fmt)
394 var_set_both_formats (var, &fmt);
398 add_value_label (var, key, label)
403 SV *errstr = get_sv("PSPP::errstr", TRUE);
404 sv_setpv (errstr, "");
406 union value the_value;
407 int width = var_get_width (var);
410 value_init (&the_value, width);
411 if ( var_is_numeric (var))
413 if ( ! looks_like_number (key))
415 sv_setpv (errstr, "Cannot add label with string key to a numeric variable");
416 value_destroy (&the_value, width);
419 the_value.f = SvNV (key);
423 value_copy_str_rpad (&the_value, width, SvPV_nolen(key), ' ');
425 ok = var_add_value_label (var, &the_value, label);
426 value_destroy (&the_value, width);
429 sv_setpv (errstr, "Something went wrong");
439 HV *attrhash = (HV *) sv_2mortal ((SV *) newHV());
441 struct attrset *as = var_get_attributes (var);
445 struct attrset_iterator iter;
446 struct attribute *attr;
448 for (attr = attrset_first (as, &iter);
450 attr = attrset_next (as, &iter))
453 const char *name = attribute_get_name (attr);
455 AV *values = newAV ();
457 for (i = 0 ; i < attribute_get_n_values (attr); ++i )
459 const char *value = attribute_get_value (attr, i);
460 av_push (values, newSVpv (value, 0));
463 hv_store (attrhash, name, strlen (name),
464 newRV_noinc ((SV*) values), 0);
468 RETVAL = newRV ((SV *) attrhash);
475 struct variable * var
477 RETVAL = var_get_name (var);
484 struct variable * var
486 RETVAL = var_get_label (var);
492 get_value_labels (var)
495 HV *labelhash = (HV *) sv_2mortal ((SV *) newHV());
496 const struct val_lab *vl;
497 struct val_labs_iterator *viter = NULL;
498 const struct val_labs *labels = var_get_value_labels (var);
502 for (vl = val_labs_first (labels);
504 vl = val_labs_next (labels, vl))
506 SV *sv = value_to_scalar (&vl->value, var);
508 const char *s = SvPV (sv, len);
509 hv_store (labelhash, s, len, newSVpv (val_lab_get_label (vl), 0), 0);
513 RETVAL = newRV ((SV *) labelhash);
519 MODULE = PSPP PACKAGE = PSPP::Sysfile
522 struct sysfile_info *
523 pxs_create_sysfile (name, dict_ref, opts_hr)
528 SV *dict_sv = SvRV (dict_ref);
529 struct dictionary *dict = (void *) SvIV (dict_sv);
530 struct sfm_write_options opts;
531 if (!SvROK (opts_hr))
533 opts = sfm_writer_default_options ();
537 HV *opt_h = (HV *) SvRV (opts_hr);
538 SV** readonly = hv_fetch(opt_h, "readonly", 8, 0);
539 SV** compress = hv_fetch(opt_h, "compress", 8, 0);
540 SV** version = hv_fetch(opt_h, "version", 7, 0);
542 opts.create_writeable = readonly ? ! SvIV (*readonly) : true;
543 opts.compress = compress ? SvIV (*compress) : false;
544 opts.version = version ? SvIV (*version) : 3 ;
547 struct file_handle *fh =
548 fh_create_file (NULL, name, fh_default_properties () );
549 struct sysfile_info *sfi = xmalloc (sizeof (*sfi));
550 sfi->writer = sfm_open_writer (fh, dict, opts);
553 sfi->dict_sv = dict_sv;
554 SvREFCNT_inc (sfi->dict_sv);
562 struct sysfile_info *sfi
564 RETVAL = sysfile_close (sfi);
570 struct sysfile_info *sfi
573 SvREFCNT_dec (sfi->dict_sv);
577 append_case (sfi, ccase)
578 struct sysfile_info *sfi
581 SV *errstr = get_sv("PSPP::errstr", TRUE);
582 sv_setpv (errstr, "");
583 if ( (!SvROK(ccase)))
589 AV *av_case = (AV*) SvRV (ccase);
591 const struct variable **vv;
596 if ( av_len (av_case) >= dict_get_var_cnt (sfi->dict))
599 c = case_create (dict_get_proto (sfi->dict));
601 dict_get_vars (sfi->dict, &vv, &nv, 1u << DC_ORDINARY | 1u << DC_SYSTEM);
603 for (sv = av_shift (av_case); SvOK (sv); sv = av_shift (av_case))
605 const struct variable *v = vv[i++];
606 const struct fmt_spec *ifmt = var_get_aux (v);
608 /* If an input format has been set, then use it.
609 Otherwise just convert the raw value.
613 struct substring ss = ss_cstr (SvPV_nolen (sv));
614 if ( ! data_in (ss, LEGACY_NATIVE, ifmt->type, 0, 0, 0,
624 scalar_to_value (case_data_rw (c, v), sv, v);
628 /* The remaining variables must be sysmis or blank string */
629 while (i < dict_get_var_cnt (sfi->dict))
631 const struct variable *v = vv[i++];
632 union value *val = case_data_rw (c, v);
633 value_set_missing (val, var_get_width (v));
635 RETVAL = casewriter_write (sfi->writer, c);
644 MODULE = PSPP PACKAGE = PSPP::Reader
646 struct sysreader_info *
647 pxs_open_sysfile (name)
650 struct casereader *reader;
651 struct sysreader_info *sri = NULL;
652 struct file_handle *fh =
653 fh_create_file (NULL, name, fh_default_properties () );
655 sri = xmalloc (sizeof (*sri));
656 sri->reader = sfm_open_reader (fh, &sri->dict, &sri->opts);
658 if ( NULL == sri->reader)
670 pxs_get_dict (reader)
671 struct sysreader_info *reader;
673 RETVAL = reader->dict;
680 struct sysreader_info *sfr;
684 if (c = casereader_read (sfr->reader))
688 EXTEND (SP, dict_get_var_cnt (sfr->dict));
689 for (v = 0; v < dict_get_var_cnt (sfr->dict); ++v )
691 const struct variable *var = dict_get_var (sfr->dict, v);
692 const union value *val = case_data (c, var);
694 PUSHs (sv_2mortal (value_to_scalar (val, var)));