X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=src%2Flanguage%2Fstats%2Fflip.c;h=6c49bd3798e5376555dd785ca85bab6ef61c7469;hb=a9acce47d67e0ab35ce1690e4f1b1ac0121c2d78;hp=0bbe637eca43199932a12a476cdd5465717b0283;hpb=92c09e564002d356d20fc1e2e131027ef89f6748;p=pspp-builds.git diff --git a/src/language/stats/flip.c b/src/language/stats/flip.c index 0bbe637e..6c49bd37 100644 --- a/src/language/stats/flip.c +++ b/src/language/stats/flip.c @@ -1,20 +1,18 @@ -/* PSPP - computes sample statistics. +/* PSPP - a program for statistical analysis. Copyright (C) 1997-9, 2000 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 . */ #include "config.h" @@ -23,9 +21,6 @@ #include #include #include -#ifdef HAVE_SYS_TYPES_H -#include -#endif #include #include @@ -33,12 +28,12 @@ #include #include #include +#include #include #include #include #include #include -#include #include #include #include @@ -48,6 +43,7 @@ #include "intprops.h" #include "minmax.h" +#include "xalloc.h" #include "gettext.h" #define _(msgid) gettext (msgid) @@ -60,14 +56,12 @@ struct varname }; /* Represents a FLIP input program. */ -struct flip_pgm +struct flip_pgm { struct pool *pool; /* Pool containing FLIP data. */ const struct variable **var; /* Variables to transpose. */ - int *idx_to_fv; /* var[]->index to compacted sink case fv. */ size_t var_cnt; /* Number of elements in `var'. */ int case_cnt; /* Pre-flip case count. */ - size_t case_size; /* Post-flip bytes per case. */ struct variable *new_names; /* Variable containing new variable names. */ struct varname *new_names_head; /* First new variable. */ @@ -104,8 +98,6 @@ cmd_flip (struct lexer *lexer, struct dataset *ds) flip = pool_create_container (struct flip_pgm, pool); flip->var = NULL; - flip->idx_to_fv = dict_get_compacted_dict_index_to_case_index (dict); - pool_register (flip->pool, free, flip->idx_to_fv); flip->var_cnt = 0; flip->case_cnt = 0; flip->new_names = NULL; @@ -121,12 +113,12 @@ cmd_flip (struct lexer *lexer, struct dataset *ds) { lex_match (lexer, '='); if (!parse_variables_const (lexer, dict, &flip->var, &flip->var_cnt, - PV_NO_DUPLICATE)) + PV_NO_DUPLICATE)) goto error; lex_match (lexer, '/'); } else - dict_get_vars (dict, &flip->var, &flip->var_cnt, 1u << DC_SYSTEM); + dict_get_vars (dict, &flip->var, &flip->var_cnt, DC_SYSTEM); pool_register (flip->pool, free, flip->var); lex_match (lexer, '/'); @@ -151,8 +143,7 @@ cmd_flip (struct lexer *lexer, struct dataset *ds) } } - output_buf = pool_nalloc (flip->pool, - flip->var_cnt, sizeof *output_buf); + output_buf = pool_nalloc (flip->pool, flip->var_cnt, sizeof *output_buf); flip->file = pool_tmpfile (flip->pool); if (flip->file == NULL) @@ -162,11 +153,11 @@ cmd_flip (struct lexer *lexer, struct dataset *ds) } /* Write variable names as first case. */ - for (i = 0; i < flip->var_cnt; i++) + for (i = 0; i < flip->var_cnt; i++) buf_copy_str_rpad (output_buf[i].s, MAX_SHORT_STRING, var_get_name (flip->var[i])); if (fwrite (output_buf, sizeof *output_buf, - flip->var_cnt, flip->file) != (size_t) flip->var_cnt) + flip->var_cnt, flip->file) != (size_t) flip->var_cnt) { msg (SE, _("Error writing FLIP file: %s."), strerror (errno)); goto error; @@ -175,11 +166,10 @@ cmd_flip (struct lexer *lexer, struct dataset *ds) flip->case_cnt = 1; /* Read the active file into a flip_sink. */ - proc_make_temporary_transformations_permanent (ds); proc_discard_output (ds); input = proc_open (ds); - while (casereader_read (input, &c)) + while (casereader_read (input, &c)) { write_flip_case (flip, &c); case_destroy (&c); @@ -188,7 +178,7 @@ cmd_flip (struct lexer *lexer, struct dataset *ds) ok = proc_commit (ds) && ok; /* Flip the data we read. */ - if (!ok || !flip_file (flip)) + if (!ok || !flip_file (flip)) { proc_discard_active_file (ds); goto error; @@ -201,11 +191,10 @@ cmd_flip (struct lexer *lexer, struct dataset *ds) proc_discard_active_file (ds); goto error; } - flip->case_size = dict_get_case_size (dict); /* Set up flipped data for reading. */ reader = casereader_create_sequential (NULL, dict_get_next_value_idx (dict), - flip->case_cnt, + flip->var_cnt, &flip_casereader_class, flip); proc_set_active_file_data (ds, reader); return lex_end_of_command (lexer); @@ -217,7 +206,7 @@ cmd_flip (struct lexer *lexer, struct dataset *ds) /* Destroys FLIP. */ static void -destroy_flip_pgm (struct flip_pgm *flip) +destroy_flip_pgm (struct flip_pgm *flip) { if (flip != NULL) pool_destroy (flip->pool); @@ -237,7 +226,7 @@ make_new_var (struct dictionary *dict, char name[]) /* Fix invalid characters. */ for (cp = name; *cp && cp < name + SHORT_NAME_LEN; cp++) - if (cp == name) + if (cp == name) { if (!lex_is_id1 (*cp) || *cp == '$') *cp = 'V'; @@ -245,11 +234,11 @@ make_new_var (struct dictionary *dict, char name[]) else { if (!lex_is_idn (*cp)) - *cp = '_'; + *cp = '_'; } *cp = '\0'; str_uppercase (name); - + if (dict_create_var (dict, name, 0)) return 1; @@ -283,14 +272,14 @@ build_dictionary (struct dictionary *dict, struct flip_pgm *flip) if (flip->new_names_head == NULL) { int i; - + if (flip->case_cnt > 99999) { msg (SE, _("Cannot create more than 99999 variable names.")); return false; } - - for (i = 0; i < flip->case_cnt; i++) + + for (i = 0; i < flip->case_cnt - 1; i++) { struct variable *v; char s[SHORT_NAME_LEN + 1]; @@ -307,27 +296,26 @@ build_dictionary (struct dictionary *dict, struct flip_pgm *flip) if (!make_new_var (dict, v->name)) return false; } - + return true; } - + /* Writes case C to the FLIP sink. Returns true if successful, false if an I/O error occurred. */ static bool write_flip_case (struct flip_pgm *flip, const struct ccase *c) { size_t i; - + flip->case_cnt++; if (flip->new_names != NULL) { struct varname *v = pool_alloc (flip->pool, sizeof *v); - int fv = flip->idx_to_fv[var_get_dict_index (flip->new_names)]; v->next = NULL; if (var_is_numeric (flip->new_names)) { - double f = case_num_idx (c, fv); + double f = case_num (c, flip->new_names); if (f == SYSMIS) strcpy (v->name, "VSYSMIS"); @@ -341,10 +329,10 @@ write_flip_case (struct flip_pgm *flip, const struct ccase *c) else { int width = MIN (var_get_width (flip->new_names), MAX_SHORT_STRING); - memcpy (v->name, case_str_idx (c, fv), width); + memcpy (v->name, case_str (c, flip->new_names), width); v->name[width] = 0; } - + if (flip->new_names_head == NULL) flip->new_names_head = v; else @@ -355,15 +343,8 @@ write_flip_case (struct flip_pgm *flip, const struct ccase *c) /* Write to external file. */ for (i = 0; i < flip->var_cnt; i++) { - double out; - - if (var_is_numeric (flip->var[i])) - { - int fv = flip->idx_to_fv[var_get_dict_index (flip->var[i])]; - out = case_num_idx (c, fv); - } - else - out = SYSMIS; + const struct variable *v = flip->var[i]; + double out = var_is_numeric (v) ? case_num (c, v) : SYSMIS; fwrite (&out, sizeof out, 1, flip->file); } return true; @@ -408,53 +389,45 @@ flip_file (struct flip_pgm *flip) output_buf = input_buf + flip->var_cnt * case_capacity; input_file = flip->file; - if (fseek (input_file, 0, SEEK_SET) != 0) + if (fseek (input_file, 0, SEEK_SET) != 0) { msg (SE, _("Error rewinding FLIP file: %s."), strerror (errno)); return false; } - + output_file = pool_tmpfile (flip->pool); - if (output_file == NULL) + if (output_file == NULL) { msg (SE, _("Error creating FLIP source file.")); return false; } - + for (case_idx = 0; case_idx < flip->case_cnt; ) { unsigned long read_cases = MIN (flip->case_cnt - case_idx, case_capacity); size_t i; - if (read_cases != fread (input_buf, case_bytes, read_cases, input_file)) + if (read_cases != fread (input_buf, case_bytes, read_cases, input_file)) { if (ferror (input_file)) msg (SE, _("Error reading FLIP file: %s."), strerror (errno)); else - msg (SE, _("Unexpected end of file reading FLIP file.")); + msg (SE, _("Unexpected end of file reading FLIP file.")); return false; } for (i = 0; i < flip->var_cnt; i++) { unsigned long j; - + for (j = 0; j < read_cases; j++) output_buf[j] = input_buf[i + j * flip->var_cnt]; -#ifndef HAVE_FSEEKO -#define fseeko fseek -#endif - -#ifndef HAVE_OFF_T -#define off_t long int -#endif - if (fseeko (output_file, sizeof *input_buf * (case_idx + (off_t) i * flip->case_cnt), - SEEK_SET) != 0) + SEEK_SET) != 0) { msg (SE, _("Error seeking FLIP source file: %s."), strerror (errno)); @@ -462,11 +435,11 @@ flip_file (struct flip_pgm *flip) } if (fwrite (output_buf, sizeof *output_buf, read_cases, output_file) - != read_cases) + != read_cases) { msg (SE, _("Error writing FLIP source file: %s."), strerror (errno)); - return false; + return false; } } @@ -480,11 +453,11 @@ flip_file (struct flip_pgm *flip) } pool_unregister (flip->pool, input_buf); free (input_buf); - - if (fseek (output_file, 0, SEEK_SET) != 0) + + if (fseek (output_file, 0, SEEK_SET) != 0) { msg (SE, _("Error rewinding FLIP source file: %s."), strerror (errno)); - return false; + return false; } flip->file = output_file; @@ -505,7 +478,7 @@ flip_casereader_read (struct casereader *reader UNUSED, void *flip_, return false; case_create (c, flip->case_cnt); - for (i = 0; i < flip->case_cnt; i++) + for (i = 0; i < flip->case_cnt; i++) { double in; if (fread (&in, sizeof in, 1, flip->file) != 1) @@ -523,7 +496,7 @@ flip_casereader_read (struct casereader *reader UNUSED, void *flip_, } case_data_rw_idx (c, i)->f = in; } - + flip->cases_read++; return true; @@ -541,7 +514,7 @@ flip_casereader_destroy (struct casereader *reader UNUSED, void *flip_) destroy_flip_pgm (flip); } -static const struct casereader_class flip_casereader_class = +static const struct casereader_class flip_casereader_class = { flip_casereader_read, flip_casereader_destroy,