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);
152 msg_init (NULL, message_handler);
153 settings_init (0, 0);
157 MODULE = PSPP PACKAGE = PSPP
160 format_value (val, var)
165 const struct fmt_spec *fmt = var_get_print_format (var);
166 union value *uv = make_value_from_scalar (val, var);
169 memset (s, '\0', fmt->w);
170 data_out (uv, fmt, s);
172 ret = newSVpv (s, fmt->w);
180 value_is_missing (val, var)
184 union value *uv = make_value_from_scalar (val, var);
185 int ret = var_is_value_missing (var, uv, MV_ANY);
193 MODULE = PSPP PACKAGE = PSPP::Dict
198 RETVAL = dict_create ();
205 struct dictionary *dict
212 struct dictionary *dict
214 RETVAL = dict_get_var_cnt (dict);
219 set_label (dict, label)
220 struct dictionary *dict
223 dict_set_label (dict, label);
226 set_documents (dict, docs)
227 struct dictionary *dict
230 dict_set_documents (dict, docs);
234 add_document (dict, doc)
235 struct dictionary *dict
238 dict_add_document_line (dict, doc);
242 clear_documents (dict)
243 struct dictionary *dict
245 dict_clear_documents (dict);
249 set_weight (dict, var)
250 struct dictionary *dict
253 dict_set_weight (dict, var);
257 pxs_get_variable (dict, idx)
258 struct dictionary *dict
261 SV *errstr = get_sv("PSPP::errstr", TRUE);
262 sv_setpv (errstr, "");
263 if ( SvIV (idx) >= dict_get_var_cnt (dict))
265 sv_setpv (errstr, "The dictionary doesn't have that many variables.");
269 RETVAL = dict_get_var (dict, SvIV (idx));
273 MODULE = PSPP PACKAGE = PSPP::Var
277 pxs_dict_create_var (dict, name, ip_fmt)
278 struct dictionary * dict
282 SV *errstr = get_sv("PSPP::errstr", TRUE);
283 sv_setpv (errstr, "");
284 if ( ! var_is_plausible_name (name, false))
286 sv_setpv (errstr, "The variable name is not valid.");
290 struct fmt_spec op_fmt;
293 op_fmt = fmt_for_output_from_input (&ip_fmt);
294 v = dict_create_var (dict, name,
295 fmt_is_string (op_fmt.type) ? op_fmt.w : 0);
298 sv_setpv (errstr, "The variable could not be created (probably already exists).");
301 var_set_both_formats (v, &op_fmt);
302 var_set_input_format (v, ip_fmt);
309 set_missing_values (var, v1, ...)
310 struct variable *var;
317 croak ("No more than 3 missing values are permitted");
319 for (i = 0; i < items - 1; ++i)
320 scalar_to_value (&val[i], ST(i+1), var);
322 struct missing_values mv;
323 mv_init (&mv, var_get_width (var));
324 for (i = 0 ; i < items - 1; ++i )
325 mv_add_value (&mv, &val[i]);
326 var_set_missing_values (var, &mv);
330 set_label (var, label)
331 struct variable *var;
334 var_set_label (var, label);
338 clear_value_labels (var)
339 struct variable *var;
341 var_clear_value_labels (var);
344 pxs_set_write_format (var, fmt)
348 var_set_write_format (var, &fmt);
352 pxs_set_print_format (var, fmt)
356 var_set_print_format (var, &fmt);
359 pxs_set_output_format (var, fmt)
363 var_set_both_formats (var, &fmt);
367 add_value_label (var, key, label)
372 SV *errstr = get_sv("PSPP::errstr", TRUE);
373 sv_setpv (errstr, "");
375 union value the_value;
377 if ( var_is_numeric (var))
379 if ( ! looks_like_number (key))
381 sv_setpv (errstr, "Cannot add label with string key to a numeric variable");
384 the_value.f = SvNV (key);
388 if ( var_is_long_string (var) )
390 sv_setpv (errstr, "Cannot add label to a long string variable");
393 strncpy (the_value.s, SvPV_nolen(key), MAX_SHORT_STRING);
395 if (! var_add_value_label (var, &the_value, label) )
397 sv_setpv (errstr, "Something went wrong");
406 struct variable * var
408 RETVAL = var_get_name (var);
415 struct variable * var
417 RETVAL = var_get_label (var);
423 get_value_labels (var)
426 HV *labelhash = (HV *) sv_2mortal ((SV *) newHV());
428 struct val_labs_iterator *viter = NULL;
429 const struct val_labs *labels = var_get_value_labels (var);
433 for (vl = val_labs_first (labels, &viter);
435 vl = val_labs_next (labels, &viter))
437 SV *sv = value_to_scalar (&vl->value, var);
439 const char *s = SvPV (sv, len);
440 hv_store (labelhash, s, len, newSVpv (vl->label, 0), 0);
444 RETVAL = newRV ((SV *) labelhash);
450 MODULE = PSPP PACKAGE = PSPP::Sysfile
453 struct sysfile_info *
454 pxs_create_sysfile (name, dict, opts_hr)
456 struct dictionary *dict
459 struct sfm_write_options opts;
460 if (!SvROK (opts_hr))
462 opts = sfm_writer_default_options ();
466 HV *opt_h = (HV *) SvRV (opts_hr);
467 SV** readonly = hv_fetch(opt_h, "readonly", 8, 0);
468 SV** compress = hv_fetch(opt_h, "compress", 8, 0);
469 SV** version = hv_fetch(opt_h, "version", 7, 0);
471 opts.create_writeable = readonly ? ! SvIV (*readonly) : true;
472 opts.compress = compress ? SvIV (*compress) : false;
473 opts.version = version ? SvIV (*version) : 3 ;
476 struct file_handle *fh =
477 fh_create_file (NULL, name, fh_default_properties () );
478 struct sysfile_info *sfi = xmalloc (sizeof (*sfi));
479 sfi->writer = sfm_open_writer (fh, dict, opts);
489 struct sysfile_info *sfi
491 RETVAL = sysfile_close (sfi);
497 struct sysfile_info *sfi
503 append_case (sfi, ccase)
504 struct sysfile_info *sfi
507 SV *errstr = get_sv("PSPP::errstr", TRUE);
508 sv_setpv (errstr, "");
509 if ( (!SvROK(ccase)))
515 AV *av_case = (AV*) SvRV (ccase);
517 const struct variable **vv;
522 if ( av_len (av_case) >= dict_get_var_cnt (sfi->dict))
525 case_create (&c, dict_get_next_value_idx (sfi->dict));
527 dict_get_vars (sfi->dict, &vv, &nv, 1u << DC_ORDINARY | 1u << DC_SYSTEM);
529 for (sv = av_shift (av_case); SvOK (sv); sv = av_shift (av_case))
531 const struct variable *v = vv[i++];
532 const struct fmt_spec *ifmt = var_get_aux (v);
534 /* If an input format has been set, then use it.
535 Otherwise just convert the raw value.
539 struct substring ss = ss_cstr (SvPV_nolen (sv));
540 if ( ! data_in (ss, LEGACY_NATIVE, ifmt->type, 0, 0, 0,
541 case_data_rw (&c, v),
550 scalar_to_value (case_data_rw (&c, v), sv, v);
554 /* The remaining variables must be sysmis or blank string */
555 while (i < dict_get_var_cnt (sfi->dict))
557 const struct variable *v = vv[i++];
558 union value *val = case_data_rw (&c, v);
559 if ( var_is_numeric (v))
562 memset (val->s, ' ', var_get_width (v));
564 RETVAL = casewriter_write (sfi->writer, &c);
566 // Case_destroy (&c);
574 MODULE = PSPP PACKAGE = PSPP::Reader
576 struct sysreader_info *
577 pxs_open_sysfile (name)
580 struct casereader *reader;
581 struct sysreader_info *sri = NULL;
582 struct file_handle *fh =
583 fh_create_file (NULL, name, fh_default_properties () );
585 sri = xmalloc (sizeof (*sri));
586 sri->reader = sfm_open_reader (fh, &sri->dict, &sri->opts);
588 if ( NULL == sri->reader)
600 pxs_get_dict (reader)
601 struct sysreader_info *reader;
603 RETVAL = reader->dict;
610 struct sysreader_info *sfr;
614 if (! casereader_read (sfr->reader, &c))
621 AV *av_case = (AV *) sv_2mortal ((SV *) newAV());
623 for (v = 0; v < dict_get_var_cnt (sfr->dict); ++v )
625 const struct variable *var = dict_get_var (sfr->dict, v);
626 const union value *val = case_data (&c, var);
628 av_push (av_case, value_to_scalar (val, var));
632 RETVAL = newRV ((SV *) av_case);