X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=perl-module%2FPSPP.xs;h=8800d0bdf4e0e87cbba8f89ec872029181395f59;hb=d6510c8bb92e55331288bd7066e4697ca3cc9348;hp=874bd3bcad22274506f6c9db7db378e8a2341190;hpb=1bed5e2c0f2497b12608bb9a332f0cf06893f11d;p=pspp diff --git a/perl-module/PSPP.xs b/perl-module/PSPP.xs index 874bd3bcad..8800d0bdf4 100644 --- a/perl-module/PSPP.xs +++ b/perl-module/PSPP.xs @@ -1,22 +1,20 @@ /* PSPP - computes sample statistics. - Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc. + Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 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 - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301, USA. */ - + along with this program. If not, see . */ +#undef VERSION #include /* The Gnulib "strftime" module defines my_strftime in for use by @@ -32,6 +30,8 @@ #include "ppport.h" #include "minmax.h" +#include +#include #include #include #include @@ -46,7 +46,6 @@ #include #include #include -#include #include #include #include @@ -78,7 +77,7 @@ struct syswriter_info /* A thin wrapper around sfm_reader */ struct sysreader_info { - struct sfm_read_info opts; + struct any_read_info opts; /* A pointer to the reader. The reader is owned by the struct */ struct casereader *reader; @@ -88,10 +87,16 @@ struct sysreader_info }; +struct input_format { + struct hmap_node hmap_node; /* In struct pspp_dict's input_formats map. */ + const struct variable *var; + struct fmt_spec input_format; +}; /* A thin wrapper around struct dictionary.*/ struct pspp_dict { struct dictionary *dict; + struct hmap input_formats; /* Contains struct input_format. */ }; @@ -133,7 +138,7 @@ scalar_to_value (union value *val, SV *scalar, const struct variable *var) const char *p = SvPV (scalar, len); int width = var_get_width (var); value_set_missing (val, width); - memcpy (value_str_rw (val, width), p, len); + memcpy (val->s, p, len); } } @@ -151,19 +156,11 @@ value_to_scalar (const union value *val, const struct variable *var) else { int width = var_get_width (var); - return newSVpvn (value_str (val, width), width); + return newSVpvn (val->s, width); } } -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 void make_value_from_scalar (union value *uv, SV *val, const struct variable *var) { @@ -176,9 +173,23 @@ create_pspp_dict (struct dictionary *dict) { struct pspp_dict *pspp_dict = xmalloc (sizeof *pspp_dict); pspp_dict->dict = dict; + hmap_init (&pspp_dict->input_formats); return pspp_dict; } +static const struct fmt_spec * +find_input_format (const struct pspp_dict *dict, const struct variable *var) +{ + struct input_format *input_format; + + HMAP_FOR_EACH_IN_BUCKET (input_format, struct input_format, hmap_node, + hash_pointer (var, 0), &dict->input_formats) + if (input_format->var == var) + return &input_format->input_format; + + return NULL; +} + MODULE = PSPP @@ -251,7 +262,16 @@ DESTROY (dict) CODE: if (dict != NULL) { - dict_destroy (dict->dict); + struct input_format *input_format, *next_input_format; + + HMAP_FOR_EACH_SAFE (input_format, next_input_format, + struct input_format, hmap_node, &dict->input_formats) + { + hmap_delete (&dict->input_formats, &input_format->hmap_node); + free (input_format); + } + hmap_destroy (&dict->input_formats); + dict_unref (dict->dict); free (dict); } @@ -353,6 +373,7 @@ INIT: } CODE: struct fmt_spec op_fmt; + struct input_format *input_format; struct variable *v; op_fmt = fmt_for_output_from_input (&ip_fmt); @@ -364,7 +385,13 @@ CODE: XSRETURN_UNDEF; } var_set_both_formats (v, &op_fmt); - var_set_input_format (v, ip_fmt); + + input_format = xmalloc (sizeof *input_format); + input_format->var = v; + input_format->input_format = ip_fmt; + hmap_insert (&dict->input_formats, &input_format->hmap_node, + hash_pointer (v, 0)); + RETVAL = v; OUTPUT: RETVAL @@ -382,12 +409,15 @@ INIT: croak ("No more than 3 missing values are permitted"); for (i = 0; i < items - 1; ++i) - scalar_to_value (&val[i], ST(i+1), var); + make_value_from_scalar (&val[i], ST(i+1), var); CODE: struct missing_values mv; mv_init (&mv, var_get_width (var)); for (i = 0 ; i < items - 1; ++i ) - mv_add_value (&mv, &val[i]); + { + mv_add_value (&mv, &val[i]); + value_destroy (&val[i], var_get_width (var)); + } var_set_missing_values (var, &mv); @@ -396,7 +426,7 @@ set_label (var, label) struct variable *var; char *label CODE: - var_set_label (var, label, false); + var_set_label (var, label); void @@ -559,7 +589,6 @@ get_value_labels (var) CODE: HV *labelhash = (HV *) sv_2mortal ((SV *) newHV()); const struct val_lab *vl; - struct val_labs_iterator *viter = NULL; const struct val_labs *labels = var_get_value_labels (var); if ( labels ) @@ -604,19 +633,21 @@ INIT: SV** version = hv_fetch(opt_h, "version", 7, 0); opts.create_writeable = readonly ? ! SvIV (*readonly) : true; - opts.compress = compress ? SvIV (*compress) : false; + opts.compression = (compress && SvIV (*compress) + ? ANY_COMP_SIMPLE + : ANY_COMP_NONE); opts.version = version ? SvIV (*version) : 3 ; } CODE: struct file_handle *fh = - fh_create_file (NULL, name, fh_default_properties () ); + fh_create_file (NULL, name, "UTF-8", fh_default_properties () ); struct syswriter_info *swi = xmalloc (sizeof (*swi)); swi->writer = sfm_open_writer (fh, dict->dict, opts); swi->dict = dict; swi->opened = true; swi->dict_sv = dict_sv; SvREFCNT_inc (swi->dict_sv); - + RETVAL = swi; OUTPUT: RETVAL @@ -668,7 +699,7 @@ CODE: for (sv = av_shift (av_case); SvOK (sv); sv = av_shift (av_case)) { const struct variable *v = vv[i++]; - const struct fmt_spec *ifmt = var_get_aux (v); + const struct fmt_spec *ifmt = find_input_format (swi->dict, v); /* If an input format has been set, then use it. Otherwise just convert the raw value. @@ -720,16 +751,14 @@ 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 () ); + fh_create_file (NULL, name, "UTF-8", fh_default_properties () ); struct dictionary *dict; sri = xmalloc (sizeof (*sri)); - sri->reader = sfm_open_reader (fh, NULL, &dict, &sri->opts); - - if ( sri->reader != NULL) + sri->reader = any_reader_open_and_decode (fh, NULL, &dict, &sri->opts); + if (sri->reader) sri->dict = create_pspp_dict (dict); else { @@ -758,7 +787,7 @@ CODE: casenumber n = casereader_get_case_cnt (sfr->reader); if (n == CASENUMBER_MAX) ret = &PL_sv_undef; - else + else ret = newSViv (n); RETVAL = ret; OUTPUT: