X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Fstats%2Fflip.c;h=a8f135806640ffb7888fccdcb0e04ea57c96ef70;hb=1c4b7cfcec922cf6d1e92a607f23f00d26e7324e;hp=59f0c090a09a07ebe5310db34a5049f6c4149988;hpb=d3fb40cc883e985d337811913b0c180fb226c76b;p=pspp-builds.git diff --git a/src/language/stats/flip.c b/src/language/stats/flip.c index 59f0c090..a8f13580 100644 --- a/src/language/stats/flip.c +++ b/src/language/stats/flip.c @@ -38,8 +38,10 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -47,6 +49,7 @@ #include #include "intprops.h" +#include "minmax.h" #include "gettext.h" #define _(msgid) gettext (msgid) @@ -78,29 +81,30 @@ struct flip_pgm }; static void destroy_flip_pgm (struct flip_pgm *); -static struct case_sink *flip_sink_create (struct flip_pgm *); +static struct case_sink *flip_sink_create (struct dictionary *d, struct flip_pgm *); static struct case_source *flip_source_create (struct flip_pgm *); static bool flip_file (struct flip_pgm *); -static int build_dictionary (struct flip_pgm *); +static int build_dictionary (struct dictionary *, struct flip_pgm *); static const struct case_source_class flip_source_class; static const struct case_sink_class flip_sink_class; /* Parses and executes FLIP. */ int -cmd_flip (void) +cmd_flip (struct lexer *lexer, struct dataset *ds) { struct flip_pgm *flip; struct case_sink *sink; + struct dictionary *dict = dataset_dict (ds); bool ok; - if (proc_make_temporary_transformations_permanent ()) + if (proc_make_temporary_transformations_permanent (ds)) msg (SW, _("FLIP ignores TEMPORARY. " "Temporary transformations will be made permanent.")); flip = pool_create_container (struct flip_pgm, pool); flip->var = NULL; - flip->idx_to_fv = dict_get_compacted_idx_to_fv (default_dict); + flip->idx_to_fv = dict_get_compacted_idx_to_fv (dict); pool_register (flip->pool, free, flip->idx_to_fv); flip->var_cnt = 0; flip->case_cnt = 0; @@ -109,29 +113,29 @@ cmd_flip (void) flip->new_names_tail = NULL; flip->file = NULL; - lex_match ('/'); - if (lex_match_id ("VARIABLES")) + lex_match (lexer, '/'); + if (lex_match_id (lexer, "VARIABLES")) { - lex_match ('='); - if (!parse_variables (default_dict, &flip->var, &flip->var_cnt, + lex_match (lexer, '='); + if (!parse_variables (lexer, dict, &flip->var, &flip->var_cnt, PV_NO_DUPLICATE)) goto error; - lex_match ('/'); + lex_match (lexer, '/'); } else - dict_get_vars (default_dict, &flip->var, &flip->var_cnt, 1u << DC_SYSTEM); + dict_get_vars (dict, &flip->var, &flip->var_cnt, 1u << DC_SYSTEM); pool_register (flip->pool, free, flip->var); - lex_match ('/'); - if (lex_match_id ("NEWNAMES")) + lex_match (lexer, '/'); + if (lex_match_id (lexer, "NEWNAMES")) { - lex_match ('='); - flip->new_names = parse_variable (); + lex_match (lexer, '='); + flip->new_names = parse_variable (lexer, dict); if (!flip->new_names) goto error; } else - flip->new_names = dict_lookup_var (default_dict, "CASE_LBL"); + flip->new_names = dict_lookup_var (dict, "CASE_LBL"); if (flip->new_names) { @@ -148,34 +152,34 @@ cmd_flip (void) /* Read the active file into a flip_sink. */ flip->case_cnt = 0; - proc_make_temporary_transformations_permanent (); - sink = flip_sink_create (flip); + proc_make_temporary_transformations_permanent (ds); + sink = flip_sink_create (dict, flip); if (sink == NULL) goto error; - proc_set_sink (sink); + proc_set_sink (ds, sink); flip->new_names_tail = NULL; - ok = procedure (NULL, NULL); + ok = procedure (ds,NULL, NULL); /* Flip the data we read. */ if (!flip_file (flip)) { - discard_variables (); + discard_variables (ds); goto error; } /* Flip the dictionary. */ - dict_clear (default_dict); - if (!build_dictionary (flip)) + dict_clear (dict); + if (!build_dictionary (dict, flip)) { - discard_variables (); + discard_variables (ds); goto error; } - flip->case_size = dict_get_case_size (default_dict); + flip->case_size = dict_get_case_size (dict); /* Set up flipped data for reading. */ - proc_set_source (flip_source_create (flip)); + proc_set_source (ds, flip_source_create (flip)); - return ok ? lex_end_of_command () : CMD_CASCADING_FAILURE; + return ok ? lex_end_of_command (lexer) : CMD_CASCADING_FAILURE; error: destroy_flip_pgm (flip); @@ -193,7 +197,7 @@ destroy_flip_pgm (struct flip_pgm *flip) /* Make a new variable with base name NAME, which is bowdlerized and mangled until acceptable, and returns success. */ static int -make_new_var (char name[]) +make_new_var (struct dictionary *dict, char name[]) { char *cp; @@ -217,7 +221,7 @@ make_new_var (char name[]) *cp = '\0'; str_uppercase (name); - if (dict_create_var (default_dict, name, 0)) + if (dict_create_var (dict, name, 0)) return 1; /* Add numeric extensions until acceptable. */ @@ -228,11 +232,11 @@ make_new_var (char name[]) for (i = 1; i < 10000000; i++) { - int ofs = min (7 - intlog10 (i), len); + int ofs = MIN (7 - intlog10 (i), len); memcpy (n, name, ofs); sprintf (&n[ofs], "%d", i); - if (dict_create_var (default_dict, n, 0)) + if (dict_create_var (dict, n, 0)) return 1; } } @@ -243,9 +247,9 @@ make_new_var (char name[]) /* Make a new dictionary for all the new variable names. */ static int -build_dictionary (struct flip_pgm *flip) +build_dictionary (struct dictionary *dict, struct flip_pgm *flip) { - dict_create_var_assert (default_dict, "CASE_LBL", 8); + dict_create_var_assert (dict, "CASE_LBL", 8); if (flip->new_names_head == NULL) { @@ -263,7 +267,7 @@ build_dictionary (struct flip_pgm *flip) char s[SHORT_NAME_LEN + 1]; sprintf (s, "VAR%03d", i); - v = dict_create_var_assert (default_dict, s, 0); + v = dict_create_var_assert (dict, s, 0); } } else @@ -271,7 +275,7 @@ build_dictionary (struct flip_pgm *flip) struct varname *v; for (v = flip->new_names_head; v; v = v->next) - if (!make_new_var (v->name)) + if (!make_new_var (dict, v->name)) return 0; } @@ -280,7 +284,7 @@ build_dictionary (struct flip_pgm *flip) /* Creates a flip sink based on FLIP. */ static struct case_sink * -flip_sink_create (struct flip_pgm *flip) +flip_sink_create (struct dictionary *dict, struct flip_pgm *flip) { size_t i; @@ -297,7 +301,7 @@ flip_sink_create (struct flip_pgm *flip) /* Write variable names as first case. */ for (i = 0; i < flip->var_cnt; i++) buf_copy_str_rpad (flip->output_buf[i].s, MAX_SHORT_STRING, - flip->var[i]->name); + var_get_name (flip->var[i])); if (fwrite (flip->output_buf, sizeof *flip->output_buf, flip->var_cnt, flip->file) != (size_t) flip->var_cnt) { @@ -307,7 +311,7 @@ flip_sink_create (struct flip_pgm *flip) flip->case_cnt = 1; - return create_case_sink (&flip_sink_class, default_dict, flip); + return create_case_sink (&flip_sink_class, dict, flip); } /* Writes case C to the FLIP sink. @@ -324,7 +328,7 @@ flip_sink_write (struct case_sink *sink, const struct ccase *c) { struct varname *v = pool_alloc (flip->pool, sizeof *v); v->next = NULL; - if (flip->new_names->type == NUMERIC) + if (var_is_numeric (flip->new_names)) { double f = case_num (c, flip->idx_to_fv[flip->new_names->index]); @@ -339,7 +343,7 @@ flip_sink_write (struct case_sink *sink, const struct ccase *c) } else { - int width = min (flip->new_names->width, MAX_SHORT_STRING); + int width = MIN (var_get_width (flip->new_names), MAX_SHORT_STRING); memcpy (v->name, case_str (c, flip->idx_to_fv[flip->new_names->index]), width); v->name[width] = 0; @@ -357,7 +361,7 @@ flip_sink_write (struct case_sink *sink, const struct ccase *c) { double out; - if (flip->var[i]->type == NUMERIC) + if (var_is_numeric (flip->var[i])) out = case_num (c, flip->idx_to_fv[flip->var[i]->index]); else out = SYSMIS; @@ -427,7 +431,7 @@ flip_file (struct flip_pgm *flip) for (case_idx = 0; case_idx < flip->case_cnt; ) { - unsigned long read_cases = min (flip->case_cnt - case_idx, + unsigned long read_cases = MIN (flip->case_cnt - case_idx, case_capacity); size_t i; @@ -537,7 +541,7 @@ flip_source_read (struct case_source *source, else if (feof (flip->file)) msg (SE, _("Unexpected end of file reading FLIP temporary file.")); else - abort (); + NOT_REACHED (); ok = false; break; }