X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Finpt-pgm.c;h=ef9c3646440cc889c8d0fcd2fee0e93ee927bd28;hb=1f053e35b27208cad9bec322c67ba4ef022c1dc1;hp=a4e9a1802b5a8f9f98f7a5840df9a08d61aa52a8;hpb=4944c86a9318bc5b5578ab145a95c116ffd2c9fd;p=pspp-builds.git diff --git a/src/inpt-pgm.c b/src/inpt-pgm.c index a4e9a180..ef9c3646 100644 --- a/src/inpt-pgm.c +++ b/src/inpt-pgm.c @@ -14,57 +14,58 @@ 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., 59 Temple Place - Suite 330, Boston, MA - 02111-1307, USA. */ + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. */ #include -#include +#include "error.h" #include #include #include "alloc.h" +#include "case.h" #include "command.h" -#include "dfm.h" +#include "data-list.h" +#include "dfm-read.h" +#include "dictionary.h" #include "error.h" -#include "expr.h" +#include "expressions/public.h" #include "file-handle.h" -#include "inpt-pgm.h" #include "lexer.h" #include "misc.h" #include "str.h" #include "var.h" #include "vfm.h" -#undef DEBUGGING -/*#define DEBUGGING 1*/ #include "debug-print.h" -/* A bit-vector of two-bit entries. The array tells INPUT PROGRAM how - to initialize each `value'. Modified by envector(), devector(), - which are called by create_variable(), also by LEAVE, COMPUTE(!). */ -unsigned char *inp_init; - -/* Number of bytes allocated for inp_init. */ -size_t inp_init_size; +/* Indicates how a `union value' should be initialized. */ +enum value_init_type + { + INP_NUMERIC = 01, /* Numeric. */ + INP_STRING = 0, /* String. */ + + INP_INIT_ONCE = 02, /* Initialize only once. */ + INP_REINIT = 0, /* Reinitialize for each iteration. */ + }; -/* Number of `values' created inside INPUT PROGRAM. */ -static int inp_nval; +struct input_program_pgm + { + enum value_init_type *init; /* How to initialize each `union value'. */ + size_t init_cnt; /* Number of elements in inp_init. */ + size_t case_size; /* Size of case in bytes. */ + }; -static int end_case_trns_proc (struct trns_header *, struct ccase *); -static int end_file_trns_proc (struct trns_header * t, struct ccase * c); -static int reread_trns_proc (struct trns_header *, struct ccase *); -static void reread_trns_free (struct trns_header *); +static trns_proc_func end_case_trns_proc, reread_trns_proc, end_file_trns_proc; +static trns_free_func reread_trns_free; int cmd_input_program (void) { - lex_match_id ("INPUT"); - lex_match_id ("PROGRAM"); discard_variables (); - vfm_source = &input_program_source; - - inp_init = NULL; - inp_init_size = 0; + /* FIXME: we shouldn't do this here, but I'm afraid that other + code will check the class of vfm_source. */ + vfm_source = create_case_source (&input_program_source_class, NULL); return lex_end_of_command (); } @@ -72,180 +73,161 @@ cmd_input_program (void) int cmd_end_input_program (void) { - lex_match_id ("END"); - lex_match_id ("INPUT"); - lex_match_id ("PROGRAM"); + struct input_program_pgm *inp; + size_t i; - if (vfm_source != &input_program_source) + if (!case_source_is_class (vfm_source, &input_program_source_class)) { msg (SE, _("No matching INPUT PROGRAM command.")); return CMD_FAILURE; } - if (default_dict.nval == 0) + if (dict_get_next_value_idx (default_dict) == 0) msg (SW, _("No data-input or transformation commands specified " "between INPUT PROGRAM and END INPUT PROGRAM.")); - /* Mark the boundary between INPUT PROGRAM and more-mundane - transformations. */ + /* Mark the boundary between INPUT PROGRAM transformations and + ordinary transformations. */ f_trns = n_trns; - /* Mark the boundary between input program `values' and - later-created `values'. */ - inp_nval = default_dict.nval; + /* Figure out how to initialize each input case. */ + inp = xmalloc (sizeof *inp); + inp->init_cnt = dict_get_next_value_idx (default_dict); + inp->init = xmalloc (inp->init_cnt * sizeof *inp->init); + for (i = 0; i < inp->init_cnt; i++) + inp->init[i] = -1; + for (i = 0; i < dict_get_var_cnt (default_dict); i++) + { + struct variable *var = dict_get_var (default_dict, i); + enum value_init_type value_init; + size_t j; + + value_init = var->type == NUMERIC ? INP_NUMERIC : INP_STRING; + value_init |= var->reinit ? INP_REINIT : INP_INIT_ONCE; + + for (j = 0; j < var->nv; j++) + inp->init[j + var->fv] = value_init; + } + for (i = 0; i < inp->init_cnt; i++) + assert (inp->init[i] != -1); + inp->case_size = dict_get_case_size (default_dict); + + /* Put inp into vfm_source for later use. */ + vfm_source->aux = inp; return lex_end_of_command (); } -/* Initializes temp_case. Called before the first case is read. */ +/* Initializes case C. Called before the first case is read. */ static void -init_case (void) +init_case (const struct input_program_pgm *inp, struct ccase *c) { - union value *val = temp_case->data; - unsigned char *cp = inp_init; - unsigned char c; - int i, j; - - /* This code is 2-3X the complexity it might be, but I felt like - it. It initializes temp_case union values to 0, or SYSMIS, or - blanks, as appropriate. */ - for (i = 0; i < inp_nval / 4; i++) - { - c = *cp++; - for (j = 0; j < 4; j++) - { - switch (c & INP_MASK) - { - case INP_NUMERIC | INP_RIGHT: - val++->f = SYSMIS; - break; - case INP_NUMERIC | INP_LEFT: - val++->f = 0.0; - break; - case INP_STRING | INP_RIGHT: - case INP_STRING | INP_LEFT: - memset (val++->s, ' ', MAX_SHORT_STRING); - break; - } - c >>= 2; - } - } - if (inp_nval % 4) - { - c = *cp; - for (j = 0; j < inp_nval % 4; j++) - { - switch (c & INP_MASK) - { - case INP_NUMERIC | INP_RIGHT: - val++->f = SYSMIS; - break; - case INP_NUMERIC | INP_LEFT: - val++->f = 0.0; - break; - case INP_STRING | INP_RIGHT: - case INP_STRING | INP_LEFT: - memset (val++->s, ' ', MAX_SHORT_STRING); - break; - } - c >>= 2; - } - } + size_t i; + + for (i = 0; i < inp->init_cnt; i++) + switch (inp->init[i]) + { + case INP_NUMERIC | INP_INIT_ONCE: + case_data_rw (c, i)->f = 0.0; + break; + case INP_NUMERIC | INP_REINIT: + case_data_rw (c, i)->f = SYSMIS; + break; + case INP_STRING | INP_INIT_ONCE: + case INP_STRING | INP_REINIT: + memset (case_data_rw (c, i)->s, ' ', sizeof case_data_rw (c, i)->s); + break; + default: + assert (0); + } } -/* Clears temp_case. Called between reading successive records. */ +/* Clears case C. Called between reading successive records. */ static void -clear_case (void) +clear_case (const struct input_program_pgm *inp, struct ccase *c) { - union value *val = temp_case->data; - unsigned char *cp = inp_init; - unsigned char c; - int i, j; - - /* This code is 2-3X the complexity it might be, but I felt like - it. It initializes temp_case values to SYSMIS, or - blanks, or does nothing, as appropriate. */ - for (i = 0; i < inp_nval / 4; i++) - { - c = *cp++; - for (j = 0; j < 4; j++) - { - if (!(c & INP_LEFT)) - { - if (c & INP_STRING) - memset (val->s, ' ', MAX_SHORT_STRING); - else - val->f = SYSMIS; - } - val++; - c >>= 2; - } - } - - if (inp_nval % 4) - { - c = *cp; - for (j = 0; j < inp_nval % 4; j++) - { - if (!(c & INP_LEFT)) - { - if (c & INP_STRING) - memset (val->s, ' ', MAX_SHORT_STRING); - else - val->f = SYSMIS; - } - val++; - c >>= 2; - } - } + size_t i; + + for (i = 0; i < inp->init_cnt; i++) + switch (inp->init[i]) + { + case INP_NUMERIC | INP_INIT_ONCE: + break; + case INP_NUMERIC | INP_REINIT: + case_data_rw (c, i)->f = SYSMIS; + break; + case INP_STRING | INP_INIT_ONCE: + break; + case INP_STRING | INP_REINIT: + memset (case_data_rw (c, i)->s, ' ', sizeof case_data_rw (c, i)->s); + break; + default: + assert (0); + } } /* Executes each transformation in turn on a `blank' case. When a transformation fails, returning -2, then that's the end of the file. -1 means go on to the next transformation. Otherwise the return value is the index of the transformation to go to next. */ -void -input_program_source_read (void) +static void +input_program_source_read (struct case_source *source, + struct ccase *c, + write_case_func *write_case, + write_case_data wc_data) { + struct input_program_pgm *inp = source->aux; int i; /* Nonzero if there were any END CASE commands in the set of - transformations. */ + transformations. If so, we don't automatically write out + cases. */ int end_case = 0; - /* We don't automatically write out cases if the user took over - that prerogative. */ + /* FIXME? This is the number of cases sent out of the input + program, not the number of cases written to the procedure. + The difference should only show up in $CASENUM in COMPUTE. + We should check behavior against SPSS. */ + int cases_written = 0; + + assert (inp != NULL); + + /* Figure end_case. */ for (i = 0; i < f_trns; i++) if (t_trns[i]->proc == end_case_trns_proc) end_case = 1; - init_case (); + /* FIXME: This is an ugly kluge. */ + for (i = 0; i < f_trns; i++) + if (t_trns[i]->proc == repeating_data_trns_proc) + repeating_data_set_write_case (t_trns[i], write_case, wc_data); + + init_case (inp, c); for (;;) { - /* Index of current transformation. */ - int i; - - /* Return value of last-called transformation. */ - int code; - - debug_printf (("input-program: ")); - /* Perform transformations on `blank' case. */ - for (i = 0; i < f_trns;) + for (i = 0; i < f_trns; ) { -#if DEBUGGING - printf ("/%d", i); - if (t_trns[i]->proc == end_case_trns_proc) - printf ("\n"); -#endif - code = t_trns[i]->proc (t_trns[i], temp_case); + int code; /* Return value of last-called transformation. */ + + if (t_trns[i]->proc == end_case_trns_proc) + { + cases_written++; + if (!write_case (wc_data)) + goto done; + clear_case (inp, c); + i++; + continue; + } + + code = t_trns[i]->proc (t_trns[i], c, cases_written + 1); switch (code) { case -1: i++; break; case -2: - return; + goto done; case -3: goto next_case; default: @@ -254,39 +236,42 @@ input_program_source_read (void) } } -#if DEBUGGING - if (!end_case) - printf ("\n"); -#endif - /* Write the case if appropriate. */ - if (!end_case) - if (!write_case ()) - return; + if (!end_case) + { + cases_written++; + if (!write_case (wc_data)) + break; + } /* Blank out the case for the next iteration. */ next_case: - clear_case (); + clear_case (inp, c); } + done: ; } +/* Destroys an INPUT PROGRAM source. */ static void -input_program_source_destroy_source (void) +input_program_source_destroy (struct case_source *source) { + struct input_program_pgm *inp = source->aux; + cancel_transformations (); - free (inp_init); - inp_init = NULL; + + if (inp != NULL) + { + free (inp->init); + free (inp); + } } -struct case_stream input_program_source = +const struct case_source_class input_program_source_class = { + "INPUT PROGRAM", NULL, input_program_source_read, - NULL, - NULL, - input_program_source_destroy_source, - NULL, - "INPUT PROGRAM", + input_program_source_destroy, }; int @@ -294,10 +279,7 @@ cmd_end_case (void) { struct trns_header *t; - lex_match_id ("END"); - lex_match_id ("CASE"); - - if (vfm_source != &input_program_source) + if (!case_source_is_class (vfm_source, &input_program_source_class)) { msg (SE, _("This command may only be executed between INPUT PROGRAM " "and END INPUT PROGRAM.")); @@ -312,16 +294,14 @@ cmd_end_case (void) return lex_end_of_command (); } +/* Should never be called, because this is handled in + input_program_source_read(). */ int -end_case_trns_proc (struct trns_header *t unused, struct ccase * c unused) +end_case_trns_proc (struct trns_header *t UNUSED, struct ccase * c UNUSED, + int case_num UNUSED) { -#if DEBUGGING - printf ("END CASE\n"); -#endif - if (!write_case ()) - return -2; - clear_case (); - return -1; + assert (0); + abort (); } /* REREAD transformation. */ @@ -329,7 +309,7 @@ struct reread_trns { struct trns_header h; - struct file_handle *handle; /* File to move file pointer back on. */ + struct dfm_reader *reader; /* File to move file pointer back on. */ struct expression *column; /* Column to reset file pointer to. */ }; @@ -337,18 +317,11 @@ struct reread_trns int cmd_reread (void) { - /* File to be re-read. */ - struct file_handle *h; - - /* Expression for column to set file pointer to. */ - struct expression *e; + struct file_handle *fh; /* File to be re-read. */ + struct expression *e; /* Expression for column to set. */ + struct reread_trns *t; /* Created transformation. */ - /* Created transformation. */ - struct reread_trns *t; - - lex_match_id ("REREAD"); - - h = default_handle; + fh = default_handle; e = NULL; while (token != '.') { @@ -363,21 +336,15 @@ cmd_reread (void) return CMD_FAILURE; } - e = expr_parse (PXP_NUMERIC); + e = expr_parse (default_dict, EXPR_NUMBER); if (!e) return CMD_FAILURE; } else if (lex_match_id ("FILE")) { lex_match ('='); - if (token != T_ID) - { - lex_error (_("expecting file handle name")); - expr_free (e); - return CMD_FAILURE; - } - h = fh_get_handle_by_name (tokid); - if (!h) + fh = fh_parse (); + if (fh == NULL) { expr_free (e); return CMD_FAILURE; @@ -394,41 +361,44 @@ cmd_reread (void) t = xmalloc (sizeof *t); t->h.proc = reread_trns_proc; t->h.free = reread_trns_free; - t->handle = h; + t->reader = dfm_open_reader (fh); t->column = e; add_transformation ((struct trns_header *) t); return CMD_SUCCESS; } +/* Executes a REREAD transformation. */ static int -reread_trns_proc (struct trns_header * pt, struct ccase * c) +reread_trns_proc (struct trns_header * pt, struct ccase * c, + int case_num) { struct reread_trns *t = (struct reread_trns *) pt; if (t->column == NULL) - dfm_bkwd_record (t->handle, 1); + dfm_reread_record (t->reader, 1); else { - union value column; - - expr_evaluate (t->column, c, &column); - if (!finite (column.f) || column.f < 1) + double column = expr_evaluate_num (t->column, c, case_num); + if (!finite (column) || column < 1) { msg (SE, _("REREAD: Column numbers must be positive finite " "numbers. Column set to 1.")); - dfm_bkwd_record (t->handle, 1); + dfm_reread_record (t->reader, 1); } else - dfm_bkwd_record (t->handle, column.f); + dfm_reread_record (t->reader, column); } return -1; } +/* Frees a REREAD transformation. */ static void -reread_trns_free (struct trns_header * t) +reread_trns_free (struct trns_header *t_) { - expr_free (((struct reread_trns *) t)->column); + struct reread_trns *t = (struct reread_trns *) t_; + expr_free (t->column); + dfm_close_reader (t->reader); } /* Parses END FILE command. */ @@ -437,10 +407,7 @@ cmd_end_file (void) { struct trns_header *t; - lex_match_id ("END"); - lex_match_id ("FILE"); - - if (vfm_source != &input_program_source) + if (!case_source_is_class (vfm_source, &input_program_source_class)) { msg (SE, _("This command may only be executed between INPUT PROGRAM " "and END INPUT PROGRAM.")); @@ -455,11 +422,10 @@ cmd_end_file (void) return lex_end_of_command (); } +/* Executes an END FILE transformation. */ static int -end_file_trns_proc (struct trns_header * t unused, struct ccase * c unused) +end_file_trns_proc (struct trns_header * t UNUSED, struct ccase * c UNUSED, + int case_num UNUSED) { -#if DEBUGGING - printf ("END FILE\n"); -#endif return -2; }