1 /* PSPP - computes sample statistics.
2 Copyright (C) 2007, 2008 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/variable.h>
35 #include <data/file-handle-def.h>
36 #include <data/sys-file-writer.h>
37 #include <data/sys-file-reader.h>
38 #include <data/value.h>
39 #include <data/value-labels.h>
40 #include <data/format.h>
41 #include <data/data-in.h>
44 typedef struct fmt_spec input_format ;
45 typedef struct fmt_spec output_format ;
48 /* A thin wrapper around sfm_writer */
53 /* A pointer to the writer. The writer is owned by the struct */
54 struct casewriter *writer;
56 /* A pointer to the dictionary. Owned externally */
57 const struct dictionary *dict;
59 /* The scalar containing the dictionary */
64 /* A thin wrapper around sfm_reader */
67 struct sfm_read_info opts;
69 /* A pointer to the reader. The reader is owned by the struct */
70 struct casereader *reader;
72 /* A pointer to the dictionary. */
73 struct dictionary *dict;
78 /* A message handler which writes messages to PSPP::errstr */
80 message_handler (const struct msg *m)
82 SV *errstr = get_sv("PSPP::errstr", TRUE);
83 sv_setpv (errstr, m->text);
87 sysfile_close (struct sysfile_info *sfi)
93 retval = casewriter_destroy (sfi->writer);
101 scalar_to_value (union value *val, SV *scalar, const struct variable *var)
103 if ( var_is_numeric (var))
105 if ( SvNOK (scalar) || SvIOK (scalar) )
106 val->f = SvNV (scalar);
113 const char *p = SvPV (scalar, len);
114 memset (val->s, ' ', var_get_width (var));
115 memcpy (val->s, p, len);
121 value_to_scalar (const union value *val, const struct variable *var)
123 if ( var_is_numeric (var))
125 if ( var_is_value_missing (var, val, MV_SYSTEM))
126 return newSVpvn ("", 0);
128 return newSVnv (val->f);
131 return newSVpvn (val->s, var_get_width (var));
136 var_set_input_format (struct variable *v, input_format ip_fmt)
138 struct fmt_spec *if_copy = malloc (sizeof (*if_copy));
139 memcpy (if_copy, &ip_fmt, sizeof (ip_fmt));
140 var_attach_aux (v, if_copy, var_dtor_free);
144 make_value_from_scalar (SV *val, const struct variable *var)
146 union value *uv = value_create (var_get_width (var));
147 scalar_to_value (uv, val, var);
154 MODULE = PSPP PACKAGE = PSPP
160 assert (0 == strcmp (ver, bare_version));
161 msg_init (NULL, message_handler);
162 settings_init (0, 0);
166 format_value (val, var)
171 const struct fmt_spec *fmt = var_get_print_format (var);
172 union value *uv = make_value_from_scalar (val, var);
175 memset (s, '\0', fmt->w);
176 data_out (uv, fmt, s);
178 ret = newSVpv (s, fmt->w);
186 value_is_missing (val, var)
190 union value *uv = make_value_from_scalar (val, var);
191 int ret = var_is_value_missing (var, uv, MV_ANY);
199 MODULE = PSPP PACKAGE = PSPP::Dict
204 RETVAL = dict_create ();
211 struct dictionary *dict
218 struct dictionary *dict
220 RETVAL = dict_get_var_cnt (dict);
225 set_label (dict, label)
226 struct dictionary *dict
229 dict_set_label (dict, label);
232 set_documents (dict, docs)
233 struct dictionary *dict
236 dict_set_documents (dict, docs);
240 add_document (dict, doc)
241 struct dictionary *dict
244 dict_add_document_line (dict, doc);
248 clear_documents (dict)
249 struct dictionary *dict
251 dict_clear_documents (dict);
255 set_weight (dict, var)
256 struct dictionary *dict
259 dict_set_weight (dict, var);
263 pxs_get_variable (dict, idx)
264 struct dictionary *dict
267 SV *errstr = get_sv("PSPP::errstr", TRUE);
268 sv_setpv (errstr, "");
269 if ( SvIV (idx) >= dict_get_var_cnt (dict))
271 sv_setpv (errstr, "The dictionary doesn't have that many variables.");
275 RETVAL = dict_get_var (dict, SvIV (idx));
281 pxs_get_var_by_name (dict, name)
282 struct dictionary *dict
285 SV *errstr = get_sv("PSPP::errstr", TRUE);
286 sv_setpv (errstr, "");
288 struct variable *var = dict_lookup_var (dict, name);
290 sv_setpv (errstr, "No such variable.");
296 MODULE = PSPP PACKAGE = PSPP::Var
300 pxs_dict_create_var (dict, name, ip_fmt)
301 struct dictionary * dict
305 SV *errstr = get_sv("PSPP::errstr", TRUE);
306 sv_setpv (errstr, "");
307 if ( ! var_is_plausible_name (name, false))
309 sv_setpv (errstr, "The variable name is not valid.");
313 struct fmt_spec op_fmt;
316 op_fmt = fmt_for_output_from_input (&ip_fmt);
317 v = dict_create_var (dict, name,
318 fmt_is_string (op_fmt.type) ? op_fmt.w : 0);
321 sv_setpv (errstr, "The variable could not be created (probably already exists).");
324 var_set_both_formats (v, &op_fmt);
325 var_set_input_format (v, ip_fmt);
332 set_missing_values (var, v1, ...)
333 struct variable *var;
340 croak ("No more than 3 missing values are permitted");
342 for (i = 0; i < items - 1; ++i)
343 scalar_to_value (&val[i], ST(i+1), var);
345 struct missing_values mv;
346 mv_init (&mv, var_get_width (var));
347 for (i = 0 ; i < items - 1; ++i )
348 mv_add_value (&mv, &val[i]);
349 var_set_missing_values (var, &mv);
353 set_label (var, label)
354 struct variable *var;
357 var_set_label (var, label);
361 clear_value_labels (var)
362 struct variable *var;
364 var_clear_value_labels (var);
367 pxs_set_write_format (var, fmt)
371 var_set_write_format (var, &fmt);
375 pxs_set_print_format (var, fmt)
379 var_set_print_format (var, &fmt);
382 pxs_set_output_format (var, fmt)
386 var_set_both_formats (var, &fmt);
390 add_value_label (var, key, label)
395 SV *errstr = get_sv("PSPP::errstr", TRUE);
396 sv_setpv (errstr, "");
398 union value the_value;
400 if ( var_is_numeric (var))
402 if ( ! looks_like_number (key))
404 sv_setpv (errstr, "Cannot add label with string key to a numeric variable");
407 the_value.f = SvNV (key);
411 if ( var_is_long_string (var) )
413 sv_setpv (errstr, "Cannot add label to a long string variable");
416 strncpy (the_value.s, SvPV_nolen(key), MAX_SHORT_STRING);
418 if (! var_add_value_label (var, &the_value, label) )
420 sv_setpv (errstr, "Something went wrong");
429 struct variable * var
431 RETVAL = var_get_name (var);
438 struct variable * var
440 RETVAL = var_get_label (var);
446 get_value_labels (var)
449 HV *labelhash = (HV *) sv_2mortal ((SV *) newHV());
451 struct val_labs_iterator *viter = NULL;
452 const struct val_labs *labels = var_get_value_labels (var);
456 for (vl = val_labs_first (labels, &viter);
458 vl = val_labs_next (labels, &viter))
460 SV *sv = value_to_scalar (&vl->value, var);
462 const char *s = SvPV (sv, len);
463 hv_store (labelhash, s, len, newSVpv (vl->label, 0), 0);
467 RETVAL = newRV ((SV *) labelhash);
473 MODULE = PSPP PACKAGE = PSPP::Sysfile
476 struct sysfile_info *
477 pxs_create_sysfile (name, dict_ref, opts_hr)
482 SV *dict_sv = SvRV (dict_ref);
483 struct dictionary *dict = (void *) SvIV (dict_sv);
484 struct sfm_write_options opts;
485 if (!SvROK (opts_hr))
487 opts = sfm_writer_default_options ();
491 HV *opt_h = (HV *) SvRV (opts_hr);
492 SV** readonly = hv_fetch(opt_h, "readonly", 8, 0);
493 SV** compress = hv_fetch(opt_h, "compress", 8, 0);
494 SV** version = hv_fetch(opt_h, "version", 7, 0);
496 opts.create_writeable = readonly ? ! SvIV (*readonly) : true;
497 opts.compress = compress ? SvIV (*compress) : false;
498 opts.version = version ? SvIV (*version) : 3 ;
501 struct file_handle *fh =
502 fh_create_file (NULL, name, fh_default_properties () );
503 struct sysfile_info *sfi = xmalloc (sizeof (*sfi));
504 sfi->writer = sfm_open_writer (fh, dict, opts);
507 sfi->dict_sv = dict_sv;
508 SvREFCNT_inc (sfi->dict_sv);
516 struct sysfile_info *sfi
518 RETVAL = sysfile_close (sfi);
524 struct sysfile_info *sfi
527 SvREFCNT_dec (sfi->dict_sv);
531 append_case (sfi, ccase)
532 struct sysfile_info *sfi
535 SV *errstr = get_sv("PSPP::errstr", TRUE);
536 sv_setpv (errstr, "");
537 if ( (!SvROK(ccase)))
543 AV *av_case = (AV*) SvRV (ccase);
545 const struct variable **vv;
550 if ( av_len (av_case) >= dict_get_var_cnt (sfi->dict))
553 c = case_create (dict_get_next_value_idx (sfi->dict));
555 dict_get_vars (sfi->dict, &vv, &nv, 1u << DC_ORDINARY | 1u << DC_SYSTEM);
557 for (sv = av_shift (av_case); SvOK (sv); sv = av_shift (av_case))
559 const struct variable *v = vv[i++];
560 const struct fmt_spec *ifmt = var_get_aux (v);
562 /* If an input format has been set, then use it.
563 Otherwise just convert the raw value.
567 struct substring ss = ss_cstr (SvPV_nolen (sv));
568 if ( ! data_in (ss, LEGACY_NATIVE, ifmt->type, 0, 0, 0,
578 scalar_to_value (case_data_rw (c, v), sv, v);
582 /* The remaining variables must be sysmis or blank string */
583 while (i < dict_get_var_cnt (sfi->dict))
585 const struct variable *v = vv[i++];
586 union value *val = case_data_rw (c, v);
587 if ( var_is_numeric (v))
590 memset (val->s, ' ', var_get_width (v));
592 RETVAL = casewriter_write (sfi->writer, c);
601 MODULE = PSPP PACKAGE = PSPP::Reader
603 struct sysreader_info *
604 pxs_open_sysfile (name)
607 struct casereader *reader;
608 struct sysreader_info *sri = NULL;
609 struct file_handle *fh =
610 fh_create_file (NULL, name, fh_default_properties () );
612 sri = xmalloc (sizeof (*sri));
613 sri->reader = sfm_open_reader (fh, &sri->dict, &sri->opts);
615 if ( NULL == sri->reader)
627 pxs_get_dict (reader)
628 struct sysreader_info *reader;
630 RETVAL = reader->dict;
637 struct sysreader_info *sfr;
641 if (! (c = casereader_read (sfr->reader)))
648 AV *av_case = (AV *) sv_2mortal ((SV *) newAV());
650 for (v = 0; v < dict_get_var_cnt (sfr->dict); ++v )
652 const struct variable *var = dict_get_var (sfr->dict, v);
653 const union value *val = case_data (c, var);
655 av_push (av_case, value_to_scalar (val, var));
659 RETVAL = newRV ((SV *) av_case);