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));
279 MODULE = PSPP PACKAGE = PSPP::Var
283 pxs_dict_create_var (dict, name, ip_fmt)
284 struct dictionary * dict
288 SV *errstr = get_sv("PSPP::errstr", TRUE);
289 sv_setpv (errstr, "");
290 if ( ! var_is_plausible_name (name, false))
292 sv_setpv (errstr, "The variable name is not valid.");
296 struct fmt_spec op_fmt;
299 op_fmt = fmt_for_output_from_input (&ip_fmt);
300 v = dict_create_var (dict, name,
301 fmt_is_string (op_fmt.type) ? op_fmt.w : 0);
304 sv_setpv (errstr, "The variable could not be created (probably already exists).");
307 var_set_both_formats (v, &op_fmt);
308 var_set_input_format (v, ip_fmt);
315 set_missing_values (var, v1, ...)
316 struct variable *var;
323 croak ("No more than 3 missing values are permitted");
325 for (i = 0; i < items - 1; ++i)
326 scalar_to_value (&val[i], ST(i+1), var);
328 struct missing_values mv;
329 mv_init (&mv, var_get_width (var));
330 for (i = 0 ; i < items - 1; ++i )
331 mv_add_value (&mv, &val[i]);
332 var_set_missing_values (var, &mv);
336 set_label (var, label)
337 struct variable *var;
340 var_set_label (var, label);
344 clear_value_labels (var)
345 struct variable *var;
347 var_clear_value_labels (var);
350 pxs_set_write_format (var, fmt)
354 var_set_write_format (var, &fmt);
358 pxs_set_print_format (var, fmt)
362 var_set_print_format (var, &fmt);
365 pxs_set_output_format (var, fmt)
369 var_set_both_formats (var, &fmt);
373 add_value_label (var, key, label)
378 SV *errstr = get_sv("PSPP::errstr", TRUE);
379 sv_setpv (errstr, "");
381 union value the_value;
383 if ( var_is_numeric (var))
385 if ( ! looks_like_number (key))
387 sv_setpv (errstr, "Cannot add label with string key to a numeric variable");
390 the_value.f = SvNV (key);
394 if ( var_is_long_string (var) )
396 sv_setpv (errstr, "Cannot add label to a long string variable");
399 strncpy (the_value.s, SvPV_nolen(key), MAX_SHORT_STRING);
401 if (! var_add_value_label (var, &the_value, label) )
403 sv_setpv (errstr, "Something went wrong");
412 struct variable * var
414 RETVAL = var_get_name (var);
421 struct variable * var
423 RETVAL = var_get_label (var);
429 get_value_labels (var)
432 HV *labelhash = (HV *) sv_2mortal ((SV *) newHV());
434 struct val_labs_iterator *viter = NULL;
435 const struct val_labs *labels = var_get_value_labels (var);
439 for (vl = val_labs_first (labels, &viter);
441 vl = val_labs_next (labels, &viter))
443 SV *sv = value_to_scalar (&vl->value, var);
445 const char *s = SvPV (sv, len);
446 hv_store (labelhash, s, len, newSVpv (vl->label, 0), 0);
450 RETVAL = newRV ((SV *) labelhash);
456 MODULE = PSPP PACKAGE = PSPP::Sysfile
459 struct sysfile_info *
460 pxs_create_sysfile (name, dict_ref, opts_hr)
465 SV *dict_sv = SvRV (dict_ref);
466 struct dictionary *dict = (void *) SvIV (dict_sv);
467 struct sfm_write_options opts;
468 if (!SvROK (opts_hr))
470 opts = sfm_writer_default_options ();
474 HV *opt_h = (HV *) SvRV (opts_hr);
475 SV** readonly = hv_fetch(opt_h, "readonly", 8, 0);
476 SV** compress = hv_fetch(opt_h, "compress", 8, 0);
477 SV** version = hv_fetch(opt_h, "version", 7, 0);
479 opts.create_writeable = readonly ? ! SvIV (*readonly) : true;
480 opts.compress = compress ? SvIV (*compress) : false;
481 opts.version = version ? SvIV (*version) : 3 ;
484 struct file_handle *fh =
485 fh_create_file (NULL, name, fh_default_properties () );
486 struct sysfile_info *sfi = xmalloc (sizeof (*sfi));
487 sfi->writer = sfm_open_writer (fh, dict, opts);
490 sfi->dict_sv = dict_sv;
491 SvREFCNT_inc (sfi->dict_sv);
499 struct sysfile_info *sfi
501 RETVAL = sysfile_close (sfi);
507 struct sysfile_info *sfi
510 SvREFCNT_dec (sfi->dict_sv);
514 append_case (sfi, ccase)
515 struct sysfile_info *sfi
518 SV *errstr = get_sv("PSPP::errstr", TRUE);
519 sv_setpv (errstr, "");
520 if ( (!SvROK(ccase)))
526 AV *av_case = (AV*) SvRV (ccase);
528 const struct variable **vv;
533 if ( av_len (av_case) >= dict_get_var_cnt (sfi->dict))
536 case_create (&c, dict_get_next_value_idx (sfi->dict));
538 dict_get_vars (sfi->dict, &vv, &nv, 1u << DC_ORDINARY | 1u << DC_SYSTEM);
540 for (sv = av_shift (av_case); SvOK (sv); sv = av_shift (av_case))
542 const struct variable *v = vv[i++];
543 const struct fmt_spec *ifmt = var_get_aux (v);
545 /* If an input format has been set, then use it.
546 Otherwise just convert the raw value.
550 struct substring ss = ss_cstr (SvPV_nolen (sv));
551 if ( ! data_in (ss, LEGACY_NATIVE, ifmt->type, 0, 0, 0,
552 case_data_rw (&c, v),
561 scalar_to_value (case_data_rw (&c, v), sv, v);
565 /* The remaining variables must be sysmis or blank string */
566 while (i < dict_get_var_cnt (sfi->dict))
568 const struct variable *v = vv[i++];
569 union value *val = case_data_rw (&c, v);
570 if ( var_is_numeric (v))
573 memset (val->s, ' ', var_get_width (v));
575 RETVAL = casewriter_write (sfi->writer, &c);
577 // Case_destroy (&c);
585 MODULE = PSPP PACKAGE = PSPP::Reader
587 struct sysreader_info *
588 pxs_open_sysfile (name)
591 struct casereader *reader;
592 struct sysreader_info *sri = NULL;
593 struct file_handle *fh =
594 fh_create_file (NULL, name, fh_default_properties () );
596 sri = xmalloc (sizeof (*sri));
597 sri->reader = sfm_open_reader (fh, &sri->dict, &sri->opts);
599 if ( NULL == sri->reader)
611 pxs_get_dict (reader)
612 struct sysreader_info *reader;
614 RETVAL = reader->dict;
621 struct sysreader_info *sfr;
625 if (! casereader_read (sfr->reader, &c))
632 AV *av_case = (AV *) sv_2mortal ((SV *) newAV());
634 for (v = 0; v < dict_get_var_cnt (sfr->dict); ++v )
636 const struct variable *var = dict_get_var (sfr->dict, v);
637 const union value *val = case_data (&c, var);
639 av_push (av_case, value_to_scalar (val, var));
643 RETVAL = newRV ((SV *) av_case);