-/* IMPORT command. */
-
-/* IMPORT input program. */
-struct import_pgm
- {
- struct pfm_reader *reader; /* Portable file reader. */
- struct case_map *map; /* Map from system file to active file dict. */
- struct ccase bounce; /* Bounce buffer. */
- };
-
-static void import_pgm_free (struct import_pgm *);
-
-/* Parses the IMPORT command. */
-int
-cmd_import (void)
-{
- struct import_pgm *pgm = NULL;
- struct file_handle *fh = NULL;
- struct dictionary *dict = NULL;
- int type;
-
- pgm = xmalloc (sizeof *pgm);
- pgm->reader = NULL;
- pgm->map = NULL;
- case_nullify (&pgm->bounce);
-
- for (;;)
- {
- lex_match ('/');
-
- if (lex_match_id ("FILE") || token == T_STRING)
- {
- lex_match ('=');
-
- fh = fh_parse ();
- if (fh == NULL)
- return CMD_FAILURE;
- }
- else if (lex_match_id ("TYPE"))
- {
- lex_match ('=');
-
- if (lex_match_id ("COMM"))
- type = PFM_COMM;
- else if (lex_match_id ("TAPE"))
- type = PFM_TAPE;
- else
- {
- lex_error (_("expecting COMM or TAPE"));
- return CMD_FAILURE;
- }
- }
- else break;
- }
- if (!lex_match ('/') && token != '.')
- {
- lex_error (NULL);
- return CMD_FAILURE;
- }
-
- discard_variables ();
-
- pgm->reader = pfm_open_reader (fh, &dict, NULL);
- if (pgm->reader == NULL)
- return CMD_FAILURE;
- case_create (&pgm->bounce, dict_get_next_value_idx (dict));
-
- start_case_map (dict);
- if (!trim_dictionary (dict, OP_READ, NULL))
- goto error;
- pgm->map = finish_case_map (dict);
-
- dict_destroy (default_dict);
- default_dict = dict;
-
- vfm_source = create_case_source (&import_source_class, pgm);
-
- return CMD_SUCCESS;
-
- error:
- import_pgm_free (pgm);
- if (dict != NULL)
- dict_destroy (dict);
- return CMD_FAILURE;
-}
-
-/* Frees a struct import_pgm. */
-static void
-import_pgm_free (struct import_pgm *pgm)
-{
- if (pgm != NULL)
- {
- pfm_close_reader (pgm->reader);
- destroy_case_map (pgm->map);
- case_destroy (&pgm->bounce);
- free (pgm);
- }
-}
-
-/* Clears internal state related to IMPORT input procedure. */
-static void
-import_source_destroy (struct case_source *source)
-{
- struct import_pgm *pgm = source->aux;
- import_pgm_free (pgm);
-}
-
-/* Reads all the cases from the data file into C and passes them
- to WRITE_CASE one by one, passing WC_DATA. */
-static void
-import_source_read (struct case_source *source,
- struct ccase *c,
- write_case_func *write_case, write_case_data wc_data)
-{
- struct import_pgm *pgm = source->aux;
- int ok;
-
- do
- {
- if (pgm->map == NULL)
- ok = pfm_read_case (pgm->reader, c);
- else
- {
- ok = pfm_read_case (pgm->reader, &pgm->bounce);
- if (ok)
- map_case (pgm->map, &pgm->bounce, c);
- }
-
- if (ok)
- ok = write_case (wc_data);
- }
- while (ok);
-}
-
-const struct case_source_class import_source_class =
- {
- "IMPORT",
- NULL,
- import_source_read,
- import_source_destroy,
- };