1 /* PSPP - computes sample statistics.
2 Copyright (C) 2007 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/value.h>
38 #include <data/format.h>
39 #include <data/data-in.h>
42 typedef struct fmt_spec input_format ;
43 typedef struct fmt_spec output_format ;
46 /* A thin wrapper around sfm_writer */
51 /* A pointer to the writer. The writer is owned by the struct */
52 struct casewriter *writer;
54 /* A pointer to the dictionary. Owned externally */
55 const struct dictionary *dict;
59 /* A message handler which writes messages to PSPP::errstr */
61 message_handler (const struct msg *m)
63 SV *errstr = get_sv("PSPP::errstr", TRUE);
64 sv_setpv (errstr, m->text);
68 sysfile_close (struct sysfile_info *sfi)
74 retval = casewriter_destroy (sfi->writer);
82 scalar_to_value (union value *val, SV *scalar)
84 if ( looks_like_number (scalar))
86 val->f = SvNV (scalar);
91 char *p = SvPV (scalar, len);
92 memset (val->s, ' ', MAX_SHORT_STRING);
93 memcpy (val->s, p, len);
101 msg_init (NULL, message_handler);
102 settings_init (0, 0);
106 MODULE = PSPP PACKAGE = PSPP::Dict
111 RETVAL = dict_create ();
118 struct dictionary *dict
124 set_label (dict, label)
125 struct dictionary *dict
128 dict_set_label (dict, label);
131 set_documents (dict, docs)
132 struct dictionary *dict
135 dict_set_documents (dict, docs);
139 add_document (dict, doc)
140 struct dictionary *dict
143 dict_add_document_line (dict, doc);
147 clear_documents (dict)
148 struct dictionary *dict
150 dict_clear_documents (dict);
154 set_weight (dict, var)
155 struct dictionary *dict
158 dict_set_weight (dict, var);
162 MODULE = PSPP PACKAGE = PSPP::Var
165 _dict_create_var (dict, name, ip_fmt)
166 struct dictionary * dict
170 SV *errstr = get_sv("PSPP::errstr", TRUE);
171 sv_setpv (errstr, "");
172 if ( ! var_is_plausible_name (name, false))
174 sv_setpv (errstr, "The variable name is not valid.");
178 struct fmt_spec op_fmt;
179 struct fmt_spec *if_copy;
181 op_fmt = fmt_for_output_from_input (&ip_fmt);
182 v = dict_create_var (dict, name,
183 fmt_is_string (op_fmt.type) ? op_fmt.w : 0);
186 sv_setpv (errstr, "The variable could not be created (probably already exists).");
189 var_set_both_formats (v, &op_fmt);
190 if_copy = malloc (sizeof (*if_copy));
191 memcpy (if_copy, &ip_fmt, sizeof (ip_fmt));
192 var_attach_aux (v, if_copy, var_dtor_free);
199 set_missing_values (var, v1, ...)
200 struct variable *var;
207 croak ("No more than 3 missing values are permitted");
209 for (i = 0; i < items - 1; ++i)
210 scalar_to_value (&val[i], ST(i+1));
212 struct missing_values mv;
213 mv_init (&mv, var_get_width (var));
214 for (i = 0 ; i < items - 1; ++i )
215 mv_add_value (&mv, &val[i]);
216 var_set_missing_values (var, &mv);
220 set_label (var, label)
221 struct variable *var;
224 var_set_label (var, label);
228 clear_value_labels (var)
229 struct variable *var;
231 var_clear_value_labels (var);
234 _set_write_format (var, fmt)
238 var_set_write_format (var, &fmt);
242 _set_print_format (var, fmt)
246 var_set_print_format (var, &fmt);
249 _set_output_format (var, fmt)
253 var_set_both_formats (var, &fmt);
257 add_value_label (var, key, label)
262 SV *errstr = get_sv("PSPP::errstr", TRUE);
263 sv_setpv (errstr, "");
265 union value the_value;
267 if ( var_is_numeric (var))
269 if ( ! looks_like_number (key))
271 sv_setpv (errstr, "Cannot add label with string key to a numeric variable");
274 the_value.f = SvNV (key);
278 if ( var_is_long_string (var) )
280 sv_setpv (errstr, "Cannot add label to a long string variable");
283 strncpy (the_value.s, SvPV_nolen(key), MAX_SHORT_STRING);
285 if (! var_add_value_label (var, &the_value, label) )
287 sv_setpv (errstr, "Something went wrong");
294 MODULE = PSPP PACKAGE = PSPP::Sysfile
297 struct sysfile_info *
298 _create_sysfile (name, dict, opts_hr)
300 struct dictionary * dict
303 struct sfm_write_options opts;
304 if (!SvROK (opts_hr))
306 opts = sfm_writer_default_options ();
310 HV *opt_h = (HV *) SvRV (opts_hr);
311 SV** readonly = hv_fetch(opt_h, "readonly", 8, 0);
312 SV** compress = hv_fetch(opt_h, "compress", 8, 0);
313 SV** version = hv_fetch(opt_h, "version", 7, 0);
315 opts.create_writeable = readonly ? ! SvIV (*readonly) : true;
316 opts.compress = compress ? SvIV (*compress) : false;
317 opts.version = version ? SvIV (*version) : 3 ;
320 struct file_handle *fh =
321 fh_create_file (NULL, name, fh_default_properties () );
322 struct sysfile_info *sfi = xmalloc (sizeof (*sfi));
323 sfi->writer = sfm_open_writer (fh, dict, opts);
332 struct sysfile_info *sfi
334 RETVAL = sysfile_close (sfi);
340 struct sysfile_info *sfi
346 append_case (sfi, ccase)
347 struct sysfile_info *sfi
350 SV *errstr = get_sv("PSPP::errstr", TRUE);
351 sv_setpv (errstr, "");
352 if ( (!SvROK(ccase)))
358 AV *av_case = (AV*) SvRV (ccase);
360 const struct variable **vv;
364 if ( av_len (av_case) >= dict_get_var_cnt (sfi->dict))
367 case_create (&c, dict_get_next_value_idx (sfi->dict));
369 dict_get_vars (sfi->dict, &vv, &nv, 1u << DC_ORDINARY | 1u << DC_SYSTEM);
372 for (sv = av_shift (av_case); SvOK (sv); sv = av_shift (av_case))
374 const struct variable *v = vv[i++];
375 struct substring ss = ss_cstr (SvPV_nolen (sv));
376 struct fmt_spec *ifmt = var_get_aux (v);
378 if ( ! data_in (ss, LEGACY_NATIVE, ifmt->type, 0, 0, 0, case_data_rw (&c, v),
385 /* The remaining variables must be sysmis or blank string */
386 while (i < dict_get_var_cnt (sfi->dict))
388 const struct variable *v = vv[i++];
389 union value *val = case_data_rw (&c, v);
390 if ( var_is_numeric (v))
393 memset (val->s, ' ', var_get_width (v));
395 RETVAL = casewriter_write (sfi->writer, &c);