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));
276 MODULE = PSPP PACKAGE = PSPP::Var
280 pxs_dict_create_var (dict, name, ip_fmt)
281 struct dictionary * dict
285 SV *errstr = get_sv("PSPP::errstr", TRUE);
286 sv_setpv (errstr, "");
287 if ( ! var_is_plausible_name (name, false))
289 sv_setpv (errstr, "The variable name is not valid.");
293 struct fmt_spec op_fmt;
296 op_fmt = fmt_for_output_from_input (&ip_fmt);
297 v = dict_create_var (dict, name,
298 fmt_is_string (op_fmt.type) ? op_fmt.w : 0);
301 sv_setpv (errstr, "The variable could not be created (probably already exists).");
304 var_set_both_formats (v, &op_fmt);
305 var_set_input_format (v, ip_fmt);
312 set_missing_values (var, v1, ...)
313 struct variable *var;
320 croak ("No more than 3 missing values are permitted");
322 for (i = 0; i < items - 1; ++i)
323 scalar_to_value (&val[i], ST(i+1), var);
325 struct missing_values mv;
326 mv_init (&mv, var_get_width (var));
327 for (i = 0 ; i < items - 1; ++i )
328 mv_add_value (&mv, &val[i]);
329 var_set_missing_values (var, &mv);
333 set_label (var, label)
334 struct variable *var;
337 var_set_label (var, label);
341 clear_value_labels (var)
342 struct variable *var;
344 var_clear_value_labels (var);
347 pxs_set_write_format (var, fmt)
351 var_set_write_format (var, &fmt);
355 pxs_set_print_format (var, fmt)
359 var_set_print_format (var, &fmt);
362 pxs_set_output_format (var, fmt)
366 var_set_both_formats (var, &fmt);
370 add_value_label (var, key, label)
375 SV *errstr = get_sv("PSPP::errstr", TRUE);
376 sv_setpv (errstr, "");
378 union value the_value;
380 if ( var_is_numeric (var))
382 if ( ! looks_like_number (key))
384 sv_setpv (errstr, "Cannot add label with string key to a numeric variable");
387 the_value.f = SvNV (key);
391 if ( var_is_long_string (var) )
393 sv_setpv (errstr, "Cannot add label to a long string variable");
396 strncpy (the_value.s, SvPV_nolen(key), MAX_SHORT_STRING);
398 if (! var_add_value_label (var, &the_value, label) )
400 sv_setpv (errstr, "Something went wrong");
409 struct variable * var
411 RETVAL = var_get_name (var);
418 struct variable * var
420 RETVAL = var_get_label (var);
426 get_value_labels (var)
429 HV *labelhash = (HV *) sv_2mortal ((SV *) newHV());
431 struct val_labs_iterator *viter = NULL;
432 const struct val_labs *labels = var_get_value_labels (var);
436 for (vl = val_labs_first (labels, &viter);
438 vl = val_labs_next (labels, &viter))
440 SV *sv = value_to_scalar (&vl->value, var);
442 const char *s = SvPV (sv, len);
443 hv_store (labelhash, s, len, newSVpv (vl->label, 0), 0);
447 RETVAL = newRV ((SV *) labelhash);
453 MODULE = PSPP PACKAGE = PSPP::Sysfile
456 struct sysfile_info *
457 pxs_create_sysfile (name, dict, opts_hr)
459 struct dictionary *dict
462 struct sfm_write_options opts;
463 if (!SvROK (opts_hr))
465 opts = sfm_writer_default_options ();
469 HV *opt_h = (HV *) SvRV (opts_hr);
470 SV** readonly = hv_fetch(opt_h, "readonly", 8, 0);
471 SV** compress = hv_fetch(opt_h, "compress", 8, 0);
472 SV** version = hv_fetch(opt_h, "version", 7, 0);
474 opts.create_writeable = readonly ? ! SvIV (*readonly) : true;
475 opts.compress = compress ? SvIV (*compress) : false;
476 opts.version = version ? SvIV (*version) : 3 ;
479 struct file_handle *fh =
480 fh_create_file (NULL, name, fh_default_properties () );
481 struct sysfile_info *sfi = xmalloc (sizeof (*sfi));
482 sfi->writer = sfm_open_writer (fh, dict, opts);
492 struct sysfile_info *sfi
494 RETVAL = sysfile_close (sfi);
500 struct sysfile_info *sfi
506 append_case (sfi, ccase)
507 struct sysfile_info *sfi
510 SV *errstr = get_sv("PSPP::errstr", TRUE);
511 sv_setpv (errstr, "");
512 if ( (!SvROK(ccase)))
518 AV *av_case = (AV*) SvRV (ccase);
520 const struct variable **vv;
525 if ( av_len (av_case) >= dict_get_var_cnt (sfi->dict))
528 case_create (&c, dict_get_next_value_idx (sfi->dict));
530 dict_get_vars (sfi->dict, &vv, &nv, 1u << DC_ORDINARY | 1u << DC_SYSTEM);
532 for (sv = av_shift (av_case); SvOK (sv); sv = av_shift (av_case))
534 const struct variable *v = vv[i++];
535 const struct fmt_spec *ifmt = var_get_aux (v);
537 /* If an input format has been set, then use it.
538 Otherwise just convert the raw value.
542 struct substring ss = ss_cstr (SvPV_nolen (sv));
543 if ( ! data_in (ss, LEGACY_NATIVE, ifmt->type, 0, 0, 0,
544 case_data_rw (&c, v),
553 scalar_to_value (case_data_rw (&c, v), sv, v);
557 /* The remaining variables must be sysmis or blank string */
558 while (i < dict_get_var_cnt (sfi->dict))
560 const struct variable *v = vv[i++];
561 union value *val = case_data_rw (&c, v);
562 if ( var_is_numeric (v))
565 memset (val->s, ' ', var_get_width (v));
567 RETVAL = casewriter_write (sfi->writer, &c);
569 // Case_destroy (&c);
577 MODULE = PSPP PACKAGE = PSPP::Reader
579 struct sysreader_info *
580 pxs_open_sysfile (name)
583 struct casereader *reader;
584 struct sysreader_info *sri = NULL;
585 struct file_handle *fh =
586 fh_create_file (NULL, name, fh_default_properties () );
588 sri = xmalloc (sizeof (*sri));
589 sri->reader = sfm_open_reader (fh, &sri->dict, &sri->opts);
591 if ( NULL == sri->reader)
603 pxs_get_dict (reader)
604 struct sysreader_info *reader;
606 RETVAL = reader->dict;
613 struct sysreader_info *sfr;
617 if (! casereader_read (sfr->reader, &c))
624 AV *av_case = (AV *) sv_2mortal ((SV *) newAV());
626 for (v = 0; v < dict_get_var_cnt (sfr->dict); ++v )
628 const struct variable *var = dict_get_var (sfr->dict, v);
629 const union value *val = case_data (&c, var);
631 av_push (av_case, value_to_scalar (val, var));
635 RETVAL = newRV ((SV *) av_case);