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;
61 /* A thin wrapper around sfm_reader */
64 struct sfm_read_info opts;
66 /* A pointer to the reader. The reader is owned by the struct */
67 struct casereader *reader;
69 /* A pointer to the dictionary. */
70 struct dictionary *dict;
75 /* A message handler which writes messages to PSPP::errstr */
77 message_handler (const struct msg *m)
79 SV *errstr = get_sv("PSPP::errstr", TRUE);
80 sv_setpv (errstr, m->text);
84 sysfile_close (struct sysfile_info *sfi)
90 retval = casewriter_destroy (sfi->writer);
98 scalar_to_value (union value *val, SV *scalar, const struct variable *var)
100 if ( var_is_numeric (var))
102 if ( SvNOK (scalar) || SvIOK (scalar) )
103 val->f = SvNV (scalar);
110 const char *p = SvPV (scalar, len);
111 memset (val->s, ' ', var_get_width (var));
112 memcpy (val->s, p, len);
118 value_to_scalar (const union value *val, const struct variable *var)
120 if ( var_is_numeric (var))
122 if ( var_is_value_missing (var, val, MV_SYSTEM))
123 return newSVpvn ("", 0);
125 return newSVnv (val->f);
128 return newSVpvn (val->s, var_get_width (var));
133 var_set_input_format (struct variable *v, input_format ip_fmt)
135 struct fmt_spec *if_copy = malloc (sizeof (*if_copy));
136 memcpy (if_copy, &ip_fmt, sizeof (ip_fmt));
137 var_attach_aux (v, if_copy, var_dtor_free);
141 make_value_from_scalar (SV *val, const struct variable *var)
143 union value *uv = value_create (var_get_width (var));
144 scalar_to_value (uv, val, var);
151 MODULE = PSPP PACKAGE = PSPP
157 assert (0 == strcmp (ver, bare_version));
158 msg_init (NULL, message_handler);
159 settings_init (0, 0);
163 format_value (val, var)
168 const struct fmt_spec *fmt = var_get_print_format (var);
169 union value *uv = make_value_from_scalar (val, var);
172 memset (s, '\0', fmt->w);
173 data_out (uv, fmt, s);
175 ret = newSVpv (s, fmt->w);
183 value_is_missing (val, var)
187 union value *uv = make_value_from_scalar (val, var);
188 int ret = var_is_value_missing (var, uv, MV_ANY);
196 MODULE = PSPP PACKAGE = PSPP::Dict
201 RETVAL = dict_create ();
208 struct dictionary *dict
215 struct dictionary *dict
217 RETVAL = dict_get_var_cnt (dict);
222 set_label (dict, label)
223 struct dictionary *dict
226 dict_set_label (dict, label);
229 set_documents (dict, docs)
230 struct dictionary *dict
233 dict_set_documents (dict, docs);
237 add_document (dict, doc)
238 struct dictionary *dict
241 dict_add_document_line (dict, doc);
245 clear_documents (dict)
246 struct dictionary *dict
248 dict_clear_documents (dict);
252 set_weight (dict, var)
253 struct dictionary *dict
256 dict_set_weight (dict, var);
260 pxs_get_variable (dict, idx)
261 struct dictionary *dict
264 SV *errstr = get_sv("PSPP::errstr", TRUE);
265 sv_setpv (errstr, "");
266 if ( SvIV (idx) >= dict_get_var_cnt (dict))
268 sv_setpv (errstr, "The dictionary doesn't have that many variables.");
272 RETVAL = dict_get_var (dict, SvIV (idx));
278 pxs_get_var_by_name (dict, name)
279 struct dictionary *dict
282 SV *errstr = get_sv("PSPP::errstr", TRUE);
283 sv_setpv (errstr, "");
285 struct variable *var = dict_lookup_var (dict, name);
287 sv_setpv (errstr, "No such variable.");
293 MODULE = PSPP PACKAGE = PSPP::Var
297 pxs_dict_create_var (dict, name, ip_fmt)
298 struct dictionary * dict
302 SV *errstr = get_sv("PSPP::errstr", TRUE);
303 sv_setpv (errstr, "");
304 if ( ! var_is_plausible_name (name, false))
306 sv_setpv (errstr, "The variable name is not valid.");
310 struct fmt_spec op_fmt;
313 op_fmt = fmt_for_output_from_input (&ip_fmt);
314 v = dict_create_var (dict, name,
315 fmt_is_string (op_fmt.type) ? op_fmt.w : 0);
318 sv_setpv (errstr, "The variable could not be created (probably already exists).");
321 var_set_both_formats (v, &op_fmt);
322 var_set_input_format (v, ip_fmt);
329 set_missing_values (var, v1, ...)
330 struct variable *var;
337 croak ("No more than 3 missing values are permitted");
339 for (i = 0; i < items - 1; ++i)
340 scalar_to_value (&val[i], ST(i+1), var);
342 struct missing_values mv;
343 mv_init (&mv, var_get_width (var));
344 for (i = 0 ; i < items - 1; ++i )
345 mv_add_value (&mv, &val[i]);
346 var_set_missing_values (var, &mv);
350 set_label (var, label)
351 struct variable *var;
354 var_set_label (var, label);
358 clear_value_labels (var)
359 struct variable *var;
361 var_clear_value_labels (var);
364 pxs_set_write_format (var, fmt)
368 var_set_write_format (var, &fmt);
372 pxs_set_print_format (var, fmt)
376 var_set_print_format (var, &fmt);
379 pxs_set_output_format (var, fmt)
383 var_set_both_formats (var, &fmt);
387 add_value_label (var, key, label)
392 SV *errstr = get_sv("PSPP::errstr", TRUE);
393 sv_setpv (errstr, "");
395 union value the_value;
397 if ( var_is_numeric (var))
399 if ( ! looks_like_number (key))
401 sv_setpv (errstr, "Cannot add label with string key to a numeric variable");
404 the_value.f = SvNV (key);
408 if ( var_is_long_string (var) )
410 sv_setpv (errstr, "Cannot add label to a long string variable");
413 strncpy (the_value.s, SvPV_nolen(key), MAX_SHORT_STRING);
415 if (! var_add_value_label (var, &the_value, label) )
417 sv_setpv (errstr, "Something went wrong");
426 struct variable * var
428 RETVAL = var_get_name (var);
435 struct variable * var
437 RETVAL = var_get_label (var);
443 get_value_labels (var)
446 HV *labelhash = (HV *) sv_2mortal ((SV *) newHV());
448 struct val_labs_iterator *viter = NULL;
449 const struct val_labs *labels = var_get_value_labels (var);
453 for (vl = val_labs_first (labels, &viter);
455 vl = val_labs_next (labels, &viter))
457 SV *sv = value_to_scalar (&vl->value, var);
459 const char *s = SvPV (sv, len);
460 hv_store (labelhash, s, len, newSVpv (vl->label, 0), 0);
464 RETVAL = newRV ((SV *) labelhash);
470 MODULE = PSPP PACKAGE = PSPP::Sysfile
473 struct sysfile_info *
474 pxs_create_sysfile (name, dict, opts_hr)
476 struct dictionary *dict
479 struct sfm_write_options opts;
480 if (!SvROK (opts_hr))
482 opts = sfm_writer_default_options ();
486 HV *opt_h = (HV *) SvRV (opts_hr);
487 SV** readonly = hv_fetch(opt_h, "readonly", 8, 0);
488 SV** compress = hv_fetch(opt_h, "compress", 8, 0);
489 SV** version = hv_fetch(opt_h, "version", 7, 0);
491 opts.create_writeable = readonly ? ! SvIV (*readonly) : true;
492 opts.compress = compress ? SvIV (*compress) : false;
493 opts.version = version ? SvIV (*version) : 3 ;
496 struct file_handle *fh =
497 fh_create_file (NULL, name, fh_default_properties () );
498 struct sysfile_info *sfi = xmalloc (sizeof (*sfi));
499 sfi->writer = sfm_open_writer (fh, dict, opts);
509 struct sysfile_info *sfi
511 RETVAL = sysfile_close (sfi);
517 struct sysfile_info *sfi
523 append_case (sfi, ccase)
524 struct sysfile_info *sfi
527 SV *errstr = get_sv("PSPP::errstr", TRUE);
528 sv_setpv (errstr, "");
529 if ( (!SvROK(ccase)))
535 AV *av_case = (AV*) SvRV (ccase);
537 const struct variable **vv;
542 if ( av_len (av_case) >= dict_get_var_cnt (sfi->dict))
545 case_create (&c, dict_get_next_value_idx (sfi->dict));
547 dict_get_vars (sfi->dict, &vv, &nv, 1u << DC_ORDINARY | 1u << DC_SYSTEM);
549 for (sv = av_shift (av_case); SvOK (sv); sv = av_shift (av_case))
551 const struct variable *v = vv[i++];
552 const struct fmt_spec *ifmt = var_get_aux (v);
554 /* If an input format has been set, then use it.
555 Otherwise just convert the raw value.
559 struct substring ss = ss_cstr (SvPV_nolen (sv));
560 if ( ! data_in (ss, LEGACY_NATIVE, ifmt->type, 0, 0, 0,
561 case_data_rw (&c, v),
570 scalar_to_value (case_data_rw (&c, v), sv, v);
574 /* The remaining variables must be sysmis or blank string */
575 while (i < dict_get_var_cnt (sfi->dict))
577 const struct variable *v = vv[i++];
578 union value *val = case_data_rw (&c, v);
579 if ( var_is_numeric (v))
582 memset (val->s, ' ', var_get_width (v));
584 RETVAL = casewriter_write (sfi->writer, &c);
586 // case_destroy (&c);
594 MODULE = PSPP PACKAGE = PSPP::Reader
596 struct sysreader_info *
597 pxs_open_sysfile (name)
600 struct casereader *reader;
601 struct sysreader_info *sri = NULL;
602 struct file_handle *fh =
603 fh_create_file (NULL, name, fh_default_properties () );
605 sri = xmalloc (sizeof (*sri));
606 sri->reader = sfm_open_reader (fh, &sri->dict, &sri->opts);
608 if ( NULL == sri->reader)
620 pxs_get_dict (reader)
621 struct sysreader_info *reader;
623 RETVAL = reader->dict;
630 struct sysreader_info *sfr;
634 if (! casereader_read (sfr->reader, &c))
641 AV *av_case = (AV *) sv_2mortal ((SV *) newAV());
643 for (v = 0; v < dict_get_var_cnt (sfr->dict); ++v )
645 const struct variable *var = dict_get_var (sfr->dict, v);
646 const union value *val = case_data (&c, var);
648 av_push (av_case, value_to_scalar (val, var));
652 RETVAL = newRV ((SV *) av_case);