projects
/
pspp
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Added fh_free function.
[pspp]
/
src
/
inpt-pgm.c
diff --git
a/src/inpt-pgm.c
b/src/inpt-pgm.c
index 697d7f5ba59726120f77492504eeeb603dc35c83..eaa19532eaef4b80ecf2921dafaaa3b0efde03e0 100644
(file)
--- a/
src/inpt-pgm.c
+++ b/
src/inpt-pgm.c
@@
-14,19
+14,21
@@
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
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., 5
9 Temple Place - Suite 330
, Boston, MA
- 0211
1-1307
, USA. */
+ Foundation, Inc., 5
1 Franklin Street, Fifth Floor
, Boston, MA
+ 0211
0-1301
, USA. */
#include <config.h>
#include <config.h>
-#include
<assert.h>
+#include
"error.h"
#include <float.h>
#include <stdlib.h>
#include "alloc.h"
#include <float.h>
#include <stdlib.h>
#include "alloc.h"
+#include "case.h"
#include "command.h"
#include "data-list.h"
#include "command.h"
#include "data-list.h"
-#include "dfm.h"
+#include "dfm-read.h"
+#include "dictionary.h"
#include "error.h"
#include "error.h"
-#include "expr.h"
+#include "expr
essions/public
.h"
#include "file-handle.h"
#include "lexer.h"
#include "misc.h"
#include "file-handle.h"
#include "lexer.h"
#include "misc.h"
@@
-34,6
+36,9
@@
#include "var.h"
#include "vfm.h"
#include "var.h"
#include "vfm.h"
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+
#include "debug-print.h"
/* Indicates how a `union value' should be initialized. */
#include "debug-print.h"
/* Indicates how a `union value' should be initialized. */
@@
-50,6
+55,7
@@
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. */
{
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 trns_proc_func end_case_trns_proc, reread_trns_proc, end_file_trns_proc;
};
static trns_proc_func end_case_trns_proc, reread_trns_proc, end_file_trns_proc;
@@
-58,10
+64,10
@@
static trns_free_func reread_trns_free;
int
cmd_input_program (void)
{
int
cmd_input_program (void)
{
- lex_match_id ("INPUT");
- lex_match_id ("PROGRAM");
discard_variables ();
discard_variables ();
+ /* 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 ();
vfm_source = create_case_source (&input_program_source_class, NULL);
return lex_end_of_command ();
@@
-73,10
+79,6
@@
cmd_end_input_program (void)
struct input_program_pgm *inp;
size_t i;
struct input_program_pgm *inp;
size_t i;
- lex_match_id ("END");
- lex_match_id ("INPUT");
- lex_match_id ("PROGRAM");
-
if (!case_source_is_class (vfm_source, &input_program_source_class))
{
msg (SE, _("No matching INPUT PROGRAM command."));
if (!case_source_is_class (vfm_source, &input_program_source_class))
{
msg (SE, _("No matching INPUT PROGRAM command."));
@@
-91,10
+93,10
@@
cmd_end_input_program (void)
ordinary transformations. */
f_trns = n_trns;
ordinary transformations. */
f_trns = n_trns;
- /* Figure out how to initialize
temp_
case. */
+ /* Figure out how to initialize
each input
case. */
inp = xmalloc (sizeof *inp);
inp->init_cnt = dict_get_next_value_idx (default_dict);
inp = xmalloc (sizeof *inp);
inp->init_cnt = dict_get_next_value_idx (default_dict);
- inp->init = x
malloc (inp->init_cnt *
sizeof *inp->init);
+ inp->init = x
nmalloc (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++)
for (i = 0; i < inp->init_cnt; i++)
inp->init[i] = -1;
for (i = 0; i < dict_get_var_cnt (default_dict); i++)
@@
-111,6
+113,7
@@
cmd_end_input_program (void)
}
for (i = 0; i < inp->init_cnt; i++)
assert (inp->init[i] != -1);
}
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;
/* Put inp into vfm_source for later use. */
vfm_source->aux = inp;
@@
-118,9
+121,9
@@
cmd_end_input_program (void)
return lex_end_of_command ();
}
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
static void
-init_case (
struct input_program_pgm *inp
)
+init_case (
const struct input_program_pgm *inp, struct ccase *c
)
{
size_t i;
{
size_t i;
@@
-128,23
+131,23
@@
init_case (struct input_program_pgm *inp)
switch (inp->init[i])
{
case INP_NUMERIC | INP_INIT_ONCE:
switch (inp->init[i])
{
case INP_NUMERIC | INP_INIT_ONCE:
-
temp_case->data[i].
f = 0.0;
+
case_data_rw (c, i)->
f = 0.0;
break;
case INP_NUMERIC | INP_REINIT:
break;
case INP_NUMERIC | INP_REINIT:
-
temp_case->data[i].
f = SYSMIS;
+
case_data_rw (c, i)->
f = SYSMIS;
break;
case INP_STRING | INP_INIT_ONCE:
case INP_STRING | INP_REINIT:
break;
case INP_STRING | INP_INIT_ONCE:
case INP_STRING | INP_REINIT:
- memset (
temp_case->data[i].s, ' ', sizeof temp_case->data[i].
s);
+ memset (
case_data_rw (c, i)->s, ' ', sizeof case_data_rw (c, i)->
s);
break;
default:
assert (0);
}
}
break;
default:
assert (0);
}
}
-/* Clears
temp_case
. Called between reading successive records. */
+/* Clears
case C
. Called between reading successive records. */
static void
static void
-clear_case (
struct input_program_pgm *inp
)
+clear_case (
const struct input_program_pgm *inp, struct ccase *c
)
{
size_t i;
{
size_t i;
@@
-154,12
+157,12
@@
clear_case (struct input_program_pgm *inp)
case INP_NUMERIC | INP_INIT_ONCE:
break;
case INP_NUMERIC | INP_REINIT:
case INP_NUMERIC | INP_INIT_ONCE:
break;
case INP_NUMERIC | INP_REINIT:
-
temp_case->data[i].
f = SYSMIS;
+
case_data_rw (c, i)->
f = SYSMIS;
break;
case INP_STRING | INP_INIT_ONCE:
break;
case INP_STRING | INP_REINIT:
break;
case INP_STRING | INP_INIT_ONCE:
break;
case INP_STRING | INP_REINIT:
- memset (
temp_case->data[i].s, ' ', sizeof temp_case->data[i].
s);
+ memset (
case_data_rw (c, i)->s, ' ', sizeof case_data_rw (c, i)->
s);
break;
default:
assert (0);
break;
default:
assert (0);
@@
-172,11
+175,12
@@
clear_case (struct input_program_pgm *inp)
return value is the index of the transformation to go to next. */
static void
input_program_source_read (struct case_source *source,
return value is the index of the transformation to go to next. */
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;
write_case_func *write_case,
write_case_data wc_data)
{
struct input_program_pgm *inp = source->aux;
-
in
t i;
+
size_
t i;
/* Nonzero if there were any END CASE commands in the set of
transformations. If so, we don't automatically write out
/* Nonzero if there were any END CASE commands in the set of
transformations. If so, we don't automatically write out
@@
-190,56
+194,43
@@
input_program_source_read (struct case_source *source,
int cases_written = 0;
assert (inp != NULL);
int cases_written = 0;
assert (inp != NULL);
-
+
/* Figure end_case. */
for (i = 0; i < f_trns; i++)
/* Figure end_case. */
for (i = 0; i < f_trns; i++)
- if (t_trns[i]
->
proc == end_case_trns_proc)
+ if (t_trns[i]
.
proc == end_case_trns_proc)
end_case = 1;
end_case = 1;
- /* FIXME: This code should not be necessary. It is an ugly
- kluge. */
+ /* FIXME: This is an ugly kluge. */
for (i = 0; i < f_trns; i++)
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);
+ if (t_trns[i]
.
proc == repeating_data_trns_proc)
+ repeating_data_set_write_case (t_trns[i]
.private
, write_case, wc_data);
- init_case (inp);
+ init_case (inp
, c
);
for (;;)
{
for (;;)
{
- /* Index of current transformation. */
- int i;
-
- /* Return value of last-called transformation. */
- int code;
-
- debug_printf (("input-program: "));
-
/* Perform transformations on `blank' case. */
/* 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
+ int code; /* Return value of last-called transformation. */
- if (t_trns[i]
->
proc == end_case_trns_proc)
+ if (t_trns[i]
.
proc == end_case_trns_proc)
{
cases_written++;
if (!write_case (wc_data))
{
cases_written++;
if (!write_case (wc_data))
-
return
;
- clear_case (inp);
+
goto done
;
+ clear_case (inp
, c
);
i++;
continue;
}
i++;
continue;
}
- code = t_trns[i]
->proc (t_trns[i], temp_case
, cases_written + 1);
+ code = t_trns[i]
.proc (t_trns[i].private, c
, cases_written + 1);
switch (code)
{
case -1:
i++;
break;
case -2:
switch (code)
{
case -1:
i++;
break;
case -2:
-
return
;
+
goto done
;
case -3:
goto next_case;
default:
case -3:
goto next_case;
default:
@@
-248,22
+239,22
@@
input_program_source_read (struct case_source *source,
}
}
}
}
-#if DEBUGGING
- if (!end_case)
- printf ("\n");
-#endif
-
/* Write the case if appropriate. */
/* Write the case if appropriate. */
- if (!end_case)
- if (!write_case (wc_data))
- return;
+ if (!end_case)
+ {
+ cases_written++;
+ if (!write_case (wc_data))
+ break;
+ }
/* Blank out the case for the next iteration. */
next_case:
/* Blank out the case for the next iteration. */
next_case:
- clear_case (inp);
+ clear_case (inp
, c
);
}
}
+ done: ;
}
}
+/* Destroys an INPUT PROGRAM source. */
static void
input_program_source_destroy (struct case_source *source)
{
static void
input_program_source_destroy (struct case_source *source)
{
@@
-289,11
+280,6
@@
const struct case_source_class input_program_source_class =
int
cmd_end_case (void)
{
int
cmd_end_case (void)
{
- struct trns_header *t;
-
- lex_match_id ("END");
- lex_match_id ("CASE");
-
if (!case_source_is_class (vfm_source, &input_program_source_class))
{
msg (SE, _("This command may only be executed between INPUT PROGRAM "
if (!case_source_is_class (vfm_source, &input_program_source_class))
{
msg (SE, _("This command may only be executed between INPUT PROGRAM "
@@
-301,27
+287,25
@@
cmd_end_case (void)
return CMD_FAILURE;
}
return CMD_FAILURE;
}
- t = xmalloc (sizeof *t);
- t->proc = end_case_trns_proc;
- t->free = NULL;
- add_transformation ((struct trns_header *) t);
+ add_transformation (end_case_trns_proc, NULL, NULL);
return lex_end_of_command ();
}
return lex_end_of_command ();
}
+/* Should never be called, because this is handled in
+ input_program_source_read(). */
int
int
-end_case_trns_proc (
struct trns_header *t UNUSED, struct ccase *
c UNUSED,
+end_case_trns_proc (
void *trns_ UNUSED, struct ccase *
c UNUSED,
int case_num UNUSED)
{
assert (0);
int case_num UNUSED)
{
assert (0);
+ abort ();
}
/* REREAD transformation. */
struct reread_trns
{
}
/* REREAD transformation. */
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. */
};
struct expression *column; /* Column to reset file pointer to. */
};
@@
-329,18
+313,11
@@
struct reread_trns
int
cmd_reread (void)
{
int
cmd_reread (void)
{
- /* File to be re-read. */
- struct file_handle *h;
-
- /* Expression for column to set file pointer to. */
- struct expression *e;
-
- /* Created transformation. */
- struct reread_trns *t;
+ struct file_handle *fh; /* File to be re-read. */
+ struct expression *e; /* Expression for column to set. */
+ struct reread_trns *t; /* Created transformation. */
- lex_match_id ("REREAD");
-
- h = default_handle;
+ fh = default_handle;
e = NULL;
while (token != '.')
{
e = NULL;
while (token != '.')
{
@@
-355,21
+332,15
@@
cmd_reread (void)
return CMD_FAILURE;
}
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 (!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;
{
expr_free (e);
return CMD_FAILURE;
@@
-384,55
+355,49
@@
cmd_reread (void)
}
t = xmalloc (sizeof *t);
}
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;
t->column = e;
- add_transformation (
(struct trns_header *)
t);
+ add_transformation (
reread_trns_proc, reread_trns_free,
t);
return CMD_SUCCESS;
}
return CMD_SUCCESS;
}
+/* Executes a REREAD transformation. */
static int
static int
-reread_trns_proc (struct trns_header * pt, struct ccase * c,
- int case_num)
+reread_trns_proc (void *t_, struct ccase *c, int case_num)
{
{
- struct reread_trns *t =
(struct reread_trns *) pt
;
+ struct reread_trns *t =
t_
;
if (t->column == NULL)
if (t->column == NULL)
- dfm_
bkwd_record (t->handle
, 1);
+ dfm_
reread_record (t->reader
, 1);
else
{
else
{
- union value column;
-
- expr_evaluate (t->column, c, case_num, &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."));
{
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
}
else
- dfm_
bkwd_record (t->handle, column.f
);
+ dfm_
reread_record (t->reader, column
);
}
return -1;
}
}
return -1;
}
+/* Frees a REREAD transformation. */
static void
static void
-reread_trns_free (
struct trns_header * t
)
+reread_trns_free (
void *t_
)
{
{
- expr_free (((struct reread_trns *) t)->column);
+ struct reread_trns *t = t_;
+ expr_free (t->column);
+ dfm_close_reader (t->reader);
}
/* Parses END FILE command. */
int
cmd_end_file (void)
{
}
/* Parses END FILE command. */
int
cmd_end_file (void)
{
- struct trns_header *t;
-
- lex_match_id ("END");
- lex_match_id ("FILE");
-
if (!case_source_is_class (vfm_source, &input_program_source_class))
{
msg (SE, _("This command may only be executed between INPUT PROGRAM "
if (!case_source_is_class (vfm_source, &input_program_source_class))
{
msg (SE, _("This command may only be executed between INPUT PROGRAM "
@@
-440,20
+405,15
@@
cmd_end_file (void)
return CMD_FAILURE;
}
return CMD_FAILURE;
}
- t = xmalloc (sizeof *t);
- t->proc = end_file_trns_proc;
- t->free = NULL;
- add_transformation ((struct trns_header *) t);
+ add_transformation (end_file_trns_proc, NULL, NULL);
return lex_end_of_command ();
}
return lex_end_of_command ();
}
+/* Executes an END FILE transformation. */
static int
static int
-end_file_trns_proc (
struct trns_header * t UNUSED, struct ccase *
c UNUSED,
+end_file_trns_proc (
void *trns_ UNUSED, struct ccase *
c UNUSED,
int case_num UNUSED)
{
int case_num UNUSED)
{
-#if DEBUGGING
- printf ("END FILE\n");
-#endif
return -2;
}
return -2;
}