/* Closes reader R opened by dfm_open_reader(). */ void dfm_close_reader (struct dfm_reader *r) { if (r == NULL) return; if (fh_unlock (r->lock)) { /* File is still locked by another client. */ return; } /* This was the last client, so close the underlying file. */ if (fh_get_referent (r->fh) != FH_REF_INLINE) fn_close (fh_get_file_name (r->fh), r->file); else { /* Skip any remaining data on the inline file. */ if (r->flags & DFM_SAW_BEGIN_DATA) { dfm_reread_record (r, 0); while (!dfm_eof (r)) dfm_forward_record (r); } } fh_unref (r->fh); ds_destroy (&r->line); ds_destroy (&r->scratch); free (r); }
/* Make HANDLE unnamed, so that it can no longer be referenced by name. The caller must hold a reference to HANDLE, which is not affected by this function. */ static void unname_handle (struct file_handle *handle) { assert (handle->id != NULL); free (handle->id); handle->id = NULL; hmap_delete (&named_handles, &handle->name_node); /* Drop the reference held by the named_handles table. */ fh_unref (handle); }
/* Sets NEW_DEFAULT_HANDLE as the default handle. */ void fh_set_default_handle (struct file_handle *new_default_handle) { assert (new_default_handle == NULL || (new_default_handle->referent & (FH_REF_INLINE | FH_REF_FILE))); if (default_handle != NULL) fh_unref (default_handle); default_handle = new_default_handle; if (default_handle != NULL) fh_ref (default_handle); }
/* Opens the file designated by file handle FH for reading as a data file. Providing fh_inline_file() for FH designates the "inline file", that is, data included inline in the command file between BEGIN FILE and END FILE. Returns a reader if successful, or a null pointer otherwise. */ struct dfm_reader * dfm_open_reader (struct file_handle *fh, struct lexer *lexer) { struct dfm_reader *r; struct fh_lock *lock; /* TRANSLATORS: this fragment will be interpolated into messages in fh_lock() that identify types of files. */ lock = fh_lock (fh, FH_REF_FILE | FH_REF_INLINE, N_("data file"), FH_ACC_READ, false); if (lock == NULL) return NULL; r = fh_lock_get_aux (lock); if (r != NULL) return r; r = xmalloc (sizeof *r); r->fh = fh_ref (fh); r->lock = lock; r->lexer = lexer; ds_init_empty (&r->line); ds_init_empty (&r->scratch); r->flags = DFM_ADVANCE; r->eof_cnt = 0; r->block_left = 0; if (fh_get_referent (fh) != FH_REF_INLINE) { struct stat s; r->line_number = 0; r->file = fn_open (fh_get_file_name (fh), "rb"); if (r->file == NULL) { msg (ME, _("Could not open `%s' for reading as a data file: %s."), fh_get_file_name (r->fh), strerror (errno)); fh_unlock (r->lock); fh_unref (fh); free (r); return NULL; } r->file_size = fstat (fileno (r->file), &s) == 0 ? s.st_size : -1; } else r->file_size = -1; fh_lock_set_aux (lock, r); return r; }
/* Closes a CSV file after we're done with it. Returns true if successful, false if an I/O error occurred. */ bool close_writer (struct csv_writer *w) { size_t i; bool ok; if (w == NULL) return true; ok = true; if (w->file != NULL) { if (write_error (w)) ok = false; if (fclose (w->file) == EOF) ok = false; if (!ok) msg (ME, _("An I/O error occurred writing CSV file `%s'."), fh_get_file_name (w->fh)); if (ok ? !replace_file_commit (w->rf) : !replace_file_abort (w->rf)) ok = false; } fh_unlock (w->lock); fh_unref (w->fh); free (w->encoding); for (i = 0; i < w->n_csv_vars; i++) { struct csv_var *cv = &w->csv_vars[i]; mv_destroy (&cv->missing); val_labs_destroy (cv->val_labs); } free (w->csv_vars); free (w); return ok; }
/* Closes all the files in PROC and frees their associated data. */ static void close_all_comb_files (struct comb_proc *proc) { size_t i; for (i = 0; i < proc->n_files; i++) { struct comb_file *file = &proc->files[i]; subcase_destroy (&file->by_vars); subcase_destroy (&file->src); subcase_destroy (&file->dst); free (file->mv); fh_unref (file->handle); dict_destroy (file->dict); casereader_destroy (file->reader); case_unref (file->data); free (file->in_name); } free (proc->files); proc->files = NULL; proc->n_files = 0; }
int cmd_save_translate (struct lexer *lexer, struct dataset *ds) { enum { CSV_FILE = 1, TAB_FILE } type; struct dictionary *dict; struct case_map *map; struct casewriter *writer; struct file_handle *handle; struct csv_writer_options csv_opts; bool replace; bool retain_unselected; bool recode_user_missing; bool include_var_names; bool use_value_labels; bool use_print_formats; char decimal; char delimiter; char qualifier; bool ok; type = 0; dict = dict_clone (dataset_dict (ds)); map = NULL; handle = NULL; replace = false; retain_unselected = true; recode_user_missing = false; include_var_names = false; use_value_labels = false; use_print_formats = false; decimal = settings_get_decimal_char (FMT_F); delimiter = 0; qualifier = '"'; case_map_prepare_dict (dict); dict_delete_scratch_vars (dict); while (lex_match (lexer, T_SLASH)) { if (lex_match_id (lexer, "OUTFILE")) { if (handle != NULL) { lex_sbc_only_once ("OUTFILE"); goto error; } lex_match (lexer, T_EQUALS); handle = fh_parse (lexer, FH_REF_FILE, NULL); if (handle == NULL) goto error; } else if (lex_match_id (lexer, "TYPE")) { if (type != 0) { lex_sbc_only_once ("TYPE"); goto error; } lex_match (lexer, T_EQUALS); if (lex_match_id (lexer, "CSV")) type = CSV_FILE; else if (lex_match_id (lexer, "TAB")) type = TAB_FILE; else { lex_error_expecting (lexer, "CSV", "TAB", NULL_SENTINEL); goto error; } } else if (lex_match_id (lexer, "REPLACE")) replace = true; else if (lex_match_id (lexer, "FIELDNAMES")) include_var_names = true; else if (lex_match_id (lexer, "MISSING")) { lex_match (lexer, T_EQUALS); if (lex_match_id (lexer, "IGNORE")) recode_user_missing = false; else if (lex_match_id (lexer, "RECODE")) recode_user_missing = true; else { lex_error_expecting (lexer, "IGNORE", "RECODE", NULL_SENTINEL); goto error; } } else if (lex_match_id (lexer, "CELLS")) { lex_match (lexer, T_EQUALS); if (lex_match_id (lexer, "VALUES")) use_value_labels = false; else if (lex_match_id (lexer, "LABELS")) use_value_labels = true; else { lex_error_expecting (lexer, "VALUES", "LABELS", NULL_SENTINEL); goto error; } } else if (lex_match_id (lexer, "TEXTOPTIONS")) { lex_match (lexer, T_EQUALS); for (;;) { if (lex_match_id (lexer, "DELIMITER")) { lex_match (lexer, T_EQUALS); if (!lex_force_string (lexer)) goto error; /* XXX should support multibyte UTF-8 delimiters */ if (ss_length (lex_tokss (lexer)) != 1) { msg (SE, _("The %s string must contain exactly one " "character."), "DELIMITER"); goto error; } delimiter = ss_first (lex_tokss (lexer)); lex_get (lexer); } else if (lex_match_id (lexer, "QUALIFIER")) { lex_match (lexer, T_EQUALS); if (!lex_force_string (lexer)) goto error; /* XXX should support multibyte UTF-8 qualifiers */ if (ss_length (lex_tokss (lexer)) != 1) { msg (SE, _("The %s string must contain exactly one " "character."), "QUALIFIER"); goto error; } qualifier = ss_first (lex_tokss (lexer)); lex_get (lexer); } else if (lex_match_id (lexer, "DECIMAL")) { lex_match (lexer, T_EQUALS); if (lex_match_id (lexer, "DOT")) decimal = '.'; else if (lex_match_id (lexer, "COMMA")) decimal = ','; else { lex_error_expecting (lexer, "DOT", "COMMA", NULL_SENTINEL); goto error; } } else if (lex_match_id (lexer, "FORMAT")) { lex_match (lexer, T_EQUALS); if (lex_match_id (lexer, "PLAIN")) use_print_formats = false; else if (lex_match_id (lexer, "VARIABLE")) use_print_formats = true; else { lex_error_expecting (lexer, "PLAIN", "VARIABLE", NULL_SENTINEL); goto error; } } else break; } } else if (lex_match_id (lexer, "UNSELECTED")) { lex_match (lexer, T_EQUALS); if (lex_match_id (lexer, "RETAIN")) retain_unselected = true; else if (lex_match_id (lexer, "DELETE")) retain_unselected = false; else { lex_error_expecting (lexer, "RETAIN", "DELETE", NULL_SENTINEL); goto error; } } else if (!parse_dict_trim (lexer, dict)) goto error; } if (lex_end_of_command (lexer) != CMD_SUCCESS) goto error; if (type == 0) { lex_sbc_missing ("TYPE"); goto error; } else if (handle == NULL) { lex_sbc_missing ("OUTFILE"); goto error; } else if (!replace && fn_exists (fh_get_file_name (handle))) { msg (SE, _("Output file `%s' exists but REPLACE was not specified."), fh_get_file_name (handle)); goto error; } dict_delete_scratch_vars (dict); dict_compact_values (dict); csv_opts.recode_user_missing = recode_user_missing; csv_opts.include_var_names = include_var_names; csv_opts.use_value_labels = use_value_labels; csv_opts.use_print_formats = use_print_formats; csv_opts.decimal = decimal; csv_opts.delimiter = (delimiter ? delimiter : type == TAB_FILE ? '\t' : decimal == '.' ? ',' : ';'); csv_opts.qualifier = qualifier; writer = csv_writer_open (handle, dict, &csv_opts); if (writer == NULL) goto error; fh_unref (handle); map = case_map_from_dict (dict); if (map != NULL) writer = case_map_create_output_translator (map, writer); dict_destroy (dict); casereader_transfer (proc_open_filtering (ds, !retain_unselected), writer); ok = casewriter_destroy (writer); ok = proc_commit (ds) && ok; return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE; error: fh_unref (handle); dict_destroy (dict); case_map_destroy (map); return CMD_FAILURE; }
/* Parses and executes the AGGREGATE procedure. */ int cmd_aggregate (struct lexer *lexer, struct dataset *ds) { struct dictionary *dict = dataset_dict (ds); struct agr_proc agr; struct file_handle *out_file = NULL; struct casereader *input = NULL, *group; struct casegrouper *grouper; struct casewriter *output = NULL; bool copy_documents = false; bool presorted = false; bool saw_direction; bool ok; memset(&agr, 0 , sizeof (agr)); agr.missing = ITEMWISE; agr.src_dict = dict; subcase_init_empty (&agr.sort); /* OUTFILE subcommand must be first. */ lex_match (lexer, T_SLASH); if (!lex_force_match_id (lexer, "OUTFILE")) goto error; lex_match (lexer, T_EQUALS); if (!lex_match (lexer, T_ASTERISK)) { out_file = fh_parse (lexer, FH_REF_FILE, dataset_session (ds)); if (out_file == NULL) goto error; } if (out_file == NULL && lex_match_id (lexer, "MODE")) { lex_match (lexer, T_EQUALS); if (lex_match_id (lexer, "ADDVARIABLES")) { agr.add_variables = true; /* presorted is assumed in ADDVARIABLES mode */ presorted = true; } else if (lex_match_id (lexer, "REPLACE")) { agr.add_variables = false; } else goto error; } if ( agr.add_variables ) agr.dict = dict_clone (dict); else agr.dict = dict_create (dict_get_encoding (dict)); dict_set_label (agr.dict, dict_get_label (dict)); dict_set_documents (agr.dict, dict_get_documents (dict)); /* Read most of the subcommands. */ for (;;) { lex_match (lexer, T_SLASH); if (lex_match_id (lexer, "MISSING")) { lex_match (lexer, T_EQUALS); if (!lex_match_id (lexer, "COLUMNWISE")) { lex_error_expecting (lexer, "COLUMNWISE", NULL); goto error; } agr.missing = COLUMNWISE; } else if (lex_match_id (lexer, "DOCUMENT")) copy_documents = true; else if (lex_match_id (lexer, "PRESORTED")) presorted = true; else if (lex_force_match_id (lexer, "BREAK")) { int i; lex_match (lexer, T_EQUALS); if (!parse_sort_criteria (lexer, dict, &agr.sort, &agr.break_vars, &saw_direction)) goto error; agr.break_var_cnt = subcase_get_n_fields (&agr.sort); if (! agr.add_variables) for (i = 0; i < agr.break_var_cnt; i++) dict_clone_var_assert (agr.dict, agr.break_vars[i]); /* BREAK must follow the options. */ break; } else goto error; } if (presorted && saw_direction) msg (SW, _("When PRESORTED is specified, specifying sorting directions " "with (A) or (D) has no effect. Output data will be sorted " "the same way as the input data.")); /* Read in the aggregate functions. */ lex_match (lexer, T_SLASH); if (!parse_aggregate_functions (lexer, dict, &agr)) goto error; /* Delete documents. */ if (!copy_documents) dict_clear_documents (agr.dict); /* Cancel SPLIT FILE. */ dict_set_split_vars (agr.dict, NULL, 0); /* Initialize. */ agr.case_cnt = 0; if (out_file == NULL) { /* The active dataset will be replaced by the aggregated data, so TEMPORARY is moot. */ proc_cancel_temporary_transformations (ds); proc_discard_output (ds); output = autopaging_writer_create (dict_get_proto (agr.dict)); } else { output = any_writer_open (out_file, agr.dict); if (output == NULL) goto error; } input = proc_open (ds); if (!subcase_is_empty (&agr.sort) && !presorted) { input = sort_execute (input, &agr.sort); subcase_clear (&agr.sort); } for (grouper = casegrouper_create_vars (input, agr.break_vars, agr.break_var_cnt); casegrouper_get_next_group (grouper, &group); casereader_destroy (group)) { struct casereader *placeholder = NULL; struct ccase *c = casereader_peek (group, 0); if (c == NULL) { casereader_destroy (group); continue; } initialize_aggregate_info (&agr); if ( agr.add_variables ) placeholder = casereader_clone (group); { struct ccase *cg; for (; (cg = casereader_read (group)) != NULL; case_unref (cg)) accumulate_aggregate_info (&agr, cg); } if (agr.add_variables) { struct ccase *cg; for (; (cg = casereader_read (placeholder)) != NULL; case_unref (cg)) dump_aggregate_info (&agr, output, cg); casereader_destroy (placeholder); } else { dump_aggregate_info (&agr, output, c); } case_unref (c); } if (!casegrouper_destroy (grouper)) goto error; if (!proc_commit (ds)) { input = NULL; goto error; } input = NULL; if (out_file == NULL) { struct casereader *next_input = casewriter_make_reader (output); if (next_input == NULL) goto error; dataset_set_dict (ds, agr.dict); dataset_set_source (ds, next_input); agr.dict = NULL; } else { ok = casewriter_destroy (output); output = NULL; if (!ok) goto error; } agr_destroy (&agr); fh_unref (out_file); return CMD_SUCCESS; error: if (input != NULL) proc_commit (ds); casewriter_destroy (output); agr_destroy (&agr); fh_unref (out_file); return CMD_CASCADING_FAILURE; }
/* Parses SAVE or XSAVE or EXPORT or XEXPORT command. WRITER_TYPE identifies the type of file to write, and COMMAND_TYPE identifies the type of command. On success, returns a writer. For procedures only, sets *RETAIN_UNSELECTED to true if cases that would otherwise be excluded by FILTER or USE should be included. On failure, returns a null pointer. */ static struct casewriter * parse_write_command (struct lexer *lexer, struct dataset *ds, enum writer_type writer_type, enum command_type command_type, bool *retain_unselected) { /* Common data. */ struct file_handle *handle; /* Output file. */ struct dictionary *dict; /* Dictionary for output file. */ struct casewriter *writer; /* Writer. */ struct case_map *map; /* Map from input data to data for writer. */ /* Common options. */ bool print_map; /* Print map? TODO. */ bool print_short_names; /* Print long-to-short name map. TODO. */ struct sfm_write_options sysfile_opts; struct pfm_write_options porfile_opts; assert (writer_type == SYSFILE_WRITER || writer_type == PORFILE_WRITER); assert (command_type == XFORM_CMD || command_type == PROC_CMD); assert ((retain_unselected != NULL) == (command_type == PROC_CMD)); if (command_type == PROC_CMD) *retain_unselected = true; handle = NULL; dict = dict_clone (dataset_dict (ds)); writer = NULL; map = NULL; print_map = false; print_short_names = false; sysfile_opts = sfm_writer_default_options (); porfile_opts = pfm_writer_default_options (); case_map_prepare_dict (dict); dict_delete_scratch_vars (dict); lex_match (lexer, T_SLASH); for (;;) { if (lex_match_id (lexer, "OUTFILE")) { if (handle != NULL) { lex_sbc_only_once ("OUTFILE"); goto error; } lex_match (lexer, T_EQUALS); handle = fh_parse (lexer, FH_REF_FILE, NULL); if (handle == NULL) goto error; } else if (lex_match_id (lexer, "NAMES")) print_short_names = true; else if (lex_match_id (lexer, "PERMISSIONS")) { bool cw; lex_match (lexer, T_EQUALS); if (lex_match_id (lexer, "READONLY")) cw = false; else if (lex_match_id (lexer, "WRITEABLE")) cw = true; else { lex_error_expecting (lexer, "READONLY", "WRITEABLE", NULL_SENTINEL); goto error; } sysfile_opts.create_writeable = porfile_opts.create_writeable = cw; } else if (command_type == PROC_CMD && lex_match_id (lexer, "UNSELECTED")) { lex_match (lexer, T_EQUALS); if (lex_match_id (lexer, "RETAIN")) *retain_unselected = true; else if (lex_match_id (lexer, "DELETE")) *retain_unselected = false; else { lex_error_expecting (lexer, "RETAIN", "DELETE", NULL_SENTINEL); goto error; } } else if (writer_type == SYSFILE_WRITER && lex_match_id (lexer, "COMPRESSED")) sysfile_opts.compress = true; else if (writer_type == SYSFILE_WRITER && lex_match_id (lexer, "UNCOMPRESSED")) sysfile_opts.compress = false; else if (writer_type == SYSFILE_WRITER && lex_match_id (lexer, "VERSION")) { lex_match (lexer, T_EQUALS); if (!lex_force_int (lexer)) goto error; sysfile_opts.version = lex_integer (lexer); lex_get (lexer); } else if (writer_type == PORFILE_WRITER && lex_match_id (lexer, "TYPE")) { lex_match (lexer, T_EQUALS); if (lex_match_id (lexer, "COMMUNICATIONS")) porfile_opts.type = PFM_COMM; else if (lex_match_id (lexer, "TAPE")) porfile_opts.type = PFM_TAPE; else { lex_error_expecting (lexer, "COMM", "TAPE", NULL_SENTINEL); goto error; } } else if (writer_type == PORFILE_WRITER && lex_match_id (lexer, "DIGITS")) { lex_match (lexer, T_EQUALS); if (!lex_force_int (lexer)) goto error; porfile_opts.digits = lex_integer (lexer); lex_get (lexer); } else if (!parse_dict_trim (lexer, dict)) goto error; if (!lex_match (lexer, T_SLASH)) break; } if (lex_end_of_command (lexer) != CMD_SUCCESS) goto error; if (handle == NULL) { lex_sbc_missing ("OUTFILE"); goto error; } dict_delete_scratch_vars (dict); dict_compact_values (dict); if (fh_get_referent (handle) == FH_REF_FILE) { switch (writer_type) { case SYSFILE_WRITER: writer = sfm_open_writer (handle, dict, sysfile_opts); break; case PORFILE_WRITER: writer = pfm_open_writer (handle, dict, porfile_opts); break; } } else writer = any_writer_open (handle, dict); if (writer == NULL) goto error; map = case_map_from_dict (dict); if (map != NULL) writer = case_map_create_output_translator (map, writer); dict_destroy (dict); fh_unref (handle); return writer; error: fh_unref (handle); casewriter_destroy (writer); dict_destroy (dict); case_map_destroy (map); return NULL; }