/* PSPP - computes sample statistics.
- Copyright (C) 2007 Free Software Foundation, Inc.
+ Copyright (C) 2007, 2008 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
#include <data/variable.h>
#include <data/file-handle-def.h>
#include <data/sys-file-writer.h>
+#include <data/sys-file-reader.h>
#include <data/value.h>
+#include <data/value-labels.h>
#include <data/format.h>
#include <data/data-in.h>
#include <string.h>
/* A thin wrapper around sfm_writer */
struct sysfile_info
{
- bool opened;
+ bool opened;
/* A pointer to the writer. The writer is owned by the struct */
struct casewriter *writer;
};
+/* A thin wrapper around sfm_reader */
+struct sysreader_info
+{
+ struct sfm_read_info opts;
+
+ /* A pointer to the reader. The reader is owned by the struct */
+ struct casereader *reader;
+
+ /* A pointer to the dictionary. */
+ struct dictionary *dict;
+};
+
+
+
/* A message handler which writes messages to PSPP::errstr */
static void
message_handler (const struct msg *m)
}
static void
-scalar_to_value (union value *val, SV *scalar)
+scalar_to_value (union value *val, SV *scalar, const struct variable *var)
{
- if ( looks_like_number (scalar))
+ if ( var_is_numeric (var))
{
- val->f = SvNV (scalar);
+ if ( SvNOK (scalar) || SvIOK (scalar) )
+ val->f = SvNV (scalar);
+ else
+ val->f = SYSMIS;
}
else
{
STRLEN len;
- char *p = SvPV (scalar, len);
- memset (val->s, ' ', MAX_SHORT_STRING);
+ const char *p = SvPV (scalar, len);
+ memset (val->s, ' ', var_get_width (var));
memcpy (val->s, p, len);
}
}
+static SV *
+value_to_scalar (const union value *val, const struct variable *var)
+{
+ if ( var_is_numeric (var))
+ {
+ if ( var_is_value_missing (var, val, MV_SYSTEM))
+ return newSVpvn ("", 0);
+
+ return newSVnv (val->f);
+ }
+ else
+ return newSVpvn (val->s, var_get_width (var));
+}
+
+
+static void
+var_set_input_format (struct variable *v, input_format ip_fmt)
+{
+ struct fmt_spec *if_copy = malloc (sizeof (*if_copy));
+ memcpy (if_copy, &ip_fmt, sizeof (ip_fmt));
+ var_attach_aux (v, if_copy, var_dtor_free);
+}
+
+static union value *
+make_value_from_scalar (SV *val, const struct variable *var)
+{
+ union value *uv = value_create (var_get_width (var));
+ scalar_to_value (uv, val, var);
+ return uv;
+}
+
+
MODULE = PSPP
BOOT:
fh_init ();
+MODULE = PSPP PACKAGE = PSPP
+
+SV *
+format_value (val, var)
+ SV *val
+ struct variable *var
+CODE:
+ SV *ret;
+ const struct fmt_spec *fmt = var_get_print_format (var);
+ union value *uv = make_value_from_scalar (val, var);
+ char *s;
+ s = malloc (fmt->w);
+ memset (s, '\0', fmt->w);
+ data_out (uv, fmt, s);
+ free (uv);
+ ret = newSVpv (s, fmt->w);
+ free (s);
+ RETVAL = ret;
+ OUTPUT:
+RETVAL
+
+
+int
+value_is_missing (val, var)
+ SV *val
+ struct variable *var
+CODE:
+ union value *uv = make_value_from_scalar (val, var);
+ int ret = var_is_value_missing (var, uv, MV_ANY);
+ free (uv);
+ RETVAL = ret;
+ OUTPUT:
+RETVAL
+
+
+
MODULE = PSPP PACKAGE = PSPP::Dict
struct dictionary *
-_dict_new()
+pxs_dict_new()
CODE:
RETVAL = dict_create ();
OUTPUT:
dict_destroy (dict);
+int
+get_var_cnt (dict)
+ struct dictionary *dict
+CODE:
+ RETVAL = dict_get_var_cnt (dict);
+OUTPUT:
+RETVAL
+
void
set_label (dict, label)
struct dictionary *dict
dict_set_weight (dict, var);
+struct variable *
+pxs_get_variable (dict, idx)
+ struct dictionary *dict
+ SV *idx
+INIT:
+ SV *errstr = get_sv("PSPP::errstr", TRUE);
+ sv_setpv (errstr, "");
+ if ( SvIV (idx) >= dict_get_var_cnt (dict))
+ {
+ sv_setpv (errstr, "The dictionary doesn't have that many variables.");
+ XSRETURN_UNDEF;
+ }
+CODE:
+ RETVAL = dict_get_var (dict, SvIV (idx));
+ OUTPUT:
+RETVAL
MODULE = PSPP PACKAGE = PSPP::Var
+
struct variable *
-_dict_create_var (dict, name, ip_fmt)
+pxs_dict_create_var (dict, name, ip_fmt)
struct dictionary * dict
char *name
input_format ip_fmt
}
CODE:
struct fmt_spec op_fmt;
- struct fmt_spec *if_copy;
+
struct variable *v;
op_fmt = fmt_for_output_from_input (&ip_fmt);
v = dict_create_var (dict, name,
XSRETURN_UNDEF;
}
var_set_both_formats (v, &op_fmt);
- if_copy = malloc (sizeof (*if_copy));
- memcpy (if_copy, &ip_fmt, sizeof (ip_fmt));
- var_attach_aux (v, if_copy, var_dtor_free);
+ var_set_input_format (v, ip_fmt);
RETVAL = v;
OUTPUT:
RETVAL
croak ("No more than 3 missing values are permitted");
for (i = 0; i < items - 1; ++i)
- scalar_to_value (&val[i], ST(i+1));
+ scalar_to_value (&val[i], ST(i+1), var);
CODE:
struct missing_values mv;
mv_init (&mv, var_get_width (var));
char *label
CODE:
var_set_label (var, label);
-
+
void
clear_value_labels (var)
var_clear_value_labels (var);
void
-_set_write_format (var, fmt)
+pxs_set_write_format (var, fmt)
struct variable *var
output_format fmt
CODE:
void
-_set_print_format (var, fmt)
+pxs_set_print_format (var, fmt)
struct variable *var
output_format fmt
CODE:
var_set_print_format (var, &fmt);
void
-_set_output_format (var, fmt)
+pxs_set_output_format (var, fmt)
struct variable *var
output_format fmt
CODE:
+const char *
+get_name (var)
+ struct variable * var
+CODE:
+ RETVAL = var_get_name (var);
+ OUTPUT:
+RETVAL
+
+
+const char *
+get_label (var)
+ struct variable * var
+CODE:
+ RETVAL = var_get_label (var);
+ OUTPUT:
+RETVAL
+
+
+SV *
+get_value_labels (var)
+ struct variable *var
+CODE:
+ HV *labelhash = (HV *) sv_2mortal ((SV *) newHV());
+ struct val_lab *vl;
+ struct val_labs_iterator *viter = NULL;
+ const struct val_labs *labels = var_get_value_labels (var);
+
+ if ( labels )
+ {
+ for (vl = val_labs_first (labels, &viter);
+ vl;
+ vl = val_labs_next (labels, &viter))
+ {
+ SV *sv = value_to_scalar (&vl->value, var);
+ STRLEN len;
+ const char *s = SvPV (sv, len);
+ hv_store (labelhash, s, len, newSVpv (vl->label, 0), 0);
+ }
+ }
+
+ RETVAL = newRV ((SV *) labelhash);
+ OUTPUT:
+RETVAL
+
+
+
MODULE = PSPP PACKAGE = PSPP::Sysfile
struct sysfile_info *
-_create_sysfile (name, dict, opts_hr)
- char * name
- struct dictionary * dict
+pxs_create_sysfile (name, dict, opts_hr)
+ char *name
+ struct dictionary *dict
SV *opts_hr
INIT:
struct sfm_write_options opts;
sfi->writer = sfm_open_writer (fh, dict, opts);
sfi->dict = dict;
sfi->opened = true;
+
RETVAL = sfi;
OUTPUT:
RETVAL
const struct variable **vv;
size_t nv;
struct ccase c;
+ SV *sv;
if ( av_len (av_case) >= dict_get_var_cnt (sfi->dict))
XSRETURN_UNDEF;
dict_get_vars (sfi->dict, &vv, &nv, 1u << DC_ORDINARY | 1u << DC_SYSTEM);
- SV *sv ;
for (sv = av_shift (av_case); SvOK (sv); sv = av_shift (av_case))
{
const struct variable *v = vv[i++];
- struct substring ss = ss_cstr (SvPV_nolen (sv));
- struct fmt_spec *ifmt = var_get_aux (v);
-
- if ( ! data_in (ss, LEGACY_NATIVE, ifmt->type, 0, 0, 0, case_data_rw (&c, v),
- var_get_width (v)) )
- {
- RETVAL = 0;
- goto finish;
- }
+ const struct fmt_spec *ifmt = var_get_aux (v);
+
+ /* If an input format has been set, then use it.
+ Otherwise just convert the raw value.
+ */
+ if ( ifmt )
+ {
+ struct substring ss = ss_cstr (SvPV_nolen (sv));
+ if ( ! data_in (ss, LEGACY_NATIVE, ifmt->type, 0, 0, 0,
+ case_data_rw (&c, v),
+ var_get_width (v)) )
+ {
+ RETVAL = 0;
+ goto finish;
+ }
+ }
+ else
+ {
+ scalar_to_value (case_data_rw (&c, v), sv, v);
+ }
}
+
/* The remaining variables must be sysmis or blank string */
while (i < dict_get_var_cnt (sfi->dict))
{
}
RETVAL = casewriter_write (sfi->writer, &c);
finish:
- case_destroy (&c);
+// Case_destroy (&c);
free (vv);
OUTPUT:
RETVAL
+
+
+\f
+
+MODULE = PSPP PACKAGE = PSPP::Reader
+
+struct sysreader_info *
+pxs_open_sysfile (name)
+ char * name
+CODE:
+ struct casereader *reader;
+ struct sysreader_info *sri = NULL;
+ struct file_handle *fh =
+ fh_create_file (NULL, name, fh_default_properties () );
+
+ sri = xmalloc (sizeof (*sri));
+ sri->reader = sfm_open_reader (fh, &sri->dict, &sri->opts);
+
+ if ( NULL == sri->reader)
+ {
+ free (sri);
+ sri = NULL;
+ }
+
+ RETVAL = sri;
+ OUTPUT:
+RETVAL
+
+
+struct dictionary *
+pxs_get_dict (reader)
+ struct sysreader_info *reader;
+CODE:
+ RETVAL = reader->dict;
+ OUTPUT:
+RETVAL
+
+
+SV *
+get_next_case (sfr)
+ struct sysreader_info *sfr;
+CODE:
+ struct ccase c;
+
+ if (! casereader_read (sfr->reader, &c))
+ {
+ RETVAL = 0;
+ }
+ else
+ {
+ int v;
+ AV *av_case = (AV *) sv_2mortal ((SV *) newAV());
+
+ for (v = 0; v < dict_get_var_cnt (sfr->dict); ++v )
+ {
+ const struct variable *var = dict_get_var (sfr->dict, v);
+ const union value *val = case_data (&c, var);
+
+ av_push (av_case, value_to_scalar (val, var));
+ }
+
+ case_destroy (&c);
+ RETVAL = newRV ((SV *) av_case);
+ }
+OUTPUT:
+ RETVAL
+
+
# change 'tests => 1' to 'tests => last_test_to_print';
-use Test::More tests => 19;
+use Test::More tests => 30;
use Text::Diff;
use File::Temp qw/ tempfile tempdir /;
BEGIN { use_ok('PSPP') };
#########################
+sub compare
+{
+ my $file = shift;
+ my $pattern = shift;
+ return ! diff ("$file", \$pattern);
+}
+
sub run_pspp_syntax
{
my $tempdir = shift;
my $syntax = shift;
- my $result = shift;
+
my $syntaxfile = "$tempdir/foo.sps";
open (FH, ">$syntaxfile");
close (FH);
system ("cd $tempdir; pspp -o raw-ascii $syntaxfile");
+}
+
+sub run_pspp_syntax_cmp
+{
+ my $tempdir = shift;
+ my $syntax = shift;
+
+ my $result = shift;
+
+ run_pspp_syntax ($tempdir, $syntax);
my $diff = diff ("$tempdir/pspp.list", \$result);
}
-# Insert your test code below, the Test::More module is use()ed here so read
+# Insert your test code below, the Test::More module is used here so read
# its man page ( perldoc Test::More ) for help writing this test script.
{
}
ok (-s "$tempfile", "existance2");
- ok (run_pspp_syntax ($tempdir, <<SYNTAX, <<RESULT), "Check output");
+ ok (run_pspp_syntax_cmp ($tempdir, <<SYNTAX, <<RESULT), "Check output");
GET FILE='$tempfile'.
DISPLAY DICTIONARY.
$sysfile->close ();
- ok (run_pspp_syntax ($tempdir, <<SYNTAX, <<RESULT), "Check output 2");
+ ok (run_pspp_syntax_cmp ($tempdir, <<SYNTAX, <<RESULT), "Check output 2");
GET FILE='$tempfile'.
DISPLAY DICTIONARY.
SYNTAX
}
}
+
+sub generate_sav_file
+{
+ my $filename = shift;
+ my $tempdir = shift;
+
+ run_pspp_syntax_cmp ($tempdir, <<SYNTAX, <<RESULT);
+data list notable list /string (a8) longstring (a12) numeric (f10) date (date11) dollar (dollar8.2) datetime (datetime17)
+begin data.
+1111 One 1 1/1/1 1 1/1/1+01:01
+2222 Two 2 2/2/2 2 2/2/2+02:02
+3333 Three 3 3/3/3 3 3/3/3+03:03
+. . . . .
+5555 Five 5 5/5/5 5 5/5/5+05:05
+end data.
+
+
+variable labels string 'A Short String Variable'
+ /longstring 'A Long String Variable'
+ /numeric 'A Numeric Variable'
+ /date 'A Date Variable'
+ /dollar 'A Dollar Variable'
+ /datetime 'A Datetime Variable'.
+
+
+missing values numeric (9, 5, 999).
+
+missing values string ("3333").
+
+add value labels
+ /string '1111' 'ones' '2222' 'twos' '3333' 'threes'
+ /numeric 1 'Unity' 2 'Duality' 3 'Thripality'.
+
+save outfile='$filename'.
+SYNTAX
+
+RESULT
+
+}
+
+
+# Basic reader test
+{
+ my $tempdir = tempdir( CLEANUP => 1 );
+
+ generate_sav_file ("$tempdir/in.sav", "$tempdir");
+
+ my $sf = PSPP::Reader->open ("$tempdir/in.sav");
+
+ my $dict = $sf->get_dict ();
+
+ open (MYFILE, ">$tempdir/out.txt");
+ for ($v = 0 ; $v < $dict->get_var_cnt() ; $v++)
+ {
+ my $var = $dict->get_var ($v);
+ my $name = $var->get_name ();
+ my $label = $var->get_label ();
+
+ print MYFILE "Variable $v is \"$name\", label is \"$label\"\n";
+
+ my $vl = $var->get_value_labels ();
+
+ print MYFILE "Value Labels:\n";
+ print MYFILE "$_ => $vl->{$_}\n" for keys %$vl;
+ }
+
+ while (my $c = $sf->get_next_case () )
+ {
+ for ($v = 0; $v < $dict->get_var_cnt(); $v++)
+ {
+ print MYFILE "val$v: \"@$c[$v]\"\n";
+ }
+ print MYFILE "\n";
+ }
+
+ close (MYFILE);
+
+ok (compare ("$tempdir/out.txt", <<EOF), "Basic reader operation");
+Variable 0 is "string", label is "A Short String Variable"
+Value Labels:
+3333 => threes
+1111 => ones
+2222 => twos
+Variable 1 is "longstring", label is "A Long String Variable"
+Value Labels:
+Variable 2 is "numeric", label is "A Numeric Variable"
+Value Labels:
+1 => Unity
+3 => Thripality
+2 => Duality
+Variable 3 is "date", label is "A Date Variable"
+Value Labels:
+Variable 4 is "dollar", label is "A Dollar Variable"
+Value Labels:
+Variable 5 is "datetime", label is "A Datetime Variable"
+Value Labels:
+val0: "1111 "
+val1: "One "
+val2: "1"
+val3: "13197686400"
+val4: "1"
+val5: "13197690060"
+
+val0: "2222 "
+val1: "Two "
+val2: "2"
+val3: "13231987200"
+val4: "2"
+val5: "13231994520"
+
+val0: "3333 "
+val1: "Three "
+val2: "3"
+val3: "13266028800"
+val4: "3"
+val5: "13266039780"
+
+val0: ". "
+val1: ". "
+val2: ""
+val3: ""
+val4: ""
+val5: ""
+
+val0: "5555 "
+val1: "Five "
+val2: "5"
+val3: "13334630400"
+val4: "5"
+val5: "13334648700"
+
+EOF
+
+}
+
+
+# Check that we can stream one file into another
+{
+ my $tempdir = tempdir( CLEANUP => 1 );
+
+ generate_sav_file ("$tempdir/in.sav", "$tempdir");
+
+ my $input = PSPP::Reader->open ("$tempdir/in.sav");
+
+ my $dict = $input->get_dict ();
+
+ my $output = PSPP::Sysfile->new ("$tempdir/out.sav", $dict);
+
+ while (my $c = $input->get_next_case () )
+ {
+ $output->append_case ($c);
+ }
+
+ $output->close ();
+
+
+ #Check the two files are the same (except for metadata)
+
+ run_pspp_syntax ($tempdir, <<SYNTAX);
+ get file='$tempdir/in.sav'.
+ display dictionary.
+ list.
+
+SYNTAX
+
+ system ("cp $tempdir/pspp.list $tempdir/in.txt");
+
+ run_pspp_syntax ($tempdir, <<SYNTAX);
+ get file='$tempdir/out.sav'.
+ display dictionary.
+ list.
+
+SYNTAX
+
+ ok (! diff ("$tempdir/pspp.list", "$tempdir/in.txt"), "Streaming of files");
+}
+
+
+
+# Check that the format_value function works properly
+{
+ my $tempdir = tempdir( CLEANUP => 1 );
+
+ run_pspp_syntax ($tempdir, <<SYNTAX);
+
+data list list /d (datetime17).
+begin data.
+11/9/2001+08:20
+end data.
+
+save outfile='$tempdir/dd.sav'.
+
+SYNTAX
+
+ my $sf = PSPP::Reader->open ("$tempdir/dd.sav");
+
+ my $dict = $sf->get_dict ();
+
+ my $c = $sf->get_next_case ();
+
+ my $var = $dict->get_var (0);
+ my $val = @$c[0];
+ my $formatted = PSPP::format_value ($val, $var);
+ my $str = gmtime ($val - PSPP::PERL_EPOCH);
+ print "Formatted string is \"$formatted\"\n";
+ ok ( $formatted eq "11-SEP-2001 08:20", "format_value function");
+ ok ( $str eq "Tue Sep 11 08:20:00 2001", "Perl representation of time");
+}
+
+
+# Check that attempting to open a non-existent file results in an error
+{
+ my $tempdir = tempdir( CLEANUP => 1 );
+
+ unlink ("$tempdir/no-such-file.sav");
+
+ my $sf = PSPP::Reader->open ("$tempdir/no-such-file.sav");
+
+ ok ( !ref $sf, "Returns undef on opening failure");
+
+ ok ("$PSPP::errstr" eq "Error opening \"$tempdir/no-such-file.sav\" for reading as a system file: No such file or directory.",
+ "Error string on open failure");
+}
+
+
+# Missing value tests.
+{
+ my $tempdir = tempdir( CLEANUP => 1 );
+
+ generate_sav_file ("$tempdir/in.sav", "$tempdir");
+
+ my $sf = PSPP::Reader->open ("$tempdir/in.sav");
+
+ my $dict = $sf->get_dict ();
+
+
+ my $c = $sf->get_next_case ();
+
+ my $stringvar = $dict->get_var (0);
+ my $numericvar = $dict->get_var (2);
+ my $val = @$c[0];
+
+ ok ( !PSPP::value_is_missing ($val, $stringvar), "Missing Value Negative String");
+
+ $val = @$c[2];
+
+ ok ( !PSPP::value_is_missing ($val, $numericvar), "Missing Value Negative Num");
+
+ $c = $sf->get_next_case ();
+ $c = $sf->get_next_case ();
+
+ $val = @$c[0];
+ ok ( PSPP::value_is_missing ($val, $stringvar), "Missing Value Positive");
+
+ $c = $sf->get_next_case ();
+ $val = @$c[2];
+ ok ( PSPP::value_is_missing ($val, $numericvar), "Missing Value Positive SYS");
+
+ $c = $sf->get_next_case ();
+ $val = @$c[2];
+ ok ( PSPP::value_is_missing ($val, $numericvar), "Missing Value Positive Num");
+}